Iterates with the prefix of keys

This commit is contained in:
Xie Yanbo 2019-11-16 17:42:40 +08:00
parent 051b3aeda4
commit f63abb769a
2 changed files with 11 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import options, leveldb/raw
import options, strutils, leveldb/raw
type
LevelDb* = ref object
@ -122,6 +122,11 @@ iterator iter*(self: LevelDb, seek: string = "", reverse: bool = false): (
else:
leveldb_iter_next(iterPtr)
iterator iterPrefix*(self: LevelDb, prefix: string = ""): (string, string) =
for (key, value) in iter(self, prefix):
if key.startsWith(prefix):
yield (key, value)
proc removeDb*(name: string) =
var err: cstring = nil
let options = leveldb_options_create()

View File

@ -52,3 +52,8 @@ suite "leveldb":
initData(db)
check(toSeq(db.iter(seek = "ab", reverse = true)) ==
@[("ba", "2"), ("aa", "1")])
test "iter prefix":
initData(db)
check(toSeq(db.iterPrefix(prefix = "b")) ==
@[("ba", "2"), ("bb", "3")])