-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlsmtree_test.go
212 lines (180 loc) · 5.02 KB
/
lsmtree_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package lsmtree_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"
"github.com/krasun/lsmtree"
)
func Example() {
dbDir, err := ioutil.TempDir(os.TempDir(), "example")
if err != nil {
panic(fmt.Errorf("failed to create %s: %w", dbDir, err))
}
defer func() {
if err := os.RemoveAll(dbDir); err != nil {
panic(fmt.Errorf("failed to remove %s: %w", dbDir, err))
}
}()
tree, err := lsmtree.Open(dbDir, lsmtree.SparseKeyDistance(64), lsmtree.MemTableThreshold(1000000))
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}
err = tree.Put([]byte("Hi!"), []byte("Hello world, LSMTree!"))
if err != nil {
panic(fmt.Errorf("failed to put: %w", err))
}
err = tree.Put([]byte("Does it override key?"), []byte("No!"))
if err != nil {
panic(fmt.Errorf("failed to put: %w", err))
}
err = tree.Put([]byte("Does it override key?"), []byte("Yes, absolutely! The key has been overridden."))
if err != nil {
panic(fmt.Errorf("failed to put: %w", err))
}
if err := tree.Close(); err != nil {
panic(fmt.Errorf("failed to close: %w", err))
}
tree, err = lsmtree.Open(dbDir)
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}
value, ok, err := tree.Get([]byte("Hi!"))
if err != nil {
panic(fmt.Errorf("failed to get value: %w", err))
}
if !ok {
fmt.Println("failed to find value")
}
fmt.Println(string(value))
value, ok, err = tree.Get([]byte("Does it override key?"))
if err != nil {
panic(fmt.Errorf("failed to get value: %w", err))
}
if !ok {
fmt.Println("failed to find value")
}
if err := tree.Close(); err != nil {
panic(fmt.Errorf("failed to close: %w", err))
}
fmt.Println(string(value))
// Output:
// Hello world, LSMTree!
// Yes, absolutely! The key has been overridden.
}
func TestPutForErrors(t *testing.T) {
dbDir, err := ioutil.TempDir(os.TempDir(), "example")
if err != nil {
panic(fmt.Errorf("failed to create %s: %w", dbDir, err))
}
defer func() {
if err := os.RemoveAll(dbDir); err != nil {
panic(fmt.Errorf("failed to remove %s: %w", dbDir, err))
}
}()
tree, err := lsmtree.Open(dbDir)
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}
err = tree.Put(nil, []byte("some value"))
if !errors.Is(err, lsmtree.ErrKeyRequired) {
t.Fatalf("expected %v, but got %v", lsmtree.ErrKeyRequired, err)
}
err = tree.Put([]byte{}, []byte("some value"))
if !errors.Is(err, lsmtree.ErrKeyRequired) {
t.Fatalf("expected %v, but got %v", lsmtree.ErrKeyRequired, err)
}
err = tree.Put([]byte("some key"), nil)
if !errors.Is(err, lsmtree.ErrValueRequired) {
t.Fatalf("expected %v, but got %v", lsmtree.ErrValueRequired, err)
}
err = tree.Put([]byte("some key"), []byte{})
if !errors.Is(err, lsmtree.ErrValueRequired) {
t.Fatalf("expected %v, but got %v", lsmtree.ErrValueRequired, err)
}
var largeKey [65536]byte
err = tree.Put(largeKey[:], []byte("some value"))
if !errors.Is(err, lsmtree.ErrKeyTooLarge) {
t.Fatalf("expected %v, but got %v", lsmtree.ErrKeyTooLarge, err)
}
var largeValue [65536]byte
err = tree.Put([]byte("some key"), largeValue[:])
if !errors.Is(err, lsmtree.ErrValueTooLarge) {
t.Fatalf("expected %v, but got %v", lsmtree.ErrValueTooLarge, err)
}
}
func TestPut100(t *testing.T) {
dbDir, err := ioutil.TempDir(os.TempDir(), "example")
if err != nil {
panic(fmt.Errorf("failed to create %s: %w", dbDir, err))
}
defer func() {
if err := os.RemoveAll(dbDir); err != nil {
panic(fmt.Errorf("failed to remove %s: %w", dbDir, err))
}
}()
tree, err := lsmtree.Open(
dbDir,
lsmtree.SparseKeyDistance(64),
lsmtree.MemTableThreshold(100),
lsmtree.DiskTableNumThreshold(3),
)
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}
fmt.Println(tree)
// key = "1", value = "2"
// key = "2", value = "4"
// ...
for i := 1; i <= 100; i++ {
key := strconv.Itoa(i)
value := strconv.Itoa(i * 2)
err := tree.Put([]byte(key), []byte(value))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
}
// key = "1", value = "2"
// key = "2", value deleted
// key = "3", value = "6"
// key = "4", value deleted
// ...
for i := 1; i <= 100; i++ {
if i%2 == 0 {
key := strconv.Itoa(i)
err := tree.Delete([]byte(key))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
}
}
tree, err = lsmtree.Open(dbDir)
if err != nil {
panic(fmt.Errorf("failed to open LSM tree %s: %w", dbDir, err))
}
for i := 1; i <= 100; i++ {
key := strconv.Itoa(i)
value, ok, err := tree.Get([]byte(key))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if i%2 == 0 && ok {
t.Fatalf("key must be deleted %s, but it is not", key)
}
if i%2 != 0 {
if !ok {
t.Fatalf("key must be present %s, but it is not", key)
} else {
expectedValue := strconv.Itoa(i * 2)
if expectedValue != string(value) {
t.Fatalf("value is wrong for key %s: %s != %s", key, expectedValue, value)
}
}
}
}
if err := tree.Close(); err != nil {
panic(fmt.Errorf("failed to close: %w", err))
}
}