changeset 433:681837f7c903 api-inversion

More on the query rationalization Turn audioDB::delete_arrays and audioDB::read_data into ordinary functions. Also partially do audioDB::initialize_arrays, except for the bits that we can't yet do because we haven't got a query datum/identifier going through yet.
author mas01cr
date Wed, 24 Dec 2008 10:55:48 +0000
parents 62a0515f59be
children 7af140bf8a0a
files audioDB-internals.h audioDB.h index.cpp query.cpp
diffstat 4 files changed, 36 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB-internals.h	Wed Dec 24 10:55:44 2008 +0000
+++ b/audioDB-internals.h	Wed Dec 24 10:55:48 2008 +0000
@@ -180,3 +180,5 @@
     return (*it).second;
   }
 }
+
+int audiodb_read_data(adb_t *, int, int, double **, size_t *);
--- a/audioDB.h	Wed Dec 24 10:55:44 2008 +0000
+++ b/audioDB.h	Wed Dec 24 10:55:48 2008 +0000
@@ -319,9 +319,7 @@
   // private methods
   void error(const char* a, const char* b = "", const char *sysFunc = 0);
 
-  void initialize_arrays(int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD);
-  void delete_arrays(int track, unsigned int numVectors, double **D, double **DD);
-  void read_data(int trkfid, int track, double **data_buffer_p, size_t *data_buffer_size_p);
+  void initialize_arrays(adb_t *adb, int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD);
   void insertTimeStamps(unsigned n, std::ifstream* timesFile, double* timesdata);
   void set_up_query(double **qp, double **vqp, double **qnp, double **vqnp, double **qpp, double **vqpp, double *mqdp, unsigned int *nvp);
   void set_up_query_from_key(double **qp, double **vqp, double **qnp, double **vqnp, double **qpp, double **vqpp, double *mqdp, unsigned *nvp, Uns32T queryIndex);
--- a/index.cpp	Wed Dec 24 10:55:44 2008 +0000
+++ b/index.cpp	Wed Dec 24 10:55:48 2008 +0000
@@ -338,7 +338,8 @@
       initInputFile(prefixedString, false); // nommap, file pointer at correct position
       trackfd = infid;
     }
-    read_data(trackfd, trackID, &fvp, &nfv); // over-writes fvp and nfv
+    if(audiodb_read_data(adb, trackfd, trackID, &fvp, &nfv))
+      error("failed to read data");
     *fvpp = fvp; // Protect memory allocation and free() for track data
     
     if( dbH->flags & O2_FLAG_LARGE_ADB )
--- a/query.cpp	Wed Dec 24 10:55:44 2008 +0000
+++ b/query.cpp	Wed Dec 24 10:55:48 2008 +0000
@@ -171,7 +171,7 @@
   reporter->report(fileTable, adbQueryResponse);
 }
 
-void audioDB::initialize_arrays(int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD) {
+void audioDB::initialize_arrays(adb_t *adb, int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD) {
   unsigned int j, k, l, w;
   double *dp, *qp, *sp;
 
@@ -180,16 +180,16 @@
 
   for(j = 0; j < numVectors; j++) {
     // Sum products matrix
-    D[j] = new double[trackTable[track]]; 
+    D[j] = new double[(*adb->track_lengths)[track]]; 
     assert(D[j]);
     // Matched filter matrix
-    DD[j]=new double[trackTable[track]];
+    DD[j]=new double[(*adb->track_lengths)[track]];
     assert(DD[j]);
   }
 
   // Dot product
   for(j = 0; j < numVectors; j++)
-    for(k = 0; k < trackTable[track]; k++){
+    for(k = 0; k < (*adb->track_lengths)[track]; k++){
       qp = query + j * dbH->dim;
       sp = data_buffer + k * dbH->dim;
       DD[j][k] = 0.0; // Initialize matched filter array
@@ -208,7 +208,7 @@
       for(j = 0; j < numVectors - w; j++) { 
         sp = DD[j];
         spd = D[j+w] + w;
-        k = trackTable[track] - w;
+        k = (*adb->track_lengths)[track] - w;
 	while(k--)
 	  *sp++ += *spd++;
       }
@@ -218,7 +218,7 @@
       for(j = 0; j < numVectors - w; j += HOP_SIZE) {
         sp = DD[j];
         spd = D[j+w]+w;
-        for(k = 0; k < trackTable[track] - w; k += HOP_SIZE) {
+        for(k = 0; k < (*adb->track_lengths)[track] - w; k += HOP_SIZE) {
           *sp += *spd;
           sp += HOP_SIZE;
           spd += HOP_SIZE;
@@ -228,7 +228,7 @@
   }
 }
 
-void audioDB::delete_arrays(int track, unsigned int numVectors, double **D, double **DD) {
+static void audiodb_delete_arrays(int track, unsigned int numVectors, double **D, double **DD) {
   if(D != NULL) {
     for(unsigned int j = 0; j < numVectors; j++) {
       delete[] D[j];
@@ -241,22 +241,28 @@
   }
 }
 
-void audioDB::read_data(int trkfid, int track, double **data_buffer_p, size_t *data_buffer_size_p) {
-  if (trackTable[track] * sizeof(double) * dbH->dim > *data_buffer_size_p) {
+int audiodb_read_data(adb_t *adb, int trkfid, int track, double **data_buffer_p, size_t *data_buffer_size_p) {
+  uint32_t track_length = (*adb->track_lengths)[track];
+  size_t track_size = track_length * sizeof(double) * adb->header->dim;
+  if (track_size > *data_buffer_size_p) {
     if(*data_buffer_p) {
       free(*data_buffer_p);
     }
     { 
-      *data_buffer_size_p = trackTable[track] * sizeof(double) * dbH->dim;
-      void *tmp = malloc(*data_buffer_size_p);
+      *data_buffer_size_p = track_size;
+      void *tmp = malloc(track_size);
       if (tmp == NULL) {
-        error("error allocating data buffer");
+        goto error;
       }
       *data_buffer_p = (double *) tmp;
     }
   }
 
-  CHECKED_READ(trkfid, *data_buffer_p, trackTable[track] * sizeof(double) * dbH->dim);
+  read_or_goto_error(trkfid, *data_buffer_p, track_size);
+  return 0;
+
+ error:
+  return 1;
 }
 
 void audioDB::insertTimeStamps(unsigned numVectors, std::ifstream *timesFile, double *timesdata) {
@@ -410,7 +416,8 @@
       delete[] tmpStr;
     initInputFile(prefixedString, false); // nommap, file pointer at correct position
     size_t allocatedSize = 0;
-    read_data(infid, queryIndex, qp, &allocatedSize); // over-writes qp and allocatedSize
+    if(audiodb_read_data(adb, infid, queryIndex, qp, &allocatedSize))
+      error("failed to read data"); // over-writes qp and allocatedSize
     // Consistency check on allocated memory and query feature size
     if(*nvp*sizeof(double)*dbH->dim != allocatedSize)
       error("Query memory allocation failed consitency check","set_up_query_from_key");
@@ -422,7 +429,8 @@
     *qp = NULL;
     lseek(dbfid, dbH->dataOffset + trackOffsetTable[queryIndex] * sizeof(double), SEEK_SET);
     size_t allocatedSize = 0;
-    read_data(dbfid, queryIndex, qp, &allocatedSize);
+    if(audiodb_read_data(adb, dbfid, queryIndex, qp, &allocatedSize))
+      error("failed to read data");
     // Consistency check on allocated memory and query feature size
     if(*nvp*sizeof(double)*dbH->dim != allocatedSize)
       error("Query memory allocation failed consitency check","set_up_query_from_key");
@@ -606,7 +614,8 @@
 	  delete[] tmpStr;
 	initInputFile(prefixedString, false); // nommap, file pointer at correct position
 	// Load the feature vector data for current track into data_buffer
-	read_data(infid, pp.trackID, &data_buffer, &data_buffer_size);	
+	if(audiodb_read_data(adb, infid, pp.trackID, &data_buffer, &data_buffer_size))
+          error("failed to read data");
 	// Load power and calculate power and l2norm sequence sums
 	init_track_aux_data(pp.trackID, data_buffer, &sNorm, &snPtr, &sPower, &spPtr);
       }
@@ -626,7 +635,8 @@
 	// On currentTrack change, allocate and load track data
 	currentTrack=pp.trackID;
 	lseek(dbfid, dbH->dataOffset + trackOffset * sizeof(double), SEEK_SET);
-	read_data(dbfid, currentTrack, &data_buffer, &data_buffer_size);
+	if(audiodb_read_data(adb, dbfid, currentTrack, &data_buffer, &data_buffer_size))
+          error("failed to read data");
       }
       // Compute distance
       dist = audiodb_dot_product(query+qPos*dbH->dim, data_buffer+pp.spos*dbH->dim, dbH->dim*sequenceLength);
@@ -730,12 +740,13 @@
 
     trackIndexOffset=trackOffset/dbH->dim; // numVectors offset
 
-    read_data(dbfid, track, &data_buffer, &data_buffer_size);
+    if(audiodb_read_data(adb, dbfid, track, &data_buffer, &data_buffer_size))
+      error("failed to read data");
     if(sequenceLength <= trackTable[track]) {  // test for short sequences
       
       VERB_LOG(7,"%u.%jd.%u | ", track, (intmax_t) trackIndexOffset, trackTable[track]);
       
-      initialize_arrays(track, numVectors, query, data_buffer, D, DD);
+      initialize_arrays(adb, track, numVectors, query, data_buffer, D, DD);
 
       if(refine->flags & ADB_REFINE_DURATION_RATIO) {
         VERB_LOG(3,"meanQdur=%f meanDBdur=%f\n", meanQdur, meanDBdur[track]);
@@ -777,7 +788,7 @@
           }
         }
       } // Duration match            
-      delete_arrays(track, numVectors, D, DD);
+      audiodb_delete_arrays(track, numVectors, D, DD);
     }
   }