Iterates with range of keys
This commit is contained in:
parent
f63abb769a
commit
177190b311
|
@ -122,11 +122,22 @@ 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):
|
||||
iterator iterPrefix*(self: LevelDb, prefix: string): (string, string) =
|
||||
for (key, value) in iter(self, prefix, reverse = false):
|
||||
if key.startsWith(prefix):
|
||||
yield (key, value)
|
||||
|
||||
iterator iterRange*(self: LevelDb, start, limit: string): (string, string) =
|
||||
let reverse: bool = limit < start
|
||||
for (key, value) in iter(self, start, reverse = reverse):
|
||||
if reverse:
|
||||
if key < limit:
|
||||
break
|
||||
else:
|
||||
if key > limit:
|
||||
break
|
||||
yield (key, value)
|
||||
|
||||
proc removeDb*(name: string) =
|
||||
var err: cstring = nil
|
||||
let options = leveldb_options_create()
|
||||
|
|
|
@ -57,3 +57,13 @@ suite "leveldb":
|
|||
initData(db)
|
||||
check(toSeq(db.iterPrefix(prefix = "b")) ==
|
||||
@[("ba", "2"), ("bb", "3")])
|
||||
|
||||
test "iter range":
|
||||
initData(db)
|
||||
check(toSeq(db.iterRange(start = "a", limit = "ba")) ==
|
||||
@[("aa", "1"), ("ba", "2")])
|
||||
|
||||
test "iter range reverse":
|
||||
initData(db)
|
||||
check(toSeq(db.iterRange(start = "bb", limit = "b")) ==
|
||||
@[("bb", "3"), ("ba", "2")])
|
||||
|
|
Loading…
Reference in New Issue