comparison query.cpp @ 434:7af140bf8a0a api-inversion

adb_t-ize most of audioDB::set_up_db. All of this "adding code" nonsense (mostly because of the conversion from C++ non-local exits using audioDB::error into lame C "must return and check integer error code") has to have a payoff sometime. That day will come. That day will come.
author mas01cr
date Wed, 24 Dec 2008 10:55:52 +0000
parents 681837f7c903
children 53c487885b2c
comparison
equal deleted inserted replaced
433:681837f7c903 434:7af140bf8a0a
492 492
493 493
494 // FIXME: this is not the right name; we're not actually setting up 494 // FIXME: this is not the right name; we're not actually setting up
495 // the database, but copying various bits of it out of mmap()ed tables 495 // the database, but copying various bits of it out of mmap()ed tables
496 // in order to reduce seeks. 496 // in order to reduce seeks.
497 void audioDB::set_up_db(double **snp, double **vsnp, double **spp, double **vspp, double **mddp, unsigned int *dvp) { 497 int audioDB::set_up_db(adb_t *adb, double **snp, double **vsnp, double **spp, double **vspp, double **mddp, unsigned int *dvp) {
498 *dvp = dbH->length / (dbH->dim * sizeof(double)); 498 *dvp = adb->header->length / (adb->header->dim * sizeof(double));
499 *snp = new double[*dvp]; 499 *snp = new double[*dvp];
500 500
501 double *snpp = *snp, *sppp = 0; 501 double *snpp = *snp, *sppp = 0;
502 memcpy(*snp, l2normTable, *dvp * sizeof(double)); 502 lseek(adb->fd, adb->header->l2normTableOffset, SEEK_SET);
503 read_or_goto_error(adb->fd, *snp, *dvp * sizeof(double));
503 504
504 if (usingPower) { 505 if (usingPower) {
505 if (!(dbH->flags & O2_FLAG_POWER)) { 506 if (!(adb->header->flags & O2_FLAG_POWER)) {
506 error("database not power-enabled", dbName); 507 goto error;
507 } 508 }
508 *spp = new double[*dvp]; 509 *spp = new double[*dvp];
509 sppp = *spp; 510 sppp = *spp;
510 memcpy(*spp, powerTable, *dvp * sizeof(double)); 511 lseek(adb->fd, adb->header->powerTableOffset, SEEK_SET);
511 } 512 read_or_goto_error(adb->fd, *spp, *dvp * sizeof(double));
512 513 }
513 for(unsigned int i = 0; i < dbH->numFiles; i++){ 514
514 if(trackTable[i] >= sequenceLength) { 515 for(unsigned int i = 0; i < adb->header->numFiles; i++){
515 audiodb_sequence_sum(snpp, trackTable[i], sequenceLength); 516 size_t track_length = (*adb->track_lengths)[i];
516 audiodb_sequence_sqrt(snpp, trackTable[i], sequenceLength); 517 if(track_length >= sequenceLength) {
517 518 audiodb_sequence_sum(snpp, track_length, sequenceLength);
519 audiodb_sequence_sqrt(snpp, track_length, sequenceLength);
518 if (usingPower) { 520 if (usingPower) {
519 audiodb_sequence_sum(sppp, trackTable[i], sequenceLength); 521 audiodb_sequence_sum(sppp, track_length, sequenceLength);
520 audiodb_sequence_average(sppp, trackTable[i], sequenceLength); 522 audiodb_sequence_average(sppp, track_length, sequenceLength);
521 } 523 }
522 } 524 }
523 snpp += trackTable[i]; 525 snpp += track_length;
524 if (usingPower) { 526 if (usingPower) {
525 sppp += trackTable[i]; 527 sppp += track_length;
526 } 528 }
527 } 529 }
528 530
529 if (usingTimes) { 531 if (usingTimes) {
530 if(!(dbH->flags & O2_FLAG_TIMES)) { 532 if(!(adb->header->flags & O2_FLAG_TIMES)) {
531 error("query timestamps provided for non-timed database", dbName); 533 error("query timestamps provided for non-timed database", dbName);
532 } 534 }
533 535
534 *mddp = new double[dbH->numFiles]; 536 *mddp = new double[adb->header->numFiles];
535 537
536 for(unsigned int k = 0; k < dbH->numFiles; k++) { 538 for(unsigned int k = 0; k < adb->header->numFiles; k++) {
539 size_t track_length = (*adb->track_lengths)[k];
537 unsigned int j; 540 unsigned int j;
538 (*mddp)[k] = 0.0; 541 (*mddp)[k] = 0.0;
539 for(j = 0; j < trackTable[k]; j++) { 542 for(j = 0; j < track_length; j++) {
540 (*mddp)[k] += timesTable[2*j+1] - timesTable[2*j]; 543 (*mddp)[k] += timesTable[2*j+1] - timesTable[2*j];
541 } 544 }
542 (*mddp)[k] /= j; 545 (*mddp)[k] /= j;
543 } 546 }
544 } 547 }
545 548
546 *vsnp = *snp; 549 *vsnp = *snp;
547 *vspp = *spp; 550 *vspp = *spp;
551 return 0;
552
553 error:
554 if(*snp) {
555 delete [] *snp;
556 }
557 if(*spp) {
558 delete [] *spp;
559 }
560 if(*mddp) {
561 delete [] *mddp;
562 }
563 return 1;
564
548 } 565 }
549 566
550 // query_points() 567 // query_points()
551 // 568 //
552 // using PointPairs held in the exact_evaluation_queue compute squared distance for each PointPair 569 // using PointPairs held in the exact_evaluation_queue compute squared distance for each PointPair
573 590
574 // Compute database info 591 // Compute database info
575 // FIXME: we more than likely don't need very much of the database 592 // FIXME: we more than likely don't need very much of the database
576 // so make a new method to build these values per-track or, even better, per-point 593 // so make a new method to build these values per-track or, even better, per-point
577 if( !( dbH->flags & O2_FLAG_LARGE_ADB) ) 594 if( !( dbH->flags & O2_FLAG_LARGE_ADB) )
578 set_up_db(&sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors); 595 if(set_up_db(adb, &sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors)) {
596 error("failed to set up db");
597 }
579 598
580 VERB_LOG(1, "matching points..."); 599 VERB_LOG(1, "matching points...");
581 600
582 // We are guaranteed that the order of points is sorted by: 601 // We are guaranteed that the order of points is sorted by:
583 // trackID, spos, qpos 602 // trackID, spos, qpos
684 703
685 unsigned int dbVectors; 704 unsigned int dbVectors;
686 double *sNorm, *snPtr, *sPower = 0, *spPtr = 0; 705 double *sNorm, *snPtr, *sPower = 0, *spPtr = 0;
687 double *meanDBdur = 0; 706 double *meanDBdur = 0;
688 707
689 set_up_db(&sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors); 708 if(set_up_db(adb, &sNorm, &snPtr, &sPower, &spPtr, &meanDBdur, &dbVectors)) {
709 error("failed to set up db");
710 }
690 711
691 VERB_LOG(1, "matching tracks..."); 712 VERB_LOG(1, "matching tracks...");
692 713
693 unsigned j,k,track,trackOffset=0, HOP_SIZE=sequenceHop, wL=sequenceLength; 714 unsigned j,k,track,trackOffset=0, HOP_SIZE=sequenceHop, wL=sequenceLength;
694 double **D = 0; // Differences query and target 715 double **D = 0; // Differences query and target