This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_test.go
112 lines (95 loc) · 2.51 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package grocksdb
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestIterator(t *testing.T) {
db := newTestDB(t, nil)
defer db.Close()
// insert keys
givenKeys := [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}
wo := NewDefaultWriteOptions()
for _, k := range givenKeys {
require.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)
}
require.Nil(t, iter.Err())
require.EqualValues(t, actualKeys, givenKeys)
}
func TestIteratorWriteManyThenIter(t *testing.T) {
db := newTestDB(t, nil)
defer db.Close()
numKey := 10_000
// insert keys
wo := NewDefaultWriteOptions()
for i := 0; i < numKey; i++ {
require.Nil(t, db.Put(wo, []byte(fmt.Sprintf("key_%d", i)), []byte("val")))
}
for attempt := 0; attempt < 400; attempt++ {
ro := NewDefaultReadOptions()
ro.SetIterateUpperBound([]byte("keya"))
iter, count := db.NewIterator(ro), 0
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
count++
}
require.NoError(t, iter.Err())
require.EqualValues(t, numKey, count)
ro.Destroy()
iter.Close()
}
}
func TestIteratorCF(t *testing.T) {
db, cfs, cleanup := newTestDBMultiCF(t, []string{"default", "c1", "c2", "c3"}, nil)
defer cleanup()
// insert keys
givenKeys := [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}
wo := NewDefaultWriteOptions()
for _, k := range givenKeys {
for i := range cfs {
require.Nil(t, db.PutCF(wo, cfs[i], k, []byte("val")))
}
}
{
ro := NewDefaultReadOptions()
iter := db.NewIteratorCF(ro, cfs[0])
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)
}
require.Nil(t, iter.Err())
require.EqualValues(t, actualKeys, givenKeys)
}
{
ro := NewDefaultReadOptions()
iters, err := db.NewIterators(ro, cfs)
require.Nil(t, err)
require.EqualValues(t, len(iters), 4)
defer func() {
for i := range iters {
iters[i].Close()
}
}()
for _, iter := range iters {
var actualKeys [][]byte
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
key := make([]byte, 4)
copy(key, iter.Key().Data())
actualKeys = append(actualKeys, key)
}
require.Nil(t, iter.Err())
require.EqualValues(t, actualKeys, givenKeys)
}
}
}