# HG changeset patch # User mas01cr # Date 1228326015 0 # Node ID 58b88ab69424a46ecc96916aab1876778e062024 # Parent a8a5f2ca5380c659288341c42484e76174b8fbb6 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 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(). diff -r a8a5f2ca5380 -r 58b88ab69424 audioDB-internals.h --- 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 *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. */ diff -r a8a5f2ca5380 -r 58b88ab69424 audioDB.cpp --- 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; diff -r a8a5f2ca5380 -r 58b88ab69424 audioDB_API.h --- 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; diff -r a8a5f2ca5380 -r 58b88ab69424 close.cpp --- 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); } diff -r a8a5f2ca5380 -r 58b88ab69424 common.cpp --- 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) diff -r a8a5f2ca5380 -r 58b88ab69424 open.cpp --- 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; + 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) { diff -r a8a5f2ca5380 -r 58b88ab69424 status.cpp --- 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) {