2020-02-24 15:53:44 +00:00
|
|
|
import options, os, strutils
|
|
|
|
import leveldb/raw
|
2016-05-17 19:28:34 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
LevelDb* = ref object
|
2020-02-12 15:29:11 +00:00
|
|
|
path*: string
|
2016-05-17 19:28:34 +00:00
|
|
|
db: ptr leveldb_t
|
2020-02-27 12:25:19 +00:00
|
|
|
cache: ptr leveldb_cache_t
|
|
|
|
readOptions: ptr leveldb_readoptions_t
|
2016-05-17 19:28:34 +00:00
|
|
|
syncWriteOptions: ptr leveldb_writeoptions_t
|
|
|
|
asyncWriteOptions: ptr leveldb_writeoptions_t
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
LevelDbWriteBatch* = ref object
|
2019-12-14 10:11:20 +00:00
|
|
|
batch: ptr leveldb_writebatch_t
|
|
|
|
|
2020-02-12 15:29:32 +00:00
|
|
|
CompressionType* = enum
|
|
|
|
ctNoCompression = leveldb_no_compression,
|
|
|
|
ctSnappyCompression = leveldb_snappy_compression
|
|
|
|
|
2016-05-17 19:28:34 +00:00
|
|
|
LevelDbException* = object of Exception
|
|
|
|
|
2019-11-06 15:48:35 +00:00
|
|
|
const
|
2020-02-19 11:11:11 +00:00
|
|
|
version* = block:
|
2020-02-23 07:43:12 +00:00
|
|
|
const configFile = "leveldb.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)
|
2020-02-19 11:11:11 +00:00
|
|
|
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 = {'"'})
|
|
|
|
|
2019-11-09 03:52:46 +00:00
|
|
|
levelDbTrue = uint8(1)
|
|
|
|
levelDbFalse = uint8(0)
|
2019-11-06 15:48:35 +00:00
|
|
|
|
2019-11-09 07:52:46 +00:00
|
|
|
proc free(p: pointer) {.importc.}
|
|
|
|
|
2016-05-17 19:28:34 +00:00
|
|
|
proc checkError(errPtr: cstring) =
|
|
|
|
if errPtr != nil:
|
|
|
|
defer: free(errPtr)
|
|
|
|
raise newException(LevelDbException, $errPtr)
|
|
|
|
|
2019-11-10 17:02:57 +00:00
|
|
|
proc getLibVersion*(): (int, int) =
|
|
|
|
result[0] = leveldb_major_version()
|
|
|
|
result[1] = leveldb_minor_version()
|
|
|
|
|
2016-05-17 19:28:34 +00:00
|
|
|
proc close*(self: LevelDb) =
|
|
|
|
if self.db == nil:
|
2019-11-06 15:48:35 +00:00
|
|
|
return
|
2016-05-17 19:28:34 +00:00
|
|
|
leveldb_close(self.db)
|
|
|
|
leveldb_writeoptions_destroy(self.syncWriteOptions)
|
|
|
|
leveldb_writeoptions_destroy(self.asyncWriteOptions)
|
|
|
|
leveldb_readoptions_destroy(self.readOptions)
|
2019-12-14 10:11:56 +00:00
|
|
|
if self.cache != nil:
|
|
|
|
leveldb_cache_destroy(self.cache)
|
|
|
|
self.cache = nil
|
2016-05-17 19:28:34 +00:00
|
|
|
self.db = nil
|
|
|
|
|
2020-02-12 15:29:23 +00:00
|
|
|
proc open*(path: string, create = true, reuse = true, paranoidChecks = true,
|
2020-02-12 15:29:32 +00:00
|
|
|
compressionType = ctSnappyCompression,
|
2020-02-12 15:29:23 +00:00
|
|
|
cacheCapacity = 0, blockSize = 4 * 1024, writeBufferSize = 4*1024*1024,
|
|
|
|
maxOpenFiles = 1000, maxFileSize = 2 * 1024 * 1024,
|
2020-02-12 15:29:32 +00:00
|
|
|
blockRestartInterval = 16): LevelDb =
|
2016-05-17 19:28:34 +00:00
|
|
|
new(result, close)
|
|
|
|
|
|
|
|
let options = leveldb_options_create()
|
|
|
|
defer: leveldb_options_destroy(options)
|
|
|
|
|
|
|
|
result.syncWriteOptions = leveldb_writeoptions_create()
|
2019-11-06 15:48:35 +00:00
|
|
|
leveldb_writeoptions_set_sync(result.syncWriteOptions, levelDbTrue)
|
2016-05-17 19:28:34 +00:00
|
|
|
result.asyncWriteOptions = leveldb_writeoptions_create()
|
2019-11-06 15:48:35 +00:00
|
|
|
leveldb_writeoptions_set_sync(result.asyncWriteOptions, levelDbFalse)
|
2016-05-17 19:28:34 +00:00
|
|
|
result.readOptions = leveldb_readoptions_create()
|
|
|
|
|
2020-02-12 15:29:23 +00:00
|
|
|
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)
|
|
|
|
leveldb_options_set_block_size(options, blockSize)
|
|
|
|
leveldb_options_set_max_open_files(options, cast[cint](maxOpenFiles))
|
|
|
|
leveldb_options_set_max_file_size(options, maxFileSize)
|
|
|
|
leveldb_options_set_block_restart_interval(options,
|
2020-02-12 15:29:32 +00:00
|
|
|
cast[cint](blockRestartInterval))
|
|
|
|
leveldb_options_set_compression(options, cast[cint](compressionType.ord))
|
2020-02-12 15:29:23 +00:00
|
|
|
|
2019-12-14 10:11:56 +00:00
|
|
|
if cacheCapacity > 0:
|
|
|
|
let cache = leveldb_cache_create_lru(cacheCapacity)
|
|
|
|
leveldb_options_set_cache(options, cache)
|
|
|
|
result.cache = cache
|
|
|
|
|
2016-05-17 19:28:34 +00:00
|
|
|
var errPtr: cstring = nil
|
2020-02-12 15:29:11 +00:00
|
|
|
result.path = path
|
2016-05-17 19:28:34 +00:00
|
|
|
result.db = leveldb_open(options, path, addr errPtr)
|
|
|
|
checkError(errPtr)
|
|
|
|
|
2019-11-06 15:48:35 +00:00
|
|
|
proc put*(self: LevelDb, key: string, value: string, sync = true) =
|
2016-05-17 19:28:34 +00:00
|
|
|
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, value, value.len.csize, addr errPtr)
|
|
|
|
checkError(errPtr)
|
|
|
|
|
2019-11-16 12:24:33 +00:00
|
|
|
proc newString(cstr: cstring, length: csize): string =
|
2019-12-14 10:05:05 +00:00
|
|
|
if length > 0:
|
|
|
|
result = newString(length)
|
|
|
|
copyMem(unsafeAddr result[0], cstr, length)
|
|
|
|
else:
|
|
|
|
result = ""
|
2019-11-16 12:24:33 +00:00
|
|
|
|
2016-05-17 19:28:34 +00:00
|
|
|
proc get*(self: LevelDb, key: string): Option[string] =
|
|
|
|
var size: csize
|
|
|
|
var errPtr: cstring = nil
|
|
|
|
let s = leveldb_get(self.db, self.readOptions, key, key.len, addr size, addr errPtr)
|
|
|
|
checkError(errPtr)
|
|
|
|
|
|
|
|
if s == nil:
|
|
|
|
result = none(string)
|
|
|
|
else:
|
2019-12-14 10:05:05 +00:00
|
|
|
result = some(newString(s, size))
|
2016-05-17 19:28:34 +00:00
|
|
|
free(s)
|
|
|
|
|
2019-11-06 15:48:35 +00:00
|
|
|
proc delete*(self: LevelDb, key: string, sync = true) =
|
|
|
|
var errPtr: cstring = nil
|
|
|
|
let writeOptions = if sync: self.syncWriteOptions else: self.asyncWriteOptions
|
|
|
|
leveldb_delete(self.db, writeOptions, key, key.len, addr errPtr)
|
|
|
|
checkError(errPtr)
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc destroy*(self: LevelDbWriteBatch) =
|
2019-12-14 10:11:20 +00:00
|
|
|
if self.batch == nil:
|
|
|
|
return
|
|
|
|
leveldb_writebatch_destroy(self.batch)
|
|
|
|
self.batch = nil
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc newBatch*(): LevelDbWriteBatch =
|
2019-12-14 10:11:20 +00:00
|
|
|
new(result, destroy)
|
|
|
|
result.batch = leveldb_writebatch_create()
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc put*(self: LevelDbWriteBatch, key: string, value: string, sync = true) =
|
2019-12-14 10:11:20 +00:00
|
|
|
leveldb_writebatch_put(self.batch, key, key.len.csize, value, value.len.csize)
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc append*(self, source: LevelDbWriteBatch) =
|
2019-12-14 10:11:20 +00:00
|
|
|
leveldb_writebatch_append(self.batch, source.batch)
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc delete*(self: LevelDbWriteBatch, key: string) =
|
2019-12-14 10:11:20 +00:00
|
|
|
leveldb_writebatch_delete(self.batch, key, key.len.csize)
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc clear*(self: LevelDbWriteBatch) =
|
2019-12-14 10:11:20 +00:00
|
|
|
leveldb_writebatch_clear(self.batch)
|
|
|
|
|
2020-02-27 12:25:19 +00:00
|
|
|
proc write*(self: LevelDb, batch: LevelDbWriteBatch) =
|
2019-12-14 10:11:20 +00:00
|
|
|
var errPtr: cstring = nil
|
|
|
|
leveldb_write(self.db, self.syncWriteOptions, batch.batch, addr errPtr)
|
|
|
|
checkError(errPtr)
|
|
|
|
|
2019-12-14 10:05:05 +00:00
|
|
|
proc getIterData(iterPtr: ptr leveldb_iterator_t): (string, string) =
|
2019-11-06 15:48:35 +00:00
|
|
|
var len: csize
|
|
|
|
var str: cstring
|
|
|
|
|
|
|
|
str = leveldb_iter_key(iterPtr, addr len)
|
2019-12-14 10:05:05 +00:00
|
|
|
result[0] = newString(str, len)
|
2019-11-06 15:48:35 +00:00
|
|
|
|
|
|
|
str = leveldb_iter_value(iterPtr, addr len)
|
2019-12-14 10:05:05 +00:00
|
|
|
result[1] = newString(str, len)
|
2019-11-06 15:48:35 +00:00
|
|
|
|
|
|
|
iterator iter*(self: LevelDb, seek: string = "", reverse: bool = false): (
|
|
|
|
string, string) =
|
|
|
|
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)
|
|
|
|
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)
|
2019-12-14 10:05:05 +00:00
|
|
|
yield (key, value)
|
2019-11-06 15:48:35 +00:00
|
|
|
|
|
|
|
if reverse:
|
|
|
|
leveldb_iter_prev(iterPtr)
|
|
|
|
else:
|
|
|
|
leveldb_iter_next(iterPtr)
|
|
|
|
|
2019-11-16 09:42:48 +00:00
|
|
|
iterator iterPrefix*(self: LevelDb, prefix: string): (string, string) =
|
2020-02-18 15:46:17 +00:00
|
|
|
for key, value in iter(self, prefix, reverse = false):
|
2019-11-16 09:42:40 +00:00
|
|
|
if key.startsWith(prefix):
|
|
|
|
yield (key, value)
|
2019-11-16 11:35:02 +00:00
|
|
|
else:
|
|
|
|
break
|
2019-11-16 09:42:40 +00:00
|
|
|
|
2019-11-16 09:42:48 +00:00
|
|
|
iterator iterRange*(self: LevelDb, start, limit: string): (string, string) =
|
|
|
|
let reverse: bool = limit < start
|
2020-02-18 15:46:17 +00:00
|
|
|
for key, value in iter(self, start, reverse = reverse):
|
2019-11-16 09:42:48 +00:00
|
|
|
if reverse:
|
|
|
|
if key < limit:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if key > limit:
|
|
|
|
break
|
|
|
|
yield (key, value)
|
|
|
|
|
2019-11-09 03:52:46 +00:00
|
|
|
proc removeDb*(name: string) =
|
|
|
|
var err: cstring = nil
|
|
|
|
let options = leveldb_options_create()
|
|
|
|
leveldb_destroy_db(options, name, addr err)
|
|
|
|
checkError(err)
|
|
|
|
|
2020-02-12 15:29:11 +00:00
|
|
|
proc repairDb*(name: string) =
|
2019-11-10 17:02:57 +00:00
|
|
|
let options = leveldb_options_create()
|
2020-02-12 15:29:23 +00:00
|
|
|
leveldb_options_set_create_if_missing(options, levelDbFalse)
|
|
|
|
leveldb_options_set_error_if_exists(options, levelDbFalse)
|
2019-11-10 17:02:57 +00:00
|
|
|
var errPtr: cstring = nil
|
|
|
|
leveldb_repair_db(options, name, addr errPtr)
|
|
|
|
checkError(errPtr)
|