comparison dump.cpp @ 239:2cc06e5b05a5

Merge refactoring branch. Bug fixes: * 64-bit powertable bug; * -inf - -inf bug; * use new times information; * plus short track, O2_MAXFILES and structure padding ABI fixes (already backported) Major code changes: * split source into functional units, known as 'files'; * Reporter class for accumulating and reporting on query results; * much OAOOization, mostly from above: net 800 LOC (25%) shorter.
author mas01cr
date Thu, 13 Dec 2007 14:23:32 +0000
parents
children 4dcb09f5fe85
comparison
equal deleted inserted replaced
224:3a81da6fb1d7 239:2cc06e5b05a5
1 #include "audioDB.h"
2
3 void audioDB::dump(const char* dbName){
4 if(!dbH) {
5 initTables(dbName, 0);
6 }
7
8 if((mkdir(output, S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
9 error("error making output directory", output, "mkdir");
10 }
11
12 char *cwd = new char[PATH_MAX];
13
14 if ((getcwd(cwd, PATH_MAX)) == 0) {
15 error("error getting working directory", "", "getcwd");
16 }
17
18 if((chdir(output)) < 0) {
19 error("error changing working directory", output, "chdir");
20 }
21
22 int fLfd, tLfd = 0, pLfd = 0, kLfd;
23 FILE *fLFile, *tLFile = 0, *pLFile = 0, *kLFile;
24
25 if ((fLfd = open("featureList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
26 error("error creating featureList file", "featureList.txt", "open");
27 }
28
29 int times = dbH->flags & O2_FLAG_TIMES;
30 if (times) {
31 if ((tLfd = open("timesList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
32 error("error creating timesList file", "timesList.txt", "open");
33 }
34 }
35
36 int power = dbH->flags & O2_FLAG_POWER;
37 if (power) {
38 if ((pLfd = open("powerList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
39 error("error creating powerList file", "powerList.txt", "open");
40 }
41 }
42
43 if ((kLfd = open("keyList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
44 error("error creating keyList file", "keyList.txt", "open");
45 }
46
47 /* can these fail? I sincerely hope not. */
48 fLFile = fdopen(fLfd, "w");
49 if (times) {
50 tLFile = fdopen(tLfd, "w");
51 }
52 if (power) {
53 pLFile = fdopen(pLfd, "w");
54 }
55 kLFile = fdopen(kLfd, "w");
56
57 char *fName = new char[256];
58 int ffd, pfd;
59 FILE *tFile;
60 unsigned pos = 0;
61 lseek(dbfid, dbH->dataOffset, SEEK_SET);
62 double *data_buffer;
63 size_t data_buffer_size;
64 for(unsigned k = 0; k < dbH->numFiles; k++) {
65 fprintf(kLFile, "%s\n", fileTable + k*O2_FILETABLESIZE);
66 snprintf(fName, 256, "%05d.features", k);
67 if ((ffd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
68 error("error creating feature file", fName, "open");
69 }
70 if ((write(ffd, &dbH->dim, sizeof(uint32_t))) < 0) {
71 error("error writing dimensions", fName, "write");
72 }
73
74 /* FIXME: this repeated malloc()/free() of data buffers is
75 inefficient. */
76 data_buffer_size = trackTable[k] * dbH->dim * sizeof(double);
77
78 {
79 void *tmp = malloc(data_buffer_size);
80 if (tmp == NULL) {
81 error("error allocating data buffer");
82 }
83 data_buffer = (double *) tmp;
84 }
85
86 if ((read(dbfid, data_buffer, data_buffer_size)) != (ssize_t) data_buffer_size) {
87 error("error reading data", fName, "read");
88 }
89
90 if ((write(ffd, data_buffer, data_buffer_size)) < 0) {
91 error("error writing data", fName, "write");
92 }
93
94 free(data_buffer);
95
96 fprintf(fLFile, "%s\n", fName);
97 close(ffd);
98
99 if (times) {
100 snprintf(fName, 256, "%05d.times", k);
101 tFile = fopen(fName, "w");
102 for(unsigned i = 0; i < trackTable[k]; i++) {
103 // KLUDGE: specifying 16 digits of precision after the decimal
104 // point is (but check this!) sufficient to uniquely identify
105 // doubles; however, that will cause ugliness, as that's
106 // vastly too many for most values of interest. Moving to %a
107 // here and scanf() in the timesFile reading might fix this.
108 // -- CSR, 2007-10-19
109 fprintf(tFile, "%.16e\n", *(timesTable + 2*pos + 2*i));
110 }
111 fprintf(tFile, "%.16e\n", *(timesTable + 2*pos + 2*trackTable[k]-1));
112
113 fprintf(tLFile, "%s\n", fName);
114 }
115
116 if (power) {
117 uint32_t one = 1;
118 snprintf(fName, 256, "%05d.power", k);
119 if ((pfd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
120 error("error creating power file", fName, "open");
121 }
122 if ((write(pfd, &one, sizeof(uint32_t))) < 0) {
123 error("error writing one", fName, "write");
124 }
125 if ((write(pfd, powerTable + pos, trackTable[k] * sizeof(double))) < 0) {
126 error("error writing data", fName, "write");
127 }
128 fprintf(pLFile, "%s\n", fName);
129 close(pfd);
130 }
131
132 pos += trackTable[k];
133 std::cout << fileTable+k*O2_FILETABLESIZE << " " << trackTable[k] << std::endl;
134 }
135
136 FILE *scriptFile;
137 scriptFile = fopen("restore.sh", "w");
138 fprintf(scriptFile, "\
139 #! /bin/sh\n\
140 #\n\
141 # usage: AUDIODB=/path/to/audioDB sh ./restore.sh <newdb>\n\
142 \n\
143 if [ -z \"${AUDIODB}\" ]; then echo set AUDIODB variable; exit 1; fi\n\
144 if [ -z \"$1\" ]; then echo usage: $0 newdb; exit 1; fi\n\n\
145 \"${AUDIODB}\" -d \"$1\" -N --size=%d\n", (int) (dbH->dbSize / 1000000));
146 if(dbH->flags & O2_FLAG_L2NORM) {
147 fprintf(scriptFile, "\"${AUDIODB}\" -d \"$1\" -L\n");
148 }
149 if(power) {
150 fprintf(scriptFile, "\"${AUDIODB}\" -d \"$1\" -P\n");
151 }
152 fprintf(scriptFile, "\"${AUDIODB}\" -d \"$1\" -B -F featureList.txt -K keyList.txt");
153 if(times) {
154 fprintf(scriptFile, " -T timesList.txt");
155 }
156 if(power) {
157 fprintf(scriptFile, " -W powerList.txt");
158 }
159 fprintf(scriptFile, "\n");
160 fclose(scriptFile);
161
162 if((chdir(cwd)) < 0) {
163 error("error changing working directory", cwd, "chdir");
164 }
165
166 fclose(fLFile);
167 if(times) {
168 fclose(tLFile);
169 }
170 if(power) {
171 fclose(pLFile);
172 }
173 fclose(kLFile);
174 delete[] fName;
175
176 status(dbName);
177 }