Mercurial > hg > audiodb
changeset 430:2d14d21f826b api-inversion
Make the keys std::set in adb_t a std::map instead.
The key's index can usefully be the value, and now suddenly we can
remove audioDB::getKeyPos and its oh-so-helpful "this should be an STL
map" comment.
author | mas01cr |
---|---|
date | Wed, 24 Dec 2008 10:55:36 +0000 |
parents | 5893ec2ec246 |
children | 8632cd387e24 |
files | audioDB-internals.h audioDB.h insert.cpp open.cpp query.cpp |
diffstat | 5 files changed, 24 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/audioDB-internals.h Wed Dec 24 10:55:32 2008 +0000 +++ b/audioDB-internals.h Wed Dec 24 10:55:36 2008 +0000 @@ -12,7 +12,7 @@ int fd; int flags; adb_header_t *header; - std::set<std::string> *keys; + std::map<std::string,uint32_t> *keys; }; typedef struct { @@ -169,3 +169,13 @@ buffer++; } } + +static inline uint32_t audiodb_key_index(adb_t *adb, const char *key) { + std::map<std::string,uint32_t>::iterator it; + it = adb->keys->find(key); + if(it == adb->keys->end()) { + return (uint32_t) -1; + } else { + return (*it).second; + } +}
--- a/audioDB.h Wed Dec 24 10:55:32 2008 +0000 +++ b/audioDB.h Wed Dec 24 10:55:36 2008 +0000 @@ -11,6 +11,7 @@ #include <iostream> #include <fstream> #include <set> +#include <map> #include <string> #include <math.h> #include <sys/time.h> @@ -332,7 +333,6 @@ void initInputFile(const char *inFile, bool loadData = true); void initTables(const char* dbName, const char* inFile = 0); void initTablesFromKey(const char* dbName, const Uns32T queryIndex); - unsigned getKeyPos(const char* key); void prefix_name(char** const name, const char* prefix); public: @@ -474,7 +474,7 @@ timesTol(0.1), \ radius(0), \ query_from_key(false), \ - query_from_key_index(O2_ERR_KEYNOTFOUND), \ + query_from_key_index((uint32_t) -1), \ use_absolute_threshold(false), \ absolute_threshold(0.0), \ use_relative_threshold(false), \
--- a/insert.cpp Wed Dec 24 10:55:32 2008 +0000 +++ b/insert.cpp Wed Dec 24 10:55:36 2008 +0000 @@ -195,7 +195,7 @@ } /* 8. update adb->keys and adb->header; */ - adb->keys->insert(datum->key); + (*adb->keys)[datum->key] = adb->header->numFiles; adb->header->numFiles += 1; adb->header->length += sizeof(double) * datum->nvectors * datum->dim;
--- a/open.cpp Wed Dec 24 10:55:32 2008 +0000 +++ b/open.cpp Wed Dec 24 10:55:36 2008 +0000 @@ -30,8 +30,8 @@ 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); + for (unsigned int k = 0; k < nfiles; k++) { + (*adb->keys)[(key_table + k*O2_FILETABLE_ENTRY_SIZE)] = k; } munmap(key_table, key_table_length); } @@ -79,7 +79,7 @@ goto error; } - adb->keys = new std::set<std::string>; + adb->keys = new std::map<std::string,uint32_t>; if(!adb->keys) { goto error; }
--- a/query.cpp Wed Dec 24 10:55:32 2008 +0000 +++ b/query.cpp Wed Dec 24 10:55:36 2008 +0000 @@ -52,8 +52,8 @@ initTables(dbName, inFile); // keyKeyPos requires dbH to be initialized - if(query_from_key && (!key || (query_from_key_index = getKeyPos((char*)key))==O2_ERR_KEYNOTFOUND)) - error("Query key not found :",key); + if(query_from_key && (!key || (query_from_key_index = audiodb_key_index(adb, key)) == (uint32_t) -1)) + error("Query key not found", key); switch (queryType) { case O2_POINT_QUERY: @@ -127,24 +127,12 @@ adb_query_results_t *rs = accumulator->get_points(); for(unsigned int k = 0; k < rs->nresults; k++) { adb_result_t r = rs->results[k]; - reporter->add_point(getKeyPos(r.key), r.qpos, r.ipos, r.dist); + reporter->add_point(audiodb_key_index(adb, r.key), r.qpos, r.ipos, r.dist); } reporter->report(fileTable, adbQueryResponse); } -// return ordinal position of key in keyTable -// this should really be a STL hash map search -unsigned audioDB::getKeyPos(const char* key){ - if(!dbH) - error("dbH not initialized","getKeyPos"); - for(unsigned k=0; k<dbH->numFiles; k++) - if(strncmp(fileTable + k*O2_FILETABLE_ENTRY_SIZE, key, strlen(key))==0) - return k; - error("Key not found",key); - return O2_ERR_KEYNOTFOUND; -} - void audioDB::initialize_arrays(int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD) { unsigned int j, k, l, w; double *dp, *qp, *sp; @@ -684,7 +672,10 @@ if(trackFile) { trackFile->getline(nextKey,MAXSTR); if(!trackFile->eof()) { - track = getKeyPos(nextKey); + track = audiodb_key_index(adb, nextKey); + if(track == (uint32_t) -1) { + error("key not found", nextKey); + } trackOffset = trackOffsetTable[track]; lseek(dbfid, dbH->dataOffset + trackOffset * sizeof(double), SEEK_SET); } else {