changeset 434:7af140bf8a0a api-inversion

adb_t-ize most of audioDB::set_up_db. All of this "adding code" nonsense (mostly because of the conversion from C++ non-local exits using audioDB::error into lame C "must return and check integer error code") has to have a payoff sometime. That day will come. That day will come.
author mas01cr
date Wed, 24 Dec 2008 10:55:52 +0000
parents 681837f7c903
children 53c487885b2c
files audioDB.h audioDB_API.h query.cpp
diffstat 3 files changed, 46 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.h	Wed Dec 24 10:55:48 2008 +0000
+++ b/audioDB.h	Wed Dec 24 10:55:52 2008 +0000
@@ -323,7 +323,7 @@
   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);
-  void set_up_db(double **snp, double **vsnp, double **spp, double **vspp, double **mddp, unsigned int *dvp);
+  int set_up_db(adb_t *adb, double **snp, double **vsnp, double **spp, double **vspp, double **mddp, unsigned int *dvp);
   void query_loop(adb_query_parameters_t *params, adb_query_refine_t *refine, Uns32T queryIndex);
   void query_loop_points(double* query, double* qnPtr, double* qpPtr, double meanQdur, Uns32T numVectors, adb_query_parameters_t *params, adb_query_refine_t *refine);
   void initRNG();
--- a/audioDB_API.h	Wed Dec 24 10:55:48 2008 +0000
+++ b/audioDB_API.h	Wed Dec 24 10:55:52 2008 +0000
@@ -140,11 +140,13 @@
   adb_result_t *results;
 } adb_query_results_t;
 
+#define ADB_QUERY_ID_FLAG_EXHAUSTIVE 1
+
 typedef struct adbqueryid {
   adb_datum_t *datum;
   uint32_t sequence_length;
+  uint32_t flags;
   uint32_t sequence_start;
-  uint32_t exhaustive;
 } adb_query_id_t;
 
 typedef struct adbqueryspec {
--- a/query.cpp	Wed Dec 24 10:55:48 2008 +0000
+++ b/query.cpp	Wed Dec 24 10:55:52 2008 +0000
@@ -494,49 +494,52 @@
 // FIXME: this is not the right name; we're not actually setting up
 // the database, but copying various bits of it out of mmap()ed tables
 // in order to reduce seeks.
-void audioDB::set_up_db(double **snp, double **vsnp, double **spp, double **vspp, double **mddp, unsigned int *dvp) {
-  *dvp = dbH->length / (dbH->dim * sizeof(double));
+int audioDB::set_up_db(adb_t *adb, double **snp, double **vsnp, double **spp, double **vspp, double **mddp, unsigned int *dvp) {
+  *dvp = adb->header->length / (adb->header->dim * sizeof(double));
   *snp = new double[*dvp];
 
   double *snpp = *snp, *sppp = 0;
-  memcpy(*snp, l2normTable, *dvp * sizeof(double));
+  lseek(adb->fd, adb->header->l2normTableOffset, SEEK_SET);
+  read_or_goto_error(adb->fd, *snp, *dvp * sizeof(double));
 
   if (usingPower) {
-    if (!(dbH->flags & O2_FLAG_POWER)) {
-      error("database not power-enabled", dbName);
+    if (!(adb->header->flags & O2_FLAG_POWER)) {
+      goto error;
     }
     *spp = new double[*dvp];
     sppp = *spp;
-    memcpy(*spp, powerTable, *dvp * sizeof(double));
+    lseek(adb->fd, adb->header->powerTableOffset, SEEK_SET);
+    read_or_goto_error(adb->fd, *spp, *dvp * sizeof(double));
   }
 
-  for(unsigned int i = 0; i < dbH->numFiles; i++){
-    if(trackTable[i] >= sequenceLength) {
-      audiodb_sequence_sum(snpp, trackTable[i], sequenceLength);
-      audiodb_sequence_sqrt(snpp, trackTable[i], sequenceLength);
-
+  for(unsigned int i = 0; i < adb->header->numFiles; i++){
+    size_t track_length = (*adb->track_lengths)[i];
+    if(track_length >= sequenceLength) {
+      audiodb_sequence_sum(snpp, track_length, sequenceLength);
+      audiodb_sequence_sqrt(snpp, track_length, sequenceLength);
       if (usingPower) {
-	audiodb_sequence_sum(sppp, trackTable[i], sequenceLength);
-        audiodb_sequence_average(sppp, trackTable[i], sequenceLength);
+	audiodb_sequence_sum(sppp, track_length, sequenceLength);
+        audiodb_sequence_average(sppp, track_length, sequenceLength);
       }
     }
-    snpp += trackTable[i];
+    snpp += track_length;
     if (usingPower) {
-      sppp += trackTable[i];
+      sppp += track_length;
     }
   }
 
   if (usingTimes) {
-    if(!(dbH->flags & O2_FLAG_TIMES)) {
+    if(!(adb->header->flags & O2_FLAG_TIMES)) {
       error("query timestamps provided for non-timed database", dbName);
     }
 
-    *mddp = new double[dbH->numFiles];
+    *mddp = new double[adb->header->numFiles];
 
-    for(unsigned int k = 0; k < dbH->numFiles; k++) {
+    for(unsigned int k = 0; k < adb->header->numFiles; k++) {
+      size_t track_length = (*adb->track_lengths)[k];
       unsigned int j;
       (*mddp)[k] = 0.0;
-      for(j = 0; j < trackTable[k]; j++) {
+      for(j = 0; j < track_length; j++) {
 	(*mddp)[k] += timesTable[2*j+1] - timesTable[2*j];
       }
       (*mddp)[k] /= j;
@@ -545,6 +548,20 @@
 
   *vsnp = *snp;
   *vspp = *spp;
+  return 0;
+
+ error:
+  if(*snp) {
+    delete [] *snp;
+  }
+  if(*spp) {
+    delete [] *spp;
+  }
+  if(*mddp) {
+    delete [] *mddp;
+  }
+  return 1;
+  
 }
 
 // query_points()
@@ -575,7 +592,9 @@
   // FIXME: we more than likely don't need very much of the database
   // so make a new method to build these values per-track or, even better, per-point
   if( !( dbH->flags & O2_FLAG_LARGE_ADB) )
-    set_up_db(&sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors);
+    if(set_up_db(adb, &sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors)) {
+      error("failed to set up db");
+    }
 
   VERB_LOG(1, "matching points...");
 
@@ -686,7 +705,9 @@
   double *sNorm, *snPtr, *sPower = 0, *spPtr = 0;
   double *meanDBdur = 0;
 
-  set_up_db(&sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors);
+  if(set_up_db(adb, &sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors)) {
+    error("failed to set up db");
+  }
 
   VERB_LOG(1, "matching tracks...");