-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.cpp
658 lines (585 loc) · 21.8 KB
/
query.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
extern "C" {
#include "audioDB_API.h"
}
#include "audioDB-internals.h"
#include "accumulators.h"
bool audiodb_powers_acceptable(const adb_query_refine_t *r, double p1, double p2) {
if (r->flags & ADB_REFINE_ABSOLUTE_THRESHOLD) {
if ((p1 < r->absolute_threshold) || (p2 < r->absolute_threshold)) {
return false;
}
}
if (r->flags & ADB_REFINE_RELATIVE_THRESHOLD) {
if (fabs(p1-p2) > fabs(r->relative_threshold)) {
return false;
}
}
return true;
}
adb_query_results_t *audiodb_query_spec(adb_t *adb, const adb_query_spec_t *qspec) {
adb_qstate_internal_t qstate = {0};
qstate.allowed_keys = new std::set<std::string>;
adb_query_results_t *results;
if(qspec->refine.flags & ADB_REFINE_INCLUDE_KEYLIST) {
for(unsigned int k = 0; k < qspec->refine.include.nkeys; k++) {
qstate.allowed_keys->insert(qspec->refine.include.keys[k]);
}
} else {
for(unsigned int k = 0; k < adb->header->numFiles; k++) {
qstate.allowed_keys->insert((*adb->keys)[k]);
}
}
if(qspec->refine.flags & ADB_REFINE_EXCLUDE_KEYLIST) {
for(unsigned int k = 0; k < qspec->refine.exclude.nkeys; k++) {
qstate.allowed_keys->erase(qspec->refine.exclude.keys[k]);
}
}
switch(qspec->params.distance) {
case ADB_DISTANCE_DOT_PRODUCT:
switch(qspec->params.accumulation) {
case ADB_ACCUMULATION_DB:
qstate.accumulator = new DBAccumulator<adb_result_dist_gt>(qspec->params.npoints);
break;
case ADB_ACCUMULATION_PER_TRACK:
qstate.accumulator = new PerTrackAccumulator<adb_result_dist_gt>(qspec->params.npoints, qspec->params.ntracks);
break;
case ADB_ACCUMULATION_ONE_TO_ONE:
qstate.accumulator = new NearestAccumulator<adb_result_dist_gt>();
break;
default:
goto error;
}
break;
case ADB_DISTANCE_EUCLIDEAN_NORMED:
case ADB_DISTANCE_EUCLIDEAN:
case ADB_DISTANCE_KULLBACK_LEIBLER_DIVERGENCE:
switch(qspec->params.accumulation) {
case ADB_ACCUMULATION_DB:
qstate.accumulator = new DBAccumulator<adb_result_dist_lt>(qspec->params.npoints);
break;
case ADB_ACCUMULATION_PER_TRACK:
qstate.accumulator = new PerTrackAccumulator<adb_result_dist_lt>(qspec->params.npoints, qspec->params.ntracks);
break;
case ADB_ACCUMULATION_ONE_TO_ONE:
qstate.accumulator = new NearestAccumulator<adb_result_dist_lt>();
break;
default:
goto error;
}
break;
default:
goto error;
}
if((qspec->refine.flags & ADB_REFINE_RADIUS) && audiodb_index_exists(adb->path, qspec->refine.radius, qspec->qid.sequence_length)) {
if(audiodb_index_query_loop(adb, qspec, &qstate) < 0) {
goto error;
}
} else {
if(audiodb_query_loop(adb, qspec, &qstate)) {
goto error;
}
}
results = qstate.accumulator->get_points();
delete qstate.accumulator;
delete qstate.allowed_keys;
return results;
error:
if(qstate.accumulator)
delete qstate.accumulator;
if(qstate.allowed_keys)
delete qstate.allowed_keys;
return NULL;
}
int audiodb_query_free_results(adb_t *adb, const adb_query_spec_t *spec, adb_query_results_t *rs) {
free(rs->results);
free(rs);
return 0;
}
/* FIXME: we should check the return values from allocation */
static void audiodb_initialize_arrays(adb_t *adb, const adb_query_spec_t *spec, int track, unsigned int numVectors, double *query, double *data_buffer, double **D, double **DD) {
unsigned int j, k, l, w;
double *dp, *qp, *sp;
double a,b, tmp1;
#ifdef SYMMETRIC_KL
double tmp2;
#endif
const unsigned wL = spec->qid.sequence_length;
for(j = 0; j < numVectors; j++) {
// Sum products matrix
D[j] = new double[(*adb->track_lengths)[track]];
// Matched filter matrix
DD[j]=new double[(*adb->track_lengths)[track]];
}
// Dot product
for(j = 0; j < numVectors; j++)
for(k = 0; k < (*adb->track_lengths)[track]; k++){
qp = query + j * adb->header->dim;
sp = data_buffer + k * adb->header->dim;
DD[j][k] = 0.0; // Initialize matched filter array
dp = &D[j][k]; // point to correlation cell j,k
*dp = 0.0; // initialize correlation cell
l = adb->header->dim; // size of vectors
if (spec->params.distance!=ADB_DISTANCE_KULLBACK_LEIBLER_DIVERGENCE){
while(l--)
*dp += *qp++ * *sp++;
}
else{ // KL
while(l--){
a = *qp++;
b = *sp++;
tmp1 = a * log( a / b );
if(isnan(tmp1))
tmp1=0.0;
#ifdef SYMMETRIC_KL
tmp2 = b * log( b / a );
if(isnan(tmp2))
tmp2=0.0;
*dp += ( tmp1 + tmp2 ) / 2.0;
#else
*dp += tmp1;
#endif
}
}
}
double* spd;
if(!(spec->refine.flags & ADB_REFINE_HOP_SIZE)) {
for(w = 0; w < wL; w++) {
for(j = 0; j < numVectors - w; j++) {
sp = DD[j];
spd = D[j+w] + w;
k = (*adb->track_lengths)[track] - w;
while(k--)
*sp++ += *spd++;
}
}
} else {
uint32_t qhop = spec->refine.qhopsize;
qhop = qhop ? qhop : 1;
uint32_t ihop = spec->refine.ihopsize;
ihop = ihop ? ihop : 1;
for(w = 0; w < wL; w++) {
for(j = 0; j < numVectors - w; j += qhop) {
sp = DD[j];
spd = D[j+w]+w;
for(k = 0; k < (*adb->track_lengths)[track] - w; k += ihop) {
*sp += *spd;
sp += ihop;
spd += ihop;
}
}
}
}
}
static void audiodb_delete_arrays(int track, unsigned int numVectors, double **D, double **DD) {
if(D != NULL) {
for(unsigned int j = 0; j < numVectors; j++) {
delete[] D[j];
}
}
if(DD != NULL) {
for(unsigned int j = 0; j < numVectors; j++) {
delete[] DD[j];
}
}
}
int audiodb_read_data(adb_t *adb, int trkfid, int track, double **data_buffer_p, size_t *data_buffer_size_p) {
uint32_t track_length = (*adb->track_lengths)[track];
size_t track_size = track_length * sizeof(double) * adb->header->dim;
if (track_size > *data_buffer_size_p) {
if(*data_buffer_p) {
free(*data_buffer_p);
}
{
*data_buffer_size_p = track_size;
void *tmp = malloc(track_size);
if (tmp == NULL) {
goto error;
}
*data_buffer_p = (double *) tmp;
}
}
read_or_goto_error(trkfid, *data_buffer_p, track_size);
return 0;
error:
return 1;
}
int audiodb_track_id_datum(adb_t *adb, uint32_t track_id, adb_datum_t *d) {
off_t track_offset = (*adb->track_offsets)[track_id];
if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) {
/* create a reference/insert, then use adb_insert_create_datum() */
adb_reference_t reference = {0};
char features[ADB_MAXSTR], power[ADB_MAXSTR], times[ADB_MAXSTR];
lseek_set_or_goto_error(adb->fd, adb->header->dataOffset + track_id * ADB_FILETABLE_ENTRY_SIZE);
read_or_goto_error(adb->fd, features, ADB_MAXSTR);
reference.features = features;
if(adb->header->flags & ADB_HEADER_FLAG_POWER) {
lseek_set_or_goto_error(adb->fd, adb->header->powerTableOffset + track_id * ADB_FILETABLE_ENTRY_SIZE);
read_or_goto_error(adb->fd, power, ADB_MAXSTR);
reference.power = power;
}
if(adb->header->flags & ADB_HEADER_FLAG_TIMES) {
lseek_set_or_goto_error(adb->fd, adb->header->timesTableOffset + track_id * ADB_FILETABLE_ENTRY_SIZE);
read_or_goto_error(adb->fd, times, ADB_MAXSTR);
reference.times = times;
}
return audiodb_insert_create_datum(&reference, d);
} else {
/* initialize from sources of data that we already have */
d->nvectors = (*adb->track_lengths)[track_id];
d->dim = adb->header->dim;
d->key = (*adb->keys)[track_id].c_str();
/* read out stuff from the database tables */
malloc_and_fill_or_goto_error(double *, d->data, adb->header->dataOffset + track_offset, d->nvectors * d->dim * sizeof(double));
if(adb->header->flags & ADB_HEADER_FLAG_POWER) {
malloc_and_fill_or_goto_error(double *, d->power, adb->header->powerTableOffset + track_offset / d->dim, d->nvectors * sizeof(double));
} else {
d->power = NULL;
}
if(adb->header->flags & ADB_HEADER_FLAG_TIMES) {
malloc_and_fill_or_goto_error(double *, d->times, adb->header->timesTableOffset + 2 * track_offset / d->dim, 2 * d->nvectors * sizeof(double));
} else {
d->times = NULL;
}
return 0;
}
error:
audiodb_really_free_datum(d);
return 1;
}
int audiodb_datum_qpointers(adb_datum_t *d, uint32_t sequence_length, double **vector_data, double **vector, adb_qpointers_internal_t *qpointers) {
uint32_t nvectors = d->nvectors;
qpointers->nvectors = nvectors;
size_t vector_size = nvectors * sizeof(double) * d->dim;
*vector_data = new double[vector_size / sizeof(double) ];
memcpy(*vector_data, d->data, vector_size);
qpointers->l2norm_data = new double[vector_size / (sizeof(double)*d->dim)];
audiodb_l2norm_buffer(*vector_data, d->dim, nvectors, qpointers->l2norm_data);
audiodb_sequence_sum(qpointers->l2norm_data, nvectors, sequence_length);
audiodb_sequence_sqrt(qpointers->l2norm_data, nvectors, sequence_length);
if(d->power) {
qpointers->power_data = new double[vector_size / (sizeof(double)*d->dim)];
memcpy(qpointers->power_data, d->power, vector_size / d->dim);
audiodb_sequence_sum(qpointers->power_data, nvectors, sequence_length);
audiodb_sequence_average(qpointers->power_data, nvectors, sequence_length);
}
if(d->times) {
qpointers->mean_duration = new double[1];
*qpointers->mean_duration = 0;
for(unsigned int k = 0; k < nvectors; k++) {
*qpointers->mean_duration += d->times[2*k+1] - d->times[2*k];
}
*qpointers->mean_duration /= nvectors;
}
*vector = *vector_data;
qpointers->l2norm = qpointers->l2norm_data;
qpointers->power = qpointers->power_data;
return 0;
}
int audiodb_query_spec_qpointers(adb_t *adb, const adb_query_spec_t *spec, double **vector_data, double **vector, adb_qpointers_internal_t *qpointers) {
adb_datum_t *datum;
adb_datum_t d = {0};
uint32_t sequence_length;
uint32_t sequence_start;
datum = spec->qid.datum;
sequence_length = spec->qid.sequence_length;
sequence_start = spec->qid.sequence_start;
if(datum->data) {
if(datum->dim != adb->header->dim) {
return 1;
}
/* initialize d, and mark that nothing needs freeing later. */
d = *datum;
d.key = "";
datum = &d;
} else if (datum->key) {
uint32_t track_id;
if((track_id = audiodb_key_index(adb, datum->key)) == (uint32_t) -1) {
return 1;
}
audiodb_track_id_datum(adb, track_id, &d);
} else {
return 1;
}
/* FIXME: check the overflow logic here */
if(sequence_start + sequence_length > d.nvectors) {
if(datum != &d) {
audiodb_really_free_datum(&d);
}
return 1;
}
audiodb_datum_qpointers(&d, sequence_length, vector_data, vector, qpointers);
/* Finally, if applicable, set up the moving qpointers. */
if(spec->qid.flags & ADB_QID_FLAG_EXHAUSTIVE) {
/* the qpointers are already at the start, and so correct. */
} else {
/* adjust the qpointers to point to the correct place in the sequence */
*vector = *vector_data + spec->qid.sequence_start * d.dim;
qpointers->l2norm = qpointers->l2norm_data + spec->qid.sequence_start;
if(d.power) {
qpointers->power = qpointers->power_data + spec->qid.sequence_start;
}
qpointers->nvectors = sequence_length;
}
/* Clean up: free any bits of datum that we have ourselves
* allocated. */
if(datum != &d) {
audiodb_really_free_datum(&d);
}
return 0;
}
static int audiodb_set_up_dbpointers(adb_t *adb, const adb_query_spec_t *spec, adb_qpointers_internal_t *dbpointers) {
uint32_t nvectors = adb->header->length / (adb->header->dim * sizeof(double));
uint32_t sequence_length = spec->qid.sequence_length;
bool using_power = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD);
bool using_times = spec->refine.flags & ADB_REFINE_DURATION_RATIO;
double *times_table = NULL;
dbpointers->nvectors = nvectors;
dbpointers->l2norm_data = new double[nvectors];
double *snpp = dbpointers->l2norm_data, *sppp = 0;
lseek_set_or_goto_error(adb->fd, adb->header->l2normTableOffset);
read_or_goto_error(adb->fd, dbpointers->l2norm_data, nvectors * sizeof(double));
if (using_power) {
if (!(adb->header->flags & ADB_HEADER_FLAG_POWER)) {
goto error;
}
dbpointers->power_data = new double[nvectors];
sppp = dbpointers->power_data;
lseek_set_or_goto_error(adb->fd, adb->header->powerTableOffset);
read_or_goto_error(adb->fd, dbpointers->power_data, nvectors * sizeof(double));
}
for(unsigned int i = 0; i < adb->header->numFiles; i++){
size_t track_length = (*adb->track_lengths)[i];
if(track_length >= sequence_length) {
audiodb_sequence_sum(snpp, track_length, sequence_length);
audiodb_sequence_sqrt(snpp, track_length, sequence_length);
if (using_power) {
audiodb_sequence_sum(sppp, track_length, sequence_length);
audiodb_sequence_average(sppp, track_length, sequence_length);
}
}
snpp += track_length;
if (using_power) {
sppp += track_length;
}
}
if (using_times) {
if(!(adb->header->flags & ADB_HEADER_FLAG_TIMES)) {
goto error;
}
dbpointers->mean_duration = new double[adb->header->numFiles];
malloc_and_fill_or_goto_error(double *, times_table, adb->header->timesTableOffset, 2 * nvectors * sizeof(double));
for(unsigned int k = 0; k < adb->header->numFiles; k++) {
size_t track_length = (*adb->track_lengths)[k];
unsigned int j;
dbpointers->mean_duration[k] = 0.0;
for(j = 0; j < track_length; j++) {
dbpointers->mean_duration[k] += times_table[2*j+1] - times_table[2*j];
}
dbpointers->mean_duration[k] /= j;
}
free(times_table);
times_table = NULL;
}
dbpointers->l2norm = dbpointers->l2norm_data;
dbpointers->power = dbpointers->power_data;
return 0;
error:
maybe_delete_array(dbpointers->l2norm_data);
maybe_delete_array(dbpointers->power_data);
maybe_delete_array(dbpointers->mean_duration);
maybe_free(times_table);
return 1;
}
int audiodb_query_queue_loop(adb_t *adb, const adb_query_spec_t *spec, adb_qstate_internal_t *qstate, double *query, adb_qpointers_internal_t *qpointers) {
adb_qpointers_internal_t dbpointers = {0};
uint32_t sequence_length = spec->qid.sequence_length;
bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD);
if(qstate->exact_evaluation_queue->size() == 0) {
return 0;
}
/* We are guaranteed that the order of points is sorted by:
* {trackID, spos, qpos} so we can be relatively efficient in
* initialization of track data. We assume that points usually
* don't overlap, so we will use exhaustive dot product evaluation
* (instead of memoization of partial sums, as in query_loop()).
*/
double dist;
double *dbdata = 0, *dbdata_pointer;
uint32_t currentTrack = 0x80000000; // KLUDGE: Initialize with a value outside of track index range
uint32_t npairs = qstate->exact_evaluation_queue->size();
while(npairs--) {
PointPair pp = qstate->exact_evaluation_queue->top();
if(currentTrack != pp.trackID) {
maybe_delete_array(dbdata);
maybe_delete_array(dbpointers.l2norm_data);
maybe_delete_array(dbpointers.power_data);
maybe_delete_array(dbpointers.mean_duration);
currentTrack = pp.trackID;
adb_datum_t d = {0};
if(audiodb_track_id_datum(adb, pp.trackID, &d)) {
delete qstate->exact_evaluation_queue;
return 1;
}
if(audiodb_datum_qpointers(&d, sequence_length, &dbdata, &dbdata_pointer, &dbpointers)) {
delete qstate->exact_evaluation_queue;
audiodb_really_free_datum(&d);
return 1;
}
audiodb_really_free_datum(&d);
}
uint32_t qPos = (spec->qid.flags & ADB_QID_FLAG_EXHAUSTIVE) ? pp.qpos : 0;
uint32_t sPos = pp.spos; // index into l2norm table
// Test power thresholds before computing distance
if( ( (!power_refine) || audiodb_powers_acceptable(&spec->refine, qpointers->power[qPos], dbpointers.power[sPos])) &&
( qPos<qpointers->nvectors-sequence_length+1 && sPos<(*adb->track_lengths)[pp.trackID]-sequence_length+1 ) ){
// Compute distance
dist = 1.0e9;
if (spec->params.distance==ADB_DISTANCE_EUCLIDEAN_NORMED || spec->params.distance==ADB_DISTANCE_EUCLIDEAN)
dist = audiodb_dot_product(query + qPos*adb->header->dim, dbdata + sPos*adb->header->dim, adb->header->dim*sequence_length);
else if(spec->params.distance==ADB_DISTANCE_KULLBACK_LEIBLER_DIVERGENCE)
dist = audiodb_kullback_leibler(query + qPos*adb->header->dim, dbdata + sPos*adb->header->dim, adb->header->dim*sequence_length);
double qn = qpointers->l2norm[qPos];
double sn = dbpointers.l2norm[sPos];
switch(spec->params.distance) {
case ADB_DISTANCE_EUCLIDEAN_NORMED:
dist = 2 - (2/(qn*sn))*dist;
break;
case ADB_DISTANCE_EUCLIDEAN:
dist = qn*qn + sn*sn - 2*dist;
break;
}
if((!(spec->refine.flags & ADB_REFINE_RADIUS)) ||
dist <= (spec->refine.radius + ADB_DISTANCE_TOLERANCE)) {
adb_result_t r;
r.ikey = (*adb->keys)[pp.trackID].c_str();
r.qkey = spec->qid.datum->key;
r.dist = dist;
r.qpos = pp.qpos;
r.ipos = pp.spos;
if(qstate->set->find(r) == qstate->set->end()) {
qstate->set->insert(r);
qstate->accumulator->add_point(&r);
}
}
}
qstate->exact_evaluation_queue->pop();
}
// Cleanup
maybe_delete_array(dbdata);
maybe_delete_array(dbpointers.l2norm_data);
maybe_delete_array(dbpointers.power_data);
maybe_delete_array(dbpointers.mean_duration);
delete qstate->exact_evaluation_queue;
return 0;
}
int audiodb_query_loop(adb_t *adb, const adb_query_spec_t *spec, adb_qstate_internal_t *qstate) {
double *query, *query_data;
adb_qpointers_internal_t qpointers = {0}, dbpointers = {0};
bool power_refine = spec->refine.flags & (ADB_REFINE_ABSOLUTE_THRESHOLD|ADB_REFINE_RELATIVE_THRESHOLD);
if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) {
/* FIXME: actually it would be nice to support this mode of
* operation, but for now... */
return 1;
}
if(audiodb_query_spec_qpointers(adb, spec, &query_data, &query, &qpointers)) {
return 1;
}
if(audiodb_set_up_dbpointers(adb, spec, &dbpointers)) {
return 1;
}
unsigned j,k,track,trackOffset=0;
unsigned wL = spec->qid.sequence_length;
double **D = 0; // Differences query and target
double **DD = 0; // Matched filter distance
D = new double*[qpointers.nvectors]; // pre-allocate
DD = new double*[qpointers.nvectors];
unsigned qhop, ihop;
if(spec->refine.flags & ADB_REFINE_HOP_SIZE) {
qhop = spec->refine.qhopsize;
qhop = qhop ? qhop : 1;
ihop = spec->refine.ihopsize;
ihop = ihop ? ihop : 1;
} else {
qhop = 1;
ihop = 1;
}
off_t trackIndexOffset;
// Track loop
size_t data_buffer_size = 0;
double *data_buffer = 0;
lseek(adb->fd, adb->header->dataOffset, SEEK_SET);
std::set<std::string>::iterator keys_end = qstate->allowed_keys->end();
for(track = 0; track < adb->header->numFiles; track++) {
unsigned t = track;
while (qstate->allowed_keys->find((*adb->keys)[track]) == keys_end) {
track++;
if(track == adb->header->numFiles) {
goto loop_finish;
}
}
trackOffset = (*adb->track_offsets)[track];
if(track != t) {
lseek(adb->fd, adb->header->dataOffset + trackOffset, SEEK_SET);
}
trackIndexOffset = trackOffset / (adb->header->dim * sizeof(double)); // dbpointers.nvectors offset
if(audiodb_read_data(adb, adb->fd, track, &data_buffer, &data_buffer_size)) {
return 1;
}
if(wL <= (*adb->track_lengths)[track]) { // test for short sequences
audiodb_initialize_arrays(adb, spec, track, qpointers.nvectors, query, data_buffer, D, DD);
if((!(spec->refine.flags & ADB_REFINE_DURATION_RATIO)) ||
fabs(dbpointers.mean_duration[track]-qpointers.mean_duration[0]) < qpointers.mean_duration[0]*spec->refine.duration_ratio) {
// Search for minimum distance by shingles (concatenated vectors)
for(j = 0; j <= qpointers.nvectors - wL; j += qhop) {
for(k = 0; k <= (*adb->track_lengths)[track] - wL; k += ihop) {
double thisDist = 0;
double qn = qpointers.l2norm[j];
double sn = dbpointers.l2norm[trackIndexOffset + k];
switch(spec->params.distance) {
case ADB_DISTANCE_EUCLIDEAN_NORMED:
thisDist = 2-(2/(qn*sn))*DD[j][k];
break;
case ADB_DISTANCE_EUCLIDEAN:
thisDist = qn*qn + sn*sn - 2*DD[j][k];
break;
case ADB_DISTANCE_DOT_PRODUCT:
thisDist = DD[j][k];
break;
}
// Power test
if ((!power_refine) || audiodb_powers_acceptable(&spec->refine, qpointers.power[j], dbpointers.power[trackIndexOffset + k])) {
// radius test
if((!(spec->refine.flags & ADB_REFINE_RADIUS)) ||
thisDist <= (spec->refine.radius + ADB_DISTANCE_TOLERANCE)) {
adb_result_t r;
r.ikey = (*adb->keys)[track].c_str();
r.qkey = spec->qid.datum->key;
r.dist = thisDist;
if(spec->qid.flags & ADB_QID_FLAG_EXHAUSTIVE) {
r.qpos = j;
} else {
r.qpos = spec->qid.sequence_start;
}
r.ipos = k;
qstate->accumulator->add_point(&r);
}
}
}
}
} // Duration match
audiodb_delete_arrays(track, qpointers.nvectors, D, DD);
}
}
loop_finish:
free(data_buffer);
maybe_delete_array(query_data);
maybe_delete_array(qpointers.power_data);
maybe_delete_array(qpointers.l2norm_data);
maybe_delete_array(qpointers.mean_duration);
maybe_delete_array(dbpointers.power_data);
maybe_delete_array(dbpointers.l2norm_data);
maybe_delete_array(dbpointers.mean_duration);
maybe_delete_array(D);
maybe_delete_array(DD);
return 0;
}