-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdb.c
669 lines (590 loc) · 22.4 KB
/
mdb.c
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#include "mdb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
typedef uint32_t mdb_size_t;
typedef uint32_t mdb_ptr_t;
enum {
MDB_PTR_SIZE = sizeof(mdb_ptr_t),
MDB_DATALEN_SIZE = sizeof(mdb_size_t)
};
typedef struct {
char *db_name;
FILE *fp_superblock;
FILE *fp_index;
FILE *fp_data;
mdb_options_t options;
uint32_t index_record_size;
} mdb_int_t;
typedef struct {
mdb_ptr_t next_ptr;
mdb_ptr_t value_ptr;
mdb_size_t value_size;
char key[0];
} mdb_index_t;
static mdb_status_t mdb_status(uint8_t code, const char *desc);
static mdb_int_t *mdb_alloc(void);
static void mdb_free(mdb_int_t *db);
static uint32_t mdb_hash(const char *key);
static mdb_status_t mdb_read_bucket(mdb_int_t *db, uint32_t bucket,
mdb_ptr_t *ptr);
static mdb_status_t mdb_read_index(mdb_int_t *db, mdb_ptr_t idxptr,
mdb_index_t *index);
static mdb_status_t mdb_write_bucket(mdb_int_t *db, mdb_ptr_t bucket,
mdb_ptr_t value);
static mdb_status_t mdb_write_index(mdb_int_t *db, mdb_ptr_t idxptr,
const char *keybuf, mdb_ptr_t valptr,
mdb_size_t valsize);
static mdb_status_t mdb_read_data(mdb_int_t *db, mdb_ptr_t valptr,
mdb_size_t valsize, char *valbuf,
mdb_size_t bufsiz);
static mdb_status_t mdb_write_data(mdb_int_t *db, mdb_ptr_t valptr,
const char *valbuf, mdb_size_t valsize);
static mdb_status_t mdb_read_nextptr(mdb_int_t *db, mdb_ptr_t idxptr,
mdb_ptr_t *nextptr);
static mdb_status_t mdb_write_nextptr(mdb_int_t *db, mdb_ptr_t ptr,
mdb_ptr_t nextptr);
static mdb_status_t mdb_stretch_index_file(mdb_int_t *db, mdb_ptr_t *ptr);
static mdb_status_t mdb_index_alloc(mdb_int_t *db, mdb_ptr_t *ptr);
static mdb_status_t mdb_data_alloc(mdb_int_t *db, mdb_size_t valsize,
mdb_ptr_t *ptr);
static mdb_status_t mdb_index_free(mdb_int_t *db, mdb_ptr_t ptr);
static mdb_status_t mdb_data_free(mdb_int_t *db, mdb_ptr_t valptr,
mdb_size_t valsize);
static char pathbuf[4096];
mdb_status_t mdb_open(mdb_t *handle, const char *path) {
mdb_int_t *db = mdb_alloc();
if (db == NULL) {
return mdb_status(MDB_ERR_ALLOC,
"failed allocating memory buffer for database");
}
strcpy(pathbuf, path);
strcat(pathbuf, ".db.super");
db->fp_superblock = fopen(pathbuf, "r");
if (db->fp_superblock == NULL) {
mdb_free(db);
return mdb_status(MDB_ERR_OPEN_FILE, "cannot open superblock file as read");
}
fscanf(db->fp_superblock, "%s", db->db_name);
fscanf(db->fp_superblock, "%hu", &(db->options.key_size_max));
fscanf(db->fp_superblock, "%u", &(db->options.data_size_max));
fscanf(db->fp_superblock, "%u", &(db->options.hash_buckets));
fscanf(db->fp_superblock, "%u", &(db->options.items_max));
db->index_record_size = db->options.key_size_max
+ MDB_PTR_SIZE * 2
+ MDB_DATALEN_SIZE;
if (ferror(db->fp_superblock)) {
mdb_free(db);
return mdb_status(MDB_ERR_READ, "read error when parsing superblock");
}
strcpy(pathbuf, path);
strcat(pathbuf, ".db.index");
db->fp_index = fopen(pathbuf, "rb+");
if (db->fp_index == NULL) {
mdb_free(db);
return mdb_status(MDB_ERR_OPEN_FILE, "cannot open index file as readwrite");
}
strcpy(pathbuf, path);
strcat(pathbuf, ".db.data");
db->fp_data = fopen(pathbuf, "rb+");
if (db->fp_data == NULL) {
mdb_free(db);
return mdb_status(MDB_ERR_OPEN_FILE, "cannot open data file as readwrite");
}
if (fflush(NULL) != 0) {
mdb_free(db);
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
*handle = (mdb_t)db;
return mdb_status(MDB_OK, NULL);
}
mdb_status_t mdb_create(mdb_t *handle, mdb_options_t options) {
mdb_int_t *db = mdb_alloc();
if (db == NULL) {
return mdb_status(MDB_ERR_ALLOC,
"failed allocating memory buffer for database");
}
strcpy(db->options.db_name, options.db_name);
db->options.items_max = options.items_max;
db->options.hash_buckets = options.hash_buckets;
db->options.key_size_max = options.key_size_max;
db->options.data_size_max = options.data_size_max;
db->index_record_size = db->options.key_size_max
+ MDB_PTR_SIZE * 2
+ MDB_DATALEN_SIZE;
strcpy(pathbuf, options.db_name);
strcat(pathbuf, ".db.super");
db->fp_superblock = fopen(pathbuf, "w");
if (db->fp_superblock == NULL) {
mdb_free(db);
return mdb_status(MDB_ERR_OPEN_FILE,
"cannot open superblock file as write");
}
fprintf(db->fp_superblock, "%s\n", db->db_name);
fprintf(db->fp_superblock, "%hu\n", db->options.key_size_max);
fprintf(db->fp_superblock, "%u\n", db->options.data_size_max);
fprintf(db->fp_superblock, "%u\n", db->options.hash_buckets);
fprintf(db->fp_superblock, "%u\n", db->options.items_max);
if (ferror(db->fp_superblock)) {
mdb_free(db);
return mdb_status(MDB_ERR_WRITE, "write error when writing superblock");
}
strcpy(pathbuf, options.db_name);
strcat(pathbuf, ".db.index");
db->fp_index = fopen(pathbuf, "wb+");
if (db->fp_index == NULL) {
mdb_free(db);
return mdb_status(MDB_ERR_OPEN_FILE, "cannot open index file as readwrite");
}
mdb_ptr_t zero_ptr = 0;
if (fwrite(&zero_ptr, MDB_PTR_SIZE, 1, db->fp_index) < 1) {
mdb_free(db);
return mdb_status(MDB_ERR_WRITE, "write error when writing freeptr");
}
for (size_t i = 0; i < options.hash_buckets; i++) {
if (fwrite(&zero_ptr, MDB_PTR_SIZE, 1, db->fp_index) < 1) {
mdb_free(db);
return mdb_status(MDB_ERR_WRITE, "write error when writing hash buckets");
}
}
strcpy(pathbuf, options.db_name);
strcat(pathbuf, ".db.data");
db->fp_data = fopen(pathbuf, "wb+");
if (db->fp_data == NULL) {
mdb_free(db);
return mdb_status(MDB_ERR_OPEN_FILE, "cannot open data file as readwrite");
}
if (fflush(NULL) != 0) {
mdb_free(db);
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
*handle = (mdb_t)db;
return mdb_status(MDB_OK, NULL);
}
mdb_status_t mdb_read(mdb_t handle, const char *key, char *buf, size_t bufsiz) {
mdb_int_t *db = (mdb_int_t*)handle;
mdb_size_t bucket = mdb_hash(key) % db->options.hash_buckets;
mdb_ptr_t ptr;
mdb_status_t bucket_read_status = mdb_read_bucket(db, bucket, &ptr);
STAT_CHECK_RET(bucket_read_status, {;});
mdb_index_t *index =
alloca(sizeof(mdb_index_t) + db->options.key_size_max + 1);
while (ptr != 0) {
mdb_status_t read_status = mdb_read_index(db, ptr, index);
STAT_CHECK_RET(read_status, {;});
if (strcmp(index->key, key) == 0) {
return mdb_read_data(db, index->value_ptr, index->value_size, buf,
bufsiz);
}
ptr = index->next_ptr;
}
return mdb_status(MDB_NO_KEY, "Key not found");
}
mdb_status_t mdb_write(mdb_t handle, const char *key, const char *value) {
mdb_int_t *db = (mdb_int_t*)handle;
mdb_size_t bucket = mdb_hash(key) % db->options.hash_buckets;
mdb_size_t key_size = strlen(key);
if (key_size > db->options.key_size_max) {
return mdb_status(MDB_ERR_KEY_SIZE, "key size too large");
}
mdb_size_t value_size = strlen(value);
if (value_size > db->options.data_size_max) {
return mdb_status(MDB_ERR_VALUE_SIZE, "value size too large");
}
mdb_ptr_t save_ptr = MDB_PTR_SIZE * (bucket + 1);
mdb_ptr_t ptr;
mdb_status_t bucket_read_status = mdb_read_bucket(db, bucket, &ptr);
STAT_CHECK_RET(bucket_read_status, {;});
mdb_index_t *index = alloca(sizeof(mdb_index_t)
+ db->options.key_size_max + 1);
while (ptr != 0) {
mdb_status_t index_read_status = mdb_read_index(db, ptr, index);
STAT_CHECK_RET(index_read_status, {;});
if (strcmp(index->key, key) == 0) {
break;
}
save_ptr = ptr;
ptr = index->next_ptr;
}
if (ptr == 0) {
mdb_ptr_t index_ptr;
mdb_status_t index_alloc_status = mdb_index_alloc(db, &index_ptr);
STAT_CHECK_RET(index_alloc_status, {;});
mdb_ptr_t value_ptr;
mdb_status_t data_alloc_status = mdb_data_alloc(db, value_size, &value_ptr);
STAT_CHECK_RET(data_alloc_status, {
(void)mdb_index_free(db, index_ptr);
});
mdb_status_t data_write_status = mdb_write_data(db, value_ptr, value,
value_size);
STAT_CHECK_RET(data_write_status, {
(void)mdb_data_free(db, value_ptr, value_size);
(void)mdb_index_free(db, index_ptr);
});
mdb_status_t index_write_status = mdb_write_index(db, index_ptr, key,
value_ptr, value_size);
STAT_CHECK_RET(index_write_status, {
(void)mdb_data_free(db, value_ptr, value_size);
(void)mdb_index_free(db, index_ptr);
});
mdb_status_t ptr_update_status = mdb_write_nextptr(db, save_ptr, index_ptr);
STAT_CHECK_RET(ptr_update_status, {
(void)mdb_data_free(db, value_ptr, value_size);
(void)mdb_index_free(db, index_ptr);
});
return mdb_status(MDB_OK, NULL);
} else {
/// @todo errors are only handled roughly here, needs refinement
mdb_status_t data_free_status = mdb_data_free(db, index->value_ptr,
index->value_size);
STAT_CHECK_RET(data_free_status, {;});
mdb_ptr_t value_ptr;
mdb_status_t data_alloc_status = mdb_data_alloc(db, value_size, &value_ptr);
STAT_CHECK_RET(data_alloc_status, {;});
mdb_status_t data_write_status = mdb_write_data(db, value_ptr, value,
value_size);
STAT_CHECK_RET(data_write_status, {;});
mdb_status_t index_write_status = mdb_write_index(db, ptr, key, value_ptr,
value_size);
STAT_CHECK_RET(index_write_status, {;});
return mdb_status(MDB_OK, NULL);
}
}
mdb_status_t mdb_delete(mdb_t handle, const char *key) {
mdb_int_t *db = (mdb_int_t*)handle;
mdb_size_t bucket = mdb_hash(key) % db->options.hash_buckets;
mdb_ptr_t save_ptr = MDB_PTR_SIZE * (bucket + 1);
mdb_ptr_t ptr;
mdb_status_t bucket_read_status = mdb_read_bucket(db, bucket, &ptr);
STAT_CHECK_RET(bucket_read_status, {;});
mdb_index_t *index = alloca(sizeof(mdb_index_t)
+ db->options.key_size_max + 1);
while (ptr != 0) {
mdb_status_t index_read_status = mdb_read_index(db, ptr, index);
STAT_CHECK_RET(index_read_status, {;});
if (strcmp(index->key, key) == 0) {
break;
}
save_ptr = ptr;
ptr = index->next_ptr;
}
if (ptr == 0) {
return mdb_status(MDB_NO_KEY, NULL);
}
mdb_status_t data_free_status = mdb_data_free(db, index->value_ptr,
index->value_size);
STAT_CHECK_RET(data_free_status, {;});
mdb_status_t index_free_status = mdb_index_free(db, ptr);
STAT_CHECK_RET(index_free_status, {;});
mdb_status_t nextptr_update_status = mdb_write_nextptr(db, save_ptr,
index->next_ptr);
STAT_CHECK_RET(nextptr_update_status, {;});
return mdb_status(MDB_OK, NULL);
}
mdb_options_t mdb_get_options(mdb_t handle) {
mdb_int_t *db = (mdb_int_t*)handle;
return db->options;
}
size_t mdb_index_size(mdb_t *handle) {
mdb_int_t *db = (mdb_int_t*)handle;
(void)fseek(db->fp_index, 0, SEEK_END);
return ftell(db->fp_index);
}
size_t mdb_data_size(mdb_t *handle) {
mdb_int_t *db = (mdb_int_t*)handle;
(void)fseek(db->fp_data, 0, SEEK_END);
return ftell(db->fp_data);
}
static mdb_status_t mdb_read_bucket(mdb_int_t *db, uint32_t bucket,
mdb_ptr_t *ptr) {
if (fseek(db->fp_index, (long)(MDB_PTR_SIZE * (bucket + 1)), SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to bucket");
}
if (fread(ptr, MDB_PTR_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_READ, "cannot read bucket");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_read_index(mdb_int_t *db, mdb_ptr_t idxptr,
mdb_index_t *index) {
if (fseek(db->fp_index, (long)idxptr, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to ptr");
}
if (fread(&(index->next_ptr), MDB_PTR_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_READ, "cannot read next ptr");
}
if (fread(index->key, 1, db->options.key_size_max, db->fp_index)
!= db->options.key_size_max) {
return mdb_status(MDB_ERR_READ, "cannot read key");
}
index->key[db->options.key_size_max] = '\0';
if (fread(&(index->value_ptr), MDB_PTR_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_READ, "cannot read value ptr");
}
if (fread(&(index->value_size), MDB_DATALEN_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_READ, "cannot read value length");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_write_bucket(mdb_int_t *db, mdb_ptr_t bucket,
mdb_ptr_t value) {
if (fseek(db->fp_index, (long)(MDB_PTR_SIZE * (bucket + 1)), SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to bucket");
}
if (fwrite(&value, MDB_PTR_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write bucket");
}
if (fflush(db->fp_index) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_write_index(mdb_int_t *db, mdb_ptr_t idxptr,
const char *keybuf, mdb_ptr_t valptr,
mdb_size_t valsize) {
if (fseek(db->fp_index, (long)(idxptr + MDB_PTR_SIZE), SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to ptr");
}
size_t key_len = strlen(keybuf);
if (fwrite(keybuf, 1, key_len, db->fp_index) < key_len) {
return mdb_status(MDB_ERR_WRITE, "cannot write key part of index");
}
size_t value_ptr_pos = idxptr + MDB_PTR_SIZE + db->options.key_size_max;
if (fseek(db->fp_index, (long)value_ptr_pos, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to value part of index");
}
if (fwrite(&valptr, MDB_PTR_SIZE, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write to value part of index");
}
if (fwrite(&valsize, MDB_DATALEN_SIZE, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write to value part of index");
}
if (fflush(db->fp_index) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_read_nextptr(mdb_int_t *db, mdb_ptr_t idxptr,
mdb_ptr_t *nextptr) {
if (fseek(db->fp_index, (long)idxptr, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to ptr");
}
if (fread(nextptr, MDB_PTR_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_READ, "cannot read next ptr");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_read_data(mdb_int_t *db, mdb_ptr_t valptr,
mdb_size_t valsize, char *valbuf,
mdb_size_t bufsiz) {
if (bufsiz < valsize + 1) {
return mdb_status(MDB_ERR_BUFSIZ, "value buffer size too small");
}
if (fseek(db->fp_data, (long)valptr, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to value");
}
if (fread(valbuf, 1, valsize, db->fp_data) != valsize) {
return mdb_status(MDB_ERR_READ, "cannot read data");
}
valbuf[valsize] = '\0';
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_write_nextptr(mdb_int_t *db, mdb_ptr_t ptr,
mdb_ptr_t nextptr) {
if (fseek(db->fp_index, (long)ptr, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to head of index file");
}
if (fwrite(&nextptr, MDB_PTR_SIZE, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write to head of index file");
}
if (fflush(db->fp_index) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_write_data(mdb_int_t *db, mdb_ptr_t valptr,
const char *valbuf, mdb_size_t valsize) {
if (fseek(db->fp_data, (long)valptr, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to value");
}
if (fwrite(valbuf, 1, valsize, db->fp_data) < valsize) {
return mdb_status(MDB_ERR_WRITE, "cannot write data");
}
if (fflush(db->fp_data) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_stretch_index_file(mdb_int_t *db, mdb_ptr_t *ptr) {
if (fseek(db->fp_index, 0, SEEK_END) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to end of index file");
}
*ptr = (mdb_ptr_t)ftell(db->fp_index);
unsigned char zero = '\0';
for (size_t i = 0; i < db->index_record_size; i++) {
if (fwrite(&zero, 1, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot stretch index file");
}
}
if (fflush(db->fp_index) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_index_alloc(mdb_int_t *db, mdb_ptr_t *ptr) {
mdb_ptr_t freeptr;
mdb_status_t freeptr_read_status = mdb_read_nextptr(db, 0, &freeptr);
STAT_CHECK_RET(freeptr_read_status, {;});
if (freeptr != 0) {
mdb_ptr_t new_freeptr;
mdb_status_t nextptr_read_stat =
mdb_read_nextptr(db, freeptr, &new_freeptr);
STAT_CHECK_RET(nextptr_read_stat, {;});
mdb_status_t freeptr_update_stat = mdb_write_nextptr(db, 0, new_freeptr);
STAT_CHECK_RET(freeptr_update_stat, {;});
mdb_status_t freeptr_cleanup_stat = mdb_write_nextptr(db, freeptr, 0);
STAT_CHECK_RET(freeptr_cleanup_stat, {;});
*ptr = freeptr;
return mdb_status(MDB_OK, NULL);
} else {
return mdb_stretch_index_file(db, ptr);
}
}
static mdb_status_t mdb_data_alloc(mdb_int_t *db, mdb_size_t valsize,
mdb_ptr_t *ptr) {
if (fseek(db->fp_data, 0, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to head of data file");
}
while (!feof(db->fp_data)) {
uint8_t byte;
if (fread(&byte, 1, 1, db->fp_data) != 1 && !feof(db->fp_data)) {
return mdb_status(MDB_ERR_READ, "cannot read data file");
}
while (!feof(db->fp_data) && byte != '\0') {
if (fread(&byte, 1, 1, db->fp_data) != 1 && !feof(db->fp_data)) {
return mdb_status(MDB_ERR_READ, "cannot read data file");
}
}
mdb_ptr_t start_ptr = (mdb_ptr_t)ftell(db->fp_data);
while (!feof(db->fp_data) && byte == '\0') {
if (fread(&byte, 1, 1, db->fp_data) != 1 && !feof(db->fp_data)) {
return mdb_status(MDB_ERR_READ, "cannot read data file");
}
}
mdb_ptr_t end_ptr = (mdb_ptr_t)ftell(db->fp_data);
/// @todo currently we rely on the extra padding to keep correct results.
/// refactor and remove this padding sometime.
if (end_ptr - start_ptr >= valsize + 2) {
*ptr = start_ptr + 1;
return mdb_status(MDB_OK, NULL);
}
}
mdb_ptr_t end_ptr = (mdb_ptr_t)ftell(db->fp_data);
unsigned char zero = '\0';
for (size_t i = 0; i < valsize; i++) {
if (fwrite(&zero, 1, 1, db->fp_data) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot stretch data file");
}
}
if (fflush(db->fp_data) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
*ptr = end_ptr;
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_index_free(mdb_int_t *db, mdb_ptr_t ptr) {
if (fseek(db->fp_index, 0, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to head of index file");
}
mdb_t freeptr;
if (fread(&freeptr, MDB_PTR_SIZE, 1, db->fp_index) != 1) {
return mdb_status(MDB_ERR_READ, "cannot read free ptr");
}
if (fseek(db->fp_index, 0, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to head of index file");
}
if (fwrite(&ptr, MDB_PTR_SIZE, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write to head of index file");
}
if (fseek(db->fp_index, (long)ptr, SEEK_SET) != 0) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to ptr");
}
if (fwrite(&freeptr, MDB_PTR_SIZE, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write to ptr");
}
for (size_t i = 0; i < db->options.key_size_max; i++) {
char zero = '\0';
if (fwrite(&zero, 1, 1, db->fp_index) < 1) {
return mdb_status(MDB_ERR_WRITE, "cannot clean key part of index");
}
}
if (fflush(db->fp_index) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_data_free(mdb_int_t *db, mdb_ptr_t valptr,
mdb_size_t valsize) {
if (fseek(db->fp_data, (long)valptr, SEEK_SET)) {
return mdb_status(MDB_ERR_SEEK, "cannot seek to data record");
}
uint8_t zero = '\0';
for (size_t i = 0; i < valsize; i++) {
if (fwrite(&zero, 1, 1, db->fp_data) != 1) {
return mdb_status(MDB_ERR_WRITE, "cannot write empty data");
}
}
if (fflush(db->fp_data) != 0) {
return mdb_status(MDB_ERR_FLUSH, "fflush failed");
}
return mdb_status(MDB_OK, NULL);
}
static mdb_status_t mdb_status(uint8_t code, const char *desc) {
mdb_status_t s;
s.code = code;
s.desc = desc;
return s;
}
static mdb_int_t *mdb_alloc() {
mdb_int_t *ret = (mdb_int_t*)malloc(sizeof(mdb_int_t));
if (!ret) {
return NULL;
}
ret->db_name = (char*)malloc(DB_NAME_MAX + 1);
if (!ret->db_name) {
free(ret);
return NULL;
}
ret->options.db_name = ret->db_name;
return ret;
}
static void mdb_free(mdb_int_t *db) {
if (db->fp_superblock != NULL) {
fclose(db->fp_superblock);
}
if (db->fp_index != NULL) {
fclose(db->fp_index);
}
if (db->fp_data != NULL) {
fclose(db->fp_data);
}
free(db->db_name);
free(db);
}
static uint32_t mdb_hash(const char *key) {
uint32_t ret = 0;
for (uint32_t i = 0; *key != '\0'; ++key, ++i) {
ret += (uint32_t)*key * i;
}
return ret;
}
void mdb_close(mdb_t handle) {
mdb_int_t *db = (mdb_int_t*)handle;
fclose(db->fp_superblock);
fclose(db->fp_index);
fclose(db->fp_data);
free(db->db_name);
free(db);
}