Merge pull request #9 from xyb/batch

add batch procedure and cache option
This commit is contained in:
Michał Zieliński 2019-12-14 16:12:45 +01:00 committed by GitHub
commit 3c0b6d9b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 1 deletions

View File

@ -6,6 +6,10 @@ 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
LevelDbException* = object of Exception
@ -33,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()
@ -48,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)
@ -82,6 +94,33 @@ proc delete*(self: LevelDb, key: string, sync = true) =
leveldb_delete(self.db, writeOptions, key, key.len, addr errPtr)
checkError(errPtr)
proc destroy*(self: LevelDbBatch) =
if self.batch == nil:
return
leveldb_writebatch_destroy(self.batch)
self.batch = nil
proc newBatch*(): LevelDbBatch =
new(result, destroy)
result.batch = leveldb_writebatch_create()
proc put*(self: LevelDbBatch, key: string, value: string, sync = true) =
leveldb_writebatch_put(self.batch, key, key.len.csize, value, value.len.csize)
proc append*(self, source: LevelDbBatch) =
leveldb_writebatch_append(self.batch, source.batch)
proc delete*(self: LevelDbBatch, key: string) =
leveldb_writebatch_delete(self.batch, key, key.len.csize)
proc clear*(self: LevelDbBatch) =
leveldb_writebatch_clear(self.batch)
proc write*(self: LevelDb, batch: LevelDbBatch) =
var errPtr: cstring = nil
leveldb_write(self.db, self.syncWriteOptions, batch.batch, addr errPtr)
checkError(errPtr)
proc getIterData(iterPtr: ptr leveldb_iterator_t): (Option[string], Option[string]) =
var len: csize
var str: cstring

View File

@ -76,3 +76,36 @@ suite "leveldb":
db.put("\0z1", "\0ff")
db.put("z2\0", "ff\0")
check(toSeq(db.iter()) == @[("\0z1", "\0ff"), ("z2\0", "ff\0")])
test "batch":
db.put("a", "1")
db.put("b", "2")
let batch = newBatch()
batch.put("a", "10")
batch.put("c", "30")
batch.delete("b")
db.write(batch)
check(toSeq(db.iter()) == @[("a", "10"), ("c", "30")])
test "batch append":
let batch = newBatch()
let batch2 = newBatch()
batch.put("a", "1")
batch2.put("b", "2")
batch2.delete("a")
batch.append(batch2)
db.write(batch)
check(toSeq(db.iter()) == @[("b", "2")])
test "batch clear":
let batch = newBatch()
batch.put("a", "1")
batch.clear()
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")])