diff 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
line wrap: on
line diff
--- a/insert.cpp	Mon Dec 17 16:44:37 2007 +0000
+++ b/insert.cpp	Tue Jul 01 09:12:40 2008 +0000
@@ -1,5 +1,15 @@
 #include "audioDB.h"
 
+bool audioDB::enough_per_file_space_free() {
+  unsigned int fmaxfiles, tmaxfiles;
+  unsigned int maxfiles;
+
+  fmaxfiles = fileTableLength / O2_FILETABLE_ENTRY_SIZE;
+  tmaxfiles = trackTableLength / O2_TRACKTABLE_ENTRY_SIZE;
+  maxfiles = fmaxfiles > tmaxfiles ? tmaxfiles : fmaxfiles;
+  return(dbH->numFiles < maxfiles);
+}
+
 bool audioDB::enough_data_space_free(off_t size) {
   return(dbH->timesTableOffset > dbH->dataOffset + dbH->length + size);
 }
@@ -19,6 +29,10 @@
   if(!usingPower && (dbH->flags & O2_FLAG_POWER))
     error("Must use power with power-enabled database", dbName);
 
+  if(!enough_per_file_space_free()) {
+    error("Insert failed: no more room for metadata", inFile);
+  }
+
   if(!enough_data_space_free(statbuf.st_size - sizeof(int))) {
     error("Insert failed: no more room in database", inFile);
   }
@@ -28,7 +42,7 @@
   // Linear scan of filenames check for pre-existing feature
   unsigned alreadyInserted=0;
   for(unsigned k=0; k<dbH->numFiles; k++)
-    if(strncmp(fileTable + k*O2_FILETABLESIZE, key, strlen(key)+1)==0){
+    if(strncmp(fileTable + k*O2_FILETABLE_ENTRY_SIZE, key, strlen(key)+1)==0){
       alreadyInserted=1;
       break;
     }
@@ -50,7 +64,7 @@
     return;
   }
 
-  strncpy(fileTable + dbH->numFiles*O2_FILETABLESIZE, key, strlen(key));
+  strncpy(fileTable + dbH->numFiles*O2_FILETABLE_ENTRY_SIZE, key, strlen(key));
 
   off_t insertoffset = dbH->length;// Store current state
 
@@ -189,26 +203,36 @@
     error("Must use power with power-enabled database", dbName);
 
   unsigned totalVectors=0;
-  char *thisKey = new char[MAXSTR];
   char *thisFile = new char[MAXSTR];
+  char *thisKey = 0;
+  if (key && (key != inFile)) {
+    thisKey = new char[MAXSTR];
+  }
   char *thisTimesFileName = new char[MAXSTR];
   char *thisPowerFileName = new char[MAXSTR];
   
   do{
     filesIn->getline(thisFile,MAXSTR);
-    if(key && key!=inFile)
+    if(key && key!=inFile) {
       keysIn->getline(thisKey,MAXSTR);
-    else
+    } else {
       thisKey = thisFile;
-    if(usingTimes)
-      timesFile->getline(thisTimesFileName,MAXSTR);	  
-    if(usingPower)
+    }
+    if(usingTimes) {
+      timesFile->getline(thisTimesFileName,MAXSTR);
+    }
+    if(usingPower) {
       powerFile->getline(thisPowerFileName, MAXSTR);
+    }
     
-    if(filesIn->eof())
+    if(filesIn->eof()) {
       break;
+    }
+    initInputFile(thisFile);
 
-    initInputFile(thisFile);
+    if(!enough_per_file_space_free()) {
+      error("batchinsert failed: no more room for metadata", thisFile);
+    }
 
     if(!enough_data_space_free(statbuf.st_size - sizeof(int))) {
       error("batchinsert failed: no more room in database", thisFile);
@@ -218,7 +242,7 @@
     unsigned alreadyInserted=0;
   
     for(unsigned k=0; k<dbH->numFiles; k++)
-      if(strncmp(fileTable + k*O2_FILETABLESIZE, thisKey, strlen(thisKey)+1)==0){
+      if(strncmp(fileTable + k*O2_FILETABLE_ENTRY_SIZE, thisKey, strlen(thisKey)+1)==0){
 	alreadyInserted=1;
 	break;
       }
@@ -267,7 +291,7 @@
             close(thispowerfd);
           }
         }
-	strncpy(fileTable + dbH->numFiles*O2_FILETABLESIZE, thisKey, strlen(thisKey));
+	strncpy(fileTable + dbH->numFiles*O2_FILETABLE_ENTRY_SIZE, thisKey, strlen(thisKey));
   
 	off_t insertoffset = dbH->length;// Store current state
 
@@ -298,7 +322,17 @@
   } while(!filesIn->eof());
 
   VERB_LOG(0, "%s %s %u vectors %ju bytes.\n", COM_BATCHINSERT, dbName, totalVectors, (intmax_t) (totalVectors * dbH->dim * sizeof(double)));
+
+  delete [] thisPowerFileName;
+  if(key && (key != inFile)) {
+    delete [] thisKey;
+  }
+  delete [] thisFile;
+  delete [] thisTimesFileName;
   
+  delete filesIn;
+  delete keysIn;
+
   // Report status
   status(dbName);
 }