Mercurial > hg > audiodb
comparison audioDB.cpp @ 36:5bae5570fb2e audiodb-debian
Merge trunk changes -r44:51 onto audio-debian branch
author | mas01cr |
---|---|
date | Wed, 29 Aug 2007 16:12:46 +0000 |
parents | 5485586a5378 |
children | 06922d637752 |
comparison
equal
deleted
inserted
replaced
29:e736c35de0f6 | 36:5bae5570fb2e |
---|---|
95 | 95 |
96 #include "audioDB.h" | 96 #include "audioDB.h" |
97 | 97 |
98 #define O2_DEBUG | 98 #define O2_DEBUG |
99 | 99 |
100 void audioDB::error(const char* a, const char* b){ | 100 void audioDB::error(const char* a, const char* b, const char *sysFunc) { |
101 cerr << a << ":" << b << endl; | 101 cerr << a << ": " << b << endl; |
102 exit(1); | 102 if (sysFunc) { |
103 perror(sysFunc); | |
104 } | |
105 exit(1); | |
103 } | 106 } |
104 | 107 |
105 audioDB::audioDB(const unsigned argc, char* const argv[], adb__queryResult *adbQueryResult): | 108 audioDB::audioDB(const unsigned argc, char* const argv[], adb__queryResult *adbQueryResult): |
106 dim(0), | 109 dim(0), |
107 dbName(0), | 110 dbName(0), |
425 -------------------------------------------------------------------------- | 428 -------------------------------------------------------------------------- |
426 O2_MAXFILES * 02_MEANNUMFEATURES * sizeof(DOUBLE) | 429 O2_MAXFILES * 02_MEANNUMFEATURES * sizeof(DOUBLE) |
427 | 430 |
428 */ | 431 */ |
429 | 432 |
433 void audioDB::get_lock(int fd, bool exclusive) { | |
434 struct flock lock; | |
435 int status; | |
436 | |
437 lock.l_type = exclusive ? F_WRLCK : F_RDLCK; | |
438 lock.l_whence = SEEK_SET; | |
439 lock.l_start = 0; | |
440 lock.l_len = 0; /* "the whole file" */ | |
441 | |
442 retry: | |
443 do { | |
444 status = fcntl(fd, F_SETLKW, &lock); | |
445 } while (status != 0 && errno == EINTR); | |
446 | |
447 if (status) { | |
448 if (errno == EAGAIN) { | |
449 sleep(1); | |
450 goto retry; | |
451 } else { | |
452 error("fcntl lock error", "", "fcntl"); | |
453 } | |
454 } | |
455 } | |
456 | |
457 void audioDB::release_lock(int fd) { | |
458 struct flock lock; | |
459 int status; | |
460 | |
461 lock.l_type = F_UNLCK; | |
462 lock.l_whence = SEEK_SET; | |
463 lock.l_start = 0; | |
464 lock.l_len = 0; | |
465 | |
466 status = fcntl(fd, F_SETLKW, &lock); | |
467 | |
468 if (status) | |
469 error("fcntl unlock error", "", "fcntl"); | |
470 } | |
471 | |
430 void audioDB::create(const char* dbName){ | 472 void audioDB::create(const char* dbName){ |
431 if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) | 473 if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) |
432 error("Can't open database file", dbName); | 474 error("Can't create database file", dbName, "open"); |
475 get_lock(dbfid, 1); | |
433 | 476 |
434 // go to the location corresponding to the last byte | 477 // go to the location corresponding to the last byte |
435 if (lseek (dbfid, O2_DEFAULTDBSIZE - 1, SEEK_SET) == -1) | 478 if (lseek (dbfid, O2_DEFAULTDBSIZE - 1, SEEK_SET) == -1) |
436 error("lseek error in db file"); | 479 error("lseek error in db file", "", "lseek"); |
437 | 480 |
438 // write a dummy byte at the last location | 481 // write a dummy byte at the last location |
439 if (write (dbfid, "", 1) != 1) | 482 if (write (dbfid, "", 1) != 1) |
440 error("write error"); | 483 error("write error", "", "write"); |
441 | 484 |
442 // mmap the output file | 485 // mmap the output file |
443 if(verbosity) | 486 if(verbosity) |
444 cerr << "header size:" << O2_HEADERSIZE << endl; | 487 cerr << "header size:" << O2_HEADERSIZE << endl; |
445 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, | 488 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, |
446 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) | 489 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) |
447 error("mmap error for creating database"); | 490 error("mmap error for creating database", "", "mmap"); |
448 | 491 |
449 dbH = new dbTableHeaderT(); | 492 dbH = new dbTableHeaderT(); |
450 assert(dbH); | 493 assert(dbH); |
451 | 494 |
452 // Initialize header | 495 // Initialize header |
470 | 513 |
471 // initTables - memory map files passed as arguments | 514 // initTables - memory map files passed as arguments |
472 // Precondition: database has already been created | 515 // Precondition: database has already been created |
473 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile=0){ | 516 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile=0){ |
474 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0) | 517 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0) |
475 error("Can't open database file", dbName); | 518 error("Can't open database file", dbName, "open"); |
476 | 519 get_lock(dbfid, forWrite); |
520 | |
477 // open the input file | 521 // open the input file |
478 if (inFile && (infid = open (inFile, O_RDONLY)) < 0) | 522 if (inFile && (infid = open (inFile, O_RDONLY)) < 0) |
479 error("can't open input file for reading", inFile); | 523 error("can't open input file for reading", inFile, "open"); |
480 | 524 |
481 // find size of input file | 525 // find size of input file |
482 if (inFile && fstat (infid,&statbuf) < 0) | 526 if (inFile && fstat (infid,&statbuf) < 0) |
483 error("fstat error finding size of input"); | 527 error("fstat error finding size of input", "", "fstat"); |
484 | 528 |
485 // Get the database header info | 529 // Get the database header info |
486 dbH = new dbTableHeaderT(); | 530 dbH = new dbTableHeaderT(); |
487 assert(dbH); | 531 assert(dbH); |
488 | 532 |
513 } | 557 } |
514 | 558 |
515 // mmap the input file | 559 // mmap the input file |
516 if (inFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) | 560 if (inFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) |
517 == (caddr_t) -1) | 561 == (caddr_t) -1) |
518 error("mmap error for input"); | 562 error("mmap error for input", "", "mmap"); |
519 | 563 |
520 // mmap the database file | 564 // mmap the database file |
521 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | (forWrite ? PROT_WRITE : 0), | 565 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | (forWrite ? PROT_WRITE : 0), |
522 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) | 566 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) |
523 error("mmap error for creating database"); | 567 error("mmap error for initting tables of database", "", "mmap"); |
524 | 568 |
525 // Make some handy tables with correct types | 569 // Make some handy tables with correct types |
526 fileTable= (char*)(db+fileTableOffset); | 570 fileTable= (char*)(db+fileTableOffset); |
527 trackTable = (unsigned*)(db+trackTableOffset); | 571 trackTable = (unsigned*)(db+trackTableOffset); |
528 dataBuf = (double*)(db+dataoffset); | 572 dataBuf = (double*)(db+dataoffset); |
664 } | 708 } |
665 | 709 |
666 void audioDB::batchinsert(const char* dbName, const char* inFile){ | 710 void audioDB::batchinsert(const char* dbName, const char* inFile){ |
667 | 711 |
668 if ((dbfid = open (dbName, O_RDWR)) < 0) | 712 if ((dbfid = open (dbName, O_RDWR)) < 0) |
669 error("Can't open database file", dbName); | 713 error("Can't open database file", dbName, "open"); |
714 get_lock(dbfid, 1); | |
670 | 715 |
671 if(!key) | 716 if(!key) |
672 key=inFile; | 717 key=inFile; |
673 ifstream *filesIn = 0; | 718 ifstream *filesIn = 0; |
674 ifstream *keysIn = 0; | 719 ifstream *keysIn = 0; |
719 if(filesIn->eof()) | 764 if(filesIn->eof()) |
720 break; | 765 break; |
721 | 766 |
722 // open the input file | 767 // open the input file |
723 if (thisFile && (infid = open (thisFile, O_RDONLY)) < 0) | 768 if (thisFile && (infid = open (thisFile, O_RDONLY)) < 0) |
724 error("can't open feature file for reading", thisFile); | 769 error("can't open feature file for reading", thisFile, "open"); |
725 | 770 |
726 // find size of input file | 771 // find size of input file |
727 if (thisFile && fstat (infid,&statbuf) < 0) | 772 if (thisFile && fstat (infid,&statbuf) < 0) |
728 error("fstat error finding size of input"); | 773 error("fstat error finding size of input", "", "fstat"); |
729 | 774 |
730 // mmap the database file | 775 // mmap the database file |
731 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, | 776 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, |
732 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) | 777 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) |
733 error("mmap error for creating database"); | 778 error("mmap error for batchinsert into database", "", "mmap"); |
734 | 779 |
735 // Make some handy tables with correct types | 780 // Make some handy tables with correct types |
736 fileTable= (char*)(db+fileTableOffset); | 781 fileTable= (char*)(db+fileTableOffset); |
737 trackTable = (unsigned*)(db+trackTableOffset); | 782 trackTable = (unsigned*)(db+trackTableOffset); |
738 dataBuf = (double*)(db+dataoffset); | 783 dataBuf = (double*)(db+dataoffset); |
756 } | 801 } |
757 | 802 |
758 // mmap the input file | 803 // mmap the input file |
759 if (thisFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) | 804 if (thisFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) |
760 == (caddr_t) -1) | 805 == (caddr_t) -1) |
761 error("mmap error for input"); | 806 error("mmap error for input", "", "mmap"); |
762 | 807 |
763 | 808 |
764 // Linear scan of filenames check for pre-existing feature | 809 // Linear scan of filenames check for pre-existing feature |
765 unsigned alreadyInserted=0; | 810 unsigned alreadyInserted=0; |
766 | 811 |
831 }while(!filesIn->eof()); | 876 }while(!filesIn->eof()); |
832 | 877 |
833 // mmap the database file | 878 // mmap the database file |
834 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, | 879 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, |
835 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) | 880 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) |
836 error("mmap error for creating database"); | 881 error("mmap error for creating database", "", "mmap"); |
837 | 882 |
838 if(verbosity) | 883 if(verbosity) |
839 cerr << COM_BATCHINSERT << " " << dbName << " " << totalVectors << " vectors " | 884 cerr << COM_BATCHINSERT << " " << dbName << " " << totalVectors << " vectors " |
840 << totalVectors*dbH->dim*sizeof(double) << " bytes." << endl; | 885 << totalVectors*dbH->dim*sizeof(double) << " bytes." << endl; |
841 | 886 |