Skip to content

Commit

Permalink
add SetIterateLowerBound and testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
houfazhou authored and roysc committed Jul 14, 2021
1 parent 0449c4f commit 300fe46
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
33 changes: 33 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,36 @@ func TestIterator(t *testing.T) {
ensure.Nil(t, iter.Err())
ensure.DeepEqual(t, actualKeys, givenKeys)
}

func TestIteratorWithRange(t *testing.T) {
db := newTestDB(t, "TestIterator", nil)
defer db.Close()

// insert keys
givenKeys := [][]byte{
[]byte("key1"),
[]byte("key2"),
[]byte("key3"),
[]byte("key4"),
[]byte("key5"),
}
wo := NewDefaultWriteOptions()
for _, k := range givenKeys {
ensure.Nil(t, db.Put(wo, k, []byte("val")))
}

ro := NewDefaultReadOptions()
ro.SetIterateLowerBound([]byte("key2"))
ro.SetIterateUpperBound([]byte("key4"))

iter := db.NewIterator(ro)
defer iter.Close()
var actualKeys [][]byte
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
key := make([]byte, 4)
copy(key, iter.Key().Data())
actualKeys = append(actualKeys, key)
}
ensure.Nil(t, iter.Err())
ensure.DeepEqual(t, len(actualKeys), 2)
}
18 changes: 16 additions & 2 deletions options_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,29 @@ func (opts *ReadOptions) SetTailing(value bool) {
// not a valid entry. If iterator_extractor is not null, the Seek target
// and iterator_upper_bound need to have the same prefix.
// This is because ordering is not guaranteed outside of prefix domain.
// There is no lower bound on the iterator. If needed, that can be easily
// implemented.
// Default: nullptr
func (opts *ReadOptions) SetIterateUpperBound(key []byte) {
cKey := byteToChar(key)
cKeyLen := C.size_t(len(key))
C.rocksdb_readoptions_set_iterate_upper_bound(opts.c, cKey, cKeyLen)
}

// SetIterateLowerBound specifies "iterate_lower_bound", which defines
// the smallest key at which the backward iterator can return an entry.
// Once the bound is passed, Valid() will be false.
// `iterate_lower_bound` is inclusive ie the bound value is a valid
// entry.
//
// If prefix_extractor is not null, the Seek target and `iterate_lower_bound`
// need to have the same prefix. This is because ordering is not guaranteed
// outside of prefix domain.
// Default: nullptr
func (opts *ReadOptions) SetIterateLowerBound(key []byte) {
cKey := byteToChar(key)
cKeyLen := C.size_t(len(key))
C.rocksdb_readoptions_set_iterate_lower_bound(opts.c, cKey, cKeyLen)
}

// SetPinData specifies the value of "pin_data". If true, it keeps the blocks
// loaded by the iterator pinned in memory as long as the iterator is not deleted,
// If used when reading from tables created with
Expand Down

0 comments on commit 300fe46

Please sign in to comment.