Mercurial > hg > audiodb
comparison index.cpp @ 460:17003dff8127 api-inversion
Convert index-finding logic to C functions.
New audiodb_index_exists() and audiodb_index_get_name(). The behaviours
of the functions are unchanged, even though they are kind of weird; in
particular, passing in a buffer rather than returning a char * for
audiodb_index_get_name() is probably likely to work better eventually
(see the changes to soap.cpp, which show a clear leak of a buffer in the
error case)
author | mas01cr |
---|---|
date | Tue, 30 Dec 2008 10:36:01 +0000 |
parents | fcc6f7c4856b |
children | 35bb388d0eac |
comparison
equal
deleted
inserted
replaced
459:fcc6f7c4856b | 460:17003dff8127 |
---|---|
19 adb_qstate_internal_t *qstate; | 19 adb_qstate_internal_t *qstate; |
20 } adb_qcallback_t; | 20 } adb_qcallback_t; |
21 | 21 |
22 /************************* LSH indexing and query initialization *****************/ | 22 /************************* LSH indexing and query initialization *****************/ |
23 | 23 |
24 char* audioDB::index_get_name(const char*dbName, double radius, Uns32T sequenceLength){ | 24 /* FIXME: there are several things wrong with this: the memory |
25 char* indexName = new char[MAXSTR]; | 25 * discipline isn't ideal, the radius printing is a bit lame, the name |
26 // Attempt to make new file | 26 * getting will succeed or fail depending on whether the path was |
27 if(strlen(dbName) > (MAXSTR - 32)) | 27 * relative or absolute -- but most importantly encoding all that |
28 error("dbName is too long for LSH index filename appendages"); | 28 * information in a filename is going to lose: it's impossible to |
29 * maintain backwards-compatibility. Instead we should probably store | |
30 * the index metadata inside the audiodb instance. */ | |
31 char *audiodb_index_get_name(const char *dbName, double radius, Uns32T sequenceLength) { | |
32 char *indexName; | |
33 if(strlen(dbName) > (MAXSTR - 32)) { | |
34 return NULL; | |
35 } | |
36 indexName = new char[MAXSTR]; | |
29 strncpy(indexName, dbName, MAXSTR); | 37 strncpy(indexName, dbName, MAXSTR); |
30 sprintf(indexName+strlen(dbName), ".lsh.%019.9f.%d", radius, sequenceLength); | 38 sprintf(indexName+strlen(dbName), ".lsh.%019.9f.%d", radius, sequenceLength); |
31 return indexName; | 39 return indexName; |
32 } | 40 } |
33 | 41 |
34 // return true if index exists else return false | 42 bool audiodb_index_exists(const char *dbName, double radius, Uns32T sequenceLength) { |
35 int audioDB::index_exists(const char* dbName, double radius, Uns32T sequenceLength){ | 43 char *indexName = audiodb_index_get_name(dbName, radius, sequenceLength); |
36 // Test to see if file exists | 44 if(!indexName) { |
37 char* indexName = index_get_name(dbName, radius, sequenceLength); | 45 return false; |
38 lshfid = open (indexName, O_RDONLY); | 46 } |
39 delete[] indexName; | 47 struct stat st; |
40 close(lshfid); | 48 if(stat(indexName, &st)) { |
41 | 49 delete [] indexName; |
42 if(lshfid<0) | 50 return false; |
43 return false; | 51 } |
44 else | 52 /* FIXME: other stat checks here? */ |
53 /* FIXME: is there any better way to check whether we can open a | |
54 * file for reading than by opening a file for reading? */ | |
55 int fd = open(indexName, O_RDONLY); | |
56 delete [] indexName; | |
57 if(fd < 0) { | |
58 return false; | |
59 } else { | |
60 close(fd); | |
45 return true; | 61 return true; |
62 } | |
46 } | 63 } |
47 | 64 |
48 // If we are a server and have a memory-resident index, check the indexName against the resident index (using get_indexName()) | 65 // If we are a server and have a memory-resident index, check the indexName against the resident index (using get_indexName()) |
49 // If they match, i.e. path+dbName_resident == path+dbName_requested, use | 66 // If they match, i.e. path+dbName_resident == path+dbName_requested, use |
50 // the memory-resident index. | 67 // the memory-resident index. |
204 usingPower = true; | 221 usingPower = true; |
205 | 222 |
206 if(dbH->flags & O2_FLAG_TIMES) | 223 if(dbH->flags & O2_FLAG_TIMES) |
207 usingTimes = true; | 224 usingTimes = true; |
208 | 225 |
209 newIndexName = index_get_name(dbName, radius, sequenceLength); | 226 newIndexName = audiodb_index_get_name(dbName, radius, sequenceLength); |
227 if(!newIndexName) { | |
228 error("failed to get index name", dbName); | |
229 } | |
210 | 230 |
211 // Set unit norming flag override | 231 // Set unit norming flag override |
212 audioDB::normalizedDistance = !audioDB::no_unit_norming; | 232 audioDB::normalizedDistance = !audioDB::no_unit_norming; |
213 | 233 |
214 VERB_LOG(1, "INDEX: dim %d\n", (int)dbH->dim); | 234 VERB_LOG(1, "INDEX: dim %d\n", (int)dbH->dim); |
493 | 513 |
494 | 514 |
495 // return true if indexed query performed else return false | 515 // return true if indexed query performed else return false |
496 int audioDB::index_init_query(const char* dbName){ | 516 int audioDB::index_init_query(const char* dbName){ |
497 | 517 |
498 if(!(index_exists(dbName, radius, sequenceLength))) | 518 if(!(audiodb_index_exists(dbName, radius, sequenceLength))) |
499 return false; | 519 return false; |
500 | 520 |
501 char* indexName = index_get_name(dbName, radius, sequenceLength); | 521 char *indexName = audiodb_index_get_name(dbName, radius, sequenceLength); |
522 if(!indexName) { | |
523 error("failed to get index name", dbName); | |
524 } | |
502 | 525 |
503 // Test to see if file exists | 526 // Test to see if file exists |
504 if((lshfid = open (indexName, O_RDONLY)) < 0){ | 527 if((lshfid = open (indexName, O_RDONLY)) < 0){ |
505 delete[] indexName; | 528 delete[] indexName; |
506 return false; | 529 return false; |
595 } | 618 } |
596 | 619 |
597 if(!index_init_query(adb->path)) // sets-up LSH index structures for querying | 620 if(!index_init_query(adb->path)) // sets-up LSH index structures for querying |
598 return 0; | 621 return 0; |
599 | 622 |
600 char* database = index_get_name(adb->path, radius, sequenceLength); | 623 char *database = audiodb_index_get_name(adb->path, radius, sequenceLength); |
624 if(!database) { | |
625 error("failed to get index name", adb->path); | |
626 } | |
601 | 627 |
602 if(audiodb_query_spec_qpointers(adb, spec, &query_data, &query, &qpointers)) { | 628 if(audiodb_query_spec_qpointers(adb, spec, &query_data, &query, &qpointers)) { |
603 error("failed to set up qpointers"); | 629 error("failed to set up qpointers"); |
604 } | 630 } |
605 | 631 |