comparison query.cpp @ 509:cc2b97d020b1

Code rearrangements to tease apart library code from C++ audioDB code. There should be precisely no functional changes in this commit. Instead, the only thing that has happened is that all the abstraction violation and other horribleness is concentrated in one place: the include of "audioDB-internals.h" in audioDB.h -- the separation will be complete once that include can be removed. This include is necessary because the command-line binary / SOAP server still does some things directly rather than through an API: not least of which the operations that have not yet been integrated into the API yet, but also some messing around with constants, flags and nominally internal functions. The intent is to remove as many of these as possible and think quite hard about the rest. In the meantime, the library is now much more self-contained: the only things it uses are in the audioDB_API.h and audioDB-internals.h headers; thus there are fewer nasty surprises lurking for readers of the code. The Makefile has been adjusted to take advantage of this rearrangement in the dependencies.
author mas01cr
date Thu, 15 Jan 2009 13:57:33 +0000
parents 342822c2d49a
children 7ee6a2701d90 633614461994
comparison
equal deleted inserted replaced
508:23c47e118bc6 509:cc2b97d020b1
1 #include "audioDB.h" 1 extern "C" {
2 #include "audioDB_API.h"
3 }
2 #include "audioDB-internals.h" 4 #include "audioDB-internals.h"
3 #include "accumulators.h" 5 #include "accumulators.h"
4 6
5 bool audiodb_powers_acceptable(const adb_query_refine_t *r, double p1, double p2) { 7 bool audiodb_powers_acceptable(const adb_query_refine_t *r, double p1, double p2) {
6 if (r->flags & ADB_REFINE_ABSOLUTE_THRESHOLD) { 8 if (r->flags & ADB_REFINE_ABSOLUTE_THRESHOLD) {
196 return 1; 198 return 1;
197 } 199 }
198 200
199 int audiodb_track_id_datum(adb_t *adb, uint32_t track_id, adb_datum_t *d) { 201 int audiodb_track_id_datum(adb_t *adb, uint32_t track_id, adb_datum_t *d) {
200 off_t track_offset = (*adb->track_offsets)[track_id]; 202 off_t track_offset = (*adb->track_offsets)[track_id];
201 if(adb->header->flags & O2_FLAG_LARGE_ADB) { 203 if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) {
202 /* create a reference/insert, then use adb_insert_create_datum() */ 204 /* create a reference/insert, then use adb_insert_create_datum() */
203 adb_reference_t reference = {0}; 205 adb_reference_t reference = {0};
204 char features[MAXSTR], power[MAXSTR], times[MAXSTR]; 206 char features[ADB_MAXSTR], power[ADB_MAXSTR], times[ADB_MAXSTR];
205 lseek(adb->fd, adb->header->dataOffset + track_id * O2_FILETABLE_ENTRY_SIZE, SEEK_SET); 207 lseek(adb->fd, adb->header->dataOffset + track_id * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET);
206 read_or_goto_error(adb->fd, features, MAXSTR); 208 read_or_goto_error(adb->fd, features, ADB_MAXSTR);
207 reference.features = features; 209 reference.features = features;
208 if(adb->header->flags & O2_FLAG_POWER) { 210 if(adb->header->flags & ADB_HEADER_FLAG_POWER) {
209 lseek(adb->fd, adb->header->powerTableOffset + track_id * O2_FILETABLE_ENTRY_SIZE, SEEK_SET); 211 lseek(adb->fd, adb->header->powerTableOffset + track_id * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET);
210 read_or_goto_error(adb->fd, power, MAXSTR); 212 read_or_goto_error(adb->fd, power, ADB_MAXSTR);
211 reference.power = power; 213 reference.power = power;
212 } 214 }
213 if(adb->header->flags & O2_FLAG_TIMES) { 215 if(adb->header->flags & ADB_HEADER_FLAG_TIMES) {
214 lseek(adb->fd, adb->header->timesTableOffset + track_id * O2_FILETABLE_ENTRY_SIZE, SEEK_SET); 216 lseek(adb->fd, adb->header->timesTableOffset + track_id * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET);
215 read_or_goto_error(adb->fd, times, MAXSTR); 217 read_or_goto_error(adb->fd, times, ADB_MAXSTR);
216 reference.times = times; 218 reference.times = times;
217 } 219 }
218 return audiodb_insert_create_datum(&reference, d); 220 return audiodb_insert_create_datum(&reference, d);
219 } else { 221 } else {
220 /* initialize from sources of data that we already have */ 222 /* initialize from sources of data that we already have */
223 d->key = (*adb->keys)[track_id].c_str(); 225 d->key = (*adb->keys)[track_id].c_str();
224 /* read out stuff from the database tables */ 226 /* read out stuff from the database tables */
225 d->data = (double *) malloc(d->nvectors * d->dim * sizeof(double)); 227 d->data = (double *) malloc(d->nvectors * d->dim * sizeof(double));
226 lseek(adb->fd, adb->header->dataOffset + track_offset, SEEK_SET); 228 lseek(adb->fd, adb->header->dataOffset + track_offset, SEEK_SET);
227 read_or_goto_error(adb->fd, d->data, d->nvectors * d->dim * sizeof(double)); 229 read_or_goto_error(adb->fd, d->data, d->nvectors * d->dim * sizeof(double));
228 if(adb->header->flags & O2_FLAG_POWER) { 230 if(adb->header->flags & ADB_HEADER_FLAG_POWER) {
229 d->power = (double *) malloc(d->nvectors * sizeof(double)); 231 d->power = (double *) malloc(d->nvectors * sizeof(double));
230 lseek(adb->fd, adb->header->powerTableOffset + track_offset / d->dim, SEEK_SET); 232 lseek(adb->fd, adb->header->powerTableOffset + track_offset / d->dim, SEEK_SET);
231 read_or_goto_error(adb->fd, d->power, d->nvectors * sizeof(double)); 233 read_or_goto_error(adb->fd, d->power, d->nvectors * sizeof(double));
232 } 234 }
233 if(adb->header->flags & O2_FLAG_TIMES) { 235 if(adb->header->flags & ADB_HEADER_FLAG_TIMES) {
234 d->times = (double *) malloc(2 * d->nvectors * sizeof(double)); 236 d->times = (double *) malloc(2 * d->nvectors * sizeof(double));
235 lseek(adb->fd, adb->header->timesTableOffset + track_offset / d->dim, SEEK_SET); 237 lseek(adb->fd, adb->header->timesTableOffset + track_offset / d->dim, SEEK_SET);
236 read_or_goto_error(adb->fd, d->times, 2 * d->nvectors * sizeof(double)); 238 read_or_goto_error(adb->fd, d->times, 2 * d->nvectors * sizeof(double));
237 } 239 }
238 return 0; 240 return 0;
352 double *snpp = dbpointers->l2norm_data, *sppp = 0; 354 double *snpp = dbpointers->l2norm_data, *sppp = 0;
353 lseek(adb->fd, adb->header->l2normTableOffset, SEEK_SET); 355 lseek(adb->fd, adb->header->l2normTableOffset, SEEK_SET);
354 read_or_goto_error(adb->fd, dbpointers->l2norm_data, nvectors * sizeof(double)); 356 read_or_goto_error(adb->fd, dbpointers->l2norm_data, nvectors * sizeof(double));
355 357
356 if (using_power) { 358 if (using_power) {
357 if (!(adb->header->flags & O2_FLAG_POWER)) { 359 if (!(adb->header->flags & ADB_HEADER_FLAG_POWER)) {
358 goto error; 360 goto error;
359 } 361 }
360 dbpointers->power_data = new double[nvectors]; 362 dbpointers->power_data = new double[nvectors];
361 sppp = dbpointers->power_data; 363 sppp = dbpointers->power_data;
362 lseek(adb->fd, adb->header->powerTableOffset, SEEK_SET); 364 lseek(adb->fd, adb->header->powerTableOffset, SEEK_SET);
378 sppp += track_length; 380 sppp += track_length;
379 } 381 }
380 } 382 }
381 383
382 if (using_times) { 384 if (using_times) {
383 if(!(adb->header->flags & O2_FLAG_TIMES)) { 385 if(!(adb->header->flags & ADB_HEADER_FLAG_TIMES)) {
384 goto error; 386 goto error;
385 } 387 }
386 388
387 dbpointers->mean_duration = new double[adb->header->numFiles]; 389 dbpointers->mean_duration = new double[adb->header->numFiles];
388 390
448 Uns32T currentTrack = 0x80000000; // KLUDGE: Initialize with a value outside of track index range 450 Uns32T currentTrack = 0x80000000; // KLUDGE: Initialize with a value outside of track index range
449 Uns32T npairs = qstate->exact_evaluation_queue->size(); 451 Uns32T npairs = qstate->exact_evaluation_queue->size();
450 while(npairs--) { 452 while(npairs--) {
451 PointPair pp = qstate->exact_evaluation_queue->top(); 453 PointPair pp = qstate->exact_evaluation_queue->top();
452 if(currentTrack != pp.trackID) { 454 if(currentTrack != pp.trackID) {
453 SAFE_DELETE_ARRAY(dbdata); 455 maybe_delete_array(dbdata);
454 SAFE_DELETE_ARRAY(dbpointers.l2norm_data); 456 maybe_delete_array(dbpointers.l2norm_data);
455 SAFE_DELETE_ARRAY(dbpointers.power_data); 457 maybe_delete_array(dbpointers.power_data);
456 SAFE_DELETE_ARRAY(dbpointers.mean_duration); 458 maybe_delete_array(dbpointers.mean_duration);
457 currentTrack = pp.trackID; 459 currentTrack = pp.trackID;
458 adb_datum_t d = {0}; 460 adb_datum_t d = {0};
459 if(audiodb_track_id_datum(adb, pp.trackID, &d)) { 461 if(audiodb_track_id_datum(adb, pp.trackID, &d)) {
460 delete qstate->exact_evaluation_queue; 462 delete qstate->exact_evaluation_queue;
461 return 1; 463 return 1;
483 case ADB_DISTANCE_EUCLIDEAN: 485 case ADB_DISTANCE_EUCLIDEAN:
484 dist = qn*qn + sn*sn - 2*dist; 486 dist = qn*qn + sn*sn - 2*dist;
485 break; 487 break;
486 } 488 }
487 if((!(spec->refine.flags & ADB_REFINE_RADIUS)) || 489 if((!(spec->refine.flags & ADB_REFINE_RADIUS)) ||
488 dist <= (spec->refine.radius+O2_DISTANCE_TOLERANCE)) { 490 dist <= (spec->refine.radius + ADB_DISTANCE_TOLERANCE)) {
489 adb_result_t r; 491 adb_result_t r;
490 r.key = (*adb->keys)[pp.trackID].c_str(); 492 r.key = (*adb->keys)[pp.trackID].c_str();
491 r.dist = dist; 493 r.dist = dist;
492 r.qpos = pp.qpos; 494 r.qpos = pp.qpos;
493 r.ipos = pp.spos; 495 r.ipos = pp.spos;
495 } 497 }
496 } 498 }
497 qstate->exact_evaluation_queue->pop(); 499 qstate->exact_evaluation_queue->pop();
498 } 500 }
499 501
500
501 // Cleanup 502 // Cleanup
502 SAFE_DELETE_ARRAY(dbdata); 503 maybe_delete_array(dbdata);
503 SAFE_DELETE_ARRAY(dbpointers.l2norm_data); 504 maybe_delete_array(dbpointers.l2norm_data);
504 SAFE_DELETE_ARRAY(dbpointers.power_data); 505 maybe_delete_array(dbpointers.power_data);
505 SAFE_DELETE_ARRAY(dbpointers.mean_duration); 506 maybe_delete_array(dbpointers.mean_duration);
506 delete qstate->exact_evaluation_queue; 507 delete qstate->exact_evaluation_queue;
507 return 0; 508 return 0;
508 } 509 }
509 510
510 int audiodb_query_loop(adb_t *adb, const adb_query_spec_t *spec, adb_qstate_internal_t *qstate) { 511 int audiodb_query_loop(adb_t *adb, const adb_query_spec_t *spec, adb_qstate_internal_t *qstate) {
512 double *query, *query_data; 513 double *query, *query_data;
513 adb_qpointers_internal_t qpointers = {0}, dbpointers = {0}; 514 adb_qpointers_internal_t qpointers = {0}, dbpointers = {0};
514 515
515 bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD); 516 bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD);
516 517
517 if(adb->header->flags & O2_FLAG_LARGE_ADB) { 518 if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) {
518 /* FIXME: actually it would be nice to support this mode of 519 /* FIXME: actually it would be nice to support this mode of
519 * operation, but for now... */ 520 * operation, but for now... */
520 return 1; 521 return 1;
521 } 522 }
522 523
588 } 589 }
589 // Power test 590 // Power test
590 if ((!power_refine) || audiodb_powers_acceptable(&spec->refine, qpointers.power[j], dbpointers.power[trackIndexOffset + k])) { 591 if ((!power_refine) || audiodb_powers_acceptable(&spec->refine, qpointers.power[j], dbpointers.power[trackIndexOffset + k])) {
591 // radius test 592 // radius test
592 if((!(spec->refine.flags & ADB_REFINE_RADIUS)) || 593 if((!(spec->refine.flags & ADB_REFINE_RADIUS)) ||
593 thisDist <= (spec->refine.radius+O2_DISTANCE_TOLERANCE)) { 594 thisDist <= (spec->refine.radius + ADB_DISTANCE_TOLERANCE)) {
594 adb_result_t r; 595 adb_result_t r;
595 r.key = (*adb->keys)[track].c_str(); 596 r.key = (*adb->keys)[track].c_str();
596 r.dist = thisDist; 597 r.dist = thisDist;
597 if(spec->qid.flags & ADB_QID_FLAG_EXHAUSTIVE) { 598 if(spec->qid.flags & ADB_QID_FLAG_EXHAUSTIVE) {
598 r.qpos = j; 599 r.qpos = j;