Skip to content

Commit

Permalink
Add explicit seek position method to iterator (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjfh committed Apr 16, 2024
1 parent f37d7d4 commit eb594e3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rocksdb/rocksiterator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ proc isClosed*(iter: RocksIteratorRef): bool {.inline.} =
## Returns `true` if the iterator is closed and `false` otherwise.
iter.cPtr.isNil()

proc seekToKey*(iter: RocksIteratorRef, key: openArray[byte]) =
## Seeks to the `key` argument in the column family. If the return code is
## `false`, the iterator has become invalid and should be closed.
##
## It is not clear what happens when the `key` does not exist in the column
## family. The guess is that the interation will proceed at the next key
## position. This is suggested by a comment from the GO port at
##
## //github.com/DanielMorsing/rocksdb/blob/master/iterator.go:
##
## Seek moves the iterator the position of the key given or, if the key
## doesn't exist, the next key that does exist in the database. If the
## key doesn't exist, and there is no next key, the Iterator becomes
## invalid.
##
doAssert not iter.isClosed()
let (cKey, cLen) = (cast[cstring](unsafeAddr key[0]), csize_t(key.len))
rocksdb_iter_seek(iter.cPtr, cKey, cLen)

proc seekToFirst*(iter: RocksIteratorRef) =
## Seeks to the first entry in the column family.
doAssert not iter.isClosed()
Expand Down

0 comments on commit eb594e3

Please sign in to comment.