mirror of
https://github.com/logos-storage/nim-leveldb.git
synced 2026-01-05 23:23:12 +00:00
Iterates with the prefix of keys
This commit is contained in:
parent
051b3aeda4
commit
f63abb769a
@ -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()
|
||||
|
||||
@ -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")])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user