From 54bce342c6b6d8bba3a67f46959c49fd04d2ee34 Mon Sep 17 00:00:00 2001 From: houfazhou Date: Wed, 14 Apr 2021 19:20:40 +0800 Subject: [PATCH] add SetIterateLowerBound and testcase --- go.mod | 10 ++++++++++ iterator_test.go | 33 +++++++++++++++++++++++++++++++++ options_read.go | 16 ++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..69c71bf9 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/yiyanwannian/gorocksdb + +go 1.16 + +require ( + github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c + github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect + github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect + github.com/stretchr/testify v1.7.0 +) diff --git a/iterator_test.go b/iterator_test.go index 358400ba..2195beb6 100644 --- a/iterator_test.go +++ b/iterator_test.go @@ -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) +} \ No newline at end of file diff --git a/options_read.go b/options_read.go index 6a37cc48..bed72e62 100644 --- a/options_read.go +++ b/options_read.go @@ -93,6 +93,22 @@ func (opts *ReadOptions) SetTailing(value bool) { C.rocksdb_readoptions_set_tailing(opts.c, boolToChar(value)) } +// SetIterateLowerBound specifies "iterate_lower_bound", which defines +// the extent upto which the forward iterator can returns entries. +// Once the bound is reached, Valid() will be false. +// "iterate_lower_bound" is exclusive ie the bound value is +// not a valid entry. If iterator_extractor is not null, the Seek target +// and iterator_lower_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) SetIterateLowerBound(key []byte) { + cKey := byteToChar(key) + cKeyLen := C.size_t(len(key)) + C.rocksdb_readoptions_set_iterate_lower_bound(opts.c, cKey, cKeyLen) +} + // SetIterateUpperBound specifies "iterate_upper_bound", which defines // the extent upto which the forward iterator can returns entries. // Once the bound is reached, Valid() will be false.