comparison audioDB.cpp @ 27:0357114be285

add a forWrite flag to audioDB::initTables() so that clients can specify that they don't need write access to the database. (Tested with STATUS request)
author mas01cr
date Thu, 16 Aug 2007 11:39:46 +0000
parents 6d55ff3a21b1
children 657e638b87ef
comparison
equal deleted inserted replaced
26:6d55ff3a21b1 27:0357114be285
468 468
469 } 469 }
470 470
471 // initTables - memory map files passed as arguments 471 // initTables - memory map files passed as arguments
472 // Precondition: database has already been created 472 // Precondition: database has already been created
473 void audioDB::initTables(const char* dbName, const char* inFile=0){ 473 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile=0){
474 if ((dbfid = open (dbName, O_RDWR)) < 0) 474 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0)
475 error("Can't open database file", dbName); 475 error("Can't open database file", dbName);
476 476
477 // open the input file 477 // open the input file
478 if (inFile && (infid = open (inFile, O_RDONLY)) < 0) 478 if (inFile && (infid = open (inFile, O_RDONLY)) < 0)
479 error("can't open input file for reading", inFile); 479 error("can't open input file for reading", inFile);
516 if (inFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) 516 if (inFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0))
517 == (caddr_t) -1) 517 == (caddr_t) -1)
518 error("mmap error for input"); 518 error("mmap error for input");
519 519
520 // mmap the database file 520 // mmap the database file
521 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, 521 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | (forWrite ? PROT_WRITE : 0),
522 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) 522 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
523 error("mmap error for creating database"); 523 error("mmap error for creating database");
524 524
525 // Make some handy tables with correct types 525 // Make some handy tables with correct types
526 fileTable= (char*)(db+fileTableOffset); 526 fileTable= (char*)(db+fileTableOffset);
531 531
532 } 532 }
533 533
534 void audioDB::insert(const char* dbName, const char* inFile){ 534 void audioDB::insert(const char* dbName, const char* inFile){
535 535
536 initTables(dbName, inFile); 536 initTables(dbName, 1, inFile);
537 537
538 if(!usingTimes && (dbH->flags & O2_FLAG_TIMES)) 538 if(!usingTimes && (dbH->flags & O2_FLAG_TIMES))
539 error("Must use timestamps with timestamped database","use --times"); 539 error("Must use timestamps with timestamped database","use --times");
540 540
541 // Check that there is room for at least 1 more file 541 // Check that there is room for at least 1 more file
884 } 884 }
885 885
886 886
887 void audioDB::status(const char* dbName){ 887 void audioDB::status(const char* dbName){
888 if(!dbH) 888 if(!dbH)
889 initTables(dbName, 0); 889 initTables(dbName, 0, 0);
890 890
891 // Update Header information 891 // Update Header information
892 cout << "num files:" << dbH->numFiles << endl; 892 cout << "num files:" << dbH->numFiles << endl;
893 cout << "data dim:" << dbH->dim <<endl; 893 cout << "data dim:" << dbH->dim <<endl;
894 if(dbH->dim>0){ 894 if(dbH->dim>0){
913 } 913 }
914 914
915 915
916 void audioDB::dump(const char* dbName){ 916 void audioDB::dump(const char* dbName){
917 if(!dbH) 917 if(!dbH)
918 initTables(dbName,0); 918 initTables(dbName, 0, 0);
919 919
920 for(unsigned k=0, j=0; k<dbH->numFiles; k++){ 920 for(unsigned k=0, j=0; k<dbH->numFiles; k++){
921 cout << fileTable+k*O2_FILETABLESIZE << " " << trackTable[k] << endl; 921 cout << fileTable+k*O2_FILETABLESIZE << " " << trackTable[k] << endl;
922 j+=trackTable[k]; 922 j+=trackTable[k];
923 } 923 }
924 924
925 status(dbName); 925 status(dbName);
926 } 926 }
927 927
928 void audioDB::l2norm(const char* dbName){ 928 void audioDB::l2norm(const char* dbName){
929 initTables(dbName,0); 929 initTables(dbName, 0, 0);
930 if(dbH->length>0){ 930 if(dbH->length>0){
931 unsigned numVectors = dbH->length/(sizeof(double)*dbH->dim); 931 unsigned numVectors = dbH->length/(sizeof(double)*dbH->dim);
932 unitNormAndInsertL2(dataBuf, dbH->dim, numVectors, 0); // No append 932 unitNormAndInsertL2(dataBuf, dbH->dim, numVectors, 0); // No append
933 } 933 }
934 // Update database flags 934 // Update database flags
968 } 968 }
969 969
970 // Basic point query engine 970 // Basic point query engine
971 void audioDB::pointQuery(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){ 971 void audioDB::pointQuery(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){
972 972
973 initTables(dbName, inFile); 973 initTables(dbName, 0, inFile);
974 974
975 // For each input vector, find the closest pointNN matching output vectors and report 975 // For each input vector, find the closest pointNN matching output vectors and report
976 // we use stdout in this stub version 976 // we use stdout in this stub version
977 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim); 977 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim);
978 978
1142 1142
1143 // trackPointQuery 1143 // trackPointQuery
1144 // return the trackNN closest tracks to the query track 1144 // return the trackNN closest tracks to the query track
1145 // uses average of pointNN points per track 1145 // uses average of pointNN points per track
1146 void audioDB::trackPointQuery(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){ 1146 void audioDB::trackPointQuery(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){
1147 initTables(dbName, inFile); 1147 initTables(dbName, 0, inFile);
1148 1148
1149 // For each input vector, find the closest pointNN matching output vectors and report 1149 // For each input vector, find the closest pointNN matching output vectors and report
1150 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim); 1150 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim);
1151 unsigned numTracks = dbH->numFiles; 1151 unsigned numTracks = dbH->numFiles;
1152 1152
1393 // efficient implementation based on matched filter 1393 // efficient implementation based on matched filter
1394 // assumes normed shingles 1394 // assumes normed shingles
1395 // outputs distances of retrieved shingles, max retreived = pointNN shingles per per track 1395 // outputs distances of retrieved shingles, max retreived = pointNN shingles per per track
1396 void audioDB::trackSequenceQueryNN(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){ 1396 void audioDB::trackSequenceQueryNN(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){
1397 1397
1398 initTables(dbName, inFile); 1398 initTables(dbName, 0, inFile);
1399 1399
1400 // For each input vector, find the closest pointNN matching output vectors and report 1400 // For each input vector, find the closest pointNN matching output vectors and report
1401 // we use stdout in this stub version 1401 // we use stdout in this stub version
1402 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim); 1402 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim);
1403 unsigned numTracks = dbH->numFiles; 1403 unsigned numTracks = dbH->numFiles;
1887 // efficient implementation based on matched filter 1887 // efficient implementation based on matched filter
1888 // assumes normed shingles 1888 // assumes normed shingles
1889 // outputs count of retrieved shingles, max retreived = one shingle per query shingle per track 1889 // outputs count of retrieved shingles, max retreived = one shingle per query shingle per track
1890 void audioDB::trackSequenceQueryRad(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){ 1890 void audioDB::trackSequenceQueryRad(const char* dbName, const char* inFile, adb__queryResult *adbQueryResult){
1891 1891
1892 initTables(dbName, inFile); 1892 initTables(dbName, 0, inFile);
1893 1893
1894 // For each input vector, find the closest pointNN matching output vectors and report 1894 // For each input vector, find the closest pointNN matching output vectors and report
1895 // we use stdout in this stub version 1895 // we use stdout in this stub version
1896 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim); 1896 unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim);
1897 unsigned numTracks = dbH->numFiles; 1897 unsigned numTracks = dbH->numFiles;