This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathsortdb.c
More file actions
408 lines (357 loc) · 12.9 KB
/
Copy pathsortdb.c
File metadata and controls
408 lines (357 loc) · 12.9 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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <inttypes.h>
#include <simplehttp/queue.h>
#include <simplehttp/simplehttp.h>
#define NAME "sortdb"
#define VERSION "1.5.1"
#define DEBUG 1
void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void get_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void reload_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void exit_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
char *prev_line(char *pos);
char *map_search(char *key, size_t keylen, char *lower, char *upper, int *seeks, int allow_prefix);
void info();
int main(int argc, char **argv);
void close_dbfile();
void open_dbfile();
void hup_handler(int signum);
static void *map_base = NULL;
static char *db_filename;
static struct stat st;
static char deliminator = '\t';
static int fd = 0;
enum prefix_options { disable_prefix, enable_prefix };
static uint64_t get_hits = 0;
static uint64_t get_misses = 0;
static uint64_t fwmatch_hits = 0;
static uint64_t fwmatch_misses = 0;
static uint64_t total_seeks = 0;
char *prev_line(char *pos)
{
if (!pos) {
return NULL;
}
while (pos != map_base && *(pos - 1) != '\n') {
pos--;
}
return pos;
}
char *map_search(char *key, size_t keylen, char *lower, char *upper, int *seeks, int allow_prefix)
{
ptrdiff_t distance;
char *current;
char *line;
int rc;
distance = (upper - lower);
if (distance <= 1) {
return NULL;
}
*seeks += 1;
total_seeks++;
current = lower + (distance / 2);
line = prev_line(current);
if (!line) {
return NULL;
}
/*
char *tmp = malloc(keylen + 1);
memcpy(tmp, line, keylen);
tmp[keylen] = '\0';
if(DEBUG) fprintf(stderr, "cmp %s to %s is %d\n", key, tmp, strncmp(key, line, keylen));
*/
rc = strncmp(key, line, keylen);
if (rc < 0) {
return map_search(key, keylen, lower, current, seeks, allow_prefix);
} else if (rc > 0) {
return map_search(key, keylen, current, upper, seeks, allow_prefix);
} else if (!allow_prefix && (line[keylen] != deliminator)) {
return map_search(key, keylen, lower, current, seeks, allow_prefix);
} else {
return line;
}
}
void fwmatch_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
struct evkeyvalq args;
char *key, *line, *prev, *start, *end, *newline, buf[32];
int keylen, seeks = 0;
evhttp_parse_query(req->uri, &args);
key = (char *)evhttp_find_header(&args, "key");
keylen = key ? strlen(key) : 0;
if (DEBUG) {
fprintf(stderr, "/fwmatch %s\n", key);
}
if (key) {
if ((line = map_search(key, keylen, (char *)map_base, (char *)map_base + st.st_size, &seeks, enable_prefix))) {
/*
* Walk backwards while key prefix matches.
* There's probably a better way to do this, however
* this is easy and faults page in 4k chunks anyway.
*/
while (line != (char *)map_base && (prev = prev_line(line - 1)) != line) {
if (strncmp(key, prev, keylen) != 0) {
break;
}
line = prev;
}
/*
* Walk forwards while key prefix matches to find all
* records.
*/
start = end = line;
while ((newline = strchr(line, '\n')) != NULL
&& newline != (char *)map_base + st.st_size) {
line = end = newline + 1;
if (strncmp(key, line, keylen) != 0) {
break;
}
}
if (end != start) {
// this is only supported by libevent2+
//evbuffer_add_reference(evb, (const void *)start, (size_t)(end - start), NULL, NULL);
evbuffer_add(evb, start, (size_t)(end - start));
} else {
evbuffer_add_printf(evb, "%s\n", line);
}
fwmatch_hits++;
sprintf(buf, "%d", seeks);
evhttp_add_header(req->output_headers, "x-sortdb-seeks", buf);
} else {
fwmatch_misses++;
}
evhttp_send_reply(req, HTTP_OK, "OK", evb);
} else {
evbuffer_add_printf(evb, "missing argument: key\n");
evhttp_send_reply(req, HTTP_BADREQUEST, "MISSING_ARG_KEY", evb);
}
evhttp_clear_headers(&args);
}
void get_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
struct evkeyvalq args;
char *key, *line, *newline, *delim, buf[32];
int seeks = 0;
evhttp_parse_query(req->uri, &args);
key = (char *)evhttp_find_header(&args, "key");
if (DEBUG) {
fprintf(stderr, "/get %s\n", key);
}
if (!key) {
evbuffer_add_printf(evb, "missing argument: key\n");
evhttp_send_reply(req, HTTP_BADREQUEST, "MISSING_ARG_KEY", evb);
} else if ((line = map_search(key, strlen(key), (char *)map_base, (char *)map_base + st.st_size, &seeks, disable_prefix))) {
sprintf(buf, "%d", seeks);
evhttp_add_header(req->output_headers, "x-sortdb-seeks", buf);
delim = strchr(line, deliminator);
if (delim) {
line = delim + 1;
}
newline = strchr(line, '\n');
if (newline) {
evbuffer_add(evb, line, (newline - line) + 1);
} else {
evbuffer_add_printf(evb, "%s\n", line);
}
get_hits++;
evhttp_send_reply(req, HTTP_OK, "OK", evb);
} else {
get_misses++;
evhttp_send_reply(req, HTTP_NOTFOUND, "OK", evb);
}
evhttp_clear_headers(&args);
}
void mget_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
struct evkeyvalq args;
struct evkeyval *pair;
char *key, *line, *newline, buf[32];
int seeks = 0, nkeys = 0;
evhttp_parse_query(req->uri, &args);
TAILQ_FOREACH(pair, &args, next) {
if (pair->key[0] != 'k') {
continue;
}
key = (char *)pair->value;
nkeys++;
if (DEBUG) {
fprintf(stderr, "/mget %s\n", key);
}
if ((line = map_search(key, strlen(key), (char *)map_base, (char *)map_base + st.st_size, &seeks, disable_prefix))) {
newline = strchr(line, '\n');
if (newline) {
// this is only supported by libevent2+
//evbuffer_add_reference(evb, (const void *)line, (size_t)(newline - line) + 1, NULL, NULL);
evbuffer_add(evb, line, (size_t)(newline - line) + 1);
} else {
evbuffer_add_printf(evb, "%s\n", line);
}
get_hits++;
} else {
get_misses++;
}
}
if (nkeys) {
sprintf(buf, "%d", seeks);
evhttp_add_header(req->output_headers, "x-sortdb-seeks", buf);
evhttp_send_reply(req, HTTP_OK, "OK", evb);
} else {
evbuffer_add_printf(evb, "missing argument: key\n");
evhttp_send_reply(req, HTTP_BADREQUEST, "MISSING_ARG_KEY", evb);
}
evhttp_clear_headers(&args);
}
void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
int i;
struct evkeyvalq args;
const char *format;
struct simplehttp_stats *st;
st = simplehttp_stats_new();
simplehttp_stats_get(st);
evhttp_parse_query(req->uri, &args);
format = (char *)evhttp_find_header(&args, "format");
if ((format != NULL) && (strcmp(format, "json") == 0)) {
evbuffer_add_printf(evb, "{");
for (i = 0; i < st->callback_count; i++) {
evbuffer_add_printf(evb, "\"%s_95\": %"PRIu64",", st->stats_labels[i], st->ninety_five_percents[i]);
evbuffer_add_printf(evb, "\"%s_average_request\": %"PRIu64",", st->stats_labels[i], st->average_requests[i]);
evbuffer_add_printf(evb, "\"%s_requests\": %"PRIu64",", st->stats_labels[i], st->stats_counts[i]);
}
evbuffer_add_printf(evb, "\"get_hits\": %"PRIu64",", get_hits);
evbuffer_add_printf(evb, "\"get_misses\": %"PRIu64",", get_misses);
evbuffer_add_printf(evb, "\"fwmatch_hits\": %"PRIu64",", fwmatch_hits);
evbuffer_add_printf(evb, "\"fwmatch_misses\": %"PRIu64",", fwmatch_misses);
evbuffer_add_printf(evb, "\"total_seeks\": %"PRIu64",", total_seeks);
evbuffer_add_printf(evb, "\"total_requests\": %"PRIu64, st->requests);
evbuffer_add_printf(evb, "}\n");
} else {
for (i = 0; i < st->callback_count; i++) {
evbuffer_add_printf(evb, "/%s 95%%: %"PRIu64"\n", st->stats_labels[i], st->ninety_five_percents[i]);
evbuffer_add_printf(evb, "/%s average request (usec): %"PRIu64"\n", st->stats_labels[i], st->average_requests[i]);
evbuffer_add_printf(evb, "/%s requests: %"PRIu64"\n", st->stats_labels[i], st->stats_counts[i]);
}
evbuffer_add_printf(evb, "/get hits: %"PRIu64"\n", get_hits);
evbuffer_add_printf(evb, "/get misses: %"PRIu64"\n", get_misses);
evbuffer_add_printf(evb, "total seeks: %"PRIu64"\n", total_seeks);
evbuffer_add_printf(evb, "total requests: %"PRIu64"\n", st->requests);
}
simplehttp_stats_free(st);
evhttp_send_reply(req, HTTP_OK, "OK", evb);
evhttp_clear_headers(&args);
}
void reload_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
fprintf(stdout, "/reload request recieved\n");
close_dbfile();
open_dbfile();
if (map_base == NULL) {
fprintf(stderr, "no mmaped file; exiting\n");
exit(1);
}
evbuffer_add_printf(evb, "db reloaded\n");
evhttp_send_reply(req, HTTP_OK, "OK", evb);
}
void exit_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
fprintf(stdout, "/exit request recieved\n");
event_loopbreak();
}
void info()
{
fprintf(stdout, "%s: sorted database server.\n", NAME);
fprintf(stdout, "Version: %s, https://github.com/bitly/simplehttp/tree/master/sortdb\n", VERSION);
}
void hup_handler(int signum)
{
signal(SIGHUP, hup_handler);
fprintf(stdout, "HUP recieved\n");
close_dbfile();
open_dbfile();
if (map_base == NULL) {
fprintf(stderr, "no mmaped file; exiting\n");
exit(1);
}
}
void close_dbfile()
{
fprintf(stdout, "closing %s\n", db_filename);
if (option_get_int("memory_lock") && munlock(map_base, st.st_size)) {
fprintf(stderr, "munlock(%s) failed: %s\n", db_filename, strerror(errno));
exit(errno);
}
if (munmap(map_base, st.st_size) != 0) {
fprintf(stderr, "failed munmap\n");
exit(1);
}
if (close(fd) != 0) {
fprintf(stderr, "failed close() on %d\n", fd);
exit(1);
}
fd = 0;
map_base = NULL;
}
void open_dbfile()
{
if ((fd = open(db_filename, O_RDONLY)) < 0) {
fprintf(stderr, "open(%s) failed: %s\n", db_filename, strerror(errno));
exit(errno);
}
if (fstat(fd, &st) < 0) {
fprintf(stderr, "fstat(%s) failed: %s\n", db_filename, strerror(errno));
exit(errno);
}
fprintf(stdout, "opening %s\n", db_filename);
fprintf(stdout, "db size %ld\n", (long int)st.st_size);
if ((map_base = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
fprintf(stderr, "mmap(%s) failed: %s\n", db_filename, strerror(errno));
exit(errno);
}
if (option_get_int("memory_lock") && mlock(map_base, st.st_size)) {
fprintf(stderr, "mlock(%s) failed: %s\n", db_filename, strerror(errno));
exit(errno);
}
}
int version_cb(int value)
{
fprintf(stdout, "Version: %s\n", VERSION);
return 0;
}
int main(int argc, char **argv)
{
define_simplehttp_options();
option_define_str("db_file", OPT_REQUIRED, NULL, &db_filename, NULL, NULL);
option_define_bool("memory_lock", OPT_OPTIONAL, 0, NULL, NULL, "lock data file pages into memory");
option_define_char("field_separator", OPT_OPTIONAL, '\t', &deliminator, NULL, "field separator (eg: comma, tab, pipe). default: TAB");
option_define_bool("version", OPT_OPTIONAL, 0, NULL, version_cb, VERSION);
if (!option_parse_command_line(argc, argv)) {
return 1;
}
info();
fprintf(stdout, "--field-separator is \"%c\"\n", deliminator);
fprintf(stdout, "--db-file is %s\n", db_filename);
open_dbfile();
if (map_base == NULL) {
exit(1);
}
simplehttp_init();
signal(SIGHUP, hup_handler);
simplehttp_set_cb("/get?*", get_cb, NULL);
simplehttp_set_cb("/mget?*", mget_cb, NULL);
simplehttp_set_cb("/fwmatch?*", fwmatch_cb, NULL);
simplehttp_set_cb("/stats*", stats_cb, NULL);
simplehttp_set_cb("/reload", reload_cb, NULL);
simplehttp_set_cb("/exit", exit_cb, NULL);
simplehttp_main();
free_options();
return 0;
}