comparison query.cpp @ 463:35bb388d0eac api-inversion

audioDB::query_loop and audioDB::query_loop_points are no more. Be a little bit careful about deleting the exact_evaluation_queue in error conditions, but otherwise this was simple (If you ignore the work of the previous $N$ commits, that is). The inversion work in query.cpp is now complete, apart from physically teasing apart the API function from the body of audioDB::query and then moving the two remaining audioDB:: methods elsewhere. Unfortunately, we're still not quite done, because we have to deal with audioDB::index_query_loop too, which is still a little bit tangled. Still, we're nearly there...
author mas01cr
date Tue, 30 Dec 2008 15:38:59 +0000
parents f689510baaf4
children 1030664df98c
comparison
equal deleted inserted replaced
462:f689510baaf4 463:35bb388d0eac
238 // Test for index (again) here 238 // Test for index (again) here
239 if((qspec.refine.flags & ADB_REFINE_RADIUS) && audiodb_index_exists(adb->path, qspec.refine.radius, qspec.qid.sequence_length)){ 239 if((qspec.refine.flags & ADB_REFINE_RADIUS) && audiodb_index_exists(adb->path, qspec.refine.radius, qspec.qid.sequence_length)){
240 VERB_LOG(1, "Calling indexed query on database %s, radius=%f, sequence_length=%d\n", adb->path, qspec.refine.radius, qspec.qid.sequence_length); 240 VERB_LOG(1, "Calling indexed query on database %s, radius=%f, sequence_length=%d\n", adb->path, qspec.refine.radius, qspec.qid.sequence_length);
241 index_query_loop(adb, &qspec, &qstate); 241 index_query_loop(adb, &qspec, &qstate);
242 } 242 }
243 else{ 243 else {
244 VERB_LOG(1, "Calling brute-force query on database %s\n", dbName); 244 VERB_LOG(1, "Calling brute-force query on database %s\n", dbName);
245 if(query_loop(adb, &qspec, &qstate)) { 245 if(audiodb_query_loop(adb, &qspec, &qstate)) {
246 error("query_loop failed"); 246 error("audiodb_query_loop failed");
247 } 247 }
248 } 248 }
249 249
250 adb_query_results_t *rs = qstate.accumulator->get_points(); 250 adb_query_results_t *rs = qstate.accumulator->get_points();
251 251
626 } 626 }
627 return 1; 627 return 1;
628 628
629 } 629 }
630 630
631 // query_points() 631 int audiodb_query_queue_loop(adb_t *adb, adb_query_spec_t *spec, adb_qstate_internal_t *qstate, double *query, adb_qpointers_internal_t *qpointers) {
632 //
633 // using PointPairs held in the exact_evaluation_queue compute squared distance for each PointPair
634 // and insert result into the current reporter.
635 //
636 // Preconditions:
637 // A query inFile has been opened with setup_query(...) and query pointers initialized
638 // The database contains some points
639 // An exact_evaluation_queue has been allocated and populated
640 // A reporter has been allocated
641 //
642 // Postconditions:
643 // reporter contains the points and distances that meet the reporter constraints
644
645 void audioDB::query_loop_points(adb_t *adb, adb_query_spec_t *spec, adb_qstate_internal_t *qstate, double *query, adb_qpointers_internal_t *qpointers) {
646 adb_qpointers_internal_t dbpointers = {0}; 632 adb_qpointers_internal_t dbpointers = {0};
647 633
648 uint32_t sequence_length = spec->qid.sequence_length; 634 uint32_t sequence_length = spec->qid.sequence_length;
649 bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD); 635 bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD);
650 636
651 if(qstate->exact_evaluation_queue->size() == 0) { 637 if(qstate->exact_evaluation_queue->size() == 0) {
652 return; 638 return 0;
653 } 639 }
654 640
655 /* We are guaranteed that the order of points is sorted by: 641 /* We are guaranteed that the order of points is sorted by:
656 * {trackID, spos, qpos} so we can be relatively efficient in 642 * {trackID, spos, qpos} so we can be relatively efficient in
657 * initialization of track data. We assume that points usually 643 * initialization of track data. We assume that points usually
658 * don't overlap, so we will use exhaustive dot product evaluation 644 * don't overlap, so we will use exhaustive dot product evaluation
659 * (instead of memoization of partial sums, as in query_loop()). */ 645 * (instead of memoization of partial sums, as in query_loop()).
646 */
660 double dist; 647 double dist;
661 double *dbdata = 0, *dbdata_pointer; 648 double *dbdata = 0, *dbdata_pointer;
662 Uns32T currentTrack = 0x80000000; // KLUDGE: Initialize with a value outside of track index range 649 Uns32T currentTrack = 0x80000000; // KLUDGE: Initialize with a value outside of track index range
663 Uns32T npairs = qstate->exact_evaluation_queue->size(); 650 Uns32T npairs = qstate->exact_evaluation_queue->size();
664 while(npairs--) { 651 while(npairs--) {
669 SAFE_DELETE_ARRAY(dbpointers.power_data); 656 SAFE_DELETE_ARRAY(dbpointers.power_data);
670 SAFE_DELETE_ARRAY(dbpointers.mean_duration); 657 SAFE_DELETE_ARRAY(dbpointers.mean_duration);
671 currentTrack = pp.trackID; 658 currentTrack = pp.trackID;
672 adb_datum_t d = {0}; 659 adb_datum_t d = {0};
673 if(audiodb_track_id_datum(adb, pp.trackID, &d)) { 660 if(audiodb_track_id_datum(adb, pp.trackID, &d)) {
674 error("failed to get datum"); 661 delete qstate->exact_evaluation_queue;
662 return 1;
675 } 663 }
676 if(audiodb_datum_qpointers(&d, sequence_length, &dbdata, &dbdata_pointer, &dbpointers)) { 664 if(audiodb_datum_qpointers(&d, sequence_length, &dbdata, &dbdata_pointer, &dbpointers)) {
665 delete qstate->exact_evaluation_queue;
677 audiodb_free_datum(&d); 666 audiodb_free_datum(&d);
678 error("failed to get dbpointers"); 667 return 1;
679 } 668 }
680 audiodb_free_datum(&d); 669 audiodb_free_datum(&d);
681 } 670 }
682 Uns32T qPos = usingQueryPoint?0:pp.qpos;// index for query point 671 Uns32T qPos = (spec->qid.flags & ADB_QUERY_ID_FLAG_EXHAUSTIVE) ? pp.qpos : 0;
683 Uns32T sPos = pp.spos; // index into l2norm table 672 Uns32T sPos = pp.spos; // index into l2norm table
684 // Test power thresholds before computing distance 673 // Test power thresholds before computing distance
685 if( ( (!power_refine) || audiodb_powers_acceptable(&spec->refine, qpointers->power[qPos], dbpointers.power[sPos])) && 674 if( ( (!power_refine) || audiodb_powers_acceptable(&spec->refine, qpointers->power[qPos], dbpointers.power[sPos])) &&
686 ( qPos<qpointers->nvectors-sequence_length+1 && sPos<(*adb->track_lengths)[pp.trackID]-sequence_length+1 ) ){ 675 ( qPos<qpointers->nvectors-sequence_length+1 && sPos<(*adb->track_lengths)[pp.trackID]-sequence_length+1 ) ){
687 // Compute distance 676 // Compute distance
694 break; 683 break;
695 case ADB_DISTANCE_EUCLIDEAN: 684 case ADB_DISTANCE_EUCLIDEAN:
696 dist = qn*qn + sn*sn - 2*dist; 685 dist = qn*qn + sn*sn - 2*dist;
697 break; 686 break;
698 } 687 }
699 if((!radius) || dist <= (O2_LSH_EXACT_MULT*radius+O2_DISTANCE_TOLERANCE)) { 688 if((!(spec->refine.flags & ADB_REFINE_RADIUS)) ||
689 dist <= (spec->refine.radius+O2_DISTANCE_TOLERANCE)) {
700 adb_result_t r; 690 adb_result_t r;
701 r.key = (*adb->keys)[pp.trackID].c_str(); 691 r.key = (*adb->keys)[pp.trackID].c_str();
702 r.dist = dist; 692 r.dist = dist;
703 r.qpos = pp.qpos; 693 r.qpos = pp.qpos;
704 r.ipos = pp.spos; 694 r.ipos = pp.spos;
711 SAFE_DELETE_ARRAY(dbdata); 701 SAFE_DELETE_ARRAY(dbdata);
712 SAFE_DELETE_ARRAY(dbpointers.l2norm_data); 702 SAFE_DELETE_ARRAY(dbpointers.l2norm_data);
713 SAFE_DELETE_ARRAY(dbpointers.power_data); 703 SAFE_DELETE_ARRAY(dbpointers.power_data);
714 SAFE_DELETE_ARRAY(dbpointers.mean_duration); 704 SAFE_DELETE_ARRAY(dbpointers.mean_duration);
715 delete qstate->exact_evaluation_queue; 705 delete qstate->exact_evaluation_queue;
716 } 706 return 0;
717 707 }
718 int audioDB::query_loop(adb_t *adb, adb_query_spec_t *spec, adb_qstate_internal_t *qstate) { 708
709 int audiodb_query_loop(adb_t *adb, adb_query_spec_t *spec, adb_qstate_internal_t *qstate) {
719 710
720 double *query, *query_data; 711 double *query, *query_data;
721 adb_qpointers_internal_t qpointers = {0}, dbpointers = {0}; 712 adb_qpointers_internal_t qpointers = {0}, dbpointers = {0};
722 713
723 bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD); 714 bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD);