-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
135 lines (119 loc) · 3.35 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package GoKeeper
import (
"GoKeeper/util"
"fmt"
"github.com/stretchr/testify/assert"
"log"
"os"
"testing"
)
func TestDB_NewIterator(t *testing.T) {
// 传入配置文件,启动数据库
opts := DefaultOptions
dir, err := os.MkdirTemp("./tmp/", "bitcask-go-put")
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := Open(opts)
defer destroyDB(db)
defer func(db *DB) {
err := db.Close()
if err != nil {
log.Println(err)
return
}
}(db)
//db.Put(util.GetRandomKey(1), util.GetRandomValue(10))
assert.Nil(t, err)
assert.NotNil(t, db)
// 创建迭代器
//iterator := db.NewIterator(DefaultIteratorOption)
//defer iterator.Close()
//assert.Equal(t, false, iterator.Valid())
}
func TestDB_Iterator_OneValue(t *testing.T) {
// 传入配置文件,启动数据库
opts := DefaultOptions
dir, err := os.MkdirTemp("./tmp/", "bitcask-go-put")
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := Open(opts)
//defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put(util.GetRandomKey(1), util.GetRandomValue(10))
assert.Nil(t, err)
// 创建迭代器
iterator := db.NewIterator(DefaultIteratorOption)
defer iterator.Close()
assert.Equal(t, true, iterator.Valid())
assert.Equal(t, util.GetRandomKey(1), iterator.Key())
value, err := iterator.Value()
assert.Nil(t, err)
t.Log(value)
}
func TestDB_Iterator_MutiValue(t *testing.T) {
// 传入配置文件,启动数据库
opts := DefaultOptions
dir, err := os.MkdirTemp("./tmp/", "bitcask-go-put")
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := Open(opts)
//defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
// 插入10个数据
for i := 0; i < 10; i++ {
err = db.Put(util.GetRandomKey(i), util.GetRandomValue(10))
assert.Nil(t, err)
}
// 创建迭代器
iterator := db.NewIterator(DefaultIteratorOption)
defer iterator.Close()
for iterator.Rewind(); iterator.Valid(); iterator.Next() {
assert.NotNil(t, iterator.Key())
}
iterator.Rewind()
for iterator.Seek([]byte("GoKeeper-key-4")); iterator.Valid(); iterator.Next() {
t.Log(string(iterator.Key()))
}
// 创建反向迭代
iterator2 := db.NewIterator(IteratorOption{
Reverse: true,
Prefix: nil,
})
for iterator2.Rewind(); iterator2.Valid(); iterator2.Next() {
assert.NotNil(t, iterator2.Key())
}
}
func TestDB_Iterator_Prefix(t *testing.T) {
// 传入配置文件,启动数据库
opts := DefaultOptions
dir, err := os.MkdirTemp("./tmp/", "bitcask-go-put")
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := Open(opts)
//defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
// 插入10个数据
db.Put([]byte("Sakurass"), util.GetRandomValue(10))
db.Put([]byte("GoKeeper-key-1"), util.GetRandomValue(10))
db.Put([]byte("Sakuras"), util.GetRandomValue(10))
db.Put([]byte("Saki"), util.GetRandomValue(10))
db.Put([]byte("GoKeeper-key-2"), util.GetRandomValue(10))
// 创建无前缀的迭代器
iterator := db.NewIterator(DefaultIteratorOption)
defer iterator.Close()
for iterator.Rewind(); iterator.Valid(); iterator.Next() {
t.Log(string(iterator.Key()))
}
fmt.Println("--------------")
// 创建指定前缀的迭代器
iterator = db.NewIterator(IteratorOption{
Prefix: []byte("GoKeeper"),
Reverse: false,
})
for iterator.Rewind(); iterator.Valid(); iterator.Next() {
t.Log(string(iterator.Key()))
}
}