comparison audioDB.cpp @ 32:bc84daa6c362

Where audioDB::error() is called because of a failed system call, arrange that perror() be called to print the relevant error string.
author mas01cr
date Tue, 21 Aug 2007 17:16:32 +0000
parents 9503faa2981c
children 4d9ea08b2f5f
comparison
equal deleted inserted replaced
31:9503faa2981c 32:bc84daa6c362
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 if (sysFunc) {
103 perror(sysFunc);
104 }
102 exit(1); 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),
444 if (status) { 447 if (status) {
445 if (errno == EAGAIN) { 448 if (errno == EAGAIN) {
446 sleep(1); 449 sleep(1);
447 goto retry; 450 goto retry;
448 } else { 451 } else {
449 error("fcntl lock error"); 452 error("fcntl lock error", "", "fcntl");
450 } 453 }
451 } 454 }
452 } 455 }
453 456
454 void audioDB::release_lock(int fd) { 457 void audioDB::release_lock(int fd) {
461 lock.l_len = 0; 464 lock.l_len = 0;
462 465
463 status = fcntl(fd, F_SETLKW, &lock); 466 status = fcntl(fd, F_SETLKW, &lock);
464 467
465 if (status) 468 if (status)
466 error("fcntl unlock error"); 469 error("fcntl unlock error", "", "fcntl");
467 } 470 }
468 471
469 void audioDB::create(const char* dbName){ 472 void audioDB::create(const char* dbName){
470 if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_EXCL, 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)
471 error("Can't create database file", dbName); 474 error("Can't create database file", dbName, "open");
472 get_lock(dbfid, 1); 475 get_lock(dbfid, 1);
473 476
474 // go to the location corresponding to the last byte 477 // go to the location corresponding to the last byte
475 if (lseek (dbfid, O2_DEFAULTDBSIZE - 1, SEEK_SET) == -1) 478 if (lseek (dbfid, O2_DEFAULTDBSIZE - 1, SEEK_SET) == -1)
476 error("lseek error in db file"); 479 error("lseek error in db file", "", "lseek");
477 480
478 // write a dummy byte at the last location 481 // write a dummy byte at the last location
479 if (write (dbfid, "", 1) != 1) 482 if (write (dbfid, "", 1) != 1)
480 error("write error"); 483 error("write error", "", "write");
481 484
482 // mmap the output file 485 // mmap the output file
483 if(verbosity) 486 if(verbosity)
484 cerr << "header size:" << O2_HEADERSIZE << endl; 487 cerr << "header size:" << O2_HEADERSIZE << endl;
485 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, 488 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE,
486 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) 489 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
487 error("mmap error for creating database"); 490 error("mmap error for creating database", "", "mmap");
488 491
489 dbH = new dbTableHeaderT(); 492 dbH = new dbTableHeaderT();
490 assert(dbH); 493 assert(dbH);
491 494
492 // Initialize header 495 // Initialize header
510 513
511 // initTables - memory map files passed as arguments 514 // initTables - memory map files passed as arguments
512 // Precondition: database has already been created 515 // Precondition: database has already been created
513 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){
514 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0) 517 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0)
515 error("Can't open database file", dbName); 518 error("Can't open database file", dbName, "open");
516 get_lock(dbfid, forWrite); 519 get_lock(dbfid, forWrite);
517 520
518 // open the input file 521 // open the input file
519 if (inFile && (infid = open (inFile, O_RDONLY)) < 0) 522 if (inFile && (infid = open (inFile, O_RDONLY)) < 0)
520 error("can't open input file for reading", inFile); 523 error("can't open input file for reading", inFile, "open");
521 524
522 // find size of input file 525 // find size of input file
523 if (inFile && fstat (infid,&statbuf) < 0) 526 if (inFile && fstat (infid,&statbuf) < 0)
524 error("fstat error finding size of input"); 527 error("fstat error finding size of input", "", "fstat");
525 528
526 // Get the database header info 529 // Get the database header info
527 dbH = new dbTableHeaderT(); 530 dbH = new dbTableHeaderT();
528 assert(dbH); 531 assert(dbH);
529 532
554 } 557 }
555 558
556 // mmap the input file 559 // mmap the input file
557 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))
558 == (caddr_t) -1) 561 == (caddr_t) -1)
559 error("mmap error for input"); 562 error("mmap error for input", "", "mmap");
560 563
561 // mmap the database file 564 // mmap the database file
562 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),
563 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) 566 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
564 error("mmap error for creating database"); 567 error("mmap error for initting tables of database", "", "mmap");
565 568
566 // Make some handy tables with correct types 569 // Make some handy tables with correct types
567 fileTable= (char*)(db+fileTableOffset); 570 fileTable= (char*)(db+fileTableOffset);
568 trackTable = (unsigned*)(db+trackTableOffset); 571 trackTable = (unsigned*)(db+trackTableOffset);
569 dataBuf = (double*)(db+dataoffset); 572 dataBuf = (double*)(db+dataoffset);
705 } 708 }
706 709
707 void audioDB::batchinsert(const char* dbName, const char* inFile){ 710 void audioDB::batchinsert(const char* dbName, const char* inFile){
708 711
709 if ((dbfid = open (dbName, O_RDWR)) < 0) 712 if ((dbfid = open (dbName, O_RDWR)) < 0)
710 error("Can't open database file", dbName); 713 error("Can't open database file", dbName, "open");
711 get_lock(dbfid, 1); 714 get_lock(dbfid, 1);
712 715
713 if(!key) 716 if(!key)
714 key=inFile; 717 key=inFile;
715 ifstream *filesIn = 0; 718 ifstream *filesIn = 0;
761 if(filesIn->eof()) 764 if(filesIn->eof())
762 break; 765 break;
763 766
764 // open the input file 767 // open the input file
765 if (thisFile && (infid = open (thisFile, O_RDONLY)) < 0) 768 if (thisFile && (infid = open (thisFile, O_RDONLY)) < 0)
766 error("can't open feature file for reading", thisFile); 769 error("can't open feature file for reading", thisFile, "open");
767 770
768 // find size of input file 771 // find size of input file
769 if (thisFile && fstat (infid,&statbuf) < 0) 772 if (thisFile && fstat (infid,&statbuf) < 0)
770 error("fstat error finding size of input"); 773 error("fstat error finding size of input", "", "fstat");
771 774
772 // mmap the database file 775 // mmap the database file
773 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, 776 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE,
774 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) 777 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
775 error("mmap error for creating database"); 778 error("mmap error for batchinsert into database", "", "mmap");
776 779
777 // Make some handy tables with correct types 780 // Make some handy tables with correct types
778 fileTable= (char*)(db+fileTableOffset); 781 fileTable= (char*)(db+fileTableOffset);
779 trackTable = (unsigned*)(db+trackTableOffset); 782 trackTable = (unsigned*)(db+trackTableOffset);
780 dataBuf = (double*)(db+dataoffset); 783 dataBuf = (double*)(db+dataoffset);
798 } 801 }
799 802
800 // mmap the input file 803 // mmap the input file
801 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))
802 == (caddr_t) -1) 805 == (caddr_t) -1)
803 error("mmap error for input"); 806 error("mmap error for input", "", "mmap");
804 807
805 808
806 // Linear scan of filenames check for pre-existing feature 809 // Linear scan of filenames check for pre-existing feature
807 unsigned alreadyInserted=0; 810 unsigned alreadyInserted=0;
808 811
873 }while(!filesIn->eof()); 876 }while(!filesIn->eof());
874 877
875 // mmap the database file 878 // mmap the database file
876 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE, 879 if ((db = (char*) mmap(0, O2_DEFAULTDBSIZE, PROT_READ | PROT_WRITE,
877 MAP_SHARED, dbfid, 0)) == (caddr_t) -1) 880 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
878 error("mmap error for creating database"); 881 error("mmap error for creating database", "", "mmap");
879 882
880 if(verbosity) 883 if(verbosity)
881 cerr << COM_BATCHINSERT << " " << dbName << " " << totalVectors << " vectors " 884 cerr << COM_BATCHINSERT << " " << dbName << " " << totalVectors << " vectors "
882 << totalVectors*dbH->dim*sizeof(double) << " bytes." << endl; 885 << totalVectors*dbH->dim*sizeof(double) << " bytes." << endl;
883 886