Skip to content

Commit 300fe46

Browse files
houfazhouroysc
authored andcommitted
add SetIterateLowerBound and testcase
1 parent 0449c4f commit 300fe46

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

iterator_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,36 @@ func TestIterator(t *testing.T) {
2929
ensure.Nil(t, iter.Err())
3030
ensure.DeepEqual(t, actualKeys, givenKeys)
3131
}
32+
33+
func TestIteratorWithRange(t *testing.T) {
34+
db := newTestDB(t, "TestIterator", nil)
35+
defer db.Close()
36+
37+
// insert keys
38+
givenKeys := [][]byte{
39+
[]byte("key1"),
40+
[]byte("key2"),
41+
[]byte("key3"),
42+
[]byte("key4"),
43+
[]byte("key5"),
44+
}
45+
wo := NewDefaultWriteOptions()
46+
for _, k := range givenKeys {
47+
ensure.Nil(t, db.Put(wo, k, []byte("val")))
48+
}
49+
50+
ro := NewDefaultReadOptions()
51+
ro.SetIterateLowerBound([]byte("key2"))
52+
ro.SetIterateUpperBound([]byte("key4"))
53+
54+
iter := db.NewIterator(ro)
55+
defer iter.Close()
56+
var actualKeys [][]byte
57+
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
58+
key := make([]byte, 4)
59+
copy(key, iter.Key().Data())
60+
actualKeys = append(actualKeys, key)
61+
}
62+
ensure.Nil(t, iter.Err())
63+
ensure.DeepEqual(t, len(actualKeys), 2)
64+
}

options_read.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,29 @@ func (opts *ReadOptions) SetTailing(value bool) {
100100
// not a valid entry. If iterator_extractor is not null, the Seek target
101101
// and iterator_upper_bound need to have the same prefix.
102102
// This is because ordering is not guaranteed outside of prefix domain.
103-
// There is no lower bound on the iterator. If needed, that can be easily
104-
// implemented.
105103
// Default: nullptr
106104
func (opts *ReadOptions) SetIterateUpperBound(key []byte) {
107105
cKey := byteToChar(key)
108106
cKeyLen := C.size_t(len(key))
109107
C.rocksdb_readoptions_set_iterate_upper_bound(opts.c, cKey, cKeyLen)
110108
}
111109

110+
// SetIterateLowerBound specifies "iterate_lower_bound", which defines
111+
// the smallest key at which the backward iterator can return an entry.
112+
// Once the bound is passed, Valid() will be false.
113+
// `iterate_lower_bound` is inclusive ie the bound value is a valid
114+
// entry.
115+
//
116+
// If prefix_extractor is not null, the Seek target and `iterate_lower_bound`
117+
// need to have the same prefix. This is because ordering is not guaranteed
118+
// outside of prefix domain.
119+
// Default: nullptr
120+
func (opts *ReadOptions) SetIterateLowerBound(key []byte) {
121+
cKey := byteToChar(key)
122+
cKeyLen := C.size_t(len(key))
123+
C.rocksdb_readoptions_set_iterate_lower_bound(opts.c, cKey, cKeyLen)
124+
}
125+
112126
// SetPinData specifies the value of "pin_data". If true, it keeps the blocks
113127
// loaded by the iterator pinned in memory as long as the iterator is not deleted,
114128
// If used when reading from tables created with

0 commit comments

Comments
 (0)