-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleveldb.dylan
585 lines (486 loc) · 17.6 KB
/
leveldb.dylan
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
module: leveldb
synopsis: generated bindings for the leveldb library
author: Bruce Mitchener, Jr.
copyright: See LICENSE file in this distribution.
define simple-C-mapped-subtype <C-buffer-offset> (<C-void*>)
export-map <machine-word>, export-function: identity;
end;
define C-struct <leveldb-t>
end;
define C-struct <leveldb-cache-t>
end;
define C-struct <leveldb-comparator-t>
end;
define C-struct <leveldb-env-t>
end;
define C-struct <leveldb-filelock-t>
end;
define C-struct <leveldb-filterpolicy-t>
end;
define C-struct <leveldb-iterator-t>
end;
define C-struct <leveldb-logger-t>
end;
define C-struct <leveldb-options-t>
end;
define C-struct <leveldb-randomfile-t>
end;
define C-struct <leveldb-readoptions-t>
end;
define C-struct <leveldb-seqfile-t>
end;
define C-struct <leveldb-snapshot-t>
end;
define C-struct <leveldb-writablefile-t>
end;
define C-struct <leveldb-writebatch-t>
end;
define C-struct <leveldb-writeoptions-t>
end;
define C-pointer-type <leveldb-t*> => <leveldb-t>;
define C-pointer-type <leveldb-options-t*> => <leveldb-options-t>;
define C-pointer-type <C-string> => <C-signed-char>;
define C-pointer-type <char**> => <C-string>;
define C-function %leveldb-open
input parameter options_ :: <leveldb-options-t*>;
input parameter name_ :: <C-string>;
output parameter errptr_ :: <char**>;
result res :: <leveldb-t*>;
c-name: "leveldb_open";
end;
define C-function leveldb-close
input parameter db_ :: <leveldb-t*>;
c-name: "leveldb_close";
end;
define C-pointer-type <leveldb-writeoptions-t*> => <leveldb-writeoptions-t>;
define constant <__darwin-size-t> = <C-unsigned-long>;
define constant <size-t> = <__darwin-size-t>;
define C-function %leveldb-put
input parameter db_ :: <leveldb-t*>;
input parameter options_ :: <leveldb-writeoptions-t*>;
input parameter key_ :: <C-string>;
input parameter keylen_ :: <size-t>;
input parameter val_ :: <C-buffer-offset>;
input parameter vallen_ :: <size-t>;
output parameter errptr_ :: <char**>;
c-name: "leveldb_put";
end;
define C-function %leveldb-delete
input parameter db_ :: <leveldb-t*>;
input parameter options_ :: <leveldb-writeoptions-t*>;
input parameter key_ :: <C-string>;
input parameter keylen_ :: <size-t>;
output parameter errptr_ :: <char**>;
c-name: "leveldb_delete";
end;
define C-pointer-type <leveldb-writebatch-t*> => <leveldb-writebatch-t>;
define C-function %leveldb-write
input parameter db_ :: <leveldb-t*>;
input parameter options_ :: <leveldb-writeoptions-t*>;
input parameter batch_ :: <leveldb-writebatch-t*>;
output parameter errptr_ :: <char**>;
c-name: "leveldb_write";
end;
define C-pointer-type <leveldb-readoptions-t*> => <leveldb-readoptions-t>;
define C-pointer-type <size-t*> => <size-t>;
define C-function %leveldb-get
input parameter db_ :: <leveldb-t*>;
input parameter options_ :: <leveldb-readoptions-t*>;
input parameter key_ :: <C-string>;
input parameter keylen_ :: <size-t>;
output parameter vallen_ :: <size-t*>;
output parameter errptr_ :: <char**>;
result res :: <C-string>;
c-name: "leveldb_get";
end;
define C-pointer-type <leveldb-iterator-t*> => <leveldb-iterator-t>;
define C-function leveldb-create-iterator
input parameter db_ :: <leveldb-t*>;
input parameter options_ :: <leveldb-readoptions-t*>;
result res :: <leveldb-iterator-t*>;
c-name: "leveldb_create_iterator";
end;
define C-pointer-type <leveldb-snapshot-t*> => <leveldb-snapshot-t>;
define C-function leveldb-create-snapshot
input parameter db_ :: <leveldb-t*>;
result res :: <leveldb-snapshot-t*>;
c-name: "leveldb_create_snapshot";
end;
define C-function leveldb-release-snapshot
input parameter db_ :: <leveldb-t*>;
input parameter snapshot_ :: <leveldb-snapshot-t*>;
c-name: "leveldb_release_snapshot";
end;
define C-function leveldb-property-value
input parameter db_ :: <leveldb-t*>;
input parameter propname_ :: <C-string>;
result res :: <C-string>;
c-name: "leveldb_property_value";
end;
define constant <uint64-t> = <C-unsigned-long>;
define C-pointer-type <uint64-t*> => <uint64-t>;
define C-function leveldb-approximate-sizes
input parameter db_ :: <leveldb-t*>;
input parameter num-ranges_ :: <C-signed-int>;
input parameter range-start-key_ :: <char**>;
input parameter range-start-key-len_ :: <size-t*>;
input parameter range-limit-key_ :: <char**>;
input parameter range-limit-key-len_ :: <size-t*>;
input parameter sizes_ :: <uint64-t*>;
c-name: "leveldb_approximate_sizes";
end;
define C-function %leveldb-compact-range
input parameter db_ :: <leveldb-t*>;
input parameter start-key_ :: <C-string>;
input parameter start-key-len_ :: <size-t>;
input parameter limit-key_ :: <C-string>;
input parameter limit-key-len_ :: <size-t>;
c-name: "leveldb_compact_range";
end;
define C-function %leveldb-destroy-db
input parameter options_ :: <leveldb-options-t*>;
input parameter name_ :: <C-string>;
output parameter errptr_ :: <char**>;
c-name: "leveldb_destroy_db";
end;
define C-function %leveldb-repair-db
input parameter options_ :: <leveldb-options-t*>;
input parameter name_ :: <C-string>;
output parameter errptr_ :: <char**>;
c-name: "leveldb_repair_db";
end;
define C-function leveldb-iter-destroy
input parameter arg1_ :: <leveldb-iterator-t*>;
c-name: "leveldb_iter_destroy";
end;
define C-function leveldb-iter-valid
input parameter arg1_ :: <leveldb-iterator-t*>;
result res :: <C-boolean>;
c-name: "leveldb_iter_valid";
end;
define C-function leveldb-iter-seek-to-first
input parameter arg1_ :: <leveldb-iterator-t*>;
c-name: "leveldb_iter_seek_to_first";
end;
define C-function leveldb-iter-seek-to-last
input parameter arg1_ :: <leveldb-iterator-t*>;
c-name: "leveldb_iter_seek_to_last";
end;
define C-function %leveldb-iter-seek
input parameter arg1_ :: <leveldb-iterator-t*>;
input parameter k_ :: <C-string>;
input parameter klen_ :: <size-t>;
c-name: "leveldb_iter_seek";
end;
define C-function leveldb-iter-next
input parameter arg1_ :: <leveldb-iterator-t*>;
c-name: "leveldb_iter_next";
end;
define C-function leveldb-iter-prev
input parameter arg1_ :: <leveldb-iterator-t*>;
c-name: "leveldb_iter_prev";
end;
define C-function %leveldb-iter-key
input parameter arg1_ :: <leveldb-iterator-t*>;
output parameter klen_ :: <size-t*>;
result res :: <C-string>;
c-name: "leveldb_iter_key";
end;
define C-function %leveldb-iter-value
input parameter arg1_ :: <leveldb-iterator-t*>;
output parameter vlen_ :: <size-t*>;
result res :: <C-string>;
c-name: "leveldb_iter_value";
end;
define C-function leveldb-iter-get-error
input parameter arg1_ :: <leveldb-iterator-t*>;
output parameter errptr_ :: <char**>;
c-name: "leveldb_iter_get_error";
end;
define C-function leveldb-writebatch-create
result res :: <leveldb-writebatch-t*>;
c-name: "leveldb_writebatch_create";
end;
define C-function leveldb-writebatch-destroy
input parameter arg1_ :: <leveldb-writebatch-t*>;
c-name: "leveldb_writebatch_destroy";
end;
define C-function leveldb-writebatch-clear
input parameter arg1_ :: <leveldb-writebatch-t*>;
c-name: "leveldb_writebatch_clear";
end;
define C-function %leveldb-writebatch-put
input parameter arg1_ :: <leveldb-writebatch-t*>;
input parameter key_ :: <C-string>;
input parameter klen_ :: <size-t>;
input parameter val_ :: <C-buffer-offset>;
input parameter vlen_ :: <size-t>;
c-name: "leveldb_writebatch_put";
end;
define C-function %leveldb-writebatch-delete
input parameter arg1_ :: <leveldb-writebatch-t*>;
input parameter key_ :: <C-string>;
input parameter klen_ :: <size-t>;
c-name: "leveldb_writebatch_delete";
end;
define constant <put> = <C-function-pointer>;
define constant <deleted> = <C-function-pointer>;
define C-function leveldb-writebatch-iterate
input parameter arg1_ :: <leveldb-writebatch-t*>;
input parameter state_ :: <C-void*>;
input parameter put_ :: <put>;
input parameter deleted_ :: <deleted>;
c-name: "leveldb_writebatch_iterate";
end;
define C-function leveldb-options-create
result res :: <leveldb-options-t*>;
c-name: "leveldb_options_create";
end;
define C-function leveldb-options-destroy
input parameter arg1_ :: <leveldb-options-t*>;
c-name: "leveldb_options_destroy";
end;
define C-pointer-type <leveldb-comparator-t*> => <leveldb-comparator-t>;
define C-function leveldb-options-set-comparator
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <leveldb-comparator-t*>;
c-name: "leveldb_options_set_comparator";
end;
define C-pointer-type <leveldb-filterpolicy-t*> => <leveldb-filterpolicy-t>;
define C-function leveldb-options-set-filter-policy
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <leveldb-filterpolicy-t*>;
c-name: "leveldb_options_set_filter_policy";
end;
define C-function leveldb-options-set-create-if-missing
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <C-boolean>;
c-name: "leveldb_options_set_create_if_missing";
end;
define C-function leveldb-options-set-error-if-exists
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <C-boolean>;
c-name: "leveldb_options_set_error_if_exists";
end;
define C-function leveldb-options-set-paranoid-checks
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <C-boolean>;
c-name: "leveldb_options_set_paranoid_checks";
end;
define C-pointer-type <leveldb-env-t*> => <leveldb-env-t>;
define C-function leveldb-options-set-env
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <leveldb-env-t*>;
c-name: "leveldb_options_set_env";
end;
define C-pointer-type <leveldb-logger-t*> => <leveldb-logger-t>;
define C-function leveldb-options-set-info-log
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <leveldb-logger-t*>;
c-name: "leveldb_options_set_info_log";
end;
define C-function leveldb-options-set-write-buffer-size
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <size-t>;
c-name: "leveldb_options_set_write_buffer_size";
end;
define C-function leveldb-options-set-max-open-files
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <C-signed-int>;
c-name: "leveldb_options_set_max_open_files";
end;
define C-pointer-type <leveldb-cache-t*> => <leveldb-cache-t>;
define C-function leveldb-options-set-cache
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <leveldb-cache-t*>;
c-name: "leveldb_options_set_cache";
end;
define C-function leveldb-options-set-block-size
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <size-t>;
c-name: "leveldb_options_set_block_size";
end;
define C-function leveldb-options-set-block-restart-interval
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <C-signed-int>;
c-name: "leveldb_options_set_block_restart_interval";
end;
define constant $leveldb-no-compression = 0;
define constant $leveldb-snappy-compression = 1;
define C-function leveldb-options-set-compression
input parameter arg1_ :: <leveldb-options-t*>;
input parameter arg2_ :: <C-signed-int>;
c-name: "leveldb_options_set_compression";
end;
define constant <destructor> = <C-function-pointer>;
define constant <compare> = <C-function-pointer>;
define constant <name> = <C-function-pointer>;
define C-function leveldb-comparator-create
input parameter state_ :: <C-void*>;
input parameter destructor_ :: <destructor>;
input parameter compare_ :: <compare>;
input parameter name_ :: <name>;
result res :: <leveldb-comparator-t*>;
c-name: "leveldb_comparator_create";
end;
define C-function leveldb-comparator-destroy
input parameter arg1_ :: <leveldb-comparator-t*>;
c-name: "leveldb_comparator_destroy";
end;
define constant <destructor> = <C-function-pointer>;
define constant <create-filter> = <C-function-pointer>;
define constant <key-may-match> = <C-function-pointer>;
define constant <name> = <C-function-pointer>;
define C-function leveldb-filterpolicy-create
input parameter state_ :: <C-void*>;
input parameter destructor_ :: <destructor>;
input parameter create-filter_ :: <create-filter>;
input parameter key-may-match_ :: <key-may-match>;
input parameter name_ :: <name>;
result res :: <leveldb-filterpolicy-t*>;
c-name: "leveldb_filterpolicy_create";
end;
define C-function leveldb-filterpolicy-destroy
input parameter arg1_ :: <leveldb-filterpolicy-t*>;
c-name: "leveldb_filterpolicy_destroy";
end;
define C-function leveldb-filterpolicy-create-bloom
input parameter bits-per-key_ :: <C-signed-int>;
result res :: <leveldb-filterpolicy-t*>;
c-name: "leveldb_filterpolicy_create_bloom";
end;
define C-function leveldb-readoptions-create
result res :: <leveldb-readoptions-t*>;
c-name: "leveldb_readoptions_create";
end;
define C-function leveldb-readoptions-destroy
input parameter arg1_ :: <leveldb-readoptions-t*>;
c-name: "leveldb_readoptions_destroy";
end;
define C-function leveldb-readoptions-set-verify-checksums
input parameter arg1_ :: <leveldb-readoptions-t*>;
input parameter arg2_ :: <C-boolean>;
c-name: "leveldb_readoptions_set_verify_checksums";
end;
define C-function leveldb-readoptions-set-fill-cache
input parameter arg1_ :: <leveldb-readoptions-t*>;
input parameter arg2_ :: <C-boolean>;
c-name: "leveldb_readoptions_set_fill_cache";
end;
define C-function leveldb-readoptions-set-snapshot
input parameter arg1_ :: <leveldb-readoptions-t*>;
input parameter arg2_ :: <leveldb-snapshot-t*>;
c-name: "leveldb_readoptions_set_snapshot";
end;
define C-function leveldb-writeoptions-create
result res :: <leveldb-writeoptions-t*>;
c-name: "leveldb_writeoptions_create";
end;
define C-function leveldb-writeoptions-destroy
input parameter arg1_ :: <leveldb-writeoptions-t*>;
c-name: "leveldb_writeoptions_destroy";
end;
define C-function leveldb-writeoptions-set-sync
input parameter arg1_ :: <leveldb-writeoptions-t*>;
input parameter arg2_ :: <C-boolean>;
c-name: "leveldb_writeoptions_set_sync";
end;
define C-function leveldb-cache-create-lru
input parameter capacity_ :: <size-t>;
result res :: <leveldb-cache-t*>;
c-name: "leveldb_cache_create_lru";
end;
define C-function leveldb-cache-destroy
input parameter cache_ :: <leveldb-cache-t*>;
c-name: "leveldb_cache_destroy";
end;
define C-function leveldb-create-default-env
result res :: <leveldb-env-t*>;
c-name: "leveldb_create_default_env";
end;
define C-function leveldb-env-destroy
input parameter arg1_ :: <leveldb-env-t*>;
c-name: "leveldb_env_destroy";
end;
define C-function leveldb-free
input parameter ptr_ :: <C-void*>;
c-name: "leveldb_free";
end;
define C-function leveldb-major-version
result res :: <C-signed-int>;
c-name: "leveldb_major_version";
end;
define C-function leveldb-minor-version
result res :: <C-signed-int>;
c-name: "leveldb_minor_version";
end;
define function leveldb-open (options :: <leveldb-options-t*>, name :: <byte-string>) => (db :: <leveldb-t*>)
let (db, errormsg) = %leveldb-open(options, name);
unless (null-pointer?(errormsg))
error(errormsg);
end;
db
end;
define method leveldb-put (db :: <leveldb-t*>, options :: <leveldb-writeoptions-t*>, key :: <string>, data) => ()
let errormsg = %leveldb-put(db, options, key, key.size, byte-storage-address(data), data.size);
unless (null-pointer?(errormsg))
error(errormsg);
end;
end;
define method leveldb-delete (db :: <leveldb-t*>, options :: <leveldb-writeoptions-t*>, key :: <string>) => ()
let errormsg = %leveldb-delete(db, options, key, key.size);
unless (null-pointer?(errormsg))
error(errormsg);
end;
end;
define method leveldb-write (db :: <leveldb-t*>, options :: <leveldb-writeoptions-t*>, batch :: <leveldb-writebatch-t*>) => ()
let errormsg = %leveldb-write(db, options, batch);
unless (null-pointer?(errormsg))
error(errormsg);
end;
end;
define method leveldb-get (db :: <leveldb-t*>, options :: <leveldb-readoptions-t*>, key :: <string>) => (bytes :: <byte-vector>)
let (data, length, errormsg) = %leveldb-get(db, options, key, key.size);
unless (null-pointer?(errormsg))
error(errormsg);
end;
let bytes = make(<byte-vector>, size: length);
copy-bytes(bytes, 0, data, 0, length);
bytes
end;
define function leveldb-compact-range (db :: <leveldb-t*>, start-key :: <string>, limit-key :: <string>) => ()
%leveldb-compact-range(db, start-key, start-key.size, limit-key, limit-key.size)
end;
define function leveldb-destroy-db (options :: <leveldb-options-t*>, name :: <byte-string>) => ()
let errormsg = %leveldb-destroy-db(options, name);
unless (null-pointer?(errormsg))
error(errormsg);
end;
end;
define function leveldb-repair-db (options :: <leveldb-options-t*>, name :: <byte-string>) => ()
let errormsg = %leveldb-repair-db(options, name);
unless (null-pointer?(errormsg))
error(errormsg);
end;
end;
define method leveldb-iter-key (iter :: <leveldb-iterator-t*>) => (key :: <string>)
let (key-data, length) = %leveldb-iter-key(iter);
let key = make(<string>, size: length);
copy-bytes(key, 0, key-data, 0, length);
key
end;
define method leveldb-iter-seek (iter :: <leveldb-iterator-t*>, key :: <string>) => ()
%leveldb-iter-seek(iter, key, key.size)
end;
define method leveldb-iter-value (iter :: <leveldb-iterator-t*>) => (value :: <byte-vector>)
let (value-data, length) = %leveldb-iter-value(iter);
let value = make(<byte-vector>, size: length);
copy-bytes(value, 0, value-data, 0, length);
value
end;
define method leveldb-writebatch-put (batch :: <leveldb-writebatch-t*>, key :: <string>, data) => ()
%leveldb-writebatch-put(batch, key, key.size, byte-storage-address(data), data.size);
end;
define method leveldb-writebatch-delete (batch :: <leveldb-writebatch-t*>, key :: <string>) => ()
%leveldb-writebatch-delete(batch, key, key.size);
end;