-
Notifications
You must be signed in to change notification settings - Fork 5
/
rolla.c
297 lines (249 loc) · 7.18 KB
/
rolla.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
#include "rolla.h"
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#define NO_BACKTRACE ((uint32_t)0xffffffff)
#define CACHE_SIZE (32 * 1024)
#define DEBUG_PRINT_TRUNCATE 1
struct rolla {
uint32_t offsets[NUMBUCKETS];
int mapfd;
char *path;
uint8_t *map;
uint32_t mmap_alloc;
uint32_t eof;
uint8_t cache[CACHE_SIZE];
int cache_used;
};
static void rolla_remap(rolla *r) {
if (r->map) {
msync(r->map, r->mmap_alloc, MS_SYNC);
int s = munmap(r->map, r->mmap_alloc);
assert(!s);
}
r->mmap_alloc = r->eof;
if (r->mmap_alloc) {
r->map = (uint8_t *)mmap(
NULL, r->mmap_alloc, PROT_READ, MAP_SHARED, r->mapfd, 0);
} else {
r->map = NULL;
}
r->cache_used = 0;
}
static uint32_t jenkins_one_at_a_time_hash(char *key, size_t len);
#define hash jenkins_one_at_a_time_hash
static uint32_t rolla_index_lookup(rolla *r, char *key) {
uint32_t fh = hash(key, strlen(key)) % NUMBUCKETS;
return r->offsets[fh];
}
static uint32_t rolla_index_keyval(rolla *r, char *key, uint32_t off) {
uint32_t fh = hash(key, strlen(key)) % NUMBUCKETS;
uint32_t last = r->offsets[fh];
r->offsets[fh] = off;
return last;
}
static void rolla_load(rolla *r) {
struct stat st;
r->mapfd = open(r->path,
O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
assert(r->mapfd > -1);
int s = fstat(r->mapfd, &st);
if (!s) {
r->eof = st.st_size;
}
else {
r->eof = 0;
}
rolla_remap(r);
if (r->map) {
int save_eof = r->eof;
uint8_t *p = r->map;
uint32_t off = 0;
while (1) {
if (off == r->eof) {
break; /* no recovery, clean shutdown */
}
if (off + 9 > r->eof) {
#if(DEBUG_PRINT_TRUNCATE)
fprintf(stderr, "rolla: recovering truncated db!\n");
#endif
r->eof = off;
break;
}
unsigned char klen = *(uint8_t *)p;
uint32_t vlen = *(uint32_t *)(p + 1);
uint32_t last = *(uint32_t *)(p + 5);
uint32_t jump = 9 + klen + vlen;
if (klen == 0 || (off + jump > r->eof)) {
#if(DEBUG_PRINT_TRUNCATE)
fprintf(stderr, "rolla: recovering truncated db!\n");
#endif
r->eof = off;
break;
}
char *key = (char *)(p + 9);
uint32_t prev = rolla_index_keyval(r, key, off);
assert(prev == last);
off += jump;
p += jump;
}
if (save_eof != r->eof) {
s = ftruncate(r->mapfd, (off_t)r->eof);
assert(!s);
rolla_remap(r);
}
lseek(r->mapfd, r->eof, SEEK_SET);
}
}
rolla * rolla_create(char *path) {
rolla *r = calloc(1, sizeof(rolla));
r->path = malloc(strlen(path) + 1);
strcpy(r->path, path);
r->mmap_alloc = 0;
r->cache_used = 0;
memset(r->offsets, 0xff, NUMBUCKETS * sizeof(uint32_t));
rolla_load(r);
return r;
}
uint8_t * rolla_get(rolla *r, char *key, uint32_t *len) {
uint32_t off = rolla_index_lookup(r, key);
while (off != NO_BACKTRACE) {
uint8_t *p;
if (off >= r->mmap_alloc) {
p = &r->cache[off - r->mmap_alloc];
} else {
p = &r->map[off];
}
uint8_t klen = *(uint8_t *)p;
if (!strncmp((char *)(p + 9), key, klen)) {
uint32_t vlen = *(uint32_t *)(p + 1);
if (vlen == 0) {
return NULL;
}
uint8_t *res = malloc(vlen);
*len = vlen;
memmove(res, p + 9 + klen, vlen);
return res;
}
off = *(uint32_t *)(p + 5);
}
return NULL;
}
void rolla_sync(rolla *r) {
int s = fsync(r->mapfd);
assert(!s);
}
void rolla_set(rolla *r, char *key, uint8_t *val, uint32_t vlen) {
uint8_t klen = strlen(key) + 1;
uint32_t step = 9 + klen + vlen;
/* write to the mapfd */
uint32_t last = rolla_index_keyval(r, key, r->eof);
struct iovec iov[] = {
{&klen, sizeof(uint8_t)},
{&vlen, sizeof(uint32_t)},
{&last, sizeof(uint32_t)},
{key, klen},
{val, vlen}};
int bwrite = writev(r->mapfd, iov, 5);
assert(bwrite == step);
r->eof += step;
if (r->cache_used + step > CACHE_SIZE) {
rolla_remap(r);
}
else {
/* write to trailing cache */
uint8_t *p = &r->cache[r->cache_used];
*((uint8_t *)p) = klen;
*((uint32_t *)(p + 1)) = vlen;
*((uint32_t *)(p + 5)) = last;
memmove(p + 9, key, klen);
if (vlen)
memmove(p + 9 + klen, val, vlen);
r->cache_used += step;
}
}
void rolla_del(rolla *r, char *key) {
rolla_set(r, key, NULL, 0);
}
void rolla_iter(rolla *r, rolla_iter_cb cb, void *passthrough) {
int i;
int sl = 10 * 1024;
char *s = realloc(NULL, sl);
rolla_remap(r); /* since we do not use the write-ahead cache */
for (i = 0; i < NUMBUCKETS; i++) {
uint32_t search_off = 0;
s[0] = 0;
uint32_t off = r->offsets[i];
char buf[258];
buf[0] = 1;
while (off != NO_BACKTRACE) {
uint8_t klen = *(uint8_t *)(r->map + off);
char *key = (char *)(r->map + off + 9);
strcpy(buf + 1, key);
buf[klen] = 1;
buf[klen + 1] = 0;
uint32_t skey_len = klen + 2;
if (!strstr(s, buf)) {
/* not found */
uint32_t vlen = *(uint32_t *)(r->map + off + 1);
if (vlen) {
uint8_t *val = (uint8_t *)(r->map + off + klen + 9);
cb(r, key, val, vlen, passthrough);
}
if (search_off + skey_len >= sl) {
sl *= 2;
s = realloc(s, sl);
}
strcpy(s + search_off, buf);
search_off += skey_len - 1; /* trailing \0 */
}
off = *(uint32_t *)(r->map + off + 5);
}
}
free(s);
}
static void rolla_rewrite_cb(rolla *r, char *key, uint8_t *data, uint32_t length, void *pass) {
rolla *new = (rolla *)pass;
rolla_set(new, key, data, length);
}
void rolla_close(rolla *r, int compress) {
char path[1200] = {0};
if (compress) {
assert(strlen(r->path) < 1100);
strcat(path, r->path);
strcat(path, ".rolla_rewrite");
rolla *tmp = rolla_create(path);
rolla_iter(r, rolla_rewrite_cb, (void *)tmp);
rolla_close(tmp, 0);
}
munmap(r->map, r->mmap_alloc);
close(r->mapfd);
if (compress) {
rename(path, r->path);
}
free(r->path);
free(r);
}
/* From Bob Jenkins/Dr. Dobbs */
static uint32_t jenkins_one_at_a_time_hash(char *key, size_t len)
{
uint32_t hash, i;
for(hash = i = 0; i < len; ++i)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}