-
Notifications
You must be signed in to change notification settings - Fork 10
/
import_test.go
248 lines (213 loc) · 5.81 KB
/
import_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package iavl
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
db "github.com/tendermint/tm-db"
)
func ExampleImporter() {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
if err != nil {
// handle err
}
tree.Set([]byte("a"), []byte{1})
tree.Set([]byte("b"), []byte{2})
tree.Set([]byte("c"), []byte{3})
_, version, err := tree.SaveVersion()
if err != nil {
// handle err
}
itree, err := tree.GetImmutable(version)
if err != nil {
// handle err
}
exporter := itree.Export()
defer exporter.Close()
exported := []*ExportNode{}
for {
var node *ExportNode
node, err = exporter.Next()
if err == ExportDone {
break
} else if err != nil {
// handle err
}
exported = append(exported, node)
}
newTree, err := NewMutableTree(db.NewMemDB(), 0, false)
if err != nil {
// handle err
}
importer, err := newTree.Import(version)
if err != nil {
// handle err
}
defer importer.Close()
for _, node := range exported {
err = importer.Add(node)
if err != nil {
// handle err
}
}
err = importer.Commit()
if err != nil {
// handle err
}
}
func TestImporter_NegativeVersion(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
_, err = tree.Import(-1)
require.Error(t, err)
}
func TestImporter_NotEmpty(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
tree.Set([]byte("a"), []byte{1})
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, err = tree.Import(1)
require.Error(t, err)
}
func TestImporter_NotEmptyDatabase(t *testing.T) {
db := db.NewMemDB()
tree, err := NewMutableTree(db, 0, false)
require.NoError(t, err)
tree.Set([]byte("a"), []byte{1})
_, _, err = tree.SaveVersion()
require.NoError(t, err)
tree, err = NewMutableTree(db, 0, false)
require.NoError(t, err)
_, err = tree.Load()
require.NoError(t, err)
_, err = tree.Import(1)
require.Error(t, err)
}
func TestImporter_NotEmptyUnsaved(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
tree.Set([]byte("a"), []byte{1})
_, err = tree.Import(1)
require.Error(t, err)
}
func TestImporter_Add(t *testing.T) {
k := []byte("key")
v := []byte("value")
testcases := map[string]struct {
node *ExportNode
valid bool
}{
"nil node": {nil, false},
"valid": {&ExportNode{Key: k, Value: v, Version: 1, Height: 0}, true},
"no key": {&ExportNode{Key: nil, Value: v, Version: 1, Height: 0}, false},
"no value": {&ExportNode{Key: k, Value: nil, Version: 1, Height: 0}, false},
"version too large": {&ExportNode{Key: k, Value: v, Version: 2, Height: 0}, false},
"no version": {&ExportNode{Key: k, Value: v, Version: 0, Height: 0}, false},
// further cases will be handled by Node.validate()
}
for desc, tc := range testcases {
tc := tc // appease scopelint
t.Run(desc, func(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
defer importer.Close()
err = importer.Add(tc.node)
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
func TestImporter_Add_Closed(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
importer.Close()
err = importer.Add(&ExportNode{Key: []byte("key"), Value: []byte("value"), Version: 1, Height: 0})
require.Error(t, err)
require.Equal(t, ErrNoImport, err)
}
func TestImporter_Close(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
err = importer.Add(&ExportNode{Key: []byte("key"), Value: []byte("value"), Version: 1, Height: 0})
require.NoError(t, err)
importer.Close()
has, err := tree.Has([]byte("key"))
require.NoError(t, err)
require.False(t, has)
importer.Close()
}
func TestImporter_Commit(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
err = importer.Add(&ExportNode{Key: []byte("key"), Value: []byte("value"), Version: 1, Height: 0})
require.NoError(t, err)
err = importer.Commit()
require.NoError(t, err)
has, err := tree.Has([]byte("key"))
require.NoError(t, err)
require.True(t, has)
}
func TestImporter_Commit_Closed(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
err = importer.Add(&ExportNode{Key: []byte("key"), Value: []byte("value"), Version: 1, Height: 0})
require.NoError(t, err)
importer.Close()
err = importer.Commit()
require.Error(t, err)
require.Equal(t, ErrNoImport, err)
}
func TestImporter_Commit_Empty(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := tree.Import(3)
require.NoError(t, err)
defer importer.Close()
err = importer.Commit()
require.NoError(t, err)
assert.EqualValues(t, 3, tree.Version())
}
func BenchmarkImport(b *testing.B) {
b.StopTimer()
tree := setupExportTreeSized(b, 4096)
exported := make([]*ExportNode, 0, 4096)
exporter := tree.Export()
for {
item, err := exporter.Next()
if err == ExportDone {
break
} else if err != nil {
b.Error(err)
}
exported = append(exported, item)
}
exporter.Close()
b.StartTimer()
for n := 0; n < b.N; n++ {
newTree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(b, err)
importer, err := newTree.Import(tree.Version())
require.NoError(b, err)
for _, item := range exported {
err = importer.Add(item)
if err != nil {
b.Error(err)
}
}
err = importer.Commit()
require.NoError(b, err)
}
}