Mercurial > hg > audiodb
comparison insert.cpp @ 277:abfb26e08d9c audiodb-debian
Merge trunk changes -r326:386 into audiodb-debian branch.
Plus new debian/changelog version. (Should have used an epoch really,
but couldn't be bothered; TODO: work out a sane version numbering
policy).
author | mas01cr |
---|---|
date | Tue, 01 Jul 2008 09:12:40 +0000 |
parents | 15b8ff55ea5b |
children |
comparison
equal
deleted
inserted
replaced
246:cbf51690c78c | 277:abfb26e08d9c |
---|---|
1 #include "audioDB.h" | 1 #include "audioDB.h" |
2 | |
3 bool audioDB::enough_per_file_space_free() { | |
4 unsigned int fmaxfiles, tmaxfiles; | |
5 unsigned int maxfiles; | |
6 | |
7 fmaxfiles = fileTableLength / O2_FILETABLE_ENTRY_SIZE; | |
8 tmaxfiles = trackTableLength / O2_TRACKTABLE_ENTRY_SIZE; | |
9 maxfiles = fmaxfiles > tmaxfiles ? tmaxfiles : fmaxfiles; | |
10 return(dbH->numFiles < maxfiles); | |
11 } | |
2 | 12 |
3 bool audioDB::enough_data_space_free(off_t size) { | 13 bool audioDB::enough_data_space_free(off_t size) { |
4 return(dbH->timesTableOffset > dbH->dataOffset + dbH->length + size); | 14 return(dbH->timesTableOffset > dbH->dataOffset + dbH->length + size); |
5 } | 15 } |
6 | 16 |
16 if(!usingTimes && (dbH->flags & O2_FLAG_TIMES)) | 26 if(!usingTimes && (dbH->flags & O2_FLAG_TIMES)) |
17 error("Must use timestamps with timestamped database","use --times"); | 27 error("Must use timestamps with timestamped database","use --times"); |
18 | 28 |
19 if(!usingPower && (dbH->flags & O2_FLAG_POWER)) | 29 if(!usingPower && (dbH->flags & O2_FLAG_POWER)) |
20 error("Must use power with power-enabled database", dbName); | 30 error("Must use power with power-enabled database", dbName); |
31 | |
32 if(!enough_per_file_space_free()) { | |
33 error("Insert failed: no more room for metadata", inFile); | |
34 } | |
21 | 35 |
22 if(!enough_data_space_free(statbuf.st_size - sizeof(int))) { | 36 if(!enough_data_space_free(statbuf.st_size - sizeof(int))) { |
23 error("Insert failed: no more room in database", inFile); | 37 error("Insert failed: no more room in database", inFile); |
24 } | 38 } |
25 | 39 |
26 if(!key) | 40 if(!key) |
27 key=inFile; | 41 key=inFile; |
28 // Linear scan of filenames check for pre-existing feature | 42 // Linear scan of filenames check for pre-existing feature |
29 unsigned alreadyInserted=0; | 43 unsigned alreadyInserted=0; |
30 for(unsigned k=0; k<dbH->numFiles; k++) | 44 for(unsigned k=0; k<dbH->numFiles; k++) |
31 if(strncmp(fileTable + k*O2_FILETABLESIZE, key, strlen(key)+1)==0){ | 45 if(strncmp(fileTable + k*O2_FILETABLE_ENTRY_SIZE, key, strlen(key)+1)==0){ |
32 alreadyInserted=1; | 46 alreadyInserted=1; |
33 break; | 47 break; |
34 } | 48 } |
35 | 49 |
36 if(alreadyInserted) { | 50 if(alreadyInserted) { |
48 munmap(db,dbH->dbSize); | 62 munmap(db,dbH->dbSize); |
49 close(infid); | 63 close(infid); |
50 return; | 64 return; |
51 } | 65 } |
52 | 66 |
53 strncpy(fileTable + dbH->numFiles*O2_FILETABLESIZE, key, strlen(key)); | 67 strncpy(fileTable + dbH->numFiles*O2_FILETABLE_ENTRY_SIZE, key, strlen(key)); |
54 | 68 |
55 off_t insertoffset = dbH->length;// Store current state | 69 off_t insertoffset = dbH->length;// Store current state |
56 | 70 |
57 // Check times status and insert times from file | 71 // Check times status and insert times from file |
58 unsigned indexoffset = insertoffset/(dbH->dim*sizeof(double)); | 72 unsigned indexoffset = insertoffset/(dbH->dim*sizeof(double)); |
187 | 201 |
188 if(!usingPower && (dbH->flags & O2_FLAG_POWER)) | 202 if(!usingPower && (dbH->flags & O2_FLAG_POWER)) |
189 error("Must use power with power-enabled database", dbName); | 203 error("Must use power with power-enabled database", dbName); |
190 | 204 |
191 unsigned totalVectors=0; | 205 unsigned totalVectors=0; |
192 char *thisKey = new char[MAXSTR]; | |
193 char *thisFile = new char[MAXSTR]; | 206 char *thisFile = new char[MAXSTR]; |
207 char *thisKey = 0; | |
208 if (key && (key != inFile)) { | |
209 thisKey = new char[MAXSTR]; | |
210 } | |
194 char *thisTimesFileName = new char[MAXSTR]; | 211 char *thisTimesFileName = new char[MAXSTR]; |
195 char *thisPowerFileName = new char[MAXSTR]; | 212 char *thisPowerFileName = new char[MAXSTR]; |
196 | 213 |
197 do{ | 214 do{ |
198 filesIn->getline(thisFile,MAXSTR); | 215 filesIn->getline(thisFile,MAXSTR); |
199 if(key && key!=inFile) | 216 if(key && key!=inFile) { |
200 keysIn->getline(thisKey,MAXSTR); | 217 keysIn->getline(thisKey,MAXSTR); |
201 else | 218 } else { |
202 thisKey = thisFile; | 219 thisKey = thisFile; |
203 if(usingTimes) | 220 } |
204 timesFile->getline(thisTimesFileName,MAXSTR); | 221 if(usingTimes) { |
205 if(usingPower) | 222 timesFile->getline(thisTimesFileName,MAXSTR); |
223 } | |
224 if(usingPower) { | |
206 powerFile->getline(thisPowerFileName, MAXSTR); | 225 powerFile->getline(thisPowerFileName, MAXSTR); |
226 } | |
207 | 227 |
208 if(filesIn->eof()) | 228 if(filesIn->eof()) { |
209 break; | 229 break; |
210 | 230 } |
211 initInputFile(thisFile); | 231 initInputFile(thisFile); |
232 | |
233 if(!enough_per_file_space_free()) { | |
234 error("batchinsert failed: no more room for metadata", thisFile); | |
235 } | |
212 | 236 |
213 if(!enough_data_space_free(statbuf.st_size - sizeof(int))) { | 237 if(!enough_data_space_free(statbuf.st_size - sizeof(int))) { |
214 error("batchinsert failed: no more room in database", thisFile); | 238 error("batchinsert failed: no more room in database", thisFile); |
215 } | 239 } |
216 | 240 |
217 // Linear scan of filenames check for pre-existing feature | 241 // Linear scan of filenames check for pre-existing feature |
218 unsigned alreadyInserted=0; | 242 unsigned alreadyInserted=0; |
219 | 243 |
220 for(unsigned k=0; k<dbH->numFiles; k++) | 244 for(unsigned k=0; k<dbH->numFiles; k++) |
221 if(strncmp(fileTable + k*O2_FILETABLESIZE, thisKey, strlen(thisKey)+1)==0){ | 245 if(strncmp(fileTable + k*O2_FILETABLE_ENTRY_SIZE, thisKey, strlen(thisKey)+1)==0){ |
222 alreadyInserted=1; | 246 alreadyInserted=1; |
223 break; | 247 break; |
224 } | 248 } |
225 | 249 |
226 if(alreadyInserted) { | 250 if(alreadyInserted) { |
265 insertPowerData(numVectors, thispowerfd, powerdata); | 289 insertPowerData(numVectors, thispowerfd, powerdata); |
266 if (0 < thispowerfd) { | 290 if (0 < thispowerfd) { |
267 close(thispowerfd); | 291 close(thispowerfd); |
268 } | 292 } |
269 } | 293 } |
270 strncpy(fileTable + dbH->numFiles*O2_FILETABLESIZE, thisKey, strlen(thisKey)); | 294 strncpy(fileTable + dbH->numFiles*O2_FILETABLE_ENTRY_SIZE, thisKey, strlen(thisKey)); |
271 | 295 |
272 off_t insertoffset = dbH->length;// Store current state | 296 off_t insertoffset = dbH->length;// Store current state |
273 | 297 |
274 // Increment file count | 298 // Increment file count |
275 dbH->numFiles++; | 299 dbH->numFiles++; |
296 munmap(indata,statbuf.st_size); | 320 munmap(indata,statbuf.st_size); |
297 close(infid); | 321 close(infid); |
298 } while(!filesIn->eof()); | 322 } while(!filesIn->eof()); |
299 | 323 |
300 VERB_LOG(0, "%s %s %u vectors %ju bytes.\n", COM_BATCHINSERT, dbName, totalVectors, (intmax_t) (totalVectors * dbH->dim * sizeof(double))); | 324 VERB_LOG(0, "%s %s %u vectors %ju bytes.\n", COM_BATCHINSERT, dbName, totalVectors, (intmax_t) (totalVectors * dbH->dim * sizeof(double))); |
301 | 325 |
326 delete [] thisPowerFileName; | |
327 if(key && (key != inFile)) { | |
328 delete [] thisKey; | |
329 } | |
330 delete [] thisFile; | |
331 delete [] thisTimesFileName; | |
332 | |
333 delete filesIn; | |
334 delete keysIn; | |
335 | |
302 // Report status | 336 // Report status |
303 status(dbName); | 337 status(dbName); |
304 } | 338 } |