mas01cr@239: #include "audioDB.h" mas01cr@385: extern "C" { mas01cr@385: #include "audioDB_API.h" mas01cr@385: } mas01cr@239: /* Make a new database. mas01cr@239: mas01mc@324: IF size(featuredata) < O2_LARGE_ADB_SIZE mas01cr@239: The database consists of: mas01cr@239: mas01cr@239: * a header (see dbTableHeader struct definition); mas01cr@239: * keyTable: list of keys of tracks; mas01cr@239: * trackTable: Maps implicit feature index to a feature vector mas01cr@239: matrix (sizes of tracks) mas01cr@239: * featureTable: Lots of doubles; mas01cr@239: * timesTable: (start,end) time points for each feature vector; mas01cr@239: * powerTable: associated power for each feature vector; mas01cr@239: * l2normTable: squared l2norms for each feature vector. mas01mc@324: mas01mc@324: ELSE the database consists of: mas01mc@324: mas01mc@324: * a header (see dbTableHeader struct definition); mas01mc@324: * keyTable: list of keys of tracks mas01mc@324: * trackTable: sizes of tracks mas01mc@324: * featureTable: list of feature file names mas01mc@324: * timesTable: list of times file names mas01mc@324: * powerTable: list of power file names mas01mc@324: mas01cr@239: */ mas01cr@239: mas01cr@385: int acquire_lock(int, bool); mas01cr@239: mas01cr@385: extern "C" { mas01cr@385: adb_t *audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim) { mas01cr@385: int fd; mas01cr@385: if(datasize == 0) { mas01cr@385: datasize = O2_DEFAULT_DATASIZE; mas01cr@385: } mas01cr@385: if(ntracks == 0) { mas01cr@385: ntracks = O2_DEFAULT_NTRACKS; mas01cr@385: } mas01cr@385: if(datadim == 0) { mas01cr@385: datadim = O2_DEFAULT_DATADIM; mas01cr@385: } mas01cr@239: mas01cr@385: if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) { mas01cr@385: return NULL; mas01cr@385: } mas01cr@385: if (acquire_lock(fd, true)) { mas01cr@385: return NULL; mas01cr@385: } mas01cr@385: dbTableHeaderT *dbH = new dbTableHeaderT(); mas01cr@385: mas01cr@385: // Initialize header mas01cr@385: dbH->magic = O2_MAGIC; mas01cr@385: dbH->version = O2_FORMAT_VERSION; mas01cr@385: dbH->numFiles = 0; mas01cr@385: dbH->dim = 0; mas01cr@385: dbH->flags = 0; mas01cr@385: dbH->headerSize = O2_HEADERSIZE; mas01cr@385: dbH->length = 0; mas01cr@385: dbH->fileTableOffset = ALIGN_PAGE_UP(O2_HEADERSIZE); mas01cr@385: dbH->trackTableOffset = ALIGN_PAGE_UP(dbH->fileTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks); mas01cr@385: dbH->dataOffset = ALIGN_PAGE_UP(dbH->trackTableOffset + O2_TRACKTABLE_ENTRY_SIZE*ntracks); mas01cr@385: mas01cr@385: off_t databytes = ((off_t) datasize) * 1024 * 1024; mas01cr@385: off_t auxbytes = databytes / datadim; mas01cr@385: mas01cr@385: // For backward-compatibility, Record the point-encoding parameter for LSH indexing in the adb header mas01cr@385: // If this value is 0 then it will be set to 14 mas01cr@385: mas01mc@324: #if O2_LSH_N_POINT_BITS > 15 mas01mc@324: #error "AudioDB Compile ERROR: consistency check of O2_LSH_POINT_BITS failed (>15)" mas01mc@324: #endif mas01cr@385: mas01cr@385: dbH->flags |= LSH_N_POINT_BITS << 28; mas01cr@385: mas01cr@385: // If database will fit in a single file the vectors are copied into the AudioDB instance mas01cr@385: // Else all the vectors are left on the FileSystem and we use the dataOffset as storage mas01cr@385: // for the location of the features, powers and times files (assuming that arbitrary keys are used for the fileTable) mas01cr@385: if(ntrackstimesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + databytes); mas01cr@385: dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + 2*auxbytes); mas01cr@385: dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + auxbytes); mas01cr@385: dbH->dbSize = ALIGN_PAGE_UP(dbH->l2normTableOffset + auxbytes); mas01cr@385: } else { // Create LARGE_ADB, features and powers kept on filesystem mas01cr@385: dbH->flags |= O2_FLAG_LARGE_ADB; mas01cr@385: dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + O2_FILETABLE_ENTRY_SIZE*ntracks); mas01cr@385: dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks); mas01cr@385: dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks); mas01cr@385: dbH->dbSize = dbH->l2normTableOffset; mas01cr@385: } mas01cr@385: mas01cr@385: write(fd, dbH, O2_HEADERSIZE); mas01cr@385: mas01cr@385: // go to the location corresponding to the last byte mas01cr@385: if (lseek (fd, dbH->dbSize - 1, SEEK_SET) == -1) mas01cr@385: return NULL; mas01cr@385: mas01cr@385: // write a dummy byte at the last location mas01cr@385: if (write (fd, "", 1) != 1) mas01cr@385: return NULL; mas01cr@385: mas01cr@385: return audiodb_open(path); mas01mc@324: } mas01cr@239: }