comparison audioDB.cpp @ 131:3d931368fab3

Initial cut at a dump utility; binary-based for now. Only very lightly tested; in particular, the times saving functionality is completely untested at present. Restore functionality provided by a shell script written as part of the dump process.
author mas01cr
date Fri, 19 Oct 2007 17:04:46 +0000
parents f7eba8eb272c
children a5d5a55a412d
comparison
equal deleted inserted replaced
130:63ca70f2bf37 131:3d931368fab3
208 } 208 }
209 209
210 if(args_info.DUMP_given){ 210 if(args_info.DUMP_given){
211 command=COM_DUMP; 211 command=COM_DUMP;
212 dbName=args_info.database_arg; 212 dbName=args_info.database_arg;
213 output = args_info.output_arg;
213 return 0; 214 return 0;
214 } 215 }
215 216
216 if(args_info.L2NORM_given){ 217 if(args_info.L2NORM_given){
217 command=COM_L2NORM; 218 command=COM_L2NORM;
867 adbStatusResult->flags = dbH->flags; 868 adbStatusResult->flags = dbH->flags;
868 } 869 }
869 } 870 }
870 871
871 void audioDB::dump(const char* dbName){ 872 void audioDB::dump(const char* dbName){
872 if(!dbH) 873 if(!dbH) {
873 initTables(dbName, 0, 0); 874 initTables(dbName, 0, 0);
874 875 }
875 for(unsigned k=0, j=0; k<dbH->numFiles; k++){ 876
877 if((mkdir(output, S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
878 error("error making output directory", output, "mkdir");
879 }
880
881 char *cwd = new char[PATH_MAX];
882
883 if ((getcwd(cwd, PATH_MAX)) == 0) {
884 error("error getting working directory", "", "getcwd");
885 }
886
887 if((chdir(output)) < 0) {
888 error("error changing working directory", output, "chdir");
889 }
890
891 int fLfd, tLfd = 0, kLfd;
892 FILE *fLFile, *tLFile = 0, *kLFile;
893
894 if ((fLfd = open("featureList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
895 error("error creating featureList file", "featureList.txt", "open");
896 }
897 int times = dbH->flags & O2_FLAG_TIMES;
898 if (times) {
899 if ((tLfd = open("timesList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
900 error("error creating timesList file", "timesList.txt", "open");
901 }
902 }
903 if ((kLfd = open("keyList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
904 error("error creating keyList file", "keyList.txt", "open");
905 }
906
907 /* can these fail? I sincerely hope not. */
908 fLFile = fdopen(fLfd, "w");
909 if (times) {
910 tLFile = fdopen(tLfd, "w");
911 }
912 kLFile = fdopen(kLfd, "w");
913
914 char *fName = new char[256];
915 int ffd;
916 FILE *tFile;
917 unsigned pos = 0;
918 for(unsigned k = 0; k < dbH->numFiles; k++) {
919 fprintf(kLFile, "%s\n", fileTable + k*O2_FILETABLESIZE);
920 snprintf(fName, 256, "%05d.features", k);
921 if ((ffd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
922 error("error creating feature file", fName, "open");
923 }
924 if ((write(ffd, &dbH->dim, sizeof(uint32_t))) < 0) {
925 error("error writing dimensions", fName, "write");
926 }
927
928 if ((write(ffd, dataBuf + pos * dbH->dim, trackTable[k] * dbH->dim * sizeof(double))) < 0) {
929 error("error writing data", fName, "write");
930 }
931 fprintf(fLFile, "%s\n", fName);
932 close(ffd);
933
934 if(times) {
935 snprintf(fName, 256, "%05d.times", k);
936 tFile = fopen(fName, "w");
937 for(unsigned i = 0; i < trackTable[k]; i++) {
938 // KLUDGE: specifying 16 digits of precision after the decimal
939 // point is (but check this!) sufficient to uniquely identify
940 // doubles; however, that will cause ugliness, as that's
941 // vastly too many for most values of interest. Moving to %a
942 // here and scanf() in the timesFile reading might fix this.
943 // -- CSR, 2007-10-19
944 fprintf(tFile, "%.16e\n", *(timesTable + pos + i));
945 }
946 fprintf(tLFile, "%s\n", fName);
947 }
948
949 pos += trackTable[k];
876 cout << fileTable+k*O2_FILETABLESIZE << " " << trackTable[k] << endl; 950 cout << fileTable+k*O2_FILETABLESIZE << " " << trackTable[k] << endl;
877 j+=trackTable[k]; 951 }
878 } 952
879 953 FILE *scriptFile;
954 scriptFile = fopen("restore.sh", "w");
955 fprintf(scriptFile, "\
956 #! /bin/sh\n\
957 #\n\
958 # usage: AUDIODB=/path/to/audioDB sh ./restore.sh <newdb>\n\
959 \n\
960 if [ -z \"${AUDIODB}\" ]; then echo set AUDIODB variable; exit 1; fi\n\
961 if [ -z \"$1\" ]; then echo usage: $0 newdb; exit 1; fi\n\n\
962 \"${AUDIODB}\" -d \"$1\" -N --size=%d\n", dbH->dbSize / 1000000);
963 if(dbH->flags & O2_FLAG_L2NORM) {
964 fprintf(scriptFile, "\"${AUDIODB}\" -d \"$1\" -L\n");
965 }
966 fprintf(scriptFile, "\"${AUDIODB}\" -d \"$1\" -B -F featureList.txt -K keyList.txt");
967 if(times) {
968 fprintf(scriptFile, " -T timesList.txt");
969 }
970 fprintf(scriptFile, "\n");
971 fclose(scriptFile);
972
973 if((chdir(cwd)) < 0) {
974 error("error changing working directory", cwd, "chdir");
975 }
976
977 fclose(fLFile);
978 if(times) {
979 fclose(tLFile);
980 }
981 fclose(kLFile);
982 delete[] fName;
983
880 status(dbName); 984 status(dbName);
881 } 985 }
882 986
883 void audioDB::l2norm(const char* dbName){ 987 void audioDB::l2norm(const char* dbName){
884 initTables(dbName, true, 0); 988 initTables(dbName, true, 0);