-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
95 lines (79 loc) · 2.33 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
package bitcask
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDB_NewIterator(t *testing.T) {
dir, _ := os.MkdirTemp("", "bitcask-go-iterator-1")
opts := []DBOption{WithDBDirPath(dir), WithDBDataFileSize(64 * 1024 * 1024)}
db, err := Open(opts...)
defer removeDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
iterator := db.NewIterator()
assert.NotNil(t, iterator)
assert.Equal(t, false, iterator.Valid())
}
func TestDB_Iterator_One_Value(t *testing.T) {
dir, _ := os.MkdirTemp("", "bitcask-go-iterator-2")
opts := []DBOption{WithDBDirPath(dir), WithDBDataFileSize(64 * 1024 * 1024)}
db, err := Open(opts...)
defer removeDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put(getTestKey(10), getTestKey(10))
assert.Nil(t, err)
iterator := db.NewIterator()
defer iterator.Close()
assert.NotNil(t, iterator)
assert.Equal(t, true, iterator.Valid())
assert.Equal(t, getTestKey(10), iterator.Key())
val, err := iterator.Value()
assert.Nil(t, err)
assert.Equal(t, getTestKey(10), val)
}
func TestDB_Iterator_Multi_Values(t *testing.T) {
dir, _ := os.MkdirTemp("", "bitcask-go-iterator-3")
opts := []DBOption{WithDBDirPath(dir), WithDBDataFileSize(64 * 1024 * 1024)}
db, err := Open(opts...)
defer removeDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put([]byte("annde"), randomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("cnedc"), randomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("aeeue"), randomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("esnue"), randomValue(10))
assert.Nil(t, err)
err = db.Put([]byte("bnede"), randomValue(10))
assert.Nil(t, err)
// 正向迭代
iter1 := db.NewIterator()
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()
// 反向迭代
iter2 := db.NewIterator(WithIteratorReverse(true))
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
iter3 := db.NewIterator(WithIteratorPrefix([]byte("aee")))
for iter3.Rewind(); iter3.Valid(); iter3.Next() {
assert.NotNil(t, iter3.Key())
}
iter3.Close()
}