Mercurial > hg > audiodb
comparison audioDB.cpp @ 169:cdd441dcc9a8
Better error detection (and messages) on invalid feature files
Factor out commonality of dealing with the input file (in
audioDB::initTables and audioDB::batchinsert) into initInputFile(); call
it appropriately.
author | mas01cr |
---|---|
date | Thu, 08 Nov 2007 14:18:47 +0000 |
parents | ea6936b82915 |
children | 42585bd03636 f9d16137e704 |
comparison
equal
deleted
inserted
replaced
168:ecfa25f72b7e | 169:cdd441dcc9a8 |
---|---|
470 dataBuf = (double *) (db + dbH->dataOffset); | 470 dataBuf = (double *) (db + dbH->dataOffset); |
471 l2normTable = (double *) (db + dbH->l2normTableOffset); | 471 l2normTable = (double *) (db + dbH->l2normTableOffset); |
472 timesTable = (double *) (db + dbH->timesTableOffset); | 472 timesTable = (double *) (db + dbH->timesTableOffset); |
473 } | 473 } |
474 | 474 |
475 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile = 0) { | 475 void audioDB::initInputFile (const char *inFile) { |
476 | 476 if (inFile) { |
477 initDBHeader(dbName, forWrite); | 477 if ((infid = open(inFile, O_RDONLY)) < 0) { |
478 | 478 error("can't open input file for reading", inFile, "open"); |
479 // open the input file | 479 } |
480 if (inFile && (infid = open(inFile, O_RDONLY)) < 0) { | 480 |
481 error("can't open input file for reading", inFile, "open"); | 481 if (fstat(infid, &statbuf) < 0) { |
482 } | 482 error("fstat error finding size of input", inFile, "fstat"); |
483 // find size of input file | 483 } |
484 if (inFile && fstat(infid, &statbuf) < 0) { | 484 |
485 error("fstat error finding size of input", inFile, "fstat"); | 485 if(dbH->dim == 0 && dbH->length == 0) { // empty database |
486 } | |
487 if(inFile) | |
488 if(dbH->dim == 0 && dbH->length == 0) // empty database | |
489 // initialize with input dimensionality | 486 // initialize with input dimensionality |
490 read(infid, &dbH->dim, sizeof(unsigned)); | 487 if(read(infid, &dbH->dim, sizeof(unsigned)) != sizeof(unsigned)) { |
491 else { | 488 error("short read of input file", inFile); |
489 } | |
490 if(dbH->dim == 0) { | |
491 error("dimensionality of zero in input file", inFile); | |
492 } | |
493 } else { | |
492 unsigned test; | 494 unsigned test; |
493 read(infid, &test, sizeof(unsigned)); | 495 if(read(infid, &test, sizeof(unsigned)) != sizeof(unsigned)) { |
496 error("short read of input file", inFile); | |
497 } | |
498 if(dbH->dim == 0) { | |
499 error("dimensionality of zero in input file", inFile); | |
500 } | |
494 if(dbH->dim != test) { | 501 if(dbH->dim != test) { |
495 cerr << "error: expected dimension: " << dbH->dim << ", got : " << test <<endl; | 502 cerr << "error: expected dimension: " << dbH->dim << ", got : " << test <<endl; |
496 error("feature dimensions do not match database table dimensions"); | 503 error("feature dimensions do not match database table dimensions", inFile); |
497 } | 504 } |
498 } | 505 } |
499 | 506 |
500 // mmap the input file | 507 if ((indata = (char *) mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) == (caddr_t) -1) { |
501 if (inFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) | 508 error("mmap error for input", inFile, "mmap"); |
502 == (caddr_t) -1) | 509 } |
503 error("mmap error for input", inFile, "mmap"); | 510 } |
511 } | |
512 | |
513 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile = 0) { | |
514 | |
515 initDBHeader(dbName, forWrite); | |
516 initInputFile(inFile); | |
504 } | 517 } |
505 | 518 |
506 void audioDB::insert(const char* dbName, const char* inFile){ | 519 void audioDB::insert(const char* dbName, const char* inFile){ |
507 | 520 |
508 initTables(dbName, 1, inFile); | 521 initTables(dbName, 1, inFile); |
510 if(!usingTimes && (dbH->flags & O2_FLAG_TIMES)) | 523 if(!usingTimes && (dbH->flags & O2_FLAG_TIMES)) |
511 error("Must use timestamps with timestamped database","use --times"); | 524 error("Must use timestamps with timestamped database","use --times"); |
512 | 525 |
513 // Check that there is room for at least 1 more file | 526 // Check that there is room for at least 1 more file |
514 if((char*)timesTable<((char*)dataBuf+dbH->length+statbuf.st_size-sizeof(int))) | 527 if((char*)timesTable<((char*)dataBuf+dbH->length+statbuf.st_size-sizeof(int))) |
515 error("No more room in database","insert failed: reason database is full."); | 528 error("Insert failed: no more room in database", inFile); |
516 | 529 |
517 if(!key) | 530 if(!key) |
518 key=inFile; | 531 key=inFile; |
519 // Linear scan of filenames check for pre-existing feature | 532 // Linear scan of filenames check for pre-existing feature |
520 unsigned alreadyInserted=0; | 533 unsigned alreadyInserted=0; |
673 timesFile->getline(thisTimesFileName,MAXSTR); | 686 timesFile->getline(thisTimesFileName,MAXSTR); |
674 | 687 |
675 if(filesIn->eof()) | 688 if(filesIn->eof()) |
676 break; | 689 break; |
677 | 690 |
678 // open the input file | 691 initInputFile(thisFile); |
679 if (thisFile && (infid = open (thisFile, O_RDONLY)) < 0) | |
680 error("can't open feature file for reading", thisFile, "open"); | |
681 | |
682 // find size of input file | |
683 if (thisFile && fstat (infid,&statbuf) < 0) | |
684 error("fstat error finding size of input", "", "fstat"); | |
685 | 692 |
686 // Check that there is room for at least 1 more file | 693 // Check that there is room for at least 1 more file |
687 if((char*)timesTable<((char*)dataBuf+(dbH->length+statbuf.st_size-sizeof(int)))) | 694 if((char*)timesTable<((char*)dataBuf+(dbH->length+statbuf.st_size-sizeof(int)))) |
688 error("No more room in database","insert failed: reason database is full."); | 695 error("batchinsert failed: no more room in database", thisFile); |
689 | 696 |
690 if(thisFile) | |
691 if(dbH->dim==0 && dbH->length==0) // empty database | |
692 read(infid,&dbH->dim,sizeof(unsigned)); // initialize with input dimensionality | |
693 else { | |
694 unsigned test; | |
695 read(infid,&test,sizeof(unsigned)); | |
696 if(dbH->dim!=test){ | |
697 cerr << "error: expected dimension: " << dbH->dim << ", got :" << test <<endl; | |
698 error("feature dimensions do not match database table dimensions"); | |
699 } | |
700 } | |
701 | |
702 // mmap the input file | |
703 if (thisFile && (indata = (char*)mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, infid, 0)) | |
704 == (caddr_t) -1) | |
705 error("mmap error for input", "", "mmap"); | |
706 | |
707 | |
708 // Linear scan of filenames check for pre-existing feature | 697 // Linear scan of filenames check for pre-existing feature |
709 unsigned alreadyInserted=0; | 698 unsigned alreadyInserted=0; |
710 | 699 |
711 for(unsigned k=0; k<dbH->numFiles; k++) | 700 for(unsigned k=0; k<dbH->numFiles; k++) |
712 if(strncmp(fileTable + k*O2_FILETABLESIZE, thisKey, strlen(thisKey))==0){ | 701 if(strncmp(fileTable + k*O2_FILETABLESIZE, thisKey, strlen(thisKey))==0){ |