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