comparison audioDB.cpp @ 176:8193dbd66e34 no-big-mmap

map only the database header page as db, not the entire database. Change some naughty accesses through db + offset into the right thing Move some memcpy()s (updating the database header) further down, so as to be a little bit more transactional
author mas01cr
date Wed, 14 Nov 2007 17:58:57 +0000
parents 38bdbab60972
children c32bf13c3978
comparison
equal deleted inserted replaced
175:38bdbab60972 176:8193dbd66e34
108 void audioDB::cleanup() { 108 void audioDB::cleanup() {
109 cmdline_parser_free(&args_info); 109 cmdline_parser_free(&args_info);
110 if(indata) 110 if(indata)
111 munmap(indata,statbuf.st_size); 111 munmap(indata,statbuf.st_size);
112 if(db) 112 if(db)
113 munmap(db,dbH->dbSize); 113 munmap(db,getpagesize());
114 if(fileTable) 114 if(fileTable)
115 munmap(fileTable, dbH->trackTableOffset - dbH->fileTableOffset); 115 munmap(fileTable, dbH->trackTableOffset - dbH->fileTableOffset);
116 if(trackTable) 116 if(trackTable)
117 munmap(trackTable, dbH->dataOffset - dbH->trackTableOffset); 117 munmap(trackTable, dbH->dataOffset - dbH->trackTableOffset);
118 if(dataBuf) 118 if(dataBuf)
464 // FIXME: when changing file format version, remove this workaround. 464 // FIXME: when changing file format version, remove this workaround.
465 if(dbH->dbSize == 0) { 465 if(dbH->dbSize == 0) {
466 dbH->dbSize = O2_DEFAULTDBSIZE; 466 dbH->dbSize = O2_DEFAULTDBSIZE;
467 } 467 }
468 468
469 // mmap the database file
470 if ((db = (char*) mmap(0, dbH->dbSize, PROT_READ | (forWrite ? PROT_WRITE : 0),
471 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
472 error("mmap error for initting tables of database", "", "mmap");
473
474 // Make some handy tables with correct types 469 // Make some handy tables with correct types
475 #define CHECKED_MMAP(type, var, start, end) \ 470 #define CHECKED_MMAP(type, var, start, end) \
476 { void *tmp = mmap(0, ((end) - (start)), (PROT_READ | (forWrite ? PROT_WRITE : 0)), MAP_SHARED, dbfid, (start)); \ 471 { void *tmp = mmap(0, ((end) - (start)), (PROT_READ | (forWrite ? PROT_WRITE : 0)), MAP_SHARED, dbfid, (start)); \
477 if(tmp == (void *) -1) { \ 472 if(tmp == (void *) -1) { \
478 error("mmap error for db table", #var, "mmap"); \ 473 error("mmap error for db table", #var, "mmap"); \
479 } \ 474 } \
480 var = (type) tmp; \ 475 var = (type) tmp; \
481 } 476 }
477
478 CHECKED_MMAP(char *, db, 0, getpagesize());
482 479
483 CHECKED_MMAP(char *, fileTable, dbH->fileTableOffset, dbH->trackTableOffset); 480 CHECKED_MMAP(char *, fileTable, dbH->fileTableOffset, dbH->trackTableOffset);
484 CHECKED_MMAP(unsigned *, trackTable, dbH->trackTableOffset, dbH->dataOffset); 481 CHECKED_MMAP(unsigned *, trackTable, dbH->trackTableOffset, dbH->dataOffset);
485 CHECKED_MMAP(double *, dataBuf, dbH->dataOffset, dbH->timesTableOffset); 482 CHECKED_MMAP(double *, dataBuf, dbH->dataOffset, dbH->timesTableOffset);
486 CHECKED_MMAP(double *, timesTable, dbH->timesTableOffset, dbH->l2normTableOffset); 483 CHECKED_MMAP(double *, timesTable, dbH->timesTableOffset, dbH->l2normTableOffset);
586 dbH->numFiles++; 583 dbH->numFiles++;
587 584
588 // Update Header information 585 // Update Header information
589 dbH->length+=(statbuf.st_size-sizeof(int)); 586 dbH->length+=(statbuf.st_size-sizeof(int));
590 587
591 // Copy the header back to the database
592 memcpy (db, dbH, sizeof(dbTableHeaderT));
593
594 // Update track to file index map 588 // Update track to file index map
595 //memcpy (db+trackTableOffset+(dbH->numFiles-1)*sizeof(unsigned), &numVectors, sizeof(unsigned));
596 memcpy (trackTable+dbH->numFiles-1, &numVectors, sizeof(unsigned)); 589 memcpy (trackTable+dbH->numFiles-1, &numVectors, sizeof(unsigned));
597 590
598 // Update the feature database 591 // Update the feature database
599 memcpy (db+dbH->dataOffset+insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int)); 592 memcpy (((char *) dataBuf) + insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int));
600 593
601 // Norm the vectors on input if the database is already L2 normed 594 // Norm the vectors on input if the database is already L2 normed
602 if(dbH->flags & O2_FLAG_L2NORM) 595 if(dbH->flags & O2_FLAG_L2NORM)
603 unitNormAndInsertL2((double*)(db+dbH->dataOffset+insertoffset), dbH->dim, numVectors, 1); // append 596 unitNormAndInsertL2((double*)(((char *) dataBuf) + insertoffset), dbH->dim, numVectors, 1); // append
604 597
605 // Report status 598 // Report status
606 status(dbName); 599 status(dbName);
607 if(verbosity) { 600 if(verbosity) {
608 cerr << COM_INSERT << " " << dbName << " " << numVectors << " vectors " 601 cerr << COM_INSERT << " " << dbName << " " << numVectors << " vectors "
609 << (statbuf.st_size-sizeof(int)) << " bytes." << endl; 602 << (statbuf.st_size-sizeof(int)) << " bytes." << endl;
610 } 603 }
604
605 // Copy the header back to the database
606 memcpy (db, dbH, sizeof(dbTableHeaderT));
611 607
612 // CLEAN UP 608 // CLEAN UP
613 munmap(indata,statbuf.st_size); 609 munmap(indata,statbuf.st_size);
614 close(infid); 610 close(infid);
615 } 611 }
755 // Increment file count 751 // Increment file count
756 dbH->numFiles++; 752 dbH->numFiles++;
757 753
758 // Update Header information 754 // Update Header information
759 dbH->length+=(statbuf.st_size-sizeof(int)); 755 dbH->length+=(statbuf.st_size-sizeof(int));
760 // Copy the header back to the database
761 memcpy (db, dbH, sizeof(dbTableHeaderT));
762 756
763 // Update track to file index map 757 // Update track to file index map
764 //memcpy (db+trackTableOffset+(dbH->numFiles-1)*sizeof(unsigned), &numVectors, sizeof(unsigned));
765 memcpy (trackTable+dbH->numFiles-1, &numVectors, sizeof(unsigned)); 758 memcpy (trackTable+dbH->numFiles-1, &numVectors, sizeof(unsigned));
766 759
767 // Update the feature database 760 // Update the feature database
768 memcpy (db+dbH->dataOffset+insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int)); 761 memcpy (((char *) dataBuf) + insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int));
769 762
770 // Norm the vectors on input if the database is already L2 normed 763 // Norm the vectors on input if the database is already L2 normed
771 if(dbH->flags & O2_FLAG_L2NORM) 764 if(dbH->flags & O2_FLAG_L2NORM)
772 unitNormAndInsertL2((double*)(db+dbH->dataOffset+insertoffset), dbH->dim, numVectors, 1); // append 765 unitNormAndInsertL2((double*)(((char *) dataBuf) + insertoffset), dbH->dim, numVectors, 1); // append
773 766
774 totalVectors+=numVectors; 767 totalVectors+=numVectors;
768
769 // Copy the header back to the database
770 memcpy (db, dbH, sizeof(dbTableHeaderT));
775 } 771 }
776 } 772 }
777 // CLEAN UP 773 // CLEAN UP
778 munmap(indata,statbuf.st_size); 774 munmap(indata,statbuf.st_size);
779 close(infid); 775 close(infid);