-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsmt_test.go
212 lines (185 loc) · 6.23 KB
/
lsmt_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 lsmt
import (
"testing"
"github.com/patrickgombert/lsmt/common"
c "github.com/patrickgombert/lsmt/comparator"
"github.com/patrickgombert/lsmt/config"
)
var sink *config.Sink = &config.Sink{BlockSize: 100, SSTSize: 1000, BlockCacheShards: 1, BlockCacheSize: 1000, BloomFilterSize: 1000}
var level *config.Level = &config.Level{BlockSize: 100, SSTSize: 1000, BlockCacheShards: 1, BlockCacheSize: 1000, BloomFilterSize: 1000, MaximumSSTFiles: 1}
var options *config.Options = &config.Options{Levels: []*config.Level{level}, Sink: sink, KeyMaximumSize: 10, ValueMaximumSize: 10, MemtableMaximumSize: 1000, Path: common.TEST_DIR}
func TestWriteNilOrEmptyKeyReturnsError(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, errs := Lsmt(options)
defer lsmt.Close()
if errs != nil {
t.Errorf("err %s\n", errs)
}
err := lsmt.Write(nil, []byte{0})
if err == nil {
t.Error("Expected lsmt to error when writing nil key, but did not")
}
err = lsmt.Write([]byte{}, []byte{0})
if err == nil {
t.Error("Expected lsmt to error when writing empty key, but did not")
}
}
func TestWriteNilOrEmptyValueReturnsError(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
defer lsmt.Close()
err := lsmt.Write([]byte{0}, nil)
if err == nil {
t.Error("Expected lsmt to error when writing nil value, but did not")
}
err = lsmt.Write([]byte{0}, []byte{})
if err == nil {
t.Error("Expected lsmt to error when writing empty value, but did not")
}
}
func TestDeleteNilOrEmptyKeyReturnsError(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
defer lsmt.Close()
err := lsmt.Delete(nil)
if err == nil {
t.Error("Expected lsmt to error when deleting nil key, but did not")
}
err = lsmt.Delete([]byte{})
if err == nil {
t.Error("Expected lsmt to error when deleting empty key, but did not")
}
}
func TestIteratorStartNilOrEmptyReturnsError(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
defer lsmt.Close()
_, err := lsmt.Iterator(nil, []byte{1})
if err == nil {
t.Error("Expected lsmt to error when creating an iterator with a nil start value, but did not")
}
_, err = lsmt.Iterator([]byte{}, []byte{1})
if err == nil {
t.Error("Expected lsmt to error when creating an iterator with an empty start value, but did not")
}
}
func TestIteratorEndNilOrEmptyreturnsError(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
defer lsmt.Close()
_, err := lsmt.Iterator([]byte{1}, nil)
if err == nil {
t.Error("Expected lsmt to error when creating an iterator with a nil end value, but did not")
}
_, err = lsmt.Iterator([]byte{1}, []byte{})
if err == nil {
t.Error("Expected lsmt to error when creating an iterator with an empty end value, but did not")
}
}
func TestIteratorStartNotLessThanEnd(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
defer lsmt.Close()
_, err := lsmt.Iterator([]byte{1}, []byte{0})
if err == nil {
t.Error("Expected lsmt to error when the start key is not less than the end key when creating an iterator, but did not")
}
}
func TestDoesNotAcceptWritesAfterClose(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
lsmt.Close()
err := lsmt.Write([]byte{0}, []byte{0})
if err == nil {
t.Error("Expected closed lsmt to not accept writes, but did")
}
err = lsmt.Delete([]byte{0})
if err == nil {
t.Error("Expected closed lsmt to not accept deletes, but did")
}
}
func TestFlushWithoutExistingLevel(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
lsmt.Write([]byte{1, 1, 1, 1, 1, 1}, []byte{1, 1, 1, 1, 1, 1})
lsmt.Close()
openedLsmt, err := Lsmt(options)
defer openedLsmt.Close()
if err != nil {
t.Errorf("Error opening lsmt %q", err)
}
result, _ := openedLsmt.Get([]byte{1, 1, 1, 1, 1, 1})
if c.Compare(result, []byte{1, 1, 1, 1, 1, 1}) != c.EQUAL {
t.Errorf("Expected opened lsmt to contain %q, but did not", []byte{1, 1, 1, 1, 1, 1})
}
}
func TestFlushWithExistingLevel(t *testing.T) {
common.SetUp(t)
defer common.TearDown(t)
lsmt, _ := Lsmt(options)
lsmt.Write([]byte{1, 1, 1, 1, 1, 1}, []byte{1, 1, 1, 1, 1, 1})
lsmt.Close()
lsmt, _ = Lsmt(options)
lsmt.Write([]byte{1, 0, 1}, []byte{1, 0, 1})
lsmt.Close()
openedLsmt, _ := Lsmt(options)
defer openedLsmt.Close()
result, _ := openedLsmt.Get([]byte{1, 1, 1, 1, 1, 1})
if c.Compare(result, []byte{1, 1, 1, 1, 1, 1}) != c.EQUAL {
t.Errorf("Expected opened lsmt to contain %q, but did not", []byte{1, 1, 1, 1, 1, 1})
}
result, _ = openedLsmt.Get([]byte{1, 0, 1})
if c.Compare(result, []byte{1, 0, 1}) != c.EQUAL {
t.Errorf("Expected opened lsmt to contain %q, but did not", []byte{1, 0, 1})
}
}
//func TestMultiLevelStorage(t *testing.T) {
// common.SetUp(t)
// defer common.TearDown(t)
//
// var level1 *config.Level = &config.Level{BlockSize: 6, SSTSize: 12, BlockCacheShards: 1, BlockCacheSize: 12, BloomFilterSize: 1000, MaximumSSTFiles: 1}
// var level2 *config.Level = &config.Level{BlockSize: 6, SSTSize: 12, BlockCacheShards: 1, BlockCacheSize: 12, BloomFilterSize: 1000, MaximumSSTFiles: 2}
// var sink *config.Sink = &config.Sink{BlockSize: 100, SSTSize: 1000, BlockCacheShards: 1, BlockCacheSize: 1000, BloomFilterSize: 1000}
// var options *config.Options = &config.Options{Levels: []*config.Level{level1, level2}, Sink: sink, KeyMaximumSize: 4, ValueMaximumSize: 4, MemtableMaximumSize: 8, Path: common.TEST_DIR}
//
// testValues := [][]byte{
// []byte{0},
// []byte{1},
// []byte{2},
// []byte{3},
// []byte{4},
// []byte{5},
// }
//
// lsmt, _ := Lsmt(options)
// for _, testValue := range testValues {
// lsmt.Write(testValue, testValue)
// }
// lsmt.Close()
//
// lsmt, _ = Lsmt(options)
// defer lsmt.Close()
// result, _ := lsmt.Get([]byte{5})
// for _, testValue := range testValues {
// result, _ := lsmt.Get(testValue)
// if c.Compare(result, testValue) != c.EQUAL {
// t.Errorf("Expected opened lsmt to contain %q, but did not", testValue)
// }
// }
//
// iter, _ := lsmt.Iterator([]byte{0}, []byte{6})
// defer iter.Close()
// for _, testValue := range testValues {
// common.CompareNext(iter, true, t)
// common.CompareGet(iter, testValue, testValue, t)
// }
// common.CompareNext(iter, false, t)
//}