Mercurial > hg > audiodb
comparison audioDB.cpp @ 105:10feb98abebf
Miscellaneous small refactorings:
* conditionalise signals code on O2_DEBUG;
* move audioDB initializers to .h file;
* reindent code according to One True Coding Standard;
* query types live in a different namespace to DB flags; preserve all
values (for binary compatibility) but make that clearer in the header
file.
author | mas01cr |
---|---|
date | Fri, 05 Oct 2007 11:05:22 +0000 |
parents | 97107ee61dba |
children | a0e422e3c553 |
comparison
equal
deleted
inserted
replaced
104:97107ee61dba | 105:10feb98abebf |
---|---|
1 #include "audioDB.h" | 1 #include "audioDB.h" |
2 | 2 |
3 #if defined(O2_DEBUG) | |
3 void sigterm_action(int signal, siginfo_t *info, void *context) { | 4 void sigterm_action(int signal, siginfo_t *info, void *context) { |
4 exit(128+signal); | 5 exit(128+signal); |
5 } | 6 } |
6 | 7 |
7 void sighup_action(int signal, siginfo_t *info, void *context) { | 8 void sighup_action(int signal, siginfo_t *info, void *context) { |
8 // FIXME: reread any configuration files | 9 // FIXME: reread any configuration files |
9 } | 10 } |
10 | 11 #endif |
11 #define O2_DEBUG | |
12 | 12 |
13 void audioDB::error(const char* a, const char* b, const char *sysFunc) { | 13 void audioDB::error(const char* a, const char* b, const char *sysFunc) { |
14 if(isServer) { | 14 if(isServer) { |
15 /* FIXME: I think this is leaky -- we never delete err. actually | 15 /* FIXME: I think this is leaky -- we never delete err. actually |
16 deleting it is tricky, though; it gets placed into some | 16 deleting it is tricky, though; it gets placed into some |
29 } | 29 } |
30 exit(1); | 30 exit(1); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 #define O2_AUDIODB_INITIALIZERS \ | |
35 dim(0), \ | |
36 dbName(0), \ | |
37 inFile(0), \ | |
38 key(0), \ | |
39 trackFileName(0), \ | |
40 trackFile(0), \ | |
41 command(0), \ | |
42 timesFileName(0), \ | |
43 timesFile(0), \ | |
44 dbfid(0), \ | |
45 infid(0), \ | |
46 db(0), \ | |
47 indata(0), \ | |
48 dbH(0), \ | |
49 fileTable(0), \ | |
50 trackTable(0), \ | |
51 dataBuf(0), \ | |
52 l2normTable(0), \ | |
53 qNorm(0), \ | |
54 timesTable(0), \ | |
55 verbosity(1), \ | |
56 queryType(O2_FLAG_POINT_QUERY), \ | |
57 pointNN(O2_DEFAULT_POINTNN), \ | |
58 trackNN(O2_DEFAULT_TRACKNN), \ | |
59 sequenceLength(16), \ | |
60 sequenceHop(1), \ | |
61 queryPoint(0), \ | |
62 usingQueryPoint(0), \ | |
63 usingTimes(0), \ | |
64 isClient(0), \ | |
65 isServer(0), \ | |
66 port(0), \ | |
67 timesTol(0.1), \ | |
68 radius(0) | |
69 | |
70 audioDB::audioDB(const unsigned argc, char* const argv[]): O2_AUDIODB_INITIALIZERS | 34 audioDB::audioDB(const unsigned argc, char* const argv[]): O2_AUDIODB_INITIALIZERS |
71 { | 35 { |
72 if(processArgs(argc, argv)<0){ | 36 if(processArgs(argc, argv)<0){ |
73 printf("No command found.\n"); | 37 printf("No command found.\n"); |
74 cmdline_parser_print_version (); | 38 cmdline_parser_print_version (); |
202 command=COM_SERVER; | 166 command=COM_SERVER; |
203 port=args_info.SERVER_arg; | 167 port=args_info.SERVER_arg; |
204 if(port<100 || port > 100000) | 168 if(port<100 || port > 100000) |
205 error("port out of range"); | 169 error("port out of range"); |
206 isServer=1; | 170 isServer=1; |
171 #if defined(O2_DEBUG) | |
207 struct sigaction sa; | 172 struct sigaction sa; |
208 sa.sa_sigaction = sigterm_action; | 173 sa.sa_sigaction = sigterm_action; |
209 sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER; | 174 sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER; |
210 sigaction(SIGTERM, &sa, NULL); | 175 sigaction(SIGTERM, &sa, NULL); |
211 sa.sa_sigaction = sighup_action; | 176 sa.sa_sigaction = sighup_action; |
212 sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER; | 177 sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER; |
213 sigaction(SIGHUP, &sa, NULL); | 178 sigaction(SIGHUP, &sa, NULL); |
179 #endif | |
214 return 0; | 180 return 0; |
215 } | 181 } |
216 | 182 |
217 // No return on client command, find database command | 183 // No return on client command, find database command |
218 if(args_info.client_given){ | 184 if(args_info.client_given){ |
219 command=COM_CLIENT; | 185 command=COM_CLIENT; |
220 hostport=args_info.client_arg; | 186 hostport=args_info.client_arg; |
221 isClient=1; | 187 isClient=1; |
222 } | 188 } |
223 | 189 |
224 if(args_info.NEW_given){ | 190 if(args_info.NEW_given){ |
225 command=COM_CREATE; | 191 command=COM_CREATE; |
226 dbName=args_info.database_arg; | 192 dbName=args_info.database_arg; |
227 return 0; | 193 return 0; |
228 } | 194 } |
229 | 195 |
230 if(args_info.STATUS_given){ | 196 if(args_info.STATUS_given){ |
231 command=COM_STATUS; | 197 command=COM_STATUS; |
232 dbName=args_info.database_arg; | 198 dbName=args_info.database_arg; |
233 return 0; | 199 return 0; |
234 } | 200 } |
235 | 201 |
236 if(args_info.DUMP_given){ | 202 if(args_info.DUMP_given){ |
237 command=COM_DUMP; | 203 command=COM_DUMP; |
238 dbName=args_info.database_arg; | 204 dbName=args_info.database_arg; |
239 return 0; | 205 return 0; |
240 } | 206 } |
241 | 207 |
242 if(args_info.L2NORM_given){ | 208 if(args_info.L2NORM_given){ |
243 command=COM_L2NORM; | 209 command=COM_L2NORM; |
244 dbName=args_info.database_arg; | 210 dbName=args_info.database_arg; |
245 return 0; | 211 return 0; |
246 } | 212 } |
247 | 213 |
248 if(args_info.INSERT_given){ | 214 if(args_info.INSERT_given){ |
249 command=COM_INSERT; | 215 command=COM_INSERT; |
250 dbName=args_info.database_arg; | 216 dbName=args_info.database_arg; |
251 inFile=args_info.features_arg; | 217 inFile=args_info.features_arg; |
252 if(args_info.key_given) | 218 if(args_info.key_given) |
253 key=args_info.key_arg; | 219 key=args_info.key_arg; |
254 if(args_info.times_given){ | 220 if(args_info.times_given){ |
255 timesFileName=args_info.times_arg; | 221 timesFileName=args_info.times_arg; |
256 if(strlen(timesFileName)>0){ | 222 if(strlen(timesFileName)>0){ |
257 if(!(timesFile = new ifstream(timesFileName,ios::in))) | 223 if(!(timesFile = new ifstream(timesFileName,ios::in))) |
258 error("Could not open times file for reading", timesFileName); | 224 error("Could not open times file for reading", timesFileName); |
259 usingTimes=1; | 225 usingTimes=1; |
260 } | 226 } |
261 } | 227 } |
262 return 0; | 228 return 0; |
263 } | 229 } |
264 | 230 |
265 if(args_info.BATCHINSERT_given){ | 231 if(args_info.BATCHINSERT_given){ |
266 command=COM_BATCHINSERT; | 232 command=COM_BATCHINSERT; |
267 dbName=args_info.database_arg; | 233 dbName=args_info.database_arg; |
268 inFile=args_info.featureList_arg; | 234 inFile=args_info.featureList_arg; |
269 if(args_info.keyList_given) | 235 if(args_info.keyList_given) |
270 key=args_info.keyList_arg; // INCONSISTENT NO CHECK | 236 key=args_info.keyList_arg; // INCONSISTENT NO CHECK |
271 | 237 |
272 /* TO DO: REPLACE WITH | 238 /* TO DO: REPLACE WITH |
273 if(args_info.keyList_given){ | 239 if(args_info.keyList_given){ |
274 trackFileName=args_info.keyList_arg; | 240 trackFileName=args_info.keyList_arg; |
275 if(strlen(trackFileName)>0 && !(trackFile = new ifstream(trackFileName,ios::in))) | 241 if(strlen(trackFileName)>0 && !(trackFile = new ifstream(trackFileName,ios::in))) |
276 error("Could not open keyList file for reading",trackFileName); | 242 error("Could not open keyList file for reading",trackFileName); |
277 } | 243 } |
278 AND UPDATE BATCHINSERT() | 244 AND UPDATE BATCHINSERT() |
279 */ | 245 */ |
280 | 246 |
281 if(args_info.timesList_given){ | 247 if(args_info.timesList_given){ |
282 timesFileName=args_info.timesList_arg; | 248 timesFileName=args_info.timesList_arg; |
283 if(strlen(timesFileName)>0){ | 249 if(strlen(timesFileName)>0){ |
284 if(!(timesFile = new ifstream(timesFileName,ios::in))) | 250 if(!(timesFile = new ifstream(timesFileName,ios::in))) |
285 error("Could not open timesList file for reading", timesFileName); | 251 error("Could not open timesList file for reading", timesFileName); |
286 usingTimes=1; | 252 usingTimes=1; |
287 } | 253 } |
288 } | 254 } |
289 return 0; | 255 return 0; |
290 } | 256 } |
291 | 257 |
292 // Query command and arguments | 258 // Query command and arguments |
293 if(args_info.QUERY_given){ | 259 if(args_info.QUERY_given){ |
294 command=COM_QUERY; | 260 command=COM_QUERY; |
295 dbName=args_info.database_arg; | 261 dbName=args_info.database_arg; |
296 inFile=args_info.features_arg; | 262 inFile=args_info.features_arg; |
297 | 263 |
298 if(args_info.keyList_given){ | 264 if(args_info.keyList_given){ |
299 trackFileName=args_info.keyList_arg; | 265 trackFileName=args_info.keyList_arg; |
300 if(strlen(trackFileName)>0 && !(trackFile = new ifstream(trackFileName,ios::in))) | 266 if(strlen(trackFileName)>0 && !(trackFile = new ifstream(trackFileName,ios::in))) |
301 error("Could not open keyList file for reading",trackFileName); | 267 error("Could not open keyList file for reading",trackFileName); |
302 } | 268 } |
303 | 269 |
304 if(args_info.times_given){ | 270 if(args_info.times_given){ |
305 timesFileName=args_info.times_arg; | 271 timesFileName=args_info.times_arg; |
306 if(strlen(timesFileName)>0){ | 272 if(strlen(timesFileName)>0){ |
307 if(!(timesFile = new ifstream(timesFileName,ios::in))) | 273 if(!(timesFile = new ifstream(timesFileName,ios::in))) |
308 error("Could not open times file for reading", timesFileName); | 274 error("Could not open times file for reading", timesFileName); |
309 usingTimes=1; | 275 usingTimes=1; |
310 } | 276 } |
311 } | 277 } |
312 | 278 |
313 // query type | 279 // query type |
314 if(strncmp(args_info.QUERY_arg, "track", MAXSTR)==0) | 280 if(strncmp(args_info.QUERY_arg, "track", MAXSTR)==0) |
315 queryType=O2_FLAG_TRACK_QUERY; | 281 queryType=O2_TRACK_QUERY; |
316 else if(strncmp(args_info.QUERY_arg, "point", MAXSTR)==0) | 282 else if(strncmp(args_info.QUERY_arg, "point", MAXSTR)==0) |
317 queryType=O2_FLAG_POINT_QUERY; | 283 queryType=O2_POINT_QUERY; |
318 else if(strncmp(args_info.QUERY_arg, "sequence", MAXSTR)==0) | 284 else if(strncmp(args_info.QUERY_arg, "sequence", MAXSTR)==0) |
319 queryType=O2_FLAG_SEQUENCE_QUERY; | 285 queryType=O2_SEQUENCE_QUERY; |
320 else | 286 else |
321 error("unsupported query type",args_info.QUERY_arg); | 287 error("unsupported query type",args_info.QUERY_arg); |
322 | 288 |
323 if(!args_info.exhaustive_flag){ | 289 if(!args_info.exhaustive_flag){ |
324 queryPoint = args_info.qpoint_arg; | 290 queryPoint = args_info.qpoint_arg; |
325 usingQueryPoint=1; | 291 usingQueryPoint=1; |
326 if(queryPoint<0 || queryPoint >10000) | 292 if(queryPoint<0 || queryPoint >10000) |
327 error("queryPoint out of range: 0 <= queryPoint <= 10000"); | 293 error("queryPoint out of range: 0 <= queryPoint <= 10000"); |
328 } | 294 } |
329 | 295 |
330 | 296 pointNN = args_info.pointnn_arg; |
331 pointNN=args_info.pointnn_arg; | 297 if(pointNN < 1 || pointNN > 1000) { |
332 if(pointNN<1 || pointNN >1000) | 298 error("pointNN out of range: 1 <= pointNN <= 1000"); |
333 error("pointNN out of range: 1 <= pointNN <= 1000"); | 299 } |
334 | 300 trackNN = args_info.resultlength_arg; |
335 | 301 if(trackNN < 1 || trackNN > 1000) { |
336 | 302 error("resultlength out of range: 1 <= resultlength <= 1000"); |
337 trackNN=args_info.resultlength_arg; | 303 } |
338 if(trackNN<1 || trackNN >10000) | 304 sequenceLength = args_info.sequencelength_arg; |
339 error("resultlength out of range: 1 <= resultlength <= 1000"); | 305 if(sequenceLength < 1 || sequenceLength > 1000) { |
340 | 306 error("seqlen out of range: 1 <= seqlen <= 1000"); |
341 | 307 } |
342 sequenceLength=args_info.sequencelength_arg; | 308 sequenceHop = args_info.sequencehop_arg; |
343 if(sequenceLength<1 || sequenceLength >1000) | 309 if(sequenceHop < 1 || sequenceHop > 1000) { |
344 error("seqlen out of range: 1 <= seqlen <= 1000"); | 310 error("seqhop out of range: 1 <= seqhop <= 1000"); |
345 | 311 } |
346 sequenceHop=args_info.sequencehop_arg; | 312 return 0; |
347 if(sequenceHop<1 || sequenceHop >1000) | 313 } |
348 error("seqhop out of range: 1 <= seqhop <= 1000"); | 314 return -1; // no command found |
349 | |
350 return 0; | |
351 } | |
352 return -1; // no command found | |
353 } | 315 } |
354 | 316 |
355 /* Make a new database | 317 /* Make a new database |
356 | 318 |
357 The database consists of: | 319 The database consists of: |
974 | 936 |
975 | 937 |
976 | 938 |
977 void audioDB::query(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){ | 939 void audioDB::query(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){ |
978 switch(queryType){ | 940 switch(queryType){ |
979 case O2_FLAG_POINT_QUERY: | 941 case O2_POINT_QUERY: |
980 pointQuery(dbName, inFile, adbQueryResult); | 942 pointQuery(dbName, inFile, adbQueryResult); |
981 break; | 943 break; |
982 case O2_FLAG_SEQUENCE_QUERY: | 944 case O2_SEQUENCE_QUERY: |
983 if(radius==0) | 945 if(radius==0) |
984 trackSequenceQueryNN(dbName, inFile, adbQueryResult); | 946 trackSequenceQueryNN(dbName, inFile, adbQueryResult); |
985 else | 947 else |
986 trackSequenceQueryRad(dbName, inFile, adbQueryResult); | 948 trackSequenceQueryRad(dbName, inFile, adbQueryResult); |
987 break; | 949 break; |
988 case O2_FLAG_TRACK_QUERY: | 950 case O2_TRACK_QUERY: |
989 trackPointQuery(dbName, inFile, adbQueryResult); | 951 trackPointQuery(dbName, inFile, adbQueryResult); |
990 break; | 952 break; |
991 default: | 953 default: |
992 error("unrecognized queryType in query()"); | 954 error("unrecognized queryType in query()"); |
993 | 955 |
2563 | 2525 |
2564 int adb__query(struct soap* soap, xsd__string dbName, xsd__string qKey, xsd__string keyList, xsd__string timesFileName, xsd__int qType, xsd__int qPos, xsd__int pointNN, xsd__int trackNN, xsd__int seqLen, adb__queryResult &adbQueryResult){ | 2526 int adb__query(struct soap* soap, xsd__string dbName, xsd__string qKey, xsd__string keyList, xsd__string timesFileName, xsd__int qType, xsd__int qPos, xsd__int pointNN, xsd__int trackNN, xsd__int seqLen, adb__queryResult &adbQueryResult){ |
2565 char queryType[256]; | 2527 char queryType[256]; |
2566 for(int k=0; k<256; k++) | 2528 for(int k=0; k<256; k++) |
2567 queryType[k]='\0'; | 2529 queryType[k]='\0'; |
2568 if(qType == O2_FLAG_POINT_QUERY) | 2530 if(qType == O2_POINT_QUERY) |
2569 strncpy(queryType, "point", strlen("point")); | 2531 strncpy(queryType, "point", strlen("point")); |
2570 else if (qType == O2_FLAG_SEQUENCE_QUERY) | 2532 else if (qType == O2_SEQUENCE_QUERY) |
2571 strncpy(queryType, "sequence", strlen("sequence")); | 2533 strncpy(queryType, "sequence", strlen("sequence")); |
2572 else if(qType == O2_FLAG_TRACK_QUERY) | 2534 else if(qType == O2_TRACK_QUERY) |
2573 strncpy(queryType,"track", strlen("track")); | 2535 strncpy(queryType,"track", strlen("track")); |
2574 else | 2536 else |
2575 strncpy(queryType, "", strlen("")); | 2537 strncpy(queryType, "", strlen("")); |
2576 | 2538 |
2577 if(pointNN==0) | 2539 if(pointNN==0) |