mas01cr@239
|
1 #include "audioDB.h"
|
mas01cr@239
|
2
|
mas01cr@239
|
3 #if defined(O2_DEBUG)
|
mas01cr@239
|
4 void sigterm_action(int signal, siginfo_t *info, void *context) {
|
mas01cr@239
|
5 exit(128+signal);
|
mas01cr@239
|
6 }
|
mas01cr@239
|
7
|
mas01cr@239
|
8 void sighup_action(int signal, siginfo_t *info, void *context) {
|
mas01cr@239
|
9 // FIXME: reread any configuration files
|
mas01cr@239
|
10 }
|
mas01cr@239
|
11 #endif
|
mas01cr@239
|
12
|
mas01cr@239
|
13 void audioDB::error(const char* a, const char* b, const char *sysFunc) {
|
mas01ik@355
|
14
|
mas01ik@355
|
15
|
mas01ik@355
|
16 if(isServer) {
|
mas01cr@370
|
17 /* FIXME: I think this is leaky -- we never delete err.
|
mas01cr@370
|
18 actually deleting it is tricky, though; it gets placed into
|
mas01cr@370
|
19 some soap-internal struct with uncertain extent... -- CSR,
|
mas01cr@370
|
20 2007-10-01 */
|
mas01cr@370
|
21 char *err = new char[256]; /* FIXME: overflows */
|
mas01cr@370
|
22 snprintf(err, 255, "%s: %s\n%s", a, b, sysFunc ? strerror(errno) : "");
|
mas01cr@370
|
23 /* FIXME: actually we could usefully do with a properly
|
mas01cr@370
|
24 structured type, so that we can throw separate faultstring
|
mas01cr@370
|
25 and details. -- CSR, 2007-10-01 */
|
mas01ik@355
|
26 throw(err);
|
mas01ik@355
|
27 } else {
|
mas01ik@355
|
28 std::cerr << a << ": " << b << std::endl;
|
mas01ik@355
|
29 if (sysFunc) {
|
mas01ik@355
|
30 perror(sysFunc);
|
mas01ik@355
|
31 }
|
mas01ik@355
|
32 exit(1);
|
mas01cr@239
|
33 }
|
mas01cr@239
|
34 }
|
mas01cr@239
|
35
|
mas01cr@239
|
36 void audioDB::initDBHeader(const char* dbName) {
|
mas01cr@498
|
37 if(!adb) {
|
mas01cr@498
|
38 adb = audiodb_open(dbName, forWrite ? O_RDWR : O_RDONLY);
|
mas01cr@498
|
39 if(!adb) {
|
mas01cr@498
|
40 error("Failed to open database", dbName);
|
mas01cr@498
|
41 }
|
mas01cr@239
|
42 }
|
mas01cr@498
|
43 dbfid = adb->fd;
|
mas01cr@498
|
44 dbH = adb->header;
|
mas01cr@239
|
45
|
mas01cr@239
|
46 // Make some handy tables with correct types
|
mas01cr@239
|
47 if(forWrite || (dbH->length > 0)) {
|
mas01cr@239
|
48 if(forWrite) {
|
mas01cr@239
|
49 fileTableLength = dbH->trackTableOffset - dbH->fileTableOffset;
|
mas01cr@239
|
50 trackTableLength = dbH->dataOffset - dbH->trackTableOffset;
|
mas01cr@239
|
51 timesTableLength = dbH->powerTableOffset - dbH->timesTableOffset;
|
mas01cr@239
|
52 powerTableLength = dbH->l2normTableOffset - dbH->powerTableOffset;
|
mas01cr@239
|
53 l2normTableLength = dbH->dbSize - dbH->l2normTableOffset;
|
mas01cr@239
|
54 } else {
|
mas01cr@256
|
55 fileTableLength = ALIGN_PAGE_UP(dbH->numFiles * O2_FILETABLE_ENTRY_SIZE);
|
mas01cr@256
|
56 trackTableLength = ALIGN_PAGE_UP(dbH->numFiles * O2_TRACKTABLE_ENTRY_SIZE);
|
mas01mc@324
|
57 if( dbH->flags & O2_FLAG_LARGE_ADB ){
|
mas01mc@324
|
58 timesTableLength = ALIGN_PAGE_UP(dbH->numFiles * O2_FILETABLE_ENTRY_SIZE);
|
mas01mc@324
|
59 powerTableLength = ALIGN_PAGE_UP(dbH->numFiles * O2_FILETABLE_ENTRY_SIZE);
|
mas01mc@324
|
60 l2normTableLength = 0;
|
mas01mc@324
|
61 }
|
mas01mc@324
|
62 else{
|
mas01mc@324
|
63 timesTableLength = ALIGN_PAGE_UP(2*(dbH->length / dbH->dim));
|
mas01mc@324
|
64 powerTableLength = ALIGN_PAGE_UP(dbH->length / dbH->dim);
|
mas01mc@324
|
65 l2normTableLength = ALIGN_PAGE_UP(dbH->length / dbH->dim);
|
mas01mc@324
|
66 }
|
mas01cr@239
|
67 }
|
mas01cr@239
|
68 CHECKED_MMAP(char *, fileTable, dbH->fileTableOffset, fileTableLength);
|
mas01cr@239
|
69 CHECKED_MMAP(unsigned *, trackTable, dbH->trackTableOffset, trackTableLength);
|
mas01mc@324
|
70 if( dbH->flags & O2_FLAG_LARGE_ADB ){
|
mas01mc@324
|
71 CHECKED_MMAP(char *, featureFileNameTable, dbH->dataOffset, fileTableLength);
|
mas01mc@324
|
72 if( dbH->flags & O2_FLAG_TIMES )
|
mas01mc@324
|
73 CHECKED_MMAP(char *, timesFileNameTable, dbH->timesTableOffset, fileTableLength);
|
mas01mc@324
|
74 if( dbH->flags & O2_FLAG_POWER )
|
mas01mc@324
|
75 CHECKED_MMAP(char *, powerFileNameTable, dbH->powerTableOffset, fileTableLength);
|
mas01mc@324
|
76 }
|
mas01mc@324
|
77 else{
|
mas01mc@324
|
78 CHECKED_MMAP(double *, timesTable, dbH->timesTableOffset, timesTableLength);
|
mas01mc@324
|
79 CHECKED_MMAP(double *, powerTable, dbH->powerTableOffset, powerTableLength);
|
mas01mc@324
|
80 CHECKED_MMAP(double *, l2normTable, dbH->l2normTableOffset, l2normTableLength);
|
mas01mc@324
|
81 }
|
mas01cr@239
|
82 }
|
mas01cr@239
|
83 }
|
mas01cr@239
|
84
|
mas01cr@498
|
85 void audioDB::initInputFile (const char *inFile) {
|
mas01cr@239
|
86 if (inFile) {
|
mas01cr@239
|
87 if ((infid = open(inFile, O_RDONLY)) < 0) {
|
mas01cr@239
|
88 error("can't open input file for reading", inFile, "open");
|
mas01cr@239
|
89 }
|
mas01cr@239
|
90
|
mas01cr@239
|
91 if (fstat(infid, &statbuf) < 0) {
|
mas01cr@239
|
92 error("fstat error finding size of input", inFile, "fstat");
|
mas01cr@239
|
93 }
|
mas01cr@239
|
94
|
mas01cr@239
|
95 if(dbH->dim == 0 && dbH->length == 0) { // empty database
|
mas01cr@239
|
96 // initialize with input dimensionality
|
mas01cr@239
|
97 if(read(infid, &dbH->dim, sizeof(unsigned)) != sizeof(unsigned)) {
|
mas01cr@239
|
98 error("short read of input file", inFile);
|
mas01cr@239
|
99 }
|
mas01cr@239
|
100 if(dbH->dim == 0) {
|
mas01cr@239
|
101 error("dimensionality of zero in input file", inFile);
|
mas01cr@239
|
102 }
|
mas01cr@239
|
103 } else {
|
mas01cr@239
|
104 unsigned test;
|
mas01cr@239
|
105 if(read(infid, &test, sizeof(unsigned)) != sizeof(unsigned)) {
|
mas01cr@239
|
106 error("short read of input file", inFile);
|
mas01cr@239
|
107 }
|
mas01cr@239
|
108 if(dbH->dim == 0) {
|
mas01cr@239
|
109 error("dimensionality of zero in input file", inFile);
|
mas01cr@239
|
110 }
|
mas01cr@239
|
111 if(dbH->dim != test) {
|
mas01cr@239
|
112 std::cerr << "error: expected dimension: " << dbH->dim << ", got : " << test <<std::endl;
|
mas01cr@239
|
113 error("feature dimensions do not match database table dimensions", inFile);
|
mas01cr@239
|
114 }
|
mas01cr@239
|
115 }
|
mas01cr@239
|
116 }
|
mas01cr@239
|
117 }
|
mas01cr@239
|
118
|
mas01mc@292
|
119 void audioDB::initTables(const char* dbName, const char* inFile) {
|
mas01cr@239
|
120 initDBHeader(dbName);
|
mas01mc@292
|
121 if(inFile)
|
mas01mc@292
|
122 initInputFile(inFile);
|
mas01cr@239
|
123 }
|
mas01mc@292
|
124
|
mas01mc@324
|
125 // If name is relative path, side effect name with prefix/name
|
mas01mc@324
|
126 // Do not free original pointer
|
mas01mc@324
|
127 void audioDB::prefix_name(char** const name, const char* prefix){
|
mas01mc@324
|
128 // No prefix if prefix is empty
|
mas01mc@324
|
129 if(!prefix)
|
mas01mc@324
|
130 return;
|
mas01mc@324
|
131 // Allocate new memory, keep old memory
|
mas01mc@324
|
132 assert(name && *name);
|
mas01mc@324
|
133 if (strlen(*name) + strlen(prefix) + 1 > O2_MAXFILESTR)
|
mas01mc@324
|
134 error("error: path prefix + filename too long",prefix);
|
mas01mc@324
|
135 // Do not prefix absolute path+filename
|
mas01mc@324
|
136 if(**name=='/')
|
mas01mc@324
|
137 return;
|
mas01mc@324
|
138 // OK to prefix relative path+filename
|
mas01mc@324
|
139 char* prefixedName = (char*) malloc(O2_MAXFILESTR);
|
mas01mc@324
|
140 sprintf(prefixedName, "%s/%s", prefix, *name);
|
mas01mc@324
|
141 *name = prefixedName; // side effect new name to old name
|
mas01mc@324
|
142 }
|
mas01cr@498
|
143
|
mas01cr@498
|
144 void audioDB::insertTimeStamps(unsigned numVectors, std::ifstream *timesFile, double *timesdata) {
|
mas01cr@498
|
145 assert(usingTimes);
|
mas01cr@498
|
146
|
mas01cr@498
|
147 unsigned numtimes = 0;
|
mas01cr@498
|
148
|
mas01cr@498
|
149 if(!timesFile->is_open()) {
|
mas01cr@498
|
150 error("problem opening times file on timestamped database", timesFileName);
|
mas01cr@498
|
151 }
|
mas01cr@498
|
152
|
mas01cr@498
|
153 double timepoint, next;
|
mas01cr@498
|
154 *timesFile >> timepoint;
|
mas01cr@498
|
155 if (timesFile->eof()) {
|
mas01cr@498
|
156 error("no entries in times file", timesFileName);
|
mas01cr@498
|
157 }
|
mas01cr@498
|
158 numtimes++;
|
mas01cr@498
|
159 do {
|
mas01cr@498
|
160 *timesFile >> next;
|
mas01cr@498
|
161 if (timesFile->eof()) {
|
mas01cr@498
|
162 break;
|
mas01cr@498
|
163 }
|
mas01cr@498
|
164 numtimes++;
|
mas01cr@498
|
165 timesdata[0] = timepoint;
|
mas01cr@498
|
166 timepoint = (timesdata[1] = next);
|
mas01cr@498
|
167 timesdata += 2;
|
mas01cr@498
|
168 } while (numtimes < numVectors + 1);
|
mas01cr@498
|
169
|
mas01cr@498
|
170 if (numtimes < numVectors + 1) {
|
mas01cr@498
|
171 error("too few timepoints in times file", timesFileName);
|
mas01cr@498
|
172 }
|
mas01cr@498
|
173
|
mas01cr@498
|
174 *timesFile >> next;
|
mas01cr@498
|
175 if (!timesFile->eof()) {
|
mas01cr@498
|
176 error("too many timepoints in times file", timesFileName);
|
mas01cr@498
|
177 }
|
mas01cr@498
|
178 }
|