-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_test.go
98 lines (87 loc) · 2.39 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
package skv_go
import (
"github.com/stretchr/testify/assert"
"os"
"skv-go/utils"
"testing"
)
func TestDB_NewIterator(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "test1")
opts.DirPath = dir
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
iterator := db.NewIterator(DefaultIteratorOptions)
assert.NotNil(t, iterator)
assert.Equal(t, false, iterator.Valid())
}
func TestDB_Iterator_One_Value(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "test2")
opts.DirPath = dir
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put(utils.GetTestKey(10), utils.GetTestKey(10))
assert.Nil(t, err)
iterator := db.NewIterator(DefaultIteratorOptions)
defer iterator.Close()
assert.NotNil(t, iterator)
assert.Equal(t, true, iterator.Valid())
assert.Equal(t, utils.GetTestKey(10), iterator.Key())
val, err := iterator.Value()
assert.Nil(t, err)
assert.Equal(t, utils.GetTestKey(10), val)
}
func TestDB_Iterator_Multi_Values(t *testing.T) {
opts := DefaultOptions
dir, _ := os.MkdirTemp("", "test3")
opts.DirPath = dir
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put([]byte("annde"), utils.RandomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("cnedc"), utils.RandomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("aeeue"), utils.RandomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("esnue"), utils.RandomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("bnede"), utils.RandomValue(10))
assert.Nil(t, err)
// 正向迭代
iter1 := db.NewIterator(DefaultIteratorOptions)
for iter1.Rewind(); iter1.Valid(); iter1.Next() {
assert.NotNil(t, iter1.Key())
}
iter1.Rewind()
for iter1.Seek([]byte("c")); iter1.Valid(); iter1.Next() {
assert.NotNil(t, iter1.Key())
}
iter1.Close()
// 反向迭代
iterOpts1 := DefaultIteratorOptions
iterOpts1.Reverse = true
iter2 := db.NewIterator(iterOpts1)
for iter2.Rewind(); iter2.Valid(); iter2.Next() {
assert.NotNil(t, iter2.Key())
}
iter2.Rewind()
for iter2.Seek([]byte("c")); iter2.Valid(); iter2.Next() {
assert.NotNil(t, iter2.Key())
}
iter2.Close()
// 指定了 prefix
iterOpts2 := DefaultIteratorOptions
iterOpts2.Prefix = []byte("aee")
iter3 := db.NewIterator(iterOpts2)
for iter3.Rewind(); iter3.Valid(); iter3.Next() {
assert.NotNil(t, iter3.Key())
}
iter3.Close()
}