changeset 385:4e68f7d4d524 api-inversion

Invert audiodb_create() / audioDB::create(). (This was straightforward, because audioDB::create() doesn't depend on having any kind of state or reading it from disk, except of course for the locks. New global functions acquire_lock() and divest_lock() [silly name, I know], to be used both within audioDB::get_lock() and audioDB::release_lock() and within (inverted) API functions directly. Eventually the audioDB:: versions will be able to go away entirely)
author mas01cr
date Fri, 21 Nov 2008 15:22:15 +0000
parents 25a4d1799c08
children 4ded52b104e6
files audioDB.cpp common.cpp create.cpp
diffstat 3 files changed, 96 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.cpp	Fri Nov 21 15:22:13 2008 +0000
+++ b/audioDB.cpp	Fri Nov 21 15:22:15 2008 +0000
@@ -1,4 +1,7 @@
 #include "audioDB.h"
+extern "C" {
+#include "audioDB_API.h"
+}
 
 LSH* SERVER_LSH_INDEX_SINGLETON;
 char* SERVER_ADB_ROOT;
@@ -840,6 +843,12 @@
   memcpy(db, dbH, O2_HEADERSIZE);
 }
 
+void audioDB::create(const char *dbName) {
+  if(!audiodb_create(dbName, datasize, ntracks, datadim)) {
+    error("Failed to create database file", dbName, "");
+  }
+}
+
 // Unit norm block of features
 
 /* FIXME: in fact this does not unit norm a block of features, it just
@@ -905,54 +914,6 @@
  * Christophe Rhodes c.rhodes@gold.ac.uk
  * Ian Knopke mas01ik@gold.ac.uk, ian.knopke@gmail.com */
 
-#include "audioDB_API.h"
-
-    adb_ptr audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim) {
-        const char *argv[12];
-        int argvctr=0;
-        char tempstr1[200];
-        char tempstr2[200];
-        char tempstr3[200];
-        int apierror=0;
-
-
-        argv[argvctr++] = "audioDB";
-        argv[argvctr++] = "--NEW";
-        argv[argvctr++] = "-d";
-        argv[argvctr++] = path;
-
-        if (datasize >0){
-            argv[argvctr++]="--datasize";
-            snprintf(tempstr1,sizeof(tempstr1),"%u",datasize);
-            argv[argvctr++]=tempstr1;
-        }
-
-        if (ntracks >0){
-            argv[argvctr++]="--ntracks";
-            snprintf(tempstr2,sizeof(tempstr2),"%u",ntracks);
-            argv[argvctr++]=tempstr2;
-        }
-
-        if (datadim > 0){
-            argv[argvctr++]="--datadim";
-            snprintf(tempstr3,sizeof(tempstr3),"%u",datadim);
-            argv[argvctr++]=tempstr3;
-        }
-
-        argv[argvctr]='\0';
-
-        audioDB::audioDB(argvctr, argv, &apierror);
-
-        if (!apierror){ 
-            return audiodb_open(path);
-        }
-
-        /* database exists, so fail and pass NULL */
-        return NULL;
-    }
-
-
-
   int audiodb_insert(adb_ptr mydb, adb_insert_ptr ins) {
     const char *argv[15];
     int argvctr=0;
--- a/common.cpp	Fri Nov 21 15:22:13 2008 +0000
+++ b/common.cpp	Fri Nov 21 15:22:15 2008 +0000
@@ -10,7 +10,7 @@
 }
 #endif
 
-void audioDB::get_lock(int fd, bool exclusive) {
+int acquire_lock(int fd, bool exclusive) {
   struct flock lock;
   int status;
   
@@ -29,24 +29,33 @@
       sleep(1);
       goto retry;
     } else {
-      error("fcntl lock error", "", "fcntl");
+      return status;
     }
   }
+  return 0;
 }
 
-void audioDB::release_lock(int fd) {
+int divest_lock(int fd) {
   struct flock lock;
-  int status;
 
   lock.l_type = F_UNLCK;
   lock.l_whence = SEEK_SET;
   lock.l_start = 0;
   lock.l_len = 0;
 
-  status = fcntl(fd, F_SETLKW, &lock);
+  return fcntl(fd, F_SETLKW, &lock);
+}
 
-  if (status)
+void audioDB::get_lock(int fd, bool exclusive) {
+  if(acquire_lock(fd, exclusive)) {
+    error("fcntl lock error", "", "fcntl");
+  }
+}
+
+void audioDB::release_lock(int fd) {
+  if (divest_lock(fd)) {
     error("fcntl unlock error", "", "fcntl");
+  }
 }
 
 void audioDB::error(const char* a, const char* b, const char *sysFunc) {
--- a/create.cpp	Fri Nov 21 15:22:13 2008 +0000
+++ b/create.cpp	Fri Nov 21 15:22:15 2008 +0000
@@ -1,5 +1,7 @@
 #include "audioDB.h"
-
+extern "C" {
+#include "audioDB_API.h"
+}
 /* Make a new database.
 
 IF size(featuredata) < O2_LARGE_ADB_SIZE 
@@ -25,69 +27,79 @@
 
 */
 
-void audioDB::create(const char* dbName){
-  if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
-    error("Can't create database file", dbName, "open");
-  get_lock(dbfid, 1);
+int acquire_lock(int, bool);
 
-  VERB_LOG(0, "header size: %ju\n", (intmax_t) O2_HEADERSIZE);
-  
-  dbH = new dbTableHeaderT();
-  assert(dbH);
+extern "C" {
+  adb_t *audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim) {
+    int fd;
+    if(datasize == 0) {
+      datasize = O2_DEFAULT_DATASIZE;
+    }
+    if(ntracks == 0) {
+      ntracks = O2_DEFAULT_NTRACKS;
+    }
+    if(datadim == 0) {
+      datadim = O2_DEFAULT_DATADIM;
+    }
 
-  //unsigned int maxfiles = (unsigned int) rint((double) O2_MAXFILES * (double) size / (double) O2_DEFAULTDBSIZE);
-
-  // Initialize header
-  dbH->magic = O2_MAGIC;
-  dbH->version = O2_FORMAT_VERSION;
-  dbH->numFiles = 0;
-  dbH->dim = 0;
-  dbH->flags = 0;
-  dbH->headerSize = O2_HEADERSIZE;
-  dbH->length = 0;
-  dbH->fileTableOffset = ALIGN_PAGE_UP(O2_HEADERSIZE);
-  dbH->trackTableOffset = ALIGN_PAGE_UP(dbH->fileTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
-  dbH->dataOffset = ALIGN_PAGE_UP(dbH->trackTableOffset + O2_TRACKTABLE_ENTRY_SIZE*ntracks);
-
-  off_t databytes = ((off_t) datasize) * 1024 * 1024;
-  off_t auxbytes = databytes / datadim;
-
-  // For backward-compatibility, Record the point-encoding parameter for LSH indexing in the adb header
-  // If this value is 0 then it will be set to 14
-
+    if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+      return NULL;
+    }
+    if (acquire_lock(fd, true)) {
+      return NULL;
+    }
+    dbTableHeaderT *dbH = new dbTableHeaderT();
+    
+    // Initialize header
+    dbH->magic = O2_MAGIC;
+    dbH->version = O2_FORMAT_VERSION;
+    dbH->numFiles = 0;
+    dbH->dim = 0;
+    dbH->flags = 0;
+    dbH->headerSize = O2_HEADERSIZE;
+    dbH->length = 0;
+    dbH->fileTableOffset = ALIGN_PAGE_UP(O2_HEADERSIZE);
+    dbH->trackTableOffset = ALIGN_PAGE_UP(dbH->fileTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
+    dbH->dataOffset = ALIGN_PAGE_UP(dbH->trackTableOffset + O2_TRACKTABLE_ENTRY_SIZE*ntracks);
+    
+    off_t databytes = ((off_t) datasize) * 1024 * 1024;
+    off_t auxbytes = databytes / datadim;
+    
+    // For backward-compatibility, Record the point-encoding parameter for LSH indexing in the adb header
+    // If this value is 0 then it will be set to 14
+    
 #if O2_LSH_N_POINT_BITS > 15
 #error "AudioDB Compile ERROR: consistency check of O2_LSH_POINT_BITS failed (>15)"
 #endif
-  
-  dbH->flags |= LSH_N_POINT_BITS << 28;
-
-  // If database will fit in a single file the vectors are copied into the AudioDB instance
-  // Else all the vectors are left on the FileSystem and we use the dataOffset as storage
-  // for the location of the features, powers and times files (assuming that arbitrary keys are used for the fileTable)
-  if(ntracks<O2_LARGE_ADB_NTRACKS && datasize<O2_LARGE_ADB_SIZE){
-    dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + databytes);
-    dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + 2*auxbytes);
-    dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + auxbytes);
-    dbH->dbSize = ALIGN_PAGE_UP(dbH->l2normTableOffset + auxbytes);
+    
+    dbH->flags |= LSH_N_POINT_BITS << 28;
+    
+    // If database will fit in a single file the vectors are copied into the AudioDB instance
+    // Else all the vectors are left on the FileSystem and we use the dataOffset as storage
+    // for the location of the features, powers and times files (assuming that arbitrary keys are used for the fileTable)
+    if(ntracks<O2_LARGE_ADB_NTRACKS && datasize<O2_LARGE_ADB_SIZE){
+      dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + databytes);
+      dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + 2*auxbytes);
+      dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + auxbytes);
+      dbH->dbSize = ALIGN_PAGE_UP(dbH->l2normTableOffset + auxbytes);
+    } else { // Create LARGE_ADB, features and powers kept on filesystem 
+      dbH->flags |= O2_FLAG_LARGE_ADB;
+      dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
+      dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
+      dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
+      dbH->dbSize = dbH->l2normTableOffset;
+    } 
+    
+    write(fd, dbH, O2_HEADERSIZE);
+    
+    // go to the location corresponding to the last byte
+    if (lseek (fd, dbH->dbSize - 1, SEEK_SET) == -1)
+      return NULL;
+    
+    // write a dummy byte at the last location
+    if (write (fd, "", 1) != 1)
+      return NULL;
+    
+    return audiodb_open(path);
   }
-  else{ // Create LARGE_ADB, features and powers kept on filesystem 
-    dbH->flags |= O2_FLAG_LARGE_ADB;
-    dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
-    dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
-    dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
-    dbH->dbSize = dbH->l2normTableOffset;
-  } 
-
-  CHECKED_WRITE(dbfid, dbH, O2_HEADERSIZE);
-
-  // go to the location corresponding to the last byte
-  if (lseek (dbfid, dbH->dbSize - 1, SEEK_SET) == -1)
-    error("lseek error in db file", "", "lseek");
-
-  // write a dummy byte at the last location
-  if (write (dbfid, "", 1) != 1)
-    error("write error", "", "write");
-
-  VERB_LOG(0, "%s %s\n", COM_CREATE, dbName);
 }
-