mas01mc@292
|
1 // LSH indexing
|
mas01mc@292
|
2 //
|
mas01mc@292
|
3 // Construct a persistent LSH table structure
|
mas01mc@292
|
4 // Store at the same location as dbName
|
mas01mc@292
|
5 // Naming convention:
|
mas01mc@292
|
6 // dbName.lsh.${radius}.${sequenceLength}
|
mas01mc@292
|
7 //
|
mas01mc@292
|
8 //
|
mas01mc@292
|
9 // Author: Michael Casey
|
mas01mc@292
|
10 // Date: 23 June 2008
|
mas01mc@324
|
11 //
|
mas01mc@324
|
12 // 19th August 2008 - added O2_FLAG_LARGE_ADB support
|
mas01mc@292
|
13
|
mas01mc@292
|
14 #include "audioDB.h"
|
mas01cr@426
|
15 #include "audioDB-internals.h"
|
mas01mc@292
|
16
|
mas01cr@458
|
17 typedef struct adb_qcallback {
|
mas01cr@458
|
18 adb_t *adb;
|
mas01cr@458
|
19 adb_qstate_internal_t *qstate;
|
mas01cr@458
|
20 } adb_qcallback_t;
|
mas01mc@292
|
21
|
mas01mc@292
|
22 /************************* LSH indexing and query initialization *****************/
|
mas01mc@292
|
23
|
mas01mc@292
|
24 char* audioDB::index_get_name(const char*dbName, double radius, Uns32T sequenceLength){
|
mas01mc@292
|
25 char* indexName = new char[MAXSTR];
|
mas01mc@292
|
26 // Attempt to make new file
|
mas01mc@292
|
27 if(strlen(dbName) > (MAXSTR - 32))
|
mas01mc@292
|
28 error("dbName is too long for LSH index filename appendages");
|
mas01mc@292
|
29 strncpy(indexName, dbName, MAXSTR);
|
mas01mc@292
|
30 sprintf(indexName+strlen(dbName), ".lsh.%019.9f.%d", radius, sequenceLength);
|
mas01mc@292
|
31 return indexName;
|
mas01mc@292
|
32 }
|
mas01mc@292
|
33
|
mas01mc@292
|
34 // return true if index exists else return false
|
mas01mc@292
|
35 int audioDB::index_exists(const char* dbName, double radius, Uns32T sequenceLength){
|
mas01mc@292
|
36 // Test to see if file exists
|
mas01mc@292
|
37 char* indexName = index_get_name(dbName, radius, sequenceLength);
|
mas01mc@292
|
38 lshfid = open (indexName, O_RDONLY);
|
mas01mc@292
|
39 delete[] indexName;
|
mas01mc@292
|
40 close(lshfid);
|
mas01mc@292
|
41
|
mas01mc@292
|
42 if(lshfid<0)
|
mas01mc@292
|
43 return false;
|
mas01mc@292
|
44 else
|
mas01mc@292
|
45 return true;
|
mas01mc@292
|
46 }
|
mas01mc@292
|
47
|
mas01mc@324
|
48 // If we are a server and have a memory-resident index, check the indexName against the resident index (using get_indexName())
|
mas01mc@324
|
49 // If they match, i.e. path+dbName_resident == path+dbName_requested, use
|
mas01mc@324
|
50 // the memory-resident index.
|
mas01mc@324
|
51 // Else allocate a new LSH instance and load the index from disk
|
mas01mc@308
|
52 LSH* audioDB::index_allocate(char* indexName, bool load_hashTables){
|
mas01mc@308
|
53 LSH* gIndx=SERVER_LSH_INDEX_SINGLETON;
|
mas01mc@308
|
54 if(isServer && gIndx && (strncmp(gIndx->get_indexName(), indexName, MAXSTR)==0) )
|
mas01mc@308
|
55 audioDB::lsh = gIndx; // Use the global SERVER resident index
|
mas01mc@308
|
56 else{
|
mas01mc@308
|
57 if(audioDB::lsh)
|
mas01mc@308
|
58 delete audioDB::lsh;
|
mas01mc@308
|
59 audioDB::lsh = new LSH(indexName, load_hashTables);
|
mas01mc@308
|
60 }
|
mas01mc@308
|
61 assert(audioDB::lsh);
|
mas01mc@308
|
62 return audioDB::lsh;
|
mas01mc@308
|
63 }
|
mas01mc@308
|
64
|
mas01mc@292
|
65 vector<vector<float> >* audioDB::index_initialize_shingles(Uns32T sz){
|
mas01mc@292
|
66 if(vv)
|
mas01mc@292
|
67 delete vv;
|
mas01mc@292
|
68 vv = new vector<vector<float> >(sz);
|
mas01mc@292
|
69 for(Uns32T i=0 ; i < sz ; i++)
|
mas01mc@292
|
70 (*vv)[i]=vector<float>(dbH->dim*sequenceLength); // allocate shingle storage
|
mas01mc@292
|
71 return vv;
|
mas01mc@292
|
72 }
|
mas01mc@292
|
73
|
mas01mc@292
|
74 /******************** LSH indexing audioDB database access forall s \in {S} ***********************/
|
mas01mc@292
|
75
|
mas01mc@292
|
76 // Prepare the AudioDB database for read access and allocate auxillary memory
|
mas01mc@292
|
77 void audioDB::index_initialize(double **snp, double **vsnp, double **spp, double **vspp, Uns32T *dvp) {
|
mas01mc@324
|
78 if (!(dbH->flags & O2_FLAG_POWER)) {
|
mas01mc@324
|
79 error("INDEXed database must be power-enabled", dbName);
|
mas01mc@324
|
80 }
|
mas01mc@324
|
81
|
mas01mc@325
|
82 double *snpp = 0, *sppp = 0;
|
mas01mc@324
|
83
|
mas01mc@292
|
84 *dvp = dbH->length / (dbH->dim * sizeof(double)); // number of database vectors
|
mas01mc@292
|
85 *snp = new double[*dvp]; // songs norm pointer: L2 norm table for each vector
|
mas01mc@325
|
86 snpp = *snp;
|
mas01mc@292
|
87 *spp = new double[*dvp]; // song powertable pointer
|
mas01mc@292
|
88 sppp = *spp;
|
mas01mc@325
|
89
|
mas01mc@324
|
90 memcpy(*snp, l2normTable, *dvp * sizeof(double));
|
mas01mc@292
|
91 memcpy(*spp, powerTable, *dvp * sizeof(double));
|
mas01mc@324
|
92
|
mas01mc@324
|
93
|
mas01mc@292
|
94 for(Uns32T i = 0; i < dbH->numFiles; i++){
|
mas01mc@292
|
95 if(trackTable[i] >= sequenceLength) {
|
mas01cr@427
|
96 audiodb_sequence_sum(snpp, trackTable[i], sequenceLength);
|
mas01cr@427
|
97 audiodb_sequence_sqrt(snpp, trackTable[i], sequenceLength);
|
mas01mc@292
|
98
|
mas01cr@427
|
99 audiodb_sequence_sum(sppp, trackTable[i], sequenceLength);
|
mas01cr@427
|
100 audiodb_sequence_average(sppp, trackTable[i], sequenceLength);
|
mas01mc@292
|
101 }
|
mas01mc@292
|
102 snpp += trackTable[i];
|
mas01mc@292
|
103 sppp += trackTable[i];
|
mas01mc@292
|
104 }
|
mas01mc@324
|
105
|
mas01mc@292
|
106 *vsnp = *snp;
|
mas01mc@292
|
107 *vspp = *spp;
|
mas01mc@324
|
108
|
mas01mc@292
|
109 // Move the feature vector read pointer to start of fetures in database
|
mas01mc@292
|
110 lseek(dbfid, dbH->dataOffset, SEEK_SET);
|
mas01mc@292
|
111 }
|
mas01mc@292
|
112
|
mas01mc@292
|
113
|
mas01cr@456
|
114 /********************* LSH shingle construction ***************************/
|
mas01cr@456
|
115
|
mas01cr@456
|
116 // Construct shingles out of a feature matrix
|
mas01cr@456
|
117 // inputs:
|
mas01cr@456
|
118 // idx is vector index in feature matrix
|
mas01cr@456
|
119 // fvp is base feature matrix pointer double* [numVecs x dbH->dim]
|
mas01cr@456
|
120 //
|
mas01cr@456
|
121 // pre-conditions:
|
mas01cr@456
|
122 // dbH->dim
|
mas01cr@456
|
123 // sequenceLength
|
mas01cr@456
|
124 // idx < numVectors - sequenceLength + 1
|
mas01cr@456
|
125 //
|
mas01cr@456
|
126 // post-conditions:
|
mas01cr@456
|
127 // (*vv)[idx] contains a shingle with dbH->dim*sequenceLength float values
|
mas01cr@456
|
128
|
mas01cr@456
|
129 static void audiodb_index_make_shingle(vector<vector<float> >* vv, Uns32T idx, double* fvp, Uns32T dim, Uns32T seqLen){
|
mas01cr@456
|
130 assert(idx<(*vv).size());
|
mas01cr@456
|
131 vector<float>::iterator ve = (*vv)[idx].end();
|
mas01cr@456
|
132 vector<float>::iterator vi = (*vv)[idx].begin();
|
mas01cr@456
|
133 // First feature vector in shingle
|
mas01cr@456
|
134 if(idx == 0) {
|
mas01cr@456
|
135 while(vi!=ve) {
|
mas01cr@456
|
136 *vi++ = (float)(*fvp++);
|
mas01cr@456
|
137 }
|
mas01cr@456
|
138 } else {
|
mas01cr@456
|
139 // Not first feature vector in shingle
|
mas01cr@456
|
140 vector<float>::iterator ui=(*vv)[idx-1].begin() + dim;
|
mas01cr@456
|
141 // Previous seqLen-1 dim-vectors
|
mas01cr@456
|
142 while(vi!=ve-dim) {
|
mas01cr@456
|
143 *vi++ = *ui++;
|
mas01cr@456
|
144 }
|
mas01cr@456
|
145 // Move data pointer to next feature vector
|
mas01cr@456
|
146 fvp += ( seqLen + idx - 1 ) * dim ;
|
mas01cr@456
|
147 // New d-vector
|
mas01cr@456
|
148 while(vi!=ve) {
|
mas01cr@456
|
149 *vi++ = (float)(*fvp++);
|
mas01cr@456
|
150 }
|
mas01cr@456
|
151 }
|
mas01cr@456
|
152 }
|
mas01cr@456
|
153
|
mas01cr@456
|
154 // norm shingles
|
mas01cr@456
|
155 // in-place norming, no deletions
|
mas01cr@456
|
156 // If using power, return number of shingles above power threshold
|
mas01cr@456
|
157 int audioDB::index_norm_shingles(vector<vector<float> >* vv, double* snp, double* spp){
|
mas01cr@456
|
158 int z = 0; // number of above-threshold shingles
|
mas01cr@456
|
159 float l2norm;
|
mas01cr@456
|
160 double power;
|
mas01cr@456
|
161 float oneOverRadius = 1./(float)sqrt(radius); // Passed radius is really radius^2
|
mas01cr@456
|
162 float oneOverSqrtl2NormDivRad = oneOverRadius;
|
mas01cr@456
|
163 if(!spp)
|
mas01cr@456
|
164 error("LSH indexing and query requires a power feature using -w or -W");
|
mas01cr@456
|
165 Uns32T shingleSize = sequenceLength*dbH->dim;
|
mas01cr@456
|
166 for(Uns32T a=0; a<(*vv).size(); a++){
|
mas01cr@456
|
167 l2norm = (float)(*snp++);
|
mas01cr@456
|
168 if(audioDB::normalizedDistance)
|
mas01cr@456
|
169 oneOverSqrtl2NormDivRad = (1./l2norm)*oneOverRadius;
|
mas01cr@456
|
170
|
mas01cr@456
|
171 for(Uns32T b=0; b < shingleSize ; b++)
|
mas01cr@456
|
172 (*vv)[a][b]*=oneOverSqrtl2NormDivRad;
|
mas01cr@456
|
173
|
mas01cr@456
|
174 power = *spp++;
|
mas01cr@456
|
175 if(use_absolute_threshold){
|
mas01cr@456
|
176 if ( power >= absolute_threshold )
|
mas01cr@456
|
177 z++;
|
mas01cr@456
|
178 }
|
mas01cr@456
|
179 else
|
mas01cr@456
|
180 z++;
|
mas01cr@456
|
181 }
|
mas01cr@456
|
182 return z;
|
mas01cr@456
|
183 }
|
mas01cr@456
|
184
|
mas01cr@456
|
185
|
mas01mc@292
|
186 /************************ LSH indexing ***********************************/
|
mas01mc@292
|
187 void audioDB::index_index_db(const char* dbName){
|
mas01mc@292
|
188 char* newIndexName;
|
mas01mc@292
|
189 double *fvp = 0, *sNorm = 0, *snPtr = 0, *sPower = 0, *spPtr = 0;
|
mas01mc@292
|
190 Uns32T dbVectors = 0;
|
mas01mc@292
|
191
|
mas01mc@324
|
192
|
mas01mc@292
|
193 printf("INDEX: initializing header\n");
|
mas01mc@292
|
194 // Check if audioDB exists, initialize header and open database for read
|
mas01mc@292
|
195 forWrite = false;
|
mas01mc@292
|
196 initDBHeader(dbName);
|
mas01mc@292
|
197
|
mas01mc@324
|
198 if(dbH->flags & O2_FLAG_POWER)
|
mas01mc@324
|
199 usingPower = true;
|
mas01mc@324
|
200
|
mas01mc@324
|
201 if(dbH->flags & O2_FLAG_TIMES)
|
mas01mc@324
|
202 usingTimes = true;
|
mas01mc@324
|
203
|
mas01mc@292
|
204 newIndexName = index_get_name(dbName, radius, sequenceLength);
|
mas01mc@292
|
205
|
mas01mc@292
|
206 // Set unit norming flag override
|
mas01mc@292
|
207 audioDB::normalizedDistance = !audioDB::no_unit_norming;
|
mas01mc@292
|
208
|
mas01mc@327
|
209 VERB_LOG(1, "INDEX: dim %d\n", (int)dbH->dim);
|
mas01mc@327
|
210 VERB_LOG(1, "INDEX: R %f\n", radius);
|
mas01mc@327
|
211 VERB_LOG(1, "INDEX: seqlen %d\n", sequenceLength);
|
mas01mc@327
|
212 VERB_LOG(1, "INDEX: lsh_w %f\n", lsh_param_w);
|
mas01mc@327
|
213 VERB_LOG(1, "INDEX: lsh_k %d\n", lsh_param_k);
|
mas01mc@327
|
214 VERB_LOG(1, "INDEX: lsh_m %d\n", lsh_param_m);
|
mas01mc@327
|
215 VERB_LOG(1, "INDEX: lsh_N %d\n", lsh_param_N);
|
mas01mc@327
|
216 VERB_LOG(1, "INDEX: lsh_C %d\n", lsh_param_ncols);
|
mas01mc@327
|
217 VERB_LOG(1, "INDEX: lsh_b %d\n", lsh_param_b);
|
mas01mc@327
|
218 VERB_LOG(1, "INDEX: normalized? %s\n", normalizedDistance?"true":"false");
|
mas01mc@292
|
219
|
mas01mc@292
|
220 if((lshfid = open(newIndexName,O_RDONLY))<0){
|
mas01mc@292
|
221 printf("INDEX: constructing new LSH index\n");
|
mas01mc@292
|
222 printf("INDEX: making index file %s\n", newIndexName);
|
mas01mc@292
|
223 fflush(stdout);
|
mas01mc@292
|
224 // Construct new LSH index
|
mas01mc@292
|
225 lsh = new LSH((float)lsh_param_w, lsh_param_k,
|
mas01mc@292
|
226 lsh_param_m,
|
mas01mc@292
|
227 (Uns32T)(sequenceLength*dbH->dim),
|
mas01mc@292
|
228 lsh_param_N,
|
mas01mc@292
|
229 lsh_param_ncols,
|
mas01mc@292
|
230 (float)radius);
|
mas01mc@292
|
231 assert(lsh);
|
mas01mc@292
|
232
|
mas01mc@292
|
233 Uns32T endTrack = lsh_param_b;
|
mas01mc@292
|
234 if( endTrack > dbH->numFiles)
|
mas01mc@292
|
235 endTrack = dbH->numFiles;
|
mas01mc@292
|
236 // Insert up to lsh_param_b tracks
|
mas01mc@324
|
237 if( ! (dbH->flags & O2_FLAG_LARGE_ADB) ){
|
mas01mc@324
|
238 index_initialize(&sNorm, &snPtr, &sPower, &spPtr, &dbVectors);
|
mas01mc@324
|
239 }
|
mas01mc@324
|
240 index_insert_tracks(0, endTrack, &fvp, &sNorm, &snPtr, &sPower, &spPtr);
|
mas01mc@292
|
241 lsh->serialize(newIndexName, lsh_in_core?O2_SERIAL_FILEFORMAT2:O2_SERIAL_FILEFORMAT1);
|
mas01mc@292
|
242
|
mas01mc@292
|
243 // Clean up
|
mas01mc@292
|
244 delete lsh;
|
mas01mc@308
|
245 lsh = 0;
|
mas01mc@292
|
246 close(lshfid);
|
mas01mc@292
|
247 }
|
mas01mc@292
|
248
|
mas01mc@292
|
249 // Attempt to open LSH file
|
mas01mc@292
|
250 if((lshfid = open(newIndexName,O_RDONLY))>0){
|
mas01mc@292
|
251 printf("INDEX: merging with existing LSH index\n");
|
mas01mc@292
|
252 fflush(stdout);
|
mas01mc@340
|
253 char* mergeIndexName = newIndexName;
|
mas01mc@292
|
254
|
mas01mc@292
|
255 // Get the lsh header info and find how many tracks are inserted already
|
mas01mc@340
|
256 lsh = new LSH(mergeIndexName, false); // lshInCore=false to avoid loading hashTables here
|
mas01mc@292
|
257 assert(lsh);
|
mas01cr@458
|
258 Uns32T maxs = audiodb_index_to_track_id(lsh->get_maxp(), audiodb_lsh_n_point_bits(adb))+1;
|
mas01mc@292
|
259 delete lsh;
|
mas01mc@308
|
260 lsh = 0;
|
mas01mc@292
|
261
|
mas01mc@340
|
262 // Insert up to lsh_param_b tracks
|
mas01mc@340
|
263 if( !sNorm && !(dbH->flags & O2_FLAG_LARGE_ADB) ){
|
mas01mc@340
|
264 index_initialize(&sNorm, &snPtr, &sPower, &spPtr, &dbVectors);
|
mas01mc@340
|
265 }
|
mas01mc@292
|
266 // This allows for updating index after more tracks are inserted into audioDB
|
mas01mc@292
|
267 for(Uns32T startTrack = maxs; startTrack < dbH->numFiles; startTrack+=lsh_param_b){
|
mas01mc@292
|
268
|
mas01mc@292
|
269 Uns32T endTrack = startTrack + lsh_param_b;
|
mas01mc@292
|
270 if( endTrack > dbH->numFiles)
|
mas01mc@292
|
271 endTrack = dbH->numFiles;
|
mas01mc@292
|
272 printf("Indexing track range: %d - %d\n", startTrack, endTrack);
|
mas01mc@292
|
273 fflush(stdout);
|
mas01mc@340
|
274 lsh = new LSH(mergeIndexName, false); // Initialize empty LSH tables
|
mas01mc@292
|
275 assert(lsh);
|
mas01mc@292
|
276
|
mas01mc@292
|
277 // Insert up to lsh_param_b database tracks
|
mas01mc@292
|
278 index_insert_tracks(startTrack, endTrack, &fvp, &sNorm, &snPtr, &sPower, &spPtr);
|
mas01mc@292
|
279
|
mas01mc@340
|
280 // Serialize to file (merging is performed here)
|
mas01mc@340
|
281 lsh->serialize(mergeIndexName, lsh_in_core?O2_SERIAL_FILEFORMAT2:O2_SERIAL_FILEFORMAT1); // Serialize core LSH heap to disk
|
mas01mc@292
|
282 delete lsh;
|
mas01mc@308
|
283 lsh = 0;
|
mas01mc@340
|
284 }
|
mas01mc@292
|
285
|
mas01mc@292
|
286 close(lshfid);
|
mas01mc@292
|
287 printf("INDEX: done constructing LSH index.\n");
|
mas01mc@292
|
288 fflush(stdout);
|
mas01mc@292
|
289
|
mas01mc@292
|
290 }
|
mas01mc@292
|
291 else{
|
mas01mc@292
|
292 error("Something's wrong with LSH index file");
|
mas01mc@292
|
293 exit(1);
|
mas01mc@292
|
294 }
|
mas01mc@292
|
295
|
mas01mc@324
|
296 delete[] newIndexName;
|
mas01mc@324
|
297 delete[] sNorm;
|
mas01mc@324
|
298 delete[] sPower;
|
mas01mc@324
|
299 }
|
mas01mc@292
|
300
|
mas01mc@292
|
301
|
mas01cr@405
|
302 void audioDB::insertPowerData(unsigned numVectors, int powerfd, double *powerdata) {
|
mas01cr@405
|
303 if(usingPower){
|
mas01cr@405
|
304 int one;
|
mas01cr@405
|
305 unsigned int count;
|
mas01cr@405
|
306
|
mas01cr@405
|
307 count = read(powerfd, &one, sizeof(unsigned int));
|
mas01cr@405
|
308 if (count != sizeof(unsigned int)) {
|
mas01cr@405
|
309 error("powerfd read failed", "int", "read");
|
mas01cr@405
|
310 }
|
mas01cr@405
|
311 if (one != 1) {
|
mas01cr@405
|
312 error("dimensionality of power file not 1", powerFileName);
|
mas01cr@405
|
313 }
|
mas01cr@405
|
314
|
mas01cr@405
|
315 // FIXME: should check that the powerfile is the right size for
|
mas01cr@405
|
316 // this. -- CSR, 2007-10-30
|
mas01cr@405
|
317 count = read(powerfd, powerdata, numVectors * sizeof(double));
|
mas01cr@405
|
318 if (count != numVectors * sizeof(double)) {
|
mas01cr@405
|
319 error("powerfd read failed", "double", "read");
|
mas01cr@405
|
320 }
|
mas01cr@405
|
321 }
|
mas01cr@405
|
322 }
|
mas01cr@405
|
323
|
mas01mc@324
|
324 // initialize auxillary track data from filesystem
|
mas01mc@324
|
325 // pre-conditions:
|
mas01mc@324
|
326 // dbH->flags & O2_FLAG_LARGE_ADB
|
mas01mc@324
|
327 // feature data allocated and copied (fvp)
|
mas01mc@324
|
328 //
|
mas01mc@324
|
329 // post-conditions:
|
mas01mc@324
|
330 // allocated power data
|
mas01mc@324
|
331 // allocated l2norm data
|
mas01mc@324
|
332 //
|
mas01mc@324
|
333 void audioDB::init_track_aux_data(Uns32T trackID, double* fvp, double** sNormpp,double** snPtrp, double** sPowerp, double** spPtrp){
|
mas01mc@324
|
334 if( !(dbH->flags & O2_FLAG_LARGE_ADB) )
|
mas01mc@324
|
335 error("error: init_track_large_adb required O2_FLAG_LARGE_ADB");
|
mas01mc@292
|
336
|
mas01mc@324
|
337 // Allocate and read the power sequence
|
mas01mc@324
|
338 if(trackTable[trackID]>=sequenceLength){
|
mas01mc@324
|
339
|
mas01mc@324
|
340 char* prefixedString = new char[O2_MAXFILESTR];
|
mas01mc@324
|
341 char* tmpStr = prefixedString;
|
mas01mc@324
|
342 // Open and check dimensions of power file
|
mas01mc@324
|
343 strncpy(prefixedString, powerFileNameTable+trackID*O2_FILETABLE_ENTRY_SIZE, O2_MAXFILESTR);
|
mas01mc@324
|
344 prefix_name((char ** const)&prefixedString, adb_feature_root);
|
mas01mc@324
|
345 if(prefixedString!=tmpStr)
|
mas01mc@324
|
346 delete[] tmpStr;
|
mas01mc@324
|
347 powerfd = open(prefixedString, O_RDONLY);
|
mas01mc@324
|
348 if (powerfd < 0) {
|
mas01mc@324
|
349 error("failed to open power file", prefixedString);
|
mas01mc@324
|
350 }
|
mas01mc@324
|
351 if (fstat(powerfd, &statbuf) < 0) {
|
mas01mc@324
|
352 error("fstat error finding size of power file", prefixedString, "fstat");
|
mas01mc@324
|
353 }
|
mas01mc@324
|
354
|
mas01mc@324
|
355 if( (statbuf.st_size - sizeof(int)) / (sizeof(double)) != trackTable[trackID] )
|
mas01mc@324
|
356 error("Dimension mismatch: numPowers != numVectors", prefixedString);
|
mas01mc@324
|
357
|
mas01mc@324
|
358 *sPowerp = new double[trackTable[trackID]]; // Allocate memory for power values
|
mas01mc@324
|
359 assert(*sPowerp);
|
mas01mc@324
|
360 *spPtrp = *sPowerp;
|
mas01mc@324
|
361 insertPowerData(trackTable[trackID], powerfd, *sPowerp);
|
mas01mc@324
|
362 if (0 < powerfd) {
|
mas01mc@324
|
363 close(powerfd);
|
mas01mc@324
|
364 }
|
mas01mc@324
|
365
|
mas01cr@427
|
366 audiodb_sequence_sum(*sPowerp, trackTable[trackID], sequenceLength);
|
mas01cr@427
|
367 audiodb_sequence_average(*sPowerp, trackTable[trackID], sequenceLength);
|
mas01mc@324
|
368 powerTable = 0;
|
mas01mc@324
|
369
|
mas01mc@324
|
370 // Allocate and calculate the l2norm sequence
|
mas01mc@324
|
371 *sNormpp = new double[trackTable[trackID]];
|
mas01mc@324
|
372 assert(*sNormpp);
|
mas01mc@324
|
373 *snPtrp = *sNormpp;
|
mas01cr@426
|
374 audiodb_l2norm_buffer(fvp, dbH->dim, trackTable[trackID], *sNormpp);
|
mas01cr@427
|
375 audiodb_sequence_sum(*sNormpp, trackTable[trackID], sequenceLength);
|
mas01cr@427
|
376 audiodb_sequence_sqrt(*sNormpp, trackTable[trackID], sequenceLength);
|
mas01mc@324
|
377 }
|
mas01mc@292
|
378 }
|
mas01mc@292
|
379
|
mas01mc@292
|
380 void audioDB::index_insert_tracks(Uns32T start_track, Uns32T end_track,
|
mas01mc@292
|
381 double** fvpp, double** sNormpp,double** snPtrp,
|
mas01mc@292
|
382 double** sPowerp, double** spPtrp){
|
mas01mc@292
|
383 size_t nfv = 0;
|
mas01mc@292
|
384 double* fvp = 0; // Keep pointer for memory allocation and free() for track data
|
mas01mc@292
|
385 Uns32T trackID = 0;
|
mas01mc@292
|
386
|
mas01mc@292
|
387 VERB_LOG(1, "indexing tracks...");
|
mas01mc@292
|
388
|
mas01mc@324
|
389 int trackfd = dbfid;
|
mas01mc@292
|
390 for(trackID = start_track ; trackID < end_track ; trackID++ ){
|
mas01mc@324
|
391 if( dbH->flags & O2_FLAG_LARGE_ADB ){
|
mas01mc@324
|
392 char* prefixedString = new char[O2_MAXFILESTR];
|
mas01mc@324
|
393 char* tmpStr = prefixedString;
|
mas01mc@324
|
394 // Open and check dimensions of feature file
|
mas01mc@324
|
395 strncpy(prefixedString, featureFileNameTable+trackID*O2_FILETABLE_ENTRY_SIZE, O2_MAXFILESTR);
|
mas01mc@324
|
396 prefix_name((char ** const) &prefixedString, adb_feature_root);
|
mas01mc@324
|
397 if(prefixedString!=tmpStr)
|
mas01mc@324
|
398 delete[] tmpStr;
|
mas01cr@454
|
399 initInputFile(prefixedString);
|
mas01mc@324
|
400 trackfd = infid;
|
mas01mc@324
|
401 }
|
mas01cr@433
|
402 if(audiodb_read_data(adb, trackfd, trackID, &fvp, &nfv))
|
mas01cr@433
|
403 error("failed to read data");
|
mas01mc@292
|
404 *fvpp = fvp; // Protect memory allocation and free() for track data
|
mas01mc@324
|
405
|
mas01mc@324
|
406 if( dbH->flags & O2_FLAG_LARGE_ADB )
|
mas01mc@324
|
407 // Load power and calculate power and l2norm sequence sums
|
mas01mc@324
|
408 init_track_aux_data(trackID, fvp, sNormpp, snPtrp, sPowerp, spPtrp);
|
mas01mc@324
|
409
|
mas01mc@292
|
410 if(!index_insert_track(trackID, fvpp, snPtrp, spPtrp))
|
mas01mc@292
|
411 break;
|
mas01mc@324
|
412 if ( dbH->flags & O2_FLAG_LARGE_ADB ){
|
mas01mc@324
|
413 close(infid);
|
mas01mc@324
|
414 delete[] *sNormpp;
|
mas01mc@324
|
415 delete[] *sPowerp;
|
mas01mc@324
|
416 *sNormpp = *sPowerp = *snPtrp = *snPtrp = 0;
|
mas01mc@324
|
417 }
|
mas01mc@324
|
418 } // end for(trackID = start_track ; ... )
|
mas01mc@292
|
419 std::cout << "finished inserting." << endl;
|
mas01mc@292
|
420 }
|
mas01mc@292
|
421
|
mas01mc@292
|
422 int audioDB::index_insert_track(Uns32T trackID, double** fvpp, double** snpp, double** sppp){
|
mas01mc@292
|
423 // Loop over the current input track's vectors
|
mas01cr@305
|
424 Uns32T numVecs = 0;
|
mas01cr@305
|
425 if (trackTable[trackID] > O2_MAXTRACKLEN) {
|
mas01cr@305
|
426 if (O2_MAXTRACKLEN < sequenceLength - 1) {
|
mas01cr@305
|
427 numVecs = 0;
|
mas01cr@305
|
428 } else {
|
mas01cr@305
|
429 numVecs = O2_MAXTRACKLEN - sequenceLength + 1;
|
mas01cr@305
|
430 }
|
mas01cr@305
|
431 } else {
|
mas01cr@305
|
432 if (trackTable[trackID] < sequenceLength - 1) {
|
mas01cr@305
|
433 numVecs = 0;
|
mas01cr@305
|
434 } else {
|
mas01cr@305
|
435 numVecs = trackTable[trackID] - sequenceLength + 1;
|
mas01cr@305
|
436 }
|
mas01cr@305
|
437 }
|
mas01mc@292
|
438
|
mas01mc@324
|
439 Uns32T numVecsAboveThreshold = 0, collisionCount = 0;
|
mas01mc@324
|
440 if(numVecs){
|
mas01mc@324
|
441 vv = index_initialize_shingles(numVecs);
|
mas01mc@324
|
442
|
mas01mc@324
|
443 for( Uns32T pointID = 0 ; pointID < numVecs; pointID++ )
|
mas01cr@456
|
444 audiodb_index_make_shingle(vv, pointID, *fvpp, dbH->dim, sequenceLength);
|
mas01mc@324
|
445
|
mas01mc@324
|
446 numVecsAboveThreshold = index_norm_shingles(vv, *snpp, *sppp);
|
mas01mc@324
|
447 collisionCount = index_insert_shingles(vv, trackID, *sppp);
|
mas01mc@324
|
448 }
|
mas01mc@292
|
449 float meanCollisionCount = numVecsAboveThreshold?(float)collisionCount/numVecsAboveThreshold:0;
|
mas01mc@292
|
450
|
mas01mc@292
|
451 /* index_norm_shingles() only goes as far as the end of the
|
mas01mc@292
|
452 sequence, which is right, but the space allocated is for the
|
mas01mc@292
|
453 whole track. */
|
mas01mc@292
|
454
|
mas01mc@292
|
455 /* But numVecs will be <trackTable[track] if trackTable[track]>O2_MAXTRACKLEN
|
mas01mc@292
|
456 * So let's be certain the pointers are in the correct place
|
mas01mc@292
|
457 */
|
mas01mc@292
|
458
|
mas01mc@324
|
459 if( !(dbH->flags & O2_FLAG_LARGE_ADB) ){
|
mas01mc@324
|
460 *snpp += trackTable[trackID];
|
mas01mc@324
|
461 *sppp += trackTable[trackID];
|
mas01mc@324
|
462 *fvpp += trackTable[trackID] * dbH->dim;
|
mas01mc@324
|
463 }
|
mas01mc@292
|
464
|
mas01mc@292
|
465 std::cout << " n=" << trackTable[trackID] << " n'=" << numVecsAboveThreshold << " E[#c]=" << lsh->get_mean_collision_rate() << " E[#p]=" << meanCollisionCount << endl;
|
mas01mc@292
|
466 std::cout.flush();
|
mas01mc@292
|
467 return true;
|
mas01mc@292
|
468 }
|
mas01mc@292
|
469
|
mas01mc@292
|
470 Uns32T audioDB::index_insert_shingles(vector<vector<float> >* vv, Uns32T trackID, double* spp){
|
mas01mc@292
|
471 Uns32T collisionCount = 0;
|
mas01mc@292
|
472 cout << "[" << trackID << "]" << fileTable+trackID*O2_FILETABLE_ENTRY_SIZE;
|
mas01mc@324
|
473 for( Uns32T pointID=0 ; pointID < (*vv).size(); pointID+=sequenceHop){
|
mas01mc@324
|
474 if(!use_absolute_threshold || (use_absolute_threshold && (*spp >= absolute_threshold)))
|
mas01cr@458
|
475 collisionCount += lsh->insert_point((*vv)[pointID], audiodb_index_from_trackinfo(trackID, pointID, audiodb_lsh_n_point_bits(adb)));
|
mas01mc@324
|
476 spp+=sequenceHop;
|
mas01mc@311
|
477 }
|
mas01mc@292
|
478 return collisionCount;
|
mas01mc@292
|
479 }
|
mas01mc@292
|
480
|
mas01mc@292
|
481 /*********************** LSH retrieval ****************************/
|
mas01mc@292
|
482
|
mas01mc@292
|
483
|
mas01mc@292
|
484 // return true if indexed query performed else return false
|
mas01mc@292
|
485 int audioDB::index_init_query(const char* dbName){
|
mas01mc@292
|
486
|
mas01mc@292
|
487 if(!(index_exists(dbName, radius, sequenceLength)))
|
mas01mc@292
|
488 return false;
|
mas01mc@292
|
489
|
mas01mc@292
|
490 char* indexName = index_get_name(dbName, radius, sequenceLength);
|
mas01mc@292
|
491
|
mas01mc@292
|
492 // Test to see if file exists
|
mas01mc@292
|
493 if((lshfid = open (indexName, O_RDONLY)) < 0){
|
mas01mc@292
|
494 delete[] indexName;
|
mas01mc@292
|
495 return false;
|
mas01mc@292
|
496 }
|
mas01mc@292
|
497
|
mas01mc@308
|
498 lsh = index_allocate(indexName, false); // Get the header only here
|
mas01mc@292
|
499 sequenceLength = lsh->get_lshHeader()->dataDim / dbH->dim; // shingleDim / vectorDim
|
mas01mc@292
|
500
|
mas01mc@311
|
501 if(lsh!=SERVER_LSH_INDEX_SINGLETON){
|
mas01mc@308
|
502 if( fabs(radius - lsh->get_radius())>fabs(O2_DISTANCE_TOLERANCE))
|
mas01mc@308
|
503 printf("*** Warning: adb_radius (%f) != lsh_radius (%f) ***\n", radius, lsh->get_radius());
|
mas01mc@327
|
504 VERB_LOG(1,"INDEX: dim %d\n", (int)dbH->dim);
|
mas01mc@327
|
505 VERB_LOG(1,"INDEX: R %f\n", lsh->get_radius());
|
mas01mc@327
|
506 VERB_LOG(1,"INDEX: seqlen %d\n", sequenceLength);
|
mas01mc@327
|
507 VERB_LOG(1,"INDEX: w %f\n", lsh->get_lshHeader()->get_binWidth());
|
mas01mc@327
|
508 VERB_LOG(1,"INDEX: k %d\n", lsh->get_lshHeader()->get_numFuns());
|
mas01mc@327
|
509 VERB_LOG(1,"INDEX: L (m*(m-1))/2 %d\n", lsh->get_lshHeader()->get_numTables());
|
mas01mc@327
|
510 VERB_LOG(1,"INDEX: N %d\n", lsh->get_lshHeader()->get_numRows());
|
mas01cr@458
|
511 VERB_LOG(1,"INDEX: s %d\n", audiodb_index_to_track_id(lsh->get_maxp(), audiodb_lsh_n_point_bits(adb)));
|
mas01mc@327
|
512 VERB_LOG(1,"INDEX: Opened LSH index file %s\n", indexName);
|
mas01mc@308
|
513 }
|
mas01mc@292
|
514
|
mas01mc@292
|
515 // Check to see if we are loading hash tables into core, and do so if true
|
mas01mc@292
|
516 if((lsh->get_lshHeader()->flags&O2_SERIAL_FILEFORMAT2) || lsh_in_core){
|
mas01mc@308
|
517 if(SERVER_LSH_INDEX_SINGLETON)
|
mas01mc@308
|
518 fprintf(stderr,"INDEX: using persistent hash tables: %s\n", lsh->get_indexName());
|
mas01mc@308
|
519 else
|
mas01mc@327
|
520 VERB_LOG(1,"INDEX: loading hash tables into core %s\n", (lsh->get_lshHeader()->flags&O2_SERIAL_FILEFORMAT2)?"FORMAT2":"FORMAT1");
|
mas01mc@308
|
521 lsh = index_allocate(indexName, true);
|
mas01mc@292
|
522 }
|
mas01mc@292
|
523
|
mas01mc@292
|
524 delete[] indexName;
|
mas01mc@292
|
525 return true;
|
mas01mc@292
|
526 }
|
mas01mc@292
|
527
|
mas01cr@458
|
528 void audiodb_index_add_point_approximate(void *user_data, Uns32T pointID, Uns32T qpos, float dist) {
|
mas01cr@458
|
529 adb_qcallback_t *data = (adb_qcallback_t *) user_data;
|
mas01cr@458
|
530 adb_t *adb = data->adb;
|
mas01cr@458
|
531 adb_qstate_internal_t *qstate = data->qstate;
|
mas01cr@458
|
532 uint32_t nbits = audiodb_lsh_n_point_bits(adb);
|
mas01cr@458
|
533 uint32_t trackID = audiodb_index_to_track_id(pointID, nbits);
|
mas01cr@458
|
534 uint32_t spos = audiodb_index_to_track_pos(pointID, nbits);
|
mas01cr@458
|
535 std::set<std::string>::iterator keys_end = qstate->allowed_keys->end();
|
mas01cr@458
|
536 if(qstate->allowed_keys->find((*adb->keys)[trackID]) != keys_end) {
|
mas01cr@424
|
537 adb_result_t r;
|
mas01cr@458
|
538 r.key = (*adb->keys)[trackID].c_str();
|
mas01cr@424
|
539 r.dist = dist;
|
mas01cr@424
|
540 r.qpos = qpos;
|
mas01cr@424
|
541 r.ipos = spos;
|
mas01cr@458
|
542 qstate->accumulator->add_point(&r);
|
mas01cr@424
|
543 }
|
mas01mc@292
|
544 }
|
mas01mc@292
|
545
|
mas01cr@458
|
546 // Maintain a queue of points to pass to query_loop_points() for exact
|
mas01cr@458
|
547 // evaluation
|
mas01cr@458
|
548 void audiodb_index_add_point_exact(void *user_data, Uns32T pointID, Uns32T qpos, float dist) {
|
mas01cr@458
|
549 adb_qcallback_t *data = (adb_qcallback_t *) user_data;
|
mas01cr@458
|
550 adb_t *adb = data->adb;
|
mas01cr@458
|
551 adb_qstate_internal_t *qstate = data->qstate;
|
mas01cr@458
|
552 uint32_t nbits = audiodb_lsh_n_point_bits(adb);
|
mas01cr@458
|
553 uint32_t trackID = audiodb_index_to_track_id(pointID, nbits);
|
mas01cr@458
|
554 uint32_t spos = audiodb_index_to_track_pos(pointID, nbits);
|
mas01cr@458
|
555 std::set<std::string>::iterator keys_end = qstate->allowed_keys->end();
|
mas01cr@458
|
556 if(qstate->allowed_keys->find((*adb->keys)[trackID]) != keys_end) {
|
mas01cr@458
|
557 PointPair p(trackID, qpos, spos);
|
mas01cr@458
|
558 qstate->exact_evaluation_queue->push(p);
|
mas01cr@458
|
559 }
|
mas01mc@292
|
560 }
|
mas01mc@292
|
561
|
mas01mc@292
|
562 // return 0: if index does not exist
|
mas01mc@292
|
563 // return nqv: if index exists
|
mas01cr@458
|
564 int audioDB::index_query_loop(adb_t *adb, adb_query_spec_t *spec, adb_qstate_internal_t *qstate) {
|
mas01mc@292
|
565
|
mas01mc@324
|
566 double *query = 0, *query_data = 0;
|
mas01cr@437
|
567 adb_qpointers_internal_t qpointers = {0};
|
mas01cr@437
|
568
|
mas01cr@458
|
569 adb_qcallback_t callback_data;
|
mas01cr@458
|
570 callback_data.adb = adb;
|
mas01cr@458
|
571 callback_data.qstate = qstate;
|
mas01cr@458
|
572
|
mas01mc@292
|
573 void (*add_point_func)(void*,Uns32T,Uns32T,float);
|
mas01mc@292
|
574
|
mas01cr@436
|
575 sequenceLength = spec->qid.sequence_length;
|
mas01cr@435
|
576 normalizedDistance = (spec->params.distance == ADB_DISTANCE_EUCLIDEAN_NORMED);
|
mas01cr@431
|
577
|
mas01mc@292
|
578 // Set the point-reporter callback based on the value of lsh_exact
|
mas01cr@458
|
579 if(lsh_exact) {
|
mas01cr@458
|
580 qstate->exact_evaluation_queue = new std::priority_queue<PointPair>;
|
mas01cr@458
|
581 add_point_func = &audiodb_index_add_point_exact;
|
mas01cr@458
|
582 } else {
|
mas01cr@458
|
583 add_point_func = &audiodb_index_add_point_approximate;
|
mas01mc@292
|
584 }
|
mas01mc@292
|
585
|
mas01cr@455
|
586 if(!index_init_query(adb->path)) // sets-up LSH index structures for querying
|
mas01mc@292
|
587 return 0;
|
mas01mc@292
|
588
|
mas01cr@455
|
589 char* database = index_get_name(adb->path, radius, sequenceLength);
|
mas01mc@292
|
590
|
mas01cr@444
|
591 if(audiodb_query_spec_qpointers(adb, spec, &query_data, &query, &qpointers)) {
|
mas01cr@444
|
592 error("failed to set up qpointers");
|
mas01cr@444
|
593 }
|
mas01mc@292
|
594
|
mas01mc@292
|
595 // query vector index
|
mas01cr@437
|
596 Uns32T Nq = (qpointers.nvectors>O2_MAXTRACKLEN?O2_MAXTRACKLEN:qpointers.nvectors) - sequenceLength + 1;
|
mas01mc@292
|
597 vv = index_initialize_shingles(Nq); // allocate memory to copy query vectors to shingles
|
mas01cr@447
|
598
|
mas01mc@292
|
599 // Construct shingles from query features
|
mas01mc@292
|
600 for( Uns32T pointID = 0 ; pointID < Nq ; pointID++ )
|
mas01cr@456
|
601 audiodb_index_make_shingle(vv, pointID, query, dbH->dim, sequenceLength);
|
mas01mc@292
|
602
|
mas01mc@292
|
603 // Normalize query vectors
|
mas01cr@437
|
604 Uns32T numVecsAboveThreshold = index_norm_shingles( vv, qpointers.l2norm, qpointers.power );
|
mas01mc@292
|
605
|
mas01mc@292
|
606 // Nq contains number of inspected points in query file,
|
mas01mc@292
|
607 // numVecsAboveThreshold is number of points with power >= absolute_threshold
|
mas01cr@437
|
608 double* qpp = qpointers.power; // Keep original qpPtr for possible exact evaluation
|
mas01mc@292
|
609 if(usingQueryPoint && numVecsAboveThreshold){
|
mas01mc@292
|
610 if((lsh->get_lshHeader()->flags&O2_SERIAL_FILEFORMAT2) || lsh_in_core)
|
mas01cr@458
|
611 lsh->retrieve_point((*vv)[0], queryPoint, add_point_func, &callback_data);
|
mas01mc@292
|
612 else
|
mas01cr@458
|
613 lsh->serial_retrieve_point(database, (*vv)[0], queryPoint, add_point_func, &callback_data);
|
mas01mc@292
|
614 }
|
mas01mc@292
|
615 else if(numVecsAboveThreshold)
|
mas01mc@292
|
616 for( Uns32T pointID = 0 ; pointID < Nq; pointID++ )
|
mas01cr@370
|
617 if(!use_absolute_threshold || (use_absolute_threshold && (*qpp++ >= absolute_threshold))) {
|
mas01cr@370
|
618 if((lsh->get_lshHeader()->flags&O2_SERIAL_FILEFORMAT2) || lsh_in_core) {
|
mas01cr@458
|
619 lsh->retrieve_point((*vv)[pointID], pointID, add_point_func, &callback_data);
|
mas01cr@370
|
620 } else {
|
mas01cr@458
|
621 lsh->serial_retrieve_point(database, (*vv)[pointID], pointID, add_point_func, &callback_data);
|
mas01cr@370
|
622 }
|
mas01cr@370
|
623 }
|
mas01mc@292
|
624
|
mas01mc@292
|
625 if(lsh_exact)
|
mas01mc@292
|
626 // Perform exact distance computation on point pairs in exact_evaluation_queue
|
mas01cr@458
|
627 query_loop_points(adb, spec, qstate, query, &qpointers);
|
mas01mc@292
|
628
|
mas01mc@292
|
629 // Close the index file
|
mas01mc@292
|
630 close(lshfid);
|
mas01mc@292
|
631
|
mas01mc@292
|
632 // Clean up
|
mas01mc@292
|
633 if(query_data)
|
mas01mc@292
|
634 delete[] query_data;
|
mas01cr@437
|
635 if(qpointers.l2norm_data)
|
mas01cr@437
|
636 delete[] qpointers.l2norm_data;
|
mas01cr@437
|
637 if(qpointers.power_data)
|
mas01cr@437
|
638 delete[] qpointers.power_data;
|
mas01cr@437
|
639 if(qpointers.mean_duration)
|
mas01cr@437
|
640 delete[] qpointers.mean_duration;
|
mas01mc@292
|
641 if(database)
|
mas01mc@292
|
642 delete[] database;
|
mas01mc@292
|
643
|
mas01mc@292
|
644 return Nq;
|
mas01mc@292
|
645 }
|
mas01mc@292
|
646
|