comparison create.cpp @ 385:4e68f7d4d524 api-inversion

Invert audiodb_create() / audioDB::create(). (This was straightforward, because audioDB::create() doesn't depend on having any kind of state or reading it from disk, except of course for the locks. New global functions acquire_lock() and divest_lock() [silly name, I know], to be used both within audioDB::get_lock() and audioDB::release_lock() and within (inverted) API functions directly. Eventually the audioDB:: versions will be able to go away entirely)
author mas01cr
date Fri, 21 Nov 2008 15:22:15 +0000
parents 2d5c3f8e8c22
children bdd6bf8d1e85
comparison
equal deleted inserted replaced
384:25a4d1799c08 385:4e68f7d4d524
1 #include "audioDB.h" 1 #include "audioDB.h"
2 2 extern "C" {
3 #include "audioDB_API.h"
4 }
3 /* Make a new database. 5 /* Make a new database.
4 6
5 IF size(featuredata) < O2_LARGE_ADB_SIZE 7 IF size(featuredata) < O2_LARGE_ADB_SIZE
6 The database consists of: 8 The database consists of:
7 9
23 * timesTable: list of times file names 25 * timesTable: list of times file names
24 * powerTable: list of power file names 26 * powerTable: list of power file names
25 27
26 */ 28 */
27 29
28 void audioDB::create(const char* dbName){ 30 int acquire_lock(int, bool);
29 if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
30 error("Can't create database file", dbName, "open");
31 get_lock(dbfid, 1);
32 31
33 VERB_LOG(0, "header size: %ju\n", (intmax_t) O2_HEADERSIZE); 32 extern "C" {
34 33 adb_t *audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim) {
35 dbH = new dbTableHeaderT(); 34 int fd;
36 assert(dbH); 35 if(datasize == 0) {
36 datasize = O2_DEFAULT_DATASIZE;
37 }
38 if(ntracks == 0) {
39 ntracks = O2_DEFAULT_NTRACKS;
40 }
41 if(datadim == 0) {
42 datadim = O2_DEFAULT_DATADIM;
43 }
37 44
38 //unsigned int maxfiles = (unsigned int) rint((double) O2_MAXFILES * (double) size / (double) O2_DEFAULTDBSIZE); 45 if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
39 46 return NULL;
40 // Initialize header 47 }
41 dbH->magic = O2_MAGIC; 48 if (acquire_lock(fd, true)) {
42 dbH->version = O2_FORMAT_VERSION; 49 return NULL;
43 dbH->numFiles = 0; 50 }
44 dbH->dim = 0; 51 dbTableHeaderT *dbH = new dbTableHeaderT();
45 dbH->flags = 0; 52
46 dbH->headerSize = O2_HEADERSIZE; 53 // Initialize header
47 dbH->length = 0; 54 dbH->magic = O2_MAGIC;
48 dbH->fileTableOffset = ALIGN_PAGE_UP(O2_HEADERSIZE); 55 dbH->version = O2_FORMAT_VERSION;
49 dbH->trackTableOffset = ALIGN_PAGE_UP(dbH->fileTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks); 56 dbH->numFiles = 0;
50 dbH->dataOffset = ALIGN_PAGE_UP(dbH->trackTableOffset + O2_TRACKTABLE_ENTRY_SIZE*ntracks); 57 dbH->dim = 0;
51 58 dbH->flags = 0;
52 off_t databytes = ((off_t) datasize) * 1024 * 1024; 59 dbH->headerSize = O2_HEADERSIZE;
53 off_t auxbytes = databytes / datadim; 60 dbH->length = 0;
54 61 dbH->fileTableOffset = ALIGN_PAGE_UP(O2_HEADERSIZE);
55 // For backward-compatibility, Record the point-encoding parameter for LSH indexing in the adb header 62 dbH->trackTableOffset = ALIGN_PAGE_UP(dbH->fileTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
56 // If this value is 0 then it will be set to 14 63 dbH->dataOffset = ALIGN_PAGE_UP(dbH->trackTableOffset + O2_TRACKTABLE_ENTRY_SIZE*ntracks);
57 64
65 off_t databytes = ((off_t) datasize) * 1024 * 1024;
66 off_t auxbytes = databytes / datadim;
67
68 // For backward-compatibility, Record the point-encoding parameter for LSH indexing in the adb header
69 // If this value is 0 then it will be set to 14
70
58 #if O2_LSH_N_POINT_BITS > 15 71 #if O2_LSH_N_POINT_BITS > 15
59 #error "AudioDB Compile ERROR: consistency check of O2_LSH_POINT_BITS failed (>15)" 72 #error "AudioDB Compile ERROR: consistency check of O2_LSH_POINT_BITS failed (>15)"
60 #endif 73 #endif
61 74
62 dbH->flags |= LSH_N_POINT_BITS << 28; 75 dbH->flags |= LSH_N_POINT_BITS << 28;
63 76
64 // If database will fit in a single file the vectors are copied into the AudioDB instance 77 // If database will fit in a single file the vectors are copied into the AudioDB instance
65 // Else all the vectors are left on the FileSystem and we use the dataOffset as storage 78 // Else all the vectors are left on the FileSystem and we use the dataOffset as storage
66 // for the location of the features, powers and times files (assuming that arbitrary keys are used for the fileTable) 79 // for the location of the features, powers and times files (assuming that arbitrary keys are used for the fileTable)
67 if(ntracks<O2_LARGE_ADB_NTRACKS && datasize<O2_LARGE_ADB_SIZE){ 80 if(ntracks<O2_LARGE_ADB_NTRACKS && datasize<O2_LARGE_ADB_SIZE){
68 dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + databytes); 81 dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + databytes);
69 dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + 2*auxbytes); 82 dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + 2*auxbytes);
70 dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + auxbytes); 83 dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + auxbytes);
71 dbH->dbSize = ALIGN_PAGE_UP(dbH->l2normTableOffset + auxbytes); 84 dbH->dbSize = ALIGN_PAGE_UP(dbH->l2normTableOffset + auxbytes);
85 } else { // Create LARGE_ADB, features and powers kept on filesystem
86 dbH->flags |= O2_FLAG_LARGE_ADB;
87 dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
88 dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
89 dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
90 dbH->dbSize = dbH->l2normTableOffset;
91 }
92
93 write(fd, dbH, O2_HEADERSIZE);
94
95 // go to the location corresponding to the last byte
96 if (lseek (fd, dbH->dbSize - 1, SEEK_SET) == -1)
97 return NULL;
98
99 // write a dummy byte at the last location
100 if (write (fd, "", 1) != 1)
101 return NULL;
102
103 return audiodb_open(path);
72 } 104 }
73 else{ // Create LARGE_ADB, features and powers kept on filesystem
74 dbH->flags |= O2_FLAG_LARGE_ADB;
75 dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
76 dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
77 dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
78 dbH->dbSize = dbH->l2normTableOffset;
79 }
80
81 CHECKED_WRITE(dbfid, dbH, O2_HEADERSIZE);
82
83 // go to the location corresponding to the last byte
84 if (lseek (dbfid, dbH->dbSize - 1, SEEK_SET) == -1)
85 error("lseek error in db file", "", "lseek");
86
87 // write a dummy byte at the last location
88 if (write (dbfid, "", 1) != 1)
89 error("write error", "", "write");
90
91 VERB_LOG(0, "%s %s\n", COM_CREATE, dbName);
92 } 105 }
93