forked from zielmicha/leveldb.nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveldbstatic.nim
505 lines (438 loc) · 15.1 KB
/
leveldbstatic.nim
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
## A LevelDB_ wrapper for Nim in a Nim friendly way.
##
## LevelDB is a fast and simple key/value data storage library built
## by Google that provides an ordered mapping from string keys to
## string values.
##
## .. _LevelDB: https://github.com/google/leveldb
##
## Create a database:
##
## .. code-block:: Nim
## import leveldbstatic
## import options
##
## var db = leveldb.open("/tmp/mydata")
##
## Read or modify the database content:
##
## .. code-block:: Nim
##
## assert db.getOrDefault("nothing", "") == ""
##
## db.put("hello", "world")
## db.put("bin", "GIF89a\1\0")
## echo db.get("hello")
## assert db.get("hello").isSome()
##
## var key, val = ""
## for key, val in db.iter():
## echo key, ": ", repr(val)
##
## db.delete("hello")
## assert db.get("hello").isNone()
##
## Batch writes:
##
## .. code-block:: Nim
## let batch = newBatch()
## for i in 1..10:
## batch.put("key" & $i, $i)
## batch.delete("bin")
## db.write(batch)
##
## Iterate over subset of database content:
##
## .. code-block:: Nim
## for key, val in db.iterPrefix(prefix = "key1"):
## echo key, ": ", val
## for key, val in db.iter(seek = "key3", reverse = true):
## echo key, ": ", val
##
## db.close()
import options, os, strutils
import leveldbstatic/raw
type
LevelDb* = ref object
path*: string
db: ptr leveldb_t
cache: ptr leveldb_cache_t
readOptions: ptr leveldb_readoptions_t
syncWriteOptions: ptr leveldb_writeoptions_t
asyncWriteOptions: ptr leveldb_writeoptions_t
LevelDbWriteBatch* = ref object
## Write batches for bulk data modification.
batch: ptr leveldb_writebatch_t
CompressionType* = enum
## No compression or using Snappy_ algorithm (default).
##
## .. _Snappy: http://google.github.io/snappy/
ctNoCompression = leveldb_no_compression,
ctSnappyCompression = leveldb_snappy_compression
LevelDbException* = object of CatchableError
IterNext* = proc(): (string, string) {.gcsafe, closure, raises: [LevelDbException].}
IterDispose* = proc() {.gcsafe, closure, raises: [].}
LevelDbQueryIter* = ref object
finished*: bool
next*: IterNext
dispose*: IterDispose
const
version* = block:
const configFile = "leveldbstatic.nimble"
const sourcePath = currentSourcePath()
const parentConfig = sourcePath.parentDir.parentDir / configFile
const localConfig = sourcePath.parentDir / configFile
var content: string
if fileExists(parentConfig):
content = staticRead(parentConfig)
else:
content = staticRead(localConfig)
var version_line: string
for line in content.split("\L"):
if line.startsWith("version"):
version_line = line
break
let raw = version_line.split("=", maxsplit = 1)[1]
raw.strip().strip(chars = {'"'})
levelDbTrue = uint8(1)
levelDbFalse = uint8(0)
proc free(p: pointer) {.importc.}
proc checkError(errPtr: cstring) =
if errPtr != nil:
defer: free(errPtr)
raise newException(LevelDbException, $errPtr)
proc getLibVersion*(): (int, int) =
## Get the version of leveldb C library.
result[0] = leveldb_major_version()
result[1] = leveldb_minor_version()
proc close*(self: LevelDb) =
## Closes the database.
##
## See also:
## * `open proc <#open%2Cstring%2Cint%2Cint%2Cint>`_
if self.db == nil:
return
leveldb_close(self.db)
leveldb_writeoptions_destroy(self.syncWriteOptions)
leveldb_writeoptions_destroy(self.asyncWriteOptions)
leveldb_readoptions_destroy(self.readOptions)
if self.cache != nil:
leveldb_cache_destroy(self.cache)
self.cache = nil
self.db = nil
proc open*(path: string, create = true, reuse = true, paranoidChecks = true,
compressionType = ctSnappyCompression,
cacheCapacity = 0, blockSize = 4 * 1024, writeBufferSize = 4*1024*1024,
maxOpenFiles = 1000, maxFileSize = 2 * 1024 * 1024,
blockRestartInterval = 16): LevelDb =
## Opens a database.
##
## Raises `LevelDbException` if corruption detected in the database.
##
## See also:
## * `close proc <#close%2CLevelDb>`_
new(result, close)
let options = leveldb_options_create()
defer: leveldb_options_destroy(options)
result.syncWriteOptions = leveldb_writeoptions_create()
leveldb_writeoptions_set_sync(result.syncWriteOptions, levelDbTrue)
result.asyncWriteOptions = leveldb_writeoptions_create()
leveldb_writeoptions_set_sync(result.asyncWriteOptions, levelDbFalse)
result.readOptions = leveldb_readoptions_create()
if create:
leveldb_options_set_create_if_missing(options, levelDbTrue)
else:
leveldb_options_set_create_if_missing(options, levelDbFalse)
if reuse:
leveldb_options_set_error_if_exists(options, levelDbFalse)
else:
leveldb_options_set_error_if_exists(options, levelDbTrue)
if paranoidChecks:
leveldb_options_set_paranoid_checks(options, levelDbTrue)
else:
leveldb_options_set_paranoid_checks(options, levelDbFalse)
leveldb_options_set_write_buffer_size(options, writeBufferSize.csize_t)
leveldb_options_set_block_size(options, blockSize.csize_t)
leveldb_options_set_max_open_files(options, cast[cint](maxOpenFiles))
leveldb_options_set_max_file_size(options, maxFileSize.csize_t)
leveldb_options_set_block_restart_interval(options,
cast[cint](blockRestartInterval))
leveldb_options_set_compression(options, cast[cint](compressionType.ord))
if cacheCapacity > 0:
let cache = leveldb_cache_create_lru(cacheCapacity.csize_t)
leveldb_options_set_cache(options, cache)
result.cache = cache
var errPtr: cstring = nil
result.path = path
result.db = leveldb_open(options, path, addr errPtr)
checkError(errPtr)
proc put*(self: LevelDb, key: string, value: string, sync = false) =
## Set a `value` for the specified `key`.
##
## By default, `sync` is turned off, each write to leveldb is asynchronous.
## Unless reboot, a crash of just the writing process will not cause any
## loss since even when `sync` is false.
##
## See also:
## * `put proc <#put%2CLevelDbWriteBatch%2Cstring%2Cstring>`_
runnableExamples:
let db = leveldb.open("/tmp/test")
db.put("hello", "world")
db.close()
assert self.db != nil
var errPtr: cstring = nil
let writeOptions = if sync: self.syncWriteOptions else: self.asyncWriteOptions
leveldb_put(self.db, writeOptions,
key, key.len.csize_t, value, value.len.csize_t, addr errPtr)
checkError(errPtr)
proc newString(cstr: cstring, length: csize_t): string =
if length > 0:
result = newString(length)
copyMem(unsafeAddr result[0], cstr, length)
else:
result = ""
proc get*(self: LevelDb, key: string): Option[string] =
## Get the value for the specified `key`.
##
## See also:
## * `getOrDefault proc <#getOrDefault%2CLevelDb%2Cstring>`_
runnableExamples:
let db = leveldb.open("/tmp/test")
db.put("hello", "world")
echo db.get("hello")
db.close()
var size: csize_t
var errPtr: cstring = nil
let s = leveldb_get(self.db, self.readOptions, key, key.len.csize_t, addr size, addr errPtr)
checkError(errPtr)
if s == nil:
result = none(string)
else:
result = some(newString(s, size))
free(s)
proc getOrDefault*(self: LevelDb, key: string, default = ""): string =
## Get the value for the specified `key`, or `default` if no value was set.
##
## See also:
## * `get proc <#get%2CLevelDb%2Cstring>`_
runnableExamples:
let db = leveldb.open("/tmp/test")
doAssert db.getOrDefault("what?", "nothing") == "nothing"
db.close()
let val = self.get(key)
if val.isNone():
result = default
else:
result = val.get()
proc delete*(self: LevelDb, key: string, sync = false) =
## Delete the key/value pair for the specified key.
##
## See also:
## * `delete proc <#delete%2CLevelDbWriteBatch%2Cstring>`_
var errPtr: cstring = nil
let writeOptions = if sync: self.syncWriteOptions else: self.asyncWriteOptions
leveldb_delete(self.db, writeOptions, key, key.len.csize_t, addr errPtr)
checkError(errPtr)
proc destroy*(self: LevelDbWriteBatch) =
## Destroys this batch.
##
## See also:
## * `newBatch proc <#newBatch>`_
if self.batch == nil:
return
leveldb_writebatch_destroy(self.batch)
self.batch = nil
proc newBatch*(): LevelDbWriteBatch =
## Creates a new database write batch.
##
## See also:
## * `write proc <#write%2CLevelDb%2CLevelDbWriteBatch>`_
## * `put proc <#put%2CLevelDbWriteBatch%2Cstring%2Cstring>`_
## * `delete proc <#delete%2CLevelDbWriteBatch%2Cstring>`_
## * `append proc <#append%2CLevelDbWriteBatch%2CLevelDbWriteBatch>`_
## * `clear proc <#clear%2CLevelDbWriteBatch>`_
## * `destroy proc <#destroy%2CLevelDbWriteBatch>`_
runnableExamples:
let db = leveldb.open("/tmp/test")
let batch = newBatch()
for i in 1..10:
batch.put("key" & $i, $i)
batch.delete("another")
db.write(batch)
db.close()
new(result, destroy)
result.batch = leveldb_writebatch_create()
proc put*(self: LevelDbWriteBatch, key: string, value: string, sync = false) =
## Set a `value` for the specified `key`.
## Same as `put <#put%2CLevelDb%2Cstring%2Cstring>`_ but operates on the
## write batch instead.
##
## See also:
## * `put proc <#put%2CLevelDb%2Cstring%2Cstring>`_
## * `newBatch proc <#newBatch>`_
leveldb_writebatch_put(self.batch, key, key.len.csize_t, value, value.len.csize_t)
proc append*(self, source: LevelDbWriteBatch) =
## Merges the `source` batch into this batch.
##
## See also:
## * `newBatch proc <#newBatch>`_
leveldb_writebatch_append(self.batch, source.batch)
proc delete*(self: LevelDbWriteBatch, key: string) =
## Delete the key/value pair for the specified `key`.
## Same as `delete <#delete%2CLevelDb%2Cstring>`_ but operates on the
## write batch instead.
##
## See also:
## * `delete proc <#delete%2CLevelDb%2Cstring>`_
## * `newBatch proc <#newBatch>`_
leveldb_writebatch_delete(self.batch, key, key.len.csize_t)
proc clear*(self: LevelDbWriteBatch) =
## Clear all updates buffered in this batch.
##
## See also:
## * `newBatch proc <#newBatch>`_
leveldb_writebatch_clear(self.batch)
proc write*(self: LevelDb, batch: LevelDbWriteBatch) =
## Write apply the given `batch` to the database.
##
## See also:
## * `newBatch proc <#newBatch>`_
var errPtr: cstring = nil
leveldb_write(self.db, self.syncWriteOptions, batch.batch, addr errPtr)
checkError(errPtr)
proc getIterData(iterPtr: ptr leveldb_iterator_t): (string, string) =
var len: csize_t
var str: cstring
str = leveldb_iter_key(iterPtr, addr len)
result[0] = newString(str, len)
str = leveldb_iter_value(iterPtr, addr len)
result[1] = newString(str, len)
iterator iter*(self: LevelDb, seek: string = "", reverse: bool = false): (
string, string) =
## Iterate all key/value pairs in the database from the first one or
## the specified key `seek`.
## By default, the ordering will be lexicographic byte-wise ordering
## with leveldb builtin comparator, unless `reverse` set to `true`.
##
## See also:
## * `iterPrefix iterator <#iterPrefix.i%2CLevelDb%2Cstring>`_
## * `iterRange iterator <#iterRange.i%2CLevelDb%2Cstring%2Cstring>`_
var iterPtr = leveldb_create_iterator(self.db, self.readOptions)
defer: leveldb_iter_destroy(iterPtr)
if seek.len > 0:
leveldb_iter_seek(iterPtr, seek, seek.len.csize_t)
else:
if reverse:
leveldb_iter_seek_to_last(iterPtr)
else:
leveldb_iter_seek_to_first(iterPtr)
while true:
if leveldb_iter_valid(iterPtr) == levelDbFalse:
break
var (key, value) = getIterData(iterPtr)
var err: cstring = nil
leveldb_iter_get_error(iterPtr, addr err)
checkError(err)
yield (key, value)
if reverse:
leveldb_iter_prev(iterPtr)
else:
leveldb_iter_next(iterPtr)
iterator iterPrefix*(self: LevelDb, prefix: string): (string, string) =
## Iterate subset key/value pairs in the database with a particular `prefix`.
##
## See also:
## * `iter iterator <#iter.i%2CLevelDb%2Cstring%2Cbool>`_
## * `iterRange iterator <#iterRange.i%2CLevelDb%2Cstring%2Cstring>`_
for key, value in iter(self, prefix, reverse = false):
if key.startsWith(prefix):
yield (key, value)
else:
break
iterator iterRange*(self: LevelDb, start, limit: string): (string, string) =
## Yields all key/value pairs between the `start` and `limit` keys
## (inclusive) in the database.
##
## See also:
## * `iter iterator <#iter.i%2CLevelDb%2Cstring%2Cbool>`_
## * `iterPrefix iterator <#iterPrefix.i%2CLevelDb%2Cstring>`_
let reverse: bool = limit < start
for key, value in iter(self, start, reverse = reverse):
if reverse:
if key < limit:
break
else:
if key > limit:
break
yield (key, value)
proc getIterKey(iterPtr: ptr leveldb_iterator_t): string =
var len: csize_t
var str: cstring
str = leveldb_iter_key(iterPtr, addr len)
return newString(str, len)
proc getIterValue(iterPtr: ptr leveldb_iterator_t): string =
var len: csize_t
var str: cstring
str = leveldb_iter_value(iterPtr, addr len)
return newString(str, len)
proc seekToQueryStart(iterPtr: ptr leveldb_iterator_t, prefix: string, skip: int) =
if prefix.len > 0:
leveldb_iter_seek(iterPtr, prefix, prefix.len.csize_t)
else:
leveldb_iter_seek_to_first(iterPtr)
for i in 0..<skip:
leveldb_iter_next(iterPtr)
proc closeIter(iter: LevelDbQueryIter, iterPtr: ptr leveldb_iterator_t) =
iter.finished = true
leveldb_iter_destroy(iterPtr)
proc queryIter*(self: LevelDb, prefix: string = "", keysOnly: bool = false, skip: int = 0, limit: int = 0): LevelDbQueryIter =
var iterPtr = leveldb_create_iterator(self.db, self.readOptions)
seekToQueryStart(iterPtr, prefix, skip)
var
iter = LevelDbQueryIter()
remaining = limit
let emptyResponse = ("", "")
proc getNext(): (string, string) {.gcsafe, closure.} =
if iter.finished:
return emptyResponse
if leveldb_iter_valid(iterPtr) == levelDbFalse or (limit > 0 and remaining == 0):
iter.closeIter(iterPtr)
return emptyResponse
if limit > 0:
dec remaining
let
keyStr = getIterKey(iterPtr)
valueStr = if keysOnly: "" else: getIterValue(iterPtr)
var err: cstring = nil
leveldb_iter_get_error(iterPtr, addr err)
checkError(err)
leveldb_iter_next(iterPtr)
if prefix.len > 0:
if keyStr.startsWith(prefix):
return (keyStr, valueStr)
else:
iter.closeIter(iterPtr)
return emptyResponse
else:
return (keyStr, valueStr)
proc dispose() {.gcsafe, closure.} =
if not iter.finished:
iter.closeIter(iterPtr)
iter.finished = false
iter.next = getNext
iter.dispose = dispose
return iter
proc removeDb*(name: string) =
## Remove the database `name`.
var err: cstring = nil
let options = leveldb_options_create()
leveldb_destroy_db(options, name, addr err)
checkError(err)
proc repairDb*(name: string) =
## Repairs the corrupted database `name`.
let options = leveldb_options_create()
leveldb_options_set_create_if_missing(options, levelDbFalse)
leveldb_options_set_error_if_exists(options, levelDbFalse)
var errPtr: cstring = nil
leveldb_repair_db(options, name, addr errPtr)
checkError(errPtr)