mas01cr@239
|
1 #include "audioDB.h"
|
mas01cr@385
|
2 extern "C" {
|
mas01cr@385
|
3 #include "audioDB_API.h"
|
mas01cr@385
|
4 }
|
mas01cr@239
|
5 /* Make a new database.
|
mas01cr@239
|
6
|
mas01mc@324
|
7 IF size(featuredata) < O2_LARGE_ADB_SIZE
|
mas01cr@239
|
8 The database consists of:
|
mas01cr@239
|
9
|
mas01cr@239
|
10 * a header (see dbTableHeader struct definition);
|
mas01cr@239
|
11 * keyTable: list of keys of tracks;
|
mas01cr@239
|
12 * trackTable: Maps implicit feature index to a feature vector
|
mas01cr@239
|
13 matrix (sizes of tracks)
|
mas01cr@239
|
14 * featureTable: Lots of doubles;
|
mas01cr@239
|
15 * timesTable: (start,end) time points for each feature vector;
|
mas01cr@239
|
16 * powerTable: associated power for each feature vector;
|
mas01cr@239
|
17 * l2normTable: squared l2norms for each feature vector.
|
mas01mc@324
|
18
|
mas01mc@324
|
19 ELSE the database consists of:
|
mas01mc@324
|
20
|
mas01mc@324
|
21 * a header (see dbTableHeader struct definition);
|
mas01mc@324
|
22 * keyTable: list of keys of tracks
|
mas01mc@324
|
23 * trackTable: sizes of tracks
|
mas01mc@324
|
24 * featureTable: list of feature file names
|
mas01mc@324
|
25 * timesTable: list of times file names
|
mas01mc@324
|
26 * powerTable: list of power file names
|
mas01mc@324
|
27
|
mas01cr@239
|
28 */
|
mas01cr@239
|
29
|
mas01cr@385
|
30 int acquire_lock(int, bool);
|
mas01cr@239
|
31
|
mas01cr@385
|
32 extern "C" {
|
mas01cr@385
|
33 adb_t *audiodb_create(const char *path, unsigned datasize, unsigned ntracks, unsigned datadim) {
|
mas01cr@385
|
34 int fd;
|
mas01cr@385
|
35 if(datasize == 0) {
|
mas01cr@385
|
36 datasize = O2_DEFAULT_DATASIZE;
|
mas01cr@385
|
37 }
|
mas01cr@385
|
38 if(ntracks == 0) {
|
mas01cr@385
|
39 ntracks = O2_DEFAULT_NTRACKS;
|
mas01cr@385
|
40 }
|
mas01cr@385
|
41 if(datadim == 0) {
|
mas01cr@385
|
42 datadim = O2_DEFAULT_DATADIM;
|
mas01cr@385
|
43 }
|
mas01cr@239
|
44
|
mas01cr@385
|
45 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
|
46 return NULL;
|
mas01cr@385
|
47 }
|
mas01cr@385
|
48 if (acquire_lock(fd, true)) {
|
mas01cr@385
|
49 return NULL;
|
mas01cr@385
|
50 }
|
mas01cr@385
|
51 dbTableHeaderT *dbH = new dbTableHeaderT();
|
mas01cr@385
|
52
|
mas01cr@385
|
53 // Initialize header
|
mas01cr@385
|
54 dbH->magic = O2_MAGIC;
|
mas01cr@385
|
55 dbH->version = O2_FORMAT_VERSION;
|
mas01cr@385
|
56 dbH->numFiles = 0;
|
mas01cr@385
|
57 dbH->dim = 0;
|
mas01cr@385
|
58 dbH->flags = 0;
|
mas01cr@385
|
59 dbH->headerSize = O2_HEADERSIZE;
|
mas01cr@385
|
60 dbH->length = 0;
|
mas01cr@385
|
61 dbH->fileTableOffset = ALIGN_PAGE_UP(O2_HEADERSIZE);
|
mas01cr@385
|
62 dbH->trackTableOffset = ALIGN_PAGE_UP(dbH->fileTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
|
mas01cr@385
|
63 dbH->dataOffset = ALIGN_PAGE_UP(dbH->trackTableOffset + O2_TRACKTABLE_ENTRY_SIZE*ntracks);
|
mas01cr@385
|
64
|
mas01cr@385
|
65 off_t databytes = ((off_t) datasize) * 1024 * 1024;
|
mas01cr@385
|
66 off_t auxbytes = databytes / datadim;
|
mas01cr@385
|
67
|
mas01cr@385
|
68 // For backward-compatibility, Record the point-encoding parameter for LSH indexing in the adb header
|
mas01cr@385
|
69 // If this value is 0 then it will be set to 14
|
mas01cr@385
|
70
|
mas01mc@324
|
71 #if O2_LSH_N_POINT_BITS > 15
|
mas01mc@324
|
72 #error "AudioDB Compile ERROR: consistency check of O2_LSH_POINT_BITS failed (>15)"
|
mas01mc@324
|
73 #endif
|
mas01cr@385
|
74
|
mas01cr@385
|
75 dbH->flags |= LSH_N_POINT_BITS << 28;
|
mas01cr@385
|
76
|
mas01cr@385
|
77 // If database will fit in a single file the vectors are copied into the AudioDB instance
|
mas01cr@385
|
78 // Else all the vectors are left on the FileSystem and we use the dataOffset as storage
|
mas01cr@385
|
79 // for the location of the features, powers and times files (assuming that arbitrary keys are used for the fileTable)
|
mas01cr@385
|
80 if(ntracks<O2_LARGE_ADB_NTRACKS && datasize<O2_LARGE_ADB_SIZE){
|
mas01cr@385
|
81 dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + databytes);
|
mas01cr@385
|
82 dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + 2*auxbytes);
|
mas01cr@385
|
83 dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + auxbytes);
|
mas01cr@385
|
84 dbH->dbSize = ALIGN_PAGE_UP(dbH->l2normTableOffset + auxbytes);
|
mas01cr@385
|
85 } else { // Create LARGE_ADB, features and powers kept on filesystem
|
mas01cr@385
|
86 dbH->flags |= O2_FLAG_LARGE_ADB;
|
mas01cr@385
|
87 dbH->timesTableOffset = ALIGN_PAGE_UP(dbH->dataOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
|
mas01cr@385
|
88 dbH->powerTableOffset = ALIGN_PAGE_UP(dbH->timesTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
|
mas01cr@385
|
89 dbH->l2normTableOffset = ALIGN_PAGE_UP(dbH->powerTableOffset + O2_FILETABLE_ENTRY_SIZE*ntracks);
|
mas01cr@385
|
90 dbH->dbSize = dbH->l2normTableOffset;
|
mas01cr@385
|
91 }
|
mas01cr@385
|
92
|
mas01cr@385
|
93 write(fd, dbH, O2_HEADERSIZE);
|
mas01cr@385
|
94
|
mas01cr@385
|
95 // go to the location corresponding to the last byte
|
mas01cr@385
|
96 if (lseek (fd, dbH->dbSize - 1, SEEK_SET) == -1)
|
mas01cr@385
|
97 return NULL;
|
mas01cr@385
|
98
|
mas01cr@385
|
99 // write a dummy byte at the last location
|
mas01cr@385
|
100 if (write (fd, "", 1) != 1)
|
mas01cr@385
|
101 return NULL;
|
mas01cr@385
|
102
|
mas01cr@385
|
103 return audiodb_open(path);
|
mas01mc@324
|
104 }
|
mas01cr@239
|
105 }
|