comparison audioDB-internals.h @ 509:cc2b97d020b1

Code rearrangements to tease apart library code from C++ audioDB code. There should be precisely no functional changes in this commit. Instead, the only thing that has happened is that all the abstraction violation and other horribleness is concentrated in one place: the include of "audioDB-internals.h" in audioDB.h -- the separation will be complete once that include can be removed. This include is necessary because the command-line binary / SOAP server still does some things directly rather than through an API: not least of which the operations that have not yet been integrated into the API yet, but also some messing around with constants, flags and nominally internal functions. The intent is to remove as many of these as possible and think quite hard about the rest. In the meantime, the library is now much more self-contained: the only things it uses are in the audioDB_API.h and audioDB-internals.h headers; thus there are fewer nasty surprises lurking for readers of the code. The Makefile has been adjusted to take advantage of this rearrangement in the dependencies.
author mas01cr
date Thu, 15 Jan 2009 13:57:33 +0000
parents 342822c2d49a
children cbd5841e6b70 57e459f62788
comparison
equal deleted inserted replaced
508:23c47e118bc6 509:cc2b97d020b1
1 #include <set>
2 #include <queue>
3 #include <map>
4 #include <string>
5
6 #include "pointpair.h"
1 #include "accumulator.h" 7 #include "accumulator.h"
8 #include "lshlib.h"
2 9
3 /* this struct is for writing polymorphic routines as puns. When 10 /* this struct is for writing polymorphic routines as puns. When
4 * inserting, we might have a "datum" (with actual numerical data) or 11 * inserting, we might have a "datum" (with actual numerical data) or
5 * a "reference" (with strings denoting pathnames containing numerical 12 * a "reference" (with strings denoting pathnames containing numerical
6 * data), but most of the operations are the same. This struct, used 13 * data), but most of the operations are the same. This struct, used
46 std::set<std::string> *allowed_keys; 53 std::set<std::string> *allowed_keys;
47 std::priority_queue<PointPair> *exact_evaluation_queue; 54 std::priority_queue<PointPair> *exact_evaluation_queue;
48 LSH *lsh; 55 LSH *lsh;
49 } adb_qstate_internal_t; 56 } adb_qstate_internal_t;
50 57
58 /* this struct is the in-memory representation of the binary
59 * information stored at the head of each adb file */
60 typedef struct adbheader {
61 uint32_t magic;
62 uint32_t version;
63 uint32_t numFiles;
64 uint32_t dim;
65 uint32_t flags;
66 uint32_t headerSize;
67 off_t length;
68 off_t fileTableOffset;
69 off_t trackTableOffset;
70 off_t dataOffset;
71 off_t l2normTableOffset;
72 off_t timesTableOffset;
73 off_t powerTableOffset;
74 off_t dbSize;
75 } adb_header_t;
76
77 #define ADB_HEADER_SIZE (sizeof(struct adbheader))
78
79 #define ADB_HEADER_FLAG_L2NORM (0x1U)
80 #define ADB_HEADER_FLAG_POWER (0x4U)
81 #define ADB_HEADER_FLAG_TIMES (0x20U)
82 #define ADB_HEADER_FLAG_REFERENCES (0x40U)
83
51 /* the transparent version of the opaque (forward-declared) adb_t. */ 84 /* the transparent version of the opaque (forward-declared) adb_t. */
52 struct adb { 85 struct adb {
53 char *path; 86 char *path;
54 int fd; 87 int fd;
55 int flags; 88 int flags;
109 { if(table) { \ 142 { if(table) { \
110 munmap(table, length); \ 143 munmap(table, length); \
111 } \ 144 } \
112 } 145 }
113 146
147 #define maybe_delete_array(pointer) \
148 { if(pointer) { \
149 delete [] pointer; \
150 pointer = NULL; \
151 } \
152 }
153
114 #define write_or_goto_error(fd, buffer, size) \ 154 #define write_or_goto_error(fd, buffer, size) \
115 { ssize_t tmp = size; \ 155 { ssize_t tmp = size; \
116 if(write(fd, buffer, size) != tmp) { \ 156 if(write(fd, buffer, size) != tmp) { \
117 goto error; \ 157 goto error; \
118 } \ 158 } \
132 goto error; 172 goto error;
133 } 173 }
134 if(lseek(adb->fd, (off_t) 0, SEEK_SET) == (off_t) -1) { 174 if(lseek(adb->fd, (off_t) 0, SEEK_SET) == (off_t) -1) {
135 goto error; 175 goto error;
136 } 176 }
137 if(write(adb->fd, adb->header, O2_HEADERSIZE) != O2_HEADERSIZE) { 177 if(write(adb->fd, adb->header, ADB_HEADER_SIZE) != ADB_HEADER_SIZE) {
138 goto error; 178 goto error;
139 } 179 }
140 180
141 /* can be fsync() if fdatasync() is racily exciting and new */ 181 /* can be fsync() if fdatasync() is racily exciting and new */
142 fdatasync(adb->fd); 182 fdatasync(adb->fd);
240 280
241 static inline uint32_t audiodb_index_from_trackinfo(uint32_t track_id, uint32_t track_pos, uint32_t n_point_bits) { 281 static inline uint32_t audiodb_index_from_trackinfo(uint32_t track_id, uint32_t track_pos, uint32_t n_point_bits) {
242 return ((track_id << n_point_bits) | track_pos); 282 return ((track_id << n_point_bits) | track_pos);
243 } 283 }
244 284
285 #define ADB_FIXME_DEFAULT_LSH_N_POINT_BITS 14
286 #ifndef ADB_FIXME_LSH_N_POINT_BITS
287 #define ADB_FIXME_LSH_N_POINT_BITS ADB_FIXME_DEFAULT_LSH_N_POINT_BITS
288 #endif
289
245 static inline uint32_t audiodb_lsh_n_point_bits(adb_t *adb) { 290 static inline uint32_t audiodb_lsh_n_point_bits(adb_t *adb) {
246 uint32_t nbits = adb->header->flags >> 28; 291 uint32_t nbits = adb->header->flags >> 28;
247 return (nbits ? nbits : O2_DEFAULT_LSH_N_POINT_BITS); 292 return (nbits ? nbits : ADB_FIXME_LSH_N_POINT_BITS);
248 } 293 }
249 294
250 int audiodb_read_data(adb_t *, int, int, double **, size_t *); 295 int audiodb_read_data(adb_t *, int, int, double **, size_t *);
251 int audiodb_insert_create_datum(adb_insert_t *, adb_datum_t *); 296 int audiodb_insert_create_datum(adb_insert_t *, adb_datum_t *);
252 int audiodb_track_id_datum(adb_t *, uint32_t, adb_datum_t *); 297 int audiodb_track_id_datum(adb_t *, uint32_t, adb_datum_t *);
256 int audiodb_query_queue_loop(adb_t *, const adb_query_spec_t *, adb_qstate_internal_t *, double *, adb_qpointers_internal_t *); 301 int audiodb_query_queue_loop(adb_t *, const adb_query_spec_t *, adb_qstate_internal_t *, double *, adb_qpointers_internal_t *);
257 int audiodb_query_loop(adb_t *, const adb_query_spec_t *, adb_qstate_internal_t *); 302 int audiodb_query_loop(adb_t *, const adb_query_spec_t *, adb_qstate_internal_t *);
258 char *audiodb_index_get_name(const char *, double, uint32_t); 303 char *audiodb_index_get_name(const char *, double, uint32_t);
259 bool audiodb_index_exists(const char *, double, uint32_t); 304 bool audiodb_index_exists(const char *, double, uint32_t);
260 int audiodb_index_query_loop(adb_t *, const adb_query_spec_t *, adb_qstate_internal_t *); 305 int audiodb_index_query_loop(adb_t *, const adb_query_spec_t *, adb_qstate_internal_t *);
306 LSH *audiodb_index_allocate(adb_t *, char *, bool);
307 vector<vector<float> > *audiodb_index_initialize_shingles(uint32_t, uint32_t, uint32_t);
308 void audiodb_index_delete_shingles(vector<vector<float> > *);
309 void audiodb_index_make_shingle(vector<vector<float> > *, uint32_t, double *, uint32_t, uint32_t);
310 int audiodb_index_norm_shingles(vector<vector<float> > *, double *, double *, uint32_t, uint32_t, double, bool, bool, float);
311
312 #define ADB_MAXSTR (512U)
313 #define ADB_FILETABLE_ENTRY_SIZE (256U)
314 #define ADB_TRACKTABLE_ENTRY_SIZE (sizeof(uint32_t))
315 #define ADB_DISTANCE_TOLERANCE (1e-6)
316
317 #define ADB_DEFAULT_DATASIZE (1355U) /* in MB */
318 #define ADB_DEFAULT_NTRACKS (20000U)
319 #define ADB_DEFAULT_DATADIM (9U)
320
321 #define ADB_FIXME_LARGE_ADB_SIZE (ADB_DEFAULT_DATASIZE+1)
322 #define ADB_FIXME_LARGE_ADB_NTRACKS (ADB_DEFAULT_NTRACKS+1)
323
324 #define ADB_OLD_MAGIC ('O'|'2'<<8|'D'<<16|'B'<<24)
325 #define ADB_MAGIC ('o'|'2'<<8|'d'<<16|'b'<<24)
326 #define ADB_FORMAT_VERSION (4U)
327
328 #define ADB_LSH_MAXTRACKLEN (1 << ADB_FIXME_LSH_N_POINT_BITS)
329
330 #define align_up(x,w) (((x) + ((1<<w)-1)) & ~((1<<w)-1))
331 #define align_down(x,w) ((x) & ~((1<<w)-1))
332
333 #define align_page_up(x) (((x) + (getpagesize()-1)) & ~(getpagesize()-1))
334 #define align_page_down(x) ((x) & ~(getpagesize()-1))
335