-
Notifications
You must be signed in to change notification settings - Fork 8
/
bam.c
286 lines (257 loc) · 9.11 KB
/
bam.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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <cmph.h>
#include <microhttpd.h>
#define errstr strerror(errno)
void die(const char * fmt, ...) {
va_list args;
va_start(args,fmt);
vfprintf(stderr,fmt,args);
va_end(args);
exit(1);
}
static const char *const magic = "bam index: v000";
static char *index_file = NULL;
static int port = 8080;
static int threads = 0;
static int serve_data = 1;
static const char *usage = "bam [options] [tsv file]";
static const char *opts =
" -i --index=<file> Use <file> as index or \"-\" for none\n"
" -p --port=<port> Listen on TCP port number <port>\n"
" -t --threads=<n> Serve requests using <n> threads\n"
" -x --exit Index and exit without serving data\n"
" -h --help Print help message\n";
void parse_opts(int *argcp, char ***argvp) {
static char* shortopts = "i:p:t:xh";
static struct option longopts[] = {
{ "index", required_argument, 0, 'i' },
{ "port", required_argument, 0, 'p' },
{ "threads", required_argument, 0, 't' },
{ "exit", no_argument, 0, 'x' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
int c;
while ((c = getopt_long(*argcp,*argvp,shortopts,longopts,0)) != -1) {
switch (c) {
case 'i':
index_file = optarg;
break;
case 'p':
port = atoi(optarg);
if (port <= 0) die("Invalid port number: %s\n", optarg);
break;
case 't':
threads = atoi(optarg);
if (threads <= 0) die("Invalid thread count: %s\n", optarg);
break;
case 'x':
serve_data = 0;
break;
case 'h':
printf("usage: %s\n%s", usage, opts);
exit(0);
case '?':
fprintf(stderr, "options:\n%s", opts);
exit(1);
default:
fprintf(stderr, "bam: unhandled option -- %c\n", c);
exit(1);
}
}
*argvp += optind;
*argcp -= optind;
}
void *mmap_file(const char *const file, struct stat *fs) {
FILE *f = fopen(file,"r");
if (!f) die("Error opening \"%s\": %s\n", file, errstr);
if (fstat(fileno(f),fs)) die("Error fstating \"%s\": %s\n", file, errstr);
if (!fs->st_size) die("Data file \"%s\" is empty\n", file);
void *p = mmap(0, fs->st_size, PROT_READ, MAP_SHARED, fileno(f), 0);
if (p == MAP_FAILED) die("Error mmapping \"%s\": %s\n", file, errstr);
return p;
}
static char *data;
static off_t n = 0;
static off_t *offsets;
static cmph_t *hash;
static off_t keylen(const char *const key) { return strchr(key,'\t')-key; }
static off_t vallen(const char *const val) { return strchr(val,'\n')-val+1; }
#define idx(v) (*((off_t*)v))
#define keys(i) (data + offsets[i])
static int key_read(void *v, char **key, cmph_uint32 *len) {
return *len = keylen(*key = keys(idx(v)++));
}
static void key_rewind(void *v) { idx(v) = 0; }
static void key_dispose(void *v, char *key, cmph_uint32 len) { }
static char *const err = "Resource not found\n";
static int handle_request(
void *cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **con_cls
) {
int ret;
struct MHD_Response *response;
if (strcmp(method,"GET")) return MHD_NO;
if (*url++ != '/') goto invalid_key;
off_t url_len = strlen(url);
cmph_uint32 h = cmph_search(hash, url, url_len);
if (h >= n) goto invalid_key;
char *key = keys(h);
off_t key_len = keylen(key);
if (url_len != key_len || strncmp(url, key, url_len)) goto invalid_key;
char *val = key + key_len + 1;
response = MHD_create_response_from_data(vallen(val), val, MHD_NO, MHD_NO);
MHD_add_response_header (response, "Content-Type", "text/plain");
ret = MHD_queue_response(connection, 200, response);
MHD_destroy_response(response);
return ret;
invalid_key:
response = MHD_create_response_from_data(strlen(err), err, MHD_NO, MHD_NO);
MHD_add_response_header (response, "Content-Type", "text/plain");
ret = MHD_queue_response(connection, 404, response);
MHD_destroy_response(response);
return ret;
}
int core_count() {
#ifdef __APPLE__
size_t len = 4;
uint32_t count;
int nm[2] = {CTL_HW, HW_AVAILCPU};
sysctl(nm, 2, &count, &len, NULL, 0);
if (count < 1) {
nm[1] = HW_NCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if (count < 1) { count = 1; }
}
return count;
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
int main(int argc, char **argv) {
parse_opts(&argc,&argv);
if (argc != 1) die("usage: %s\n", usage);
const char *const data_file = argv[0];
if (!index_file) {
index_file = malloc(strlen(data_file) + 5);
sprintf(index_file, "%s.idx", data_file);
}
else if (!strcmp(index_file, "-")) index_file = NULL;
struct stat fs;
data = mmap_file(data_file, &fs);
int magic_len = strlen(magic) + 1;
if (index_file) {
FILE *f = fopen(index_file, "r");
if (!f) {
if (errno == ENOENT) goto build_index;
die("Error opening \"%s\": %s\n", index_file, errstr);
}
fprintf(stderr, "Loading index file \"%s\"...\n", index_file);
char magic_buf[magic_len];
int r = fread(magic_buf, 1, magic_len, f);
if (r != magic_len) {
if (feof(f)) {
die("Premature end of index file \"%s\"\n", index_file);
} else {
die("Error reading \"%s\": %s\n", index_file, errstr);
}
}
if (strcmp(magic, magic_buf))
die("Invalid index file: %s\n", index_file);
hash = cmph_load(f);
if (!hash) die("Error loading \"%s\": Corrupt CMPH data\n", index_file);
n = cmph_size(hash);
offsets = malloc(n*sizeof(off_t));
r = fread(offsets, sizeof(off_t), n, f);
if (r != n) {
if (feof(f)) {
die("Premature end of index file\n");
} else {
die("Error reading \"%s\": %s\n", index_file, errstr);
}
}
if (fclose(f)) die("Error closing \"%s\": %s\n", index_file, errstr);
} else {
off_t allocated;
build_index:
fprintf(stderr, "Building index...\n");
allocated = 4096;
offsets = malloc(allocated*sizeof(off_t));
char *p = data, *end = p + fs.st_size;
for (;;) {
if (allocated <= n) {
allocated = (n+1)*fs.st_size/(p-data)+1;
offsets = realloc(offsets, allocated*sizeof(off_t));
}
char *nl = memchr(p, '\n', end-p);
if (!nl) break;
char *tb = memchr(p, '\t', nl-p);
if (tb) offsets[n++] = p - data;
p = nl+1;
}
if (!n) die("File \"%s\" contains no key-value pairs\n", data_file);
offsets = realloc(offsets, n*sizeof(off_t));
off_t index;
cmph_io_adapter_t adapter;
adapter.data = (void*)&index;
adapter.nkeys = n;
adapter.read = key_read;
adapter.rewind = key_rewind;
adapter.dispose = key_dispose;
// TODO: cmph segfaults on duplicate keys.
// TODO: cmph segfaults on some binary data.
cmph_config_t *config = cmph_config_new(&adapter);
cmph_config_set_algo(config, CMPH_CHD);
hash = cmph_new(config);
cmph_config_destroy(config);
if (!hash) die("error creating minimal perfect hash\n");
off_t *offsets_by_hash = malloc(n*sizeof(off_t));
int i; for (i = 0; i < n; i++) {
char *key = keys(i);
cmph_uint32 h = cmph_search(hash, key, keylen(key));
offsets_by_hash[h] = key - data;
}
free(offsets);
offsets = offsets_by_hash;
if (index_file) {
fprintf(stderr, "Saving index file \"%s\"...\n", index_file);
FILE *f = fopen(index_file, "w");
if (!f) die("Error opening \"%s\": %s\n", index_file, errstr);
int w = fwrite(magic, 1, magic_len, f);
if (w != magic_len) die("Error writing \"%s\": %s\n", index_file, errstr);
cmph_dump(hash, f);
w = fwrite(offsets, sizeof(off_t), n, f);
if (w != n) die("Error writing \"%s\": %s\n", index_file, errstr);
if (fclose(f)) die("Error closing \"%s\": %s\n", index_file, errstr);
}
}
if (!serve_data) return 0;
if (!threads) threads = core_count();
struct MHD_Daemon *daemon = MHD_start_daemon(
MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
&handle_request, NULL,
MHD_OPTION_THREAD_POOL_SIZE, threads,
MHD_OPTION_END
);
if (!daemon) die("Failed starting http server\n");
fprintf(stderr, "Serving data @ %.3f ms...\n", ((double)1000*clock())/CLOCKS_PER_SEC);
for (;;) sleep(0xffffffff);
MHD_stop_daemon (daemon);
return 0;
}