mirror of
https://github.com/codex-storage/nim-leveldb.git
synced 2025-02-19 19:08:34 +00:00
support cache option
This commit is contained in:
parent
594a5deb83
commit
ebc0544b8f
@ -6,6 +6,7 @@ type
|
||||
syncWriteOptions: ptr leveldb_writeoptions_t
|
||||
asyncWriteOptions: ptr leveldb_writeoptions_t
|
||||
readOptions: ptr leveldb_readoptions_t
|
||||
cache: ptr leveldb_cache_t
|
||||
|
||||
LevelDbBatch* = ref object
|
||||
batch: ptr leveldb_writebatch_t
|
||||
@ -36,9 +37,12 @@ proc close*(self: LevelDb) =
|
||||
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): LevelDb =
|
||||
proc open*(path: string, cacheCapacity = 0): LevelDb =
|
||||
new(result, close)
|
||||
|
||||
let options = leveldb_options_create()
|
||||
@ -51,6 +55,11 @@ proc open*(path: string): LevelDb =
|
||||
leveldb_writeoptions_set_sync(result.asyncWriteOptions, levelDbFalse)
|
||||
result.readOptions = leveldb_readoptions_create()
|
||||
|
||||
if cacheCapacity > 0:
|
||||
let cache = leveldb_cache_create_lru(cacheCapacity)
|
||||
leveldb_options_set_cache(options, cache)
|
||||
result.cache = cache
|
||||
|
||||
var errPtr: cstring = nil
|
||||
result.db = leveldb_open(options, path, addr errPtr)
|
||||
checkError(errPtr)
|
||||
|
@ -104,3 +104,8 @@ suite "leveldb":
|
||||
batch.put("b", "2")
|
||||
db.write(batch)
|
||||
check(toSeq(db.iter()) == @[("b", "2")])
|
||||
|
||||
test "open with cache":
|
||||
let ldb = leveldb.open(dbName & "-cache", cacheCapacity = 100000)
|
||||
ldb.put("a", "1")
|
||||
check(toSeq(ldb.iter()) == @[("a", "1")])
|
||||
|
Loading…
x
Reference in New Issue
Block a user