-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_dtable.cpp
More file actions
517 lines (472 loc) · 11 KB
/
Copy pathfixed_dtable.cpp
File metadata and controls
517 lines (472 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* This file is part of the Casa Mia Datastore Project at UBC.It is distributed under the terms of
* version 2 of the GNU GPL. See the file LICENSE for details. */
#define _ATFILE_SOURCE
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include "openat.h"
#include <vector>
#include "util.h"
#include "rofile.h"
#include "rwfile.h"
#include "blob_buffer.h"
#include "fixed_dtable.h"
/* fixed dtable file format:
* bytes 0-3: magic number
* bytes 4-7: format version
* bytes 8-11: key count
* bytes 12-15: value size
* byte 16: key type (0 -> invalid, 1 -> uint32, 2 -> double, 3 -> string, 4 -> blob)
* byte 17: key size (for uint32/string/blob; 1-4 bytes)
* bytes 18-21: if key type is blob, blob comparator name length
* bytes 22-n: if key type is blob and length > 0, blob comparator name
* bytes 18-m, 22-m, or n+1-m: if key type is string/blob, a string table
* byte 18 or m+1: main data tables
*
* main data table:
* [] = byte 0-m: key
* [] = byte m+1: value exists (bool)
* [] = byte m+2-n: data bytes */
fixed_dtable::iter::iter(const fixed_dtable * source)
: iter_source<fixed_dtable>(source), index(0)
{
}
bool fixed_dtable::iter::valid() const
{
return index < dt_source->key_count;
}
bool fixed_dtable::iter::next()
{
if(index == dt_source->key_count)
return false;
return ++index < dt_source->key_count;
}
bool fixed_dtable::iter::prev()
{
if(!index)
return false;
index--;
return true;
}
bool fixed_dtable::iter::first()
{
if(!dt_source->key_count)
return false;
index = 0;
return true;
}
bool fixed_dtable::iter::last()
{
if(!dt_source->key_count)
return false;
index = dt_source->key_count - 1;
return true;
}
dtype fixed_dtable::iter::key() const
{
return dt_source->get_key(index);
}
bool fixed_dtable::iter::seek(const dtype & key)
{
return dt_source->find_key(key, NULL, NULL, &index) >= 0;
}
bool fixed_dtable::iter::seek(const dtype_test & test)
{
return dt_source->find_key(test, &index) >= 0;
}
bool fixed_dtable::iter::seek_index(size_t index)
{
/* we allow seeking to one past the end, just
* as we allow getting there with next() */
if(index < 0 || index > dt_source->key_count)
return false;
this->index = index;
return index < dt_source->key_count;
}
size_t fixed_dtable::iter::get_index() const
{
return index;
}
metablob fixed_dtable::iter::meta() const
{
bool data_exists;
dt_source->get_key(index, &data_exists);
return data_exists ? metablob(dt_source->value_size) : metablob();
}
blob fixed_dtable::iter::value() const
{
return dt_source->get_value(index);
}
const dtable * fixed_dtable::iter::source() const
{
return dt_source;
}
dtable::iter * fixed_dtable::iterator(ATX_DEF) const
{
return new iter(this);
}
bool fixed_dtable::present(const dtype & key, bool * found, ATX_DEF) const
{
bool data_exists;
if(find_key(key, &data_exists) < 0)
{
*found = false;
return false;
}
*found = true;
return data_exists;
}
dtype fixed_dtable::get_key(size_t index, bool * data_exists, off_t * data_offset) const
{
assert(index < key_count);
uint8_t read_size = key_size + 1;
uint8_t bytes[read_size];
int r;
r = fp->read(key_start_off + record_size * index, bytes, read_size);
assert(r == read_size);
if(data_exists)
*data_exists = bytes[key_size];
if(data_offset)
*data_offset = index * record_size + read_size;
switch(ktype)
{
case dtype::UINT32:
return dtype(util::read_bytes(bytes, 0, key_size));
case dtype::DOUBLE:
{
double value;
util::memcpy(&value, bytes, sizeof(double));
return dtype(value);
}
case dtype::STRING:
return dtype(st.get(util::read_bytes(bytes, 0, key_size)));
case dtype::BLOB:
return dtype(st.get_blob(util::read_bytes(bytes, 0, key_size)));
}
abort();
}
template<class T>
int fixed_dtable::find_key(const T & test, size_t * index, bool * data_exists, off_t * data_offset) const
{
/* binary search */
ssize_t min = 0, max = key_count - 1;
assert(ktype != dtype::BLOB || !cmp_name == !blob_cmp);
while(min <= max)
{
/* watch out for overflow! */
ssize_t mid = min + (max - min) / 2;
dtype value = get_key(mid, data_exists, data_offset);
int c = test(value);
if(c < 0)
min = mid + 1;
else if(c > 0)
max = mid - 1;
else
{
if(index)
*index = mid;
return 0;
}
}
if(index)
*index = min;
return -ENOENT;
}
blob fixed_dtable::get_value(size_t index, off_t data_offset) const
{
size_t length;
if(!value_size)
return blob::empty;
blob_buffer value(value_size);
value.set_size(value_size, false);
assert(value_size == value.size());
length = fp->read(key_start_off + data_offset, &value[0], value_size);
assert(length == value_size);
return value;
}
blob fixed_dtable::get_value(size_t index) const
{
assert(index < key_count);
bool data_exists;
off_t data_offset;
dtype key = get_key(index, &data_exists, &data_offset);
return data_exists ? get_value(index, data_offset) : blob();
}
blob fixed_dtable::lookup(const dtype & key, bool * found, ATX_DEF) const
{
bool data_exists;
size_t index;
off_t data_offset;
int r = find_key(key, &data_exists, &data_offset, &index);
if(r < 0)
{
*found = false;
return blob();
}
*found = true;
if(!data_exists)
return blob();
return get_value(index, data_offset);
}
blob fixed_dtable::index(size_t index) const
{
if(index < 0 || index >= key_count)
return blob();
return get_value(index);
}
bool fixed_dtable::contains_index(size_t index) const
{
bool data_exists;
if(index < 0 || index >= key_count)
return false;
get_key(index, &data_exists);
return data_exists;
}
int fixed_dtable::init(int dfd, const char * file, const params & config, sys_journal * sysj)
{
int r = -1;
dtable_header header;
if(fp)
deinit();
fp = rofile::open_mmap<64, 24>(dfd, file);
if(!fp)
return -1;
if(fp->read_type(0, &header) < 0)
goto fail;
if(header.magic != FDTABLE_MAGIC || header.version != FDTABLE_VERSION)
goto fail;
key_count = header.key_count;
key_start_off = sizeof(header);
value_size = header.value_size;
key_size = header.key_size;
record_size = key_size + 1 + value_size;
switch(header.key_type)
{
case 1:
ktype = dtype::UINT32;
if(key_size > 4)
goto fail;
break;
case 2:
ktype = dtype::DOUBLE;
if(key_size != sizeof(double))
goto fail;
break;
case 4:
uint32_t length;
if(fp->read_type(key_start_off, &length) < 0)
goto fail;
key_start_off += sizeof(length);
if(length)
{
char string[length];
if(fp->read(key_start_off, string, length) != (ssize_t) length)
goto fail;
key_start_off += length;
cmp_name = istr(string, length);
}
/* fall through */
case 3:
ktype = (header.key_type == 3) ? dtype::STRING : dtype::BLOB;
if(key_size > 4)
goto fail;
r = st.init(fp, key_start_off);
if(r < 0)
goto fail;
key_start_off += st.get_size();
break;
default:
goto fail;
}
return 0;
fail:
delete fp;
fp = NULL;
return (r < 0) ? r : -1;
}
void fixed_dtable::deinit()
{
if(fp)
{
if(ktype == dtype::STRING)
st.deinit();
delete fp;
fp = NULL;
dtable::deinit();
}
}
int fixed_dtable::create(int dfd, const char * file, const params & config, dtable::iter * source, const ktable * shadow)
{
std::vector<istr> strings;
std::vector<blob> blobs;
dtype::ctype key_type = source->key_type();
const blob_comparator * blob_cmp = source->get_blob_cmp();
bool value_size_known = false;
size_t key_count = 0;
uint32_t max_key = 0;
dtable_header header;
rwfile out;
int r;
if(!source_shadow_ok(source, shadow))
return -EINVAL;
if(!config.get("value_size", &r, 0) || r < 0)
return -EINVAL;
if(r)
{
header.value_size = r;
value_size_known = true;
}
else
header.value_size = 0;
/* just to be sure */
source->first();
while(source->valid())
{
dtype key = source->key();
metablob meta = source->meta();
source->next();
if(!meta.exists())
/* omit non-existent entries no longer needed */
if(!shadow || !shadow->contains(key))
continue;
assert(key.type == key_type);
switch(key.type)
{
case dtype::UINT32:
if(key.u32 > max_key)
max_key = key.u32;
break;
case dtype::DOUBLE:
/* nothing to do */
break;
case dtype::STRING:
strings.push_back(key.str);
break;
case dtype::BLOB:
blobs.push_back(key.blb);
break;
}
key_count++;
if(meta.exists() && !value_size_known)
{
header.value_size = meta.size();
value_size_known = true;
}
}
/* if we still don't know the value size, we'll just stick with 0 */
/* now write the file */
header.magic = FDTABLE_MAGIC;
header.version = FDTABLE_VERSION;
header.key_count = key_count;
switch(key_type)
{
case dtype::UINT32:
header.key_type = 1;
header.key_size = util::byte_size(max_key);
break;
case dtype::DOUBLE:
header.key_type = 2;
header.key_size = sizeof(double);
break;
case dtype::STRING:
header.key_type = 3;
header.key_size = util::byte_size(strings.size() - 1);
break;
case dtype::BLOB:
header.key_type = 4;
header.key_size = util::byte_size(blobs.size() - 1);
break;
}
r = out.create(dfd, file);
if(r < 0)
return r;
r = out.append(&header);
if(r < 0)
goto fail_unlink;
if(key_type == dtype::BLOB)
{
uint32_t length = blob_cmp ? strlen(blob_cmp->name) : 0;
out.append(&length);
if(length)
out.append(blob_cmp->name);
}
if(key_type == dtype::STRING)
{
r = stringtbl::create(&out, strings);
if(r < 0)
goto fail_unlink;
}
else if(key_type == dtype::BLOB)
{
r = stringtbl::create(&out, blobs);
if(r < 0)
goto fail_unlink;
}
/* now the key array */
max_key = 0;
source->first();
while(source->valid())
{
int i = 0;
uint8_t bytes[header.key_size + 1];
dtype key = source->key();
blob value = source->value();
if(!value.exists())
/* omit non-existent entries no longer needed */
if(!shadow || !shadow->contains(key))
{
source->next();
continue;
}
switch(key.type)
{
case dtype::UINT32:
util::layout_bytes(bytes, &i, key.u32, header.key_size);
break;
case dtype::DOUBLE:
util::memcpy(bytes, &key.dbl, sizeof(double));
i += sizeof(double);
break;
case dtype::STRING:
/* no need to locate the string; it's the next one */
util::layout_bytes(bytes, &i, max_key, header.key_size);
max_key++;
break;
case dtype::BLOB:
/* no need to locate the blob; it's the next one */
util::layout_bytes(bytes, &i, max_key, header.key_size);
max_key++;
break;
}
bytes[i++] = value.exists();
r = out.append(bytes, i);
if(r != i)
goto fail_unlink;
/* and the data itself */
if(value.exists() && value.size() != header.value_size)
{
/* all the items in this dtable must be the same size */
if(!source->reject(&value))
goto fail_unlink;
if(value.exists() && value.size() != header.value_size)
goto fail_unlink;
}
if(value.exists())
r = out.append(value);
else
r = out.pad(header.value_size);
if(r < 0)
goto fail_unlink;
source->next();
}
r = out.close();
if(r < 0)
goto fail_unlink;
return 0;
fail_unlink:
out.close();
unlinkat(dfd, file, 0);
return (r < 0) ? r : -1;
}
DEFINE_RO_FACTORY(fixed_dtable);