forked from cosmos/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator_test.go
64 lines (55 loc) · 1.42 KB
/
iterator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package gorocksdb
import (
"testing"
"github.com/facebookgo/ensure"
)
func TestIterator(t *testing.T) {
db := newTestDB(t, "TestIterator", nil)
defer db.Close()
// insert keys
givenKeys := [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}
wo := NewDefaultWriteOptions()
for _, k := range givenKeys {
ensure.Nil(t, db.Put(wo, k, []byte("val")))
}
ro := NewDefaultReadOptions()
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, 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)
}