mas01cr@457
|
1 #include "accumulator.h"
|
mas01cr@457
|
2
|
mas01cr@457
|
3 /* this struct is for writing polymorphic routines as puns. When
|
mas01cr@457
|
4 * inserting, we might have a "datum" (with actual numerical data) or
|
mas01cr@457
|
5 * a "reference" (with strings denoting pathnames containing numerical
|
mas01cr@457
|
6 * data), but most of the operations are the same. This struct, used
|
mas01cr@457
|
7 * only internally, allows us to write the main body of the insert
|
mas01cr@457
|
8 * code only once.
|
mas01cr@457
|
9 */
|
mas01cr@408
|
10 typedef struct adb_datum_internal {
|
mas01cr@408
|
11 uint32_t nvectors;
|
mas01cr@408
|
12 uint32_t dim;
|
mas01cr@408
|
13 const char *key;
|
mas01cr@408
|
14 void *data;
|
mas01cr@408
|
15 void *times;
|
mas01cr@408
|
16 void *power;
|
mas01cr@408
|
17 } adb_datum_internal_t;
|
mas01cr@408
|
18
|
mas01cr@457
|
19 /* this struct is for maintaining per-query state. We don't want to
|
mas01cr@457
|
20 * store this stuff in the adb struct itself, because (a) it doesn't
|
mas01cr@457
|
21 * belong there and (b) in principle people might do two queries in
|
mas01cr@457
|
22 * parallel using the same adb handle. (b) is in practice a little
|
mas01cr@457
|
23 * bit academic because at the moment we're seeking all over the disk
|
mas01cr@457
|
24 * using adb->fd, but changing to use pread() might win us
|
mas01cr@457
|
25 * threadsafety eventually.
|
mas01cr@457
|
26 */
|
mas01cr@458
|
27 // FIXME moved to audioDB.h for now
|
mas01cr@457
|
28
|
mas01cr@402
|
29 struct adb {
|
mas01cr@402
|
30 char *path;
|
mas01cr@402
|
31 int fd;
|
mas01cr@402
|
32 int flags;
|
mas01cr@402
|
33 adb_header_t *header;
|
mas01cr@453
|
34 std::vector<std::string> *keys;
|
mas01cr@453
|
35 std::map<std::string,uint32_t> *keymap;
|
mas01cr@432
|
36 std::vector<uint32_t> *track_lengths;
|
mas01cr@442
|
37 std::vector<off_t> *track_offsets;
|
mas01cr@402
|
38 };
|
mas01cr@402
|
39
|
mas01cr@416
|
40 typedef struct {
|
mas01cr@416
|
41 bool operator() (const adb_result_t &r1, const adb_result_t &r2) {
|
mas01cr@416
|
42 return strcmp(r1.key, r2.key) < 0;
|
mas01cr@416
|
43 }
|
mas01cr@416
|
44 } adb_result_key_lt;
|
mas01cr@416
|
45
|
mas01cr@416
|
46 typedef struct {
|
mas01cr@416
|
47 bool operator() (const adb_result_t &r1, const adb_result_t &r2) {
|
mas01cr@416
|
48 return r1.qpos < r2.qpos;
|
mas01cr@416
|
49 }
|
mas01cr@416
|
50 } adb_result_qpos_lt;
|
mas01cr@416
|
51
|
mas01cr@416
|
52 typedef struct {
|
mas01cr@416
|
53 bool operator() (const adb_result_t &r1, const adb_result_t &r2) {
|
mas01cr@416
|
54 return r1.dist < r2.dist;
|
mas01cr@416
|
55 }
|
mas01cr@416
|
56 } adb_result_dist_lt;
|
mas01cr@416
|
57
|
mas01cr@416
|
58 typedef struct {
|
mas01cr@416
|
59 bool operator() (const adb_result_t &r1, const adb_result_t &r2) {
|
mas01cr@416
|
60 return r1.dist > r2.dist;
|
mas01cr@416
|
61 }
|
mas01cr@416
|
62 } adb_result_dist_gt;
|
mas01cr@416
|
63
|
mas01cr@416
|
64 typedef struct {
|
mas01cr@416
|
65 bool operator() (const adb_result_t &r1, const adb_result_t &r2) {
|
mas01cr@416
|
66 return ((r1.ipos < r2.ipos) ||
|
mas01cr@416
|
67 ((r1.ipos == r2.ipos) &&
|
mas01cr@416
|
68 ((r1.qpos < r2.qpos) ||
|
mas01cr@416
|
69 ((r1.qpos == r2.qpos) && (strcmp(r1.key, r2.key) < 0)))));
|
mas01cr@416
|
70 }
|
mas01cr@416
|
71 } adb_result_triple_lt;
|
mas01cr@416
|
72
|
mas01cr@401
|
73 /* We could go gcc-specific here and use typeof() instead of passing
|
mas01cr@401
|
74 * in an explicit type. Answers on a postcard as to whether that's a
|
mas01cr@401
|
75 * good plan or not. */
|
mas01cr@401
|
76 #define mmap_or_goto_error(type, var, start, length) \
|
mas01cr@401
|
77 { void *tmp = mmap(0, length, PROT_READ, MAP_SHARED, adb->fd, (start)); \
|
mas01cr@401
|
78 if(tmp == (void *) -1) { \
|
mas01cr@401
|
79 goto error; \
|
mas01cr@401
|
80 } \
|
mas01cr@401
|
81 var = (type) tmp; \
|
mas01cr@401
|
82 }
|
mas01cr@401
|
83
|
mas01cr@401
|
84 #define maybe_munmap(table, length) \
|
mas01cr@401
|
85 { if(table) { \
|
mas01cr@401
|
86 munmap(table, length); \
|
mas01cr@401
|
87 } \
|
mas01cr@401
|
88 }
|
mas01cr@401
|
89
|
mas01cr@410
|
90 #define write_or_goto_error(fd, buffer, size) \
|
mas01cr@410
|
91 { ssize_t tmp = size; \
|
mas01cr@410
|
92 if(write(fd, buffer, size) != tmp) { \
|
mas01cr@410
|
93 goto error; \
|
mas01cr@410
|
94 } \
|
mas01cr@410
|
95 }
|
mas01cr@410
|
96
|
mas01cr@410
|
97 #define read_or_goto_error(fd, buffer, size) \
|
mas01cr@410
|
98 { ssize_t tmp = size; \
|
mas01cr@410
|
99 if(read(fd, buffer, size) != tmp) { \
|
mas01cr@410
|
100 goto error; \
|
mas01cr@410
|
101 } \
|
mas01cr@410
|
102 }
|
mas01cr@410
|
103
|
mas01cr@401
|
104 static inline int audiodb_sync_header(adb_t *adb) {
|
mas01cr@401
|
105 off_t pos;
|
mas01cr@401
|
106 pos = lseek(adb->fd, (off_t) 0, SEEK_CUR);
|
mas01cr@401
|
107 if(pos == (off_t) -1) {
|
mas01cr@401
|
108 goto error;
|
mas01cr@401
|
109 }
|
mas01cr@401
|
110 if(lseek(adb->fd, (off_t) 0, SEEK_SET) == (off_t) -1) {
|
mas01cr@401
|
111 goto error;
|
mas01cr@401
|
112 }
|
mas01cr@401
|
113 if(write(adb->fd, adb->header, O2_HEADERSIZE) != O2_HEADERSIZE) {
|
mas01cr@401
|
114 goto error;
|
mas01cr@401
|
115 }
|
mas01cr@401
|
116
|
mas01cr@401
|
117 /* can be fsync() if fdatasync() is racily exciting and new */
|
mas01cr@401
|
118 fdatasync(adb->fd);
|
mas01cr@401
|
119 if(lseek(adb->fd, pos, SEEK_SET) == (off_t) -1) {
|
mas01cr@401
|
120 goto error;
|
mas01cr@401
|
121 }
|
mas01cr@401
|
122 return 0;
|
mas01cr@401
|
123
|
mas01cr@401
|
124 error:
|
mas01cr@401
|
125 return 1;
|
mas01cr@401
|
126 }
|
mas01cr@425
|
127
|
mas01cr@425
|
128 static inline double audiodb_dot_product(double *p, double *q, size_t count) {
|
mas01cr@425
|
129 double result = 0;
|
mas01cr@425
|
130 while(count--) {
|
mas01cr@425
|
131 result += *p++ * *q++;
|
mas01cr@425
|
132 }
|
mas01cr@425
|
133 return result;
|
mas01cr@425
|
134 }
|
mas01cr@426
|
135
|
mas01cr@426
|
136 static inline void audiodb_l2norm_buffer(double *d, size_t dim, size_t nvectors, double *l) {
|
mas01cr@426
|
137 while(nvectors--) {
|
mas01cr@426
|
138 double *d1 = d;
|
mas01cr@426
|
139 double *d2 = d;
|
mas01cr@426
|
140 *l++ = audiodb_dot_product(d1, d2, dim);
|
mas01cr@426
|
141 d += dim;
|
mas01cr@426
|
142 }
|
mas01cr@426
|
143 }
|
mas01cr@427
|
144
|
mas01cr@427
|
145 // This is a common pattern in sequence queries: what we are doing is
|
mas01cr@427
|
146 // taking a window of length seqlen over a buffer of length length,
|
mas01cr@427
|
147 // and placing the sum of the elements in that window in the first
|
mas01cr@427
|
148 // element of the window: thus replacing all but the last seqlen
|
mas01cr@427
|
149 // elements in the buffer with the corresponding windowed sum.
|
mas01cr@427
|
150 static inline void audiodb_sequence_sum(double *buffer, int length, int seqlen) {
|
mas01cr@427
|
151 double tmp1, tmp2, *ps;
|
mas01cr@427
|
152 int j, w;
|
mas01cr@427
|
153
|
mas01cr@427
|
154 tmp1 = *buffer;
|
mas01cr@427
|
155 j = 1;
|
mas01cr@427
|
156 w = seqlen - 1;
|
mas01cr@427
|
157 while(w--) {
|
mas01cr@427
|
158 *buffer += buffer[j++];
|
mas01cr@427
|
159 }
|
mas01cr@427
|
160 ps = buffer + 1;
|
mas01cr@427
|
161 w = length - seqlen; // +1 - 1
|
mas01cr@427
|
162 while(w--) {
|
mas01cr@427
|
163 tmp2 = *ps;
|
mas01cr@427
|
164 if(isfinite(tmp1)) {
|
mas01cr@427
|
165 *ps = *(ps - 1) - tmp1 + *(ps + seqlen - 1);
|
mas01cr@427
|
166 } else {
|
mas01cr@427
|
167 for(int i = 1; i < seqlen; i++) {
|
mas01cr@427
|
168 *ps += *(ps + i);
|
mas01cr@427
|
169 }
|
mas01cr@427
|
170 }
|
mas01cr@427
|
171 tmp1 = tmp2;
|
mas01cr@427
|
172 ps++;
|
mas01cr@427
|
173 }
|
mas01cr@427
|
174 }
|
mas01cr@427
|
175
|
mas01cr@427
|
176 // In contrast to audiodb_sequence_sum() above,
|
mas01cr@427
|
177 // audiodb_sequence_sqrt() and audiodb_sequence_average() below are
|
mas01cr@427
|
178 // simple mappers across the sequence.
|
mas01cr@427
|
179 static inline void audiodb_sequence_sqrt(double *buffer, int length, int seqlen) {
|
mas01cr@427
|
180 int w = length - seqlen + 1;
|
mas01cr@427
|
181 while(w--) {
|
mas01cr@427
|
182 *buffer = sqrt(*buffer);
|
mas01cr@427
|
183 buffer++;
|
mas01cr@427
|
184 }
|
mas01cr@427
|
185 }
|
mas01cr@427
|
186
|
mas01cr@427
|
187 static inline void audiodb_sequence_average(double *buffer, int length, int seqlen) {
|
mas01cr@427
|
188 int w = length - seqlen + 1;
|
mas01cr@427
|
189 while(w--) {
|
mas01cr@427
|
190 *buffer /= seqlen;
|
mas01cr@427
|
191 buffer++;
|
mas01cr@427
|
192 }
|
mas01cr@427
|
193 }
|
mas01cr@430
|
194
|
mas01cr@430
|
195 static inline uint32_t audiodb_key_index(adb_t *adb, const char *key) {
|
mas01cr@430
|
196 std::map<std::string,uint32_t>::iterator it;
|
mas01cr@453
|
197 it = adb->keymap->find(key);
|
mas01cr@453
|
198 if(it == adb->keymap->end()) {
|
mas01cr@430
|
199 return (uint32_t) -1;
|
mas01cr@430
|
200 } else {
|
mas01cr@430
|
201 return (*it).second;
|
mas01cr@430
|
202 }
|
mas01cr@430
|
203 }
|
mas01cr@433
|
204
|
mas01cr@458
|
205 static inline uint32_t audiodb_index_to_track_id(uint32_t lshid, uint32_t n_point_bits) {
|
mas01cr@458
|
206 return (lshid >> n_point_bits);
|
mas01cr@458
|
207 }
|
mas01cr@458
|
208
|
mas01cr@458
|
209 static inline uint32_t audiodb_index_to_track_pos(uint32_t lshid, uint32_t n_point_bits) {
|
mas01cr@458
|
210 return (lshid & ((1 << n_point_bits) - 1));
|
mas01cr@458
|
211 }
|
mas01cr@458
|
212
|
mas01cr@458
|
213 static inline uint32_t audiodb_index_from_trackinfo(uint32_t track_id, uint32_t track_pos, uint32_t n_point_bits) {
|
mas01cr@458
|
214 return ((track_id << n_point_bits) | track_pos);
|
mas01cr@458
|
215 }
|
mas01cr@458
|
216
|
mas01cr@458
|
217 static inline uint32_t audiodb_lsh_n_point_bits(adb_t *adb) {
|
mas01cr@458
|
218 uint32_t nbits = adb->header->flags >> 28;
|
mas01cr@458
|
219 return (nbits ? nbits : O2_DEFAULT_LSH_N_POINT_BITS);
|
mas01cr@458
|
220 }
|
mas01cr@458
|
221
|
mas01cr@433
|
222 int audiodb_read_data(adb_t *, int, int, double **, size_t *);
|
mas01cr@443
|
223 int audiodb_insert_create_datum(adb_insert_t *, adb_datum_t *);
|
mas01cr@461
|
224 int audiodb_track_id_datum(adb_t *, uint32_t, adb_datum_t *);
|
mas01cr@443
|
225 int audiodb_free_datum(adb_datum_t *);
|
mas01cr@461
|
226 int audiodb_datum_qpointers(adb_datum_t *, uint32_t, double **, double **, adb_qpointers_internal_t *);
|
mas01cr@444
|
227 int audiodb_query_spec_qpointers(adb_t *, adb_query_spec_t *, double **, double **, adb_qpointers_internal_t *);
|
mas01cr@460
|
228 char *audiodb_index_get_name(const char *, double, uint32_t);
|
mas01cr@460
|
229 bool audiodb_index_exists(const char *, double, uint32_t);
|