changeset 402:58b88ab69424 api-inversion

Move the struct adb definition from the auidioDB_API.h into the audioDB-internals.h header file, leaving only the typedef behind. Thus a user of the API sees only an incomplete type, which cannot be instantiated (but /pointers/ to it can); there's then less temptation to break the abstraction barrier by using structure fields in client code. Not only that, but we can now safely put C++ stuff in the structure. Take advantage of this by putting a std::set<std::string> in there, to hold all the keys currently in the database; populate this field on audiodb_open() (and delete it on audiodb_close). This will be useful when we come to implement variants of audiodb_insert().
author mas01cr
date Wed, 03 Dec 2008 17:40:15 +0000
parents a8a5f2ca5380
children 7038f31124d1
files audioDB-internals.h audioDB.cpp audioDB_API.h close.cpp common.cpp open.cpp status.cpp
diffstat 7 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB-internals.h	Wed Dec 03 14:53:20 2008 +0000
+++ b/audioDB-internals.h	Wed Dec 03 17:40:15 2008 +0000
@@ -1,3 +1,11 @@
+struct adb {
+  char *path;
+  int fd;
+  int flags;
+  adb_header_t *header;
+  std::set<std::string> *keys;
+};
+
 /* We could go gcc-specific here and use typeof() instead of passing
  * in an explicit type.  Answers on a postcard as to whether that's a
  * good plan or not. */
--- a/audioDB.cpp	Wed Dec 03 14:53:20 2008 +0000
+++ b/audioDB.cpp	Wed Dec 03 17:40:15 2008 +0000
@@ -1,6 +1,7 @@
 #include "audioDB.h"
 extern "C" {
 #include "audioDB_API.h"
+#include "audioDB-internals.h"
 }
 
 LSH* SERVER_LSH_INDEX_SINGLETON;
--- a/audioDB_API.h	Wed Dec 03 14:53:20 2008 +0000
+++ b/audioDB_API.h	Wed Dec 03 17:40:15 2008 +0000
@@ -17,12 +17,6 @@
  * kinds of other interesting information */
 /* This basically gets passed around to all of the other functions */
 
-struct adb {
-  char *path;
-  int fd;
-  int flags;
-  adb_header_t *header;
-};
 /* FIXME: it might be that "adb_" isn't such a good prefix to use, and
    that we should prefer "audiodb_" */
 typedef struct adb adb_t, *adb_ptr;
--- a/close.cpp	Wed Dec 03 14:53:20 2008 +0000
+++ b/close.cpp	Wed Dec 03 17:40:15 2008 +0000
@@ -1,11 +1,13 @@
 #include "audioDB.h"
 extern "C" {
 #include "audioDB_API.h"
+#include "audioDB-internals.h"
 }
 
 void audiodb_close(adb_t *adb) {
   free(adb->path);
   free(adb->header);
+  delete adb->keys;
   close(adb->fd);
   free(adb);
 }
--- a/common.cpp	Wed Dec 03 14:53:20 2008 +0000
+++ b/common.cpp	Wed Dec 03 17:40:15 2008 +0000
@@ -1,6 +1,7 @@
 #include "audioDB.h"
 extern "C" {
 #include "audioDB_API.h"
+#include "audioDB-internals.h"
 }
 
 #if defined(O2_DEBUG)
--- a/open.cpp	Wed Dec 03 14:53:20 2008 +0000
+++ b/open.cpp	Wed Dec 03 17:40:15 2008 +0000
@@ -1,9 +1,10 @@
 #include "audioDB.h"
 extern "C" {
 #include "audioDB_API.h"
+#include "audioDB-internals.h"
 }
 
-bool audiodb_check_header(adb_header_t *header) {
+static bool audiodb_check_header(adb_header_t *header) {
   /* FIXME: use syslog() or write to stderr or something to give the
      poor user some diagnostics. */
   if(header->magic == O2_OLD_MAGIC) {
@@ -21,6 +22,27 @@
   return true;
 }
 
+static int audiodb_collect_keys(adb_t *adb) {
+  char *key_table = 0;
+  size_t key_table_length = 0;
+
+  if(adb->header->length > 0) {
+    unsigned nfiles = adb->header->numFiles;
+    key_table_length = ALIGN_PAGE_UP(nfiles * O2_FILETABLE_ENTRY_SIZE);
+    mmap_or_goto_error(char *, key_table, adb->header->fileTableOffset, key_table_length);
+    for (unsigned i = 0; i < nfiles; i++) {
+      adb->keys->insert(key_table + i*O2_FILETABLE_ENTRY_SIZE);
+    }
+    munmap(key_table, key_table_length);
+  }
+
+  return 0;
+
+ error:
+  maybe_munmap(key_table, key_table_length);
+  return 1;
+}
+
 adb_t *audiodb_open(const char *path, int flags) {
   adb_t *adb = 0;
   int fd = -1;
@@ -57,6 +79,13 @@
     goto error;
   }
 
+  adb->keys = new std::set<std::string>;
+  if(!adb->keys) {
+    goto error;
+  }
+  if(audiodb_collect_keys(adb)) {
+    goto error;
+  }
   return adb;
 
  error:
@@ -67,6 +96,9 @@
     if(adb->path) {
       free(adb->path);
     }
+    if(adb->keys) {
+      delete adb->keys;
+    }
     free(adb);
   }
   if(fd != -1) {
--- a/status.cpp	Wed Dec 03 14:53:20 2008 +0000
+++ b/status.cpp	Wed Dec 03 17:40:15 2008 +0000
@@ -1,6 +1,7 @@
 #include "audioDB.h"
 extern "C" {
 #include "audioDB_API.h"
+#include "audioDB-internals.h"
 }
 
 int audiodb_status(adb_t *adb, adb_status_t *status) {