forked from RichardAH/snugdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnug.hpp
691 lines (530 loc) · 24.2 KB
/
snug.hpp
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <array>
#include <mutex>
#include <shared_mutex>
#include <vector>
#include <memory>
#include <algorithm>
namespace snug
{
int compare_entries_reverse(const void* a, const void* b)
{
const uint64_t* a_key = static_cast<const uint64_t*>(a);
const uint64_t* b_key = static_cast<const uint64_t*>(b);
// Unrolled comparison of 4 uint64_t values (4 * 8 = 32 bytes)
if (b_key[0] > a_key[0]) return 1;
if (b_key[0] < a_key[0]) return -1;
if (b_key[1] > a_key[1]) return 1;
if (b_key[1] < a_key[1]) return -1;
if (b_key[2] > a_key[2]) return 1;
if (b_key[2] < a_key[2]) return -1;
if (b_key[3] > a_key[3]) return 1;
if (b_key[3] < a_key[3]) return -1;
return 0; // Keys are equal
}
class SnugDB
{
private:
static constexpr uint64_t SNUGSIZE = 256ull*1024ull*1024ull*1024ull; // 256 GiB
static constexpr uint64_t BIGSIZE = 10ull*1024ull*1024ull*1024ull*1024ull; // 10 TiB
static constexpr size_t BUCKET_COUNT = 1048576;
std::unique_ptr<std::shared_mutex[]> mutexes =
std::make_unique<std::shared_mutex[]>(BUCKET_COUNT);
// each file snug.0 snug.1 ... is mmaped and the pointer
uint8_t* mapped_files[1024];
uint64_t mapped_files_count { 0 };
uint8_t* big_file; // this file has 64kib blocks in it which are used
// as an overflow for large blobs
std::mutex big_file_mutex; // locked when incrementing the "next new block" pointer
// only used when adding a new file
std::mutex mapped_files_count_mutex;
std::string const path;
// 0 = success
// 1 = could not open
// 2 = could not seek
// 3 = could not write at end of file
int alloc_file(char const* fn, uint64_t size)
{
int fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
return 1;
// must be a multiple of bufsize
if (lseek(fd, size, SEEK_SET) == -1)
{
close(fd);
unlink(fn);
return 2;
}
if (write(fd, "", 1) != 1)
{
close(fd);
unlink(fn);
return 3;
}
close(fd);
return 0;
}
// 0 = file exists and is right size
int check_file(char const* fn, uint64_t size)
{
struct stat st;
int file_exists = (stat(fn, &st) == 0);
if (!file_exists)
return 1;
if (st.st_size != size + 1)
return 2;
return 0;
}
#define OFFSET(byte0, byte1, byte2)\
(((((uint64_t)(byte0 & 0xFFU)) << 12) +\
(((uint64_t)(byte1 & 0xFFU)) << 4) +\
((uint64_t)(byte2 & 0xFU))) << 18)
// check if 32 bytes are 0, which they will be for a zero entry
#define IS_ZERO_ENTRY(x)\
(*((uint64_t*)((x)+ 0)) == 0 && \
*((uint64_t*)((x)+ 8)) == 0 && \
*((uint64_t*)((x)+16)) == 0 && \
*((uint64_t*)((x)+24)) == 0)
#define IS_ENTRY(x,y)\
(*((uint64_t*)((x)+ 0)) == *((uint64_t*)((y)+ 0)) && \
*((uint64_t*)((x)+ 8)) == *((uint64_t*)((y)+ 8)) && \
*((uint64_t*)((x)+16)) == *((uint64_t*)((y)+16)) && \
*((uint64_t*)((x)+24)) == *((uint64_t*)((y)+24)))
#define WRITE_KEY(x /* dst */, y /* src */, flags)\
{\
*((uint64_t*)((x)+ 0)) = *((uint64_t*)((y)+ 0)); \
*((uint64_t*)((x)+ 8)) = *((uint64_t*)((y)+ 8)); \
*((uint64_t*)((x)+16)) = *((uint64_t*)((y)+16)); \
*((uint64_t*)((x)+24)) = *((uint64_t*)((y)+24)); \
*((uint64_t*)((x)+32)) = flags;\
}
// if an entry exceeds 984 bytes then the overflow is written
// into the snug.big file in a linked list of 32kib blocks
// the first of those blocks is a control block
uint64_t get_big_block()
{
std::unique_lock<std::mutex> lock(big_file_mutex);
uint64_t free_blocks = *((uint64_t*)(big_file + 8));
if (free_blocks == 0)
{
// no free blocks, allocate a new one
uint64_t next_block = *((uint64_t*)big_file);
*((uint64_t*)(big_file)) += 32768;
if (next_block + 32768 > BIGSIZE)
return 0;
return next_block;
}
// grab the nth one
uint8_t* offset = big_file + 16
+ 8 * (free_blocks - 1);
// decrement free block counter
*(uint64_t*)(big_file + 8) -= 1;
return *((uint64_t*)offset);
}
void unalloc_blocks(uint64_t next_block)
{
if (next_block != 0)
{
// scope the lock only if called with non-zero nextblock
std::unique_lock<std::mutex> lock(big_file_mutex);
do
{
uint64_t free_blocks = *((uint64_t*)(big_file + 8));
if (free_blocks >= 4095)
break;
uint8_t* offset = big_file + 16
+ 8 * free_blocks;
*((uint64_t*) offset) = next_block;
*((uint64_t*)(big_file + 8)) += 1;
uint8_t* big_ptr = big_file + next_block;
uint64_t previous = next_block;
next_block = *((uint64_t*)(big_file + next_block));
// clear the pointer on the old block
*((uint64_t*)(big_file + previous)) = 0;
}
while (next_block != 0);
}
}
/*
* First big entry is control block:
* 0 - 7: The next free new block
* 8 - 15: The number of free blocks blow
* 16 - 23 [... repeating]: The next free unused block
*/
/*
* Big entry format:
* 0 - 7: next block in chain, if any.
* 8 - 32767: payload
*/
// return 0 = failure
// > 0 = first block in the chain
uint64_t write_big_entry_internal(uint8_t* data, ssize_t len, uint64_t next_block)
{
uint64_t first_block = 0;
uint64_t* last_block_ptr = 0;
do
{
// if next_block is populated we follow an existing pathway
// otherwise allocate a new block now
if (!next_block)
next_block = get_big_block();
if (!next_block)
return 0;
if (!first_block)
first_block = next_block;
if (last_block_ptr)
*last_block_ptr = next_block;
uint8_t* big_ptr = big_file + next_block;
// copy to the block
ssize_t to_write = len > 32760 ? 32760 : len;
memcpy(big_ptr + 8, data, to_write);
data += to_write;
len -= to_write;
next_block = *((uint64_t*)big_ptr);
last_block_ptr = (uint64_t*)big_ptr;
}
while (len > 0);
// if there's a dangling chain we'll unallocate it
if (next_block != 0)
unalloc_blocks(next_block);
return first_block;
}
/*
* Entry format:
* 0 - 31: the 32 byte key
* 32 - 39: flags (high 4 bytes are flags, low 4 are size)
* 40 - 1023: data (up to 984 bytes)
*/
// 0 = success
// 1 = bucket full
// 2 = big blocks full
int write_entry_internal(uint8_t* data, uint8_t* key, uint8_t* val, uint32_t len)
{
// find the entry
uint64_t offset = OFFSET(key[0], key[1], (key[2]>>4));
// lock the bucket for writing
std::unique_lock<std::shared_mutex> lock(mutexes[offset >> 18]);
uint8_t* start = data + offset;
for (int i = 0; i < 256*1024; i+=1024)
{
if (!IS_ENTRY(start + i, key) && !IS_ZERO_ENTRY(start + i))
continue;
// read flags
uint64_t flags = *((uint64_t*)(start + i + 32));
// big entries are tricky
bool const old_big = (flags >> 32) != 0;
bool const new_big = len > 984;
if (new_big)
{
//write_big_entry_internal(uint8_t* data, ssize_t len, uint64_t next_block)
uint64_t first_block =
write_big_entry_internal(val + 984, len - 984, (old_big ? (flags >> 32) : 0));
if (first_block == 0) // error state
{
if (old_big)
unalloc_blocks(flags >> 32);
return 2;
}
flags = (first_block << 32) + len;
}
else if (old_big) // big blocks exist but new value is small
{
// unallocate the old chain
unalloc_blocks(flags >> 32);
}
if (!new_big)
flags = len;
/// write entry
WRITE_KEY(start + i, key, flags);
memcpy(start + i + 40, val, (len > 984 ? 984 : len));
// sort the bucket backwards so 0's appear at the end
qsort(start, 256, 1024, compare_entries_reverse);
return 0;
}
/// file (bucket) full
return 1;
}
// out_len carries the length of the output buffer when calling and is replaced
// with the length of the data found when returning
int read_entry_internal(uint8_t* data, uint8_t* key, uint8_t* val_out, uint64_t* out_len)
{
uint64_t buf_len = *out_len;
// find the entry
uint64_t offset = OFFSET(key[0], key[1], (key[2]>>4));
uint8_t* start = data + offset;
// lock the bucket for reading
std::shared_lock<std::shared_mutex> lock(mutexes[offset >> 18]);
for (int i = 0; i < 256*1024; i+=1024)
{
if (IS_ZERO_ENTRY(start + i))
return 1;
if (!IS_ENTRY(start + i, key))
continue;
// read out the value
uint64_t flags = *((uint64_t*)(start + i + 32));
uint32_t size = flags & 0xFFFFFFFFUL;
uint64_t next_block = flags >> 32;
if (size > buf_len)
return 2;
*out_len = size;
size_t to_read = size > 984 ? 984: size;
memcpy(val_out, start + i + 40, to_read);
val_out += to_read;
size -= to_read;
// big block read logic
while (size > 0)
{
// follow big block pointers
if (!next_block)
{
printf("End while size=%d\n", size);
return 3;
}
uint8_t* big_ptr = big_file + next_block;
to_read = size > 32760 ? 32760 : size;
memcpy(val_out, big_ptr + 8, to_read);
val_out += to_read;
size -= to_read;
next_block = *((uint64_t*)big_ptr);
}
return 0;
}
return 1;
}
void setup()
{
struct stat path_stat;
if (stat(path.c_str(), &path_stat) != 0)
throw std::runtime_error("Error checking path: " + path + " - " + std::string(strerror(errno)));
if (!S_ISDIR(path_stat.st_mode))
throw std::runtime_error("Path is not a directory: " + path);
if (access(path.c_str(), R_OK | W_OK | X_OK) != 0)
throw std::runtime_error("Insufficient permissions for path: " + path);
// Search for existing snug files sequentially
std::vector<std::string> snug_files;
for (int file_index = 0; file_index < 1024; ++file_index)
{
std::string filename = "snug." + std::to_string(file_index);
std::string full_path = path + "/" + filename;
if (access(full_path.c_str(), F_OK) != 0)
break;
snug_files.push_back(filename);
}
// If no files found, create snug.0
if (snug_files.empty())
{
std::string new_file = path + "/snug.0";
int result = alloc_file(new_file.c_str(), SNUGSIZE);
if (result != 0)
throw std::runtime_error("Failed to create initial file: " + new_file);
snug_files.push_back("snug.0");
}
// Memory map all files
for (const auto& file : snug_files)
{
std::string full_path = path + "/" + file;
if (check_file(full_path.c_str(), SNUGSIZE) != 0)
throw std::runtime_error("File was the wrong size: " + file);
int fd = open(full_path.c_str(), O_RDWR);
if (fd == -1)
throw std::runtime_error("Unable to open file: " + full_path);
struct stat file_stat;
if (fstat(fd, &file_stat) == -1)
{
close(fd);
throw std::runtime_error("Unable to get file stats: " + full_path);
}
void* mapped = mmap(nullptr, file_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd); // Can close fd after mmap
if (mapped == MAP_FAILED)
throw std::runtime_error("Unable to mmap file: " + full_path);
mapped_files[mapped_files_count++] = static_cast<uint8_t*>(mapped);
}
// create and map snug.big overflow file
{
std::string new_file = path + "/snug.big";
if (check_file(new_file.c_str(), BIGSIZE) != 0)
{
int result = alloc_file(new_file.c_str(), BIGSIZE);
if (result != 0)
throw std::runtime_error("Failed to create initial file: " + new_file);
}
int fd = open(new_file.c_str(), O_RDWR);
if (fd == -1)
throw std::runtime_error("Unable to open file: " + new_file);
struct stat file_stat;
if (fstat(fd, &file_stat) == -1)
{
close(fd);
throw std::runtime_error("Unable to get file stats: " + new_file);
}
void* mapped = mmap(nullptr, file_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd); // Can close fd after mmap
if (mapped == MAP_FAILED)
throw std::runtime_error("Unable to mmap file: " + new_file);
big_file = static_cast<uint8_t*>(mapped);
}
}
public:
SnugDB(std::string path_) : path(path_)
{
setup();
}
~SnugDB()
{
// Unmap all files in destructor
// RH TODO: consider lock here
for (int i = 0; i < mapped_files_count; ++i)
munmap(mapped_files[i], SNUGSIZE);
// unmap the big file
munmap(big_file, BIGSIZE);
}
int write_entry(uint8_t* key, uint8_t* val, ssize_t len)
{
for (size_t i = 0; i < mapped_files_count; ++i)
{
int result = write_entry_internal(mapped_files[i], key, val, len);
if (result == 0)
return 0;
if (result != 1) // only bucket full falls through
return result;
}
// All existing files are full, allocate a new one
{
// acquire the mutex
const std::lock_guard<std::mutex> lock(mapped_files_count_mutex);
std::string new_file = path + "/snug." + std::to_string(mapped_files_count);
int alloc_result = alloc_file(new_file.c_str(), SNUGSIZE);
if (alloc_result != 0)
return alloc_result + 10; // Return error code from alloc_file if it fails (+10)
int fd = open(new_file.c_str(), O_RDWR);
if (fd == -1)
return 1; // Return 1 for open failure
struct stat file_stat;
if (fstat(fd, &file_stat) == -1)
{
close(fd);
return 2; // Return 2 for fstat failure
}
void* mapped = mmap(nullptr, file_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd); // Can close fd after mmap
if (mapped == MAP_FAILED)
return 3; // Return 3 for mmap failure
// add the new file to the map, and increment the counter
mapped_files[mapped_files_count] = static_cast<uint8_t*>(mapped);
// this is the last possible thing done
mapped_files_count++;
}
// finally write the entry
// RH TODO: consider adding a recursion guard here
return write_entry(key, val, len);
}
int read_entry(uint8_t* key, uint8_t* val_out, uint64_t* out_len_orig)
{
for (size_t i = 0; i < mapped_files_count; ++i)
{
uint64_t out_len = *out_len_orig;
int result =
read_entry_internal(mapped_files[i], key, val_out, &out_len);
if (result == 0)
{
*out_len_orig = out_len;
return 0; // Entry found and read successfully
}
if (result == 2)
return 2; // Output buffer too small
}
// Entry not found in any file
return 1;
}
void visit_all(void (*f)(uint8_t*, uint8_t*, uint64_t, void* /*opaque caller val*/),
void* opaque)
{
// to visit all we only need to check snug.0 to begin with
// we go to the first bucket
// if we find no entries there we go to the next bucket
// if we find entries there then we need to count them,
// if we find 256 entries there then we go to snug.1 and so on until we run out
// we merge sort the entries into a list for the visit
for (uint64_t bucket = 0; bucket < BUCKET_COUNT; ++bucket)
{
// acquire the bucket lock
std::shared_lock<std::shared_mutex> lock(mutexes[bucket]);
// check the bucket
uint8_t* ptr = mapped_files[0] + (bucket << 18);
if (*((uint64_t*)(ptr + 32)) == 0)
continue;
//if (IS_ZERO_ENTRY(ptr))
// continue;
// live bucket, collect entries
std::vector<uint8_t*> entries;
{
// need to acquire the mutex to prevent a race condition
// where a new file is being added while we're searching
const std::lock_guard<std::mutex> lock(mapped_files_count_mutex);
// preallocate worst case scenario, RIP memory
entries.reserve(mapped_files_count * 256);
for (int i = 0; i < mapped_files_count; ++i)
{
uint8_t* ptr = mapped_files[i] + (bucket << 18);
for (int entry_count = 0;
!IS_ZERO_ENTRY(ptr) && entry_count < 256;
++entry_count, ptr += 1024)
entries.push_back(ptr);
}
}
if (entries.empty())
continue;
// sort the entries
std::sort(entries.begin(), entries.end(),
[](const uint8_t* a, const uint8_t* b)
{
return memcmp(a, b, 32) < 0;
});
for (auto e : entries)
{
// visitation
uint8_t* entry = &e[0];
uint64_t flags = *((uint64_t*)(entry + 32));
uint64_t next_block = flags >> 32;
uint64_t size = flags & 0xFFFFFFFFULL;
if (size <= 984)
{
f(entry, entry + 40, size, opaque);
continue;
}
// copy big entry to a buffer
std::unique_ptr<uint8_t[]> copybuf = std::make_unique<uint8_t[]>(size);
uint8_t* data = &(copybuf[0]);
memcpy(data, entry + 40, 984);
data += 984;
size -= 984;
// big block read logic
while (size > 0)
{
// follow big block pointers
if (!next_block)
{
printf("End while size=%lu\n", size);
return;
}
uint8_t* big_ptr = big_file + next_block;
uint64_t to_read = size > 32760 ? 32760 : size;
memcpy(data, big_ptr + 8, to_read);
data += to_read;
size -= to_read;
next_block = *((uint64_t*)big_ptr);
}
f(entry, data, (flags & 0xFFFFFFFFULL), opaque);
}
}
}
};
}