forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_test.go
327 lines (277 loc) · 8.47 KB
/
export_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package iavl
import (
"math"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
db "github.com/cosmos/cosmos-db"
)
// setupExportTreeBasic sets up a basic tree with a handful of
// create/update/delete operations over a few versions.
func setupExportTreeBasic(t require.TestingT) *ImmutableTree {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
_, err = tree.Set([]byte("x"), []byte{255})
require.NoError(t, err)
_, err = tree.Set([]byte("z"), []byte{255})
require.NoError(t, err)
_, err = tree.Set([]byte("a"), []byte{1})
require.NoError(t, err)
_, err = tree.Set([]byte("b"), []byte{2})
require.NoError(t, err)
_, err = tree.Set([]byte("c"), []byte{3})
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, _, err = tree.Remove([]byte("x"))
require.NoError(t, err)
_, _, err = tree.Remove([]byte("b"))
require.NoError(t, err)
_, err = tree.Set([]byte("c"), []byte{255})
require.NoError(t, err)
_, err = tree.Set([]byte("d"), []byte{4})
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, err = tree.Set([]byte("b"), []byte{2})
require.NoError(t, err)
_, err = tree.Set([]byte("c"), []byte{3})
require.NoError(t, err)
_, err = tree.Set([]byte("e"), []byte{5})
require.NoError(t, err)
_, _, err = tree.Remove([]byte("z"))
require.NoError(t, err)
_, version, err := tree.SaveVersion()
require.NoError(t, err)
itree, err := tree.GetImmutable(version)
require.NoError(t, err)
return itree
}
// setupExportTreeRandom sets up a randomly generated tree.
// nolint: dupl
func setupExportTreeRandom(t *testing.T) *ImmutableTree {
const (
randSeed = 49872768940 // For deterministic tests
keySize = 16
valueSize = 16
versions = 8 // number of versions to generate
versionOps = 1024 // number of operations (create/update/delete) per version
updateRatio = 0.4 // ratio of updates out of all operations
deleteRatio = 0.2 // ratio of deletes out of all operations
)
r := rand.New(rand.NewSource(randSeed))
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
var version int64
keys := make([][]byte, 0, versionOps)
for i := 0; i < versions; i++ {
for j := 0; j < versionOps; j++ {
key := make([]byte, keySize)
value := make([]byte, valueSize)
// The performance of this is likely to be terrible, but that's fine for small tests
switch {
case len(keys) > 0 && r.Float64() <= deleteRatio:
index := r.Intn(len(keys))
key = keys[index]
keys = append(keys[:index], keys[index+1:]...)
_, removed, err := tree.Remove(key)
require.NoError(t, err)
require.True(t, removed)
case len(keys) > 0 && r.Float64() <= updateRatio:
key = keys[r.Intn(len(keys))]
r.Read(value)
updated, err := tree.Set(key, value)
require.NoError(t, err)
require.True(t, updated)
default:
r.Read(key)
r.Read(value)
// If we get an update, set again
for updated, err := tree.Set(key, value); updated && err == nil; {
key = make([]byte, keySize)
r.Read(key)
}
keys = append(keys, key)
}
}
_, version, err = tree.SaveVersion()
require.NoError(t, err)
}
require.EqualValues(t, versions, tree.Version())
require.GreaterOrEqual(t, tree.Size(), int64(math.Trunc(versions*versionOps*(1-updateRatio-deleteRatio))/2))
itree, err := tree.GetImmutable(version)
require.NoError(t, err)
return itree
}
// setupExportTreeSized sets up a single-version tree with a given number
// of randomly generated key/value pairs, useful for benchmarking.
func setupExportTreeSized(t require.TestingT, treeSize int) *ImmutableTree { //nolint:unparam
const (
randSeed = 49872768940 // For deterministic tests
keySize = 16
valueSize = 16
)
r := rand.New(rand.NewSource(randSeed))
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
for i := 0; i < treeSize; i++ {
key := make([]byte, keySize)
value := make([]byte, valueSize)
r.Read(key)
r.Read(value)
updated, err := tree.Set(key, value)
require.NoError(t, err)
if updated {
i--
}
}
_, version, err := tree.SaveVersion()
require.NoError(t, err)
itree, err := tree.GetImmutable(version)
require.NoError(t, err)
return itree
}
func TestExporter(t *testing.T) {
tree := setupExportTreeBasic(t)
expect := []*ExportNode{
{Key: []byte("a"), Value: []byte{1}, Version: 1, Height: 0},
{Key: []byte("b"), Value: []byte{2}, Version: 3, Height: 0},
{Key: []byte("b"), Value: nil, Version: 3, Height: 1},
{Key: []byte("c"), Value: []byte{3}, Version: 3, Height: 0},
{Key: []byte("c"), Value: nil, Version: 3, Height: 2},
{Key: []byte("d"), Value: []byte{4}, Version: 2, Height: 0},
{Key: []byte("e"), Value: []byte{5}, Version: 3, Height: 0},
{Key: []byte("e"), Value: nil, Version: 3, Height: 1},
{Key: []byte("d"), Value: nil, Version: 3, Height: 3},
}
actual := make([]*ExportNode, 0, len(expect))
exporter, err := tree.Export()
require.NoError(t, err)
defer exporter.Close()
for {
node, err := exporter.Next()
if err == ErrorExportDone {
break
}
require.NoError(t, err)
actual = append(actual, node)
}
assert.Equal(t, expect, actual)
}
func TestExporter_Import(t *testing.T) {
testcases := map[string]*ImmutableTree{
"empty tree": NewImmutableTree(db.NewMemDB(), 0, false),
"basic tree": setupExportTreeBasic(t),
}
if !testing.Short() {
testcases["sized tree"] = setupExportTreeSized(t, 4096)
testcases["random tree"] = setupExportTreeRandom(t)
}
for desc, tree := range testcases {
tree := tree
t.Run(desc, func(t *testing.T) {
t.Parallel()
exporter, err := tree.Export()
require.NoError(t, err)
defer exporter.Close()
newTree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
importer, err := newTree.Import(tree.Version())
require.NoError(t, err)
defer importer.Close()
for {
item, err := exporter.Next()
if err == ErrorExportDone {
err = importer.Commit()
require.NoError(t, err)
break
}
require.NoError(t, err)
err = importer.Add(item)
require.NoError(t, err)
}
treeHash, err := tree.Hash()
require.NoError(t, err)
newTreeHash, err := newTree.Hash()
require.NoError(t, err)
require.Equal(t, treeHash, newTreeHash, "Tree hash mismatch")
require.Equal(t, tree.Size(), newTree.Size(), "Tree size mismatch")
require.Equal(t, tree.Version(), newTree.Version(), "Tree version mismatch")
tree.Iterate(func(key, value []byte) bool { //nolint:errcheck
index, _, err := tree.GetWithIndex(key)
require.NoError(t, err)
newIndex, newValue, err := newTree.GetWithIndex(key)
require.NoError(t, err)
require.Equal(t, index, newIndex, "Index mismatch for key %v", key)
require.Equal(t, value, newValue, "Value mismatch for key %v", key)
return false
})
})
}
}
func TestExporter_Close(t *testing.T) {
tree := setupExportTreeSized(t, 4096)
exporter, err := tree.Export()
require.NoError(t, err)
node, err := exporter.Next()
require.NoError(t, err)
require.NotNil(t, node)
exporter.Close()
node, err = exporter.Next()
require.Error(t, err)
require.Equal(t, ErrorExportDone, err)
require.Nil(t, node)
node, err = exporter.Next()
require.Error(t, err)
require.Equal(t, ErrorExportDone, err)
require.Nil(t, node)
exporter.Close()
exporter.Close()
}
func TestExporter_DeleteVersionErrors(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
require.NoError(t, err)
_, err = tree.Set([]byte("a"), []byte{1})
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, err = tree.Set([]byte("b"), []byte{2})
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, err = tree.Set([]byte("c"), []byte{3})
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
itree, err := tree.GetImmutable(2)
require.NoError(t, err)
exporter, err := itree.Export()
require.NoError(t, err)
defer exporter.Close()
err = tree.DeleteVersionsTo(1)
require.NoError(t, err)
err = tree.DeleteVersionsTo(2)
require.Error(t, err)
exporter.Close()
err = tree.DeleteVersionsTo(2)
require.NoError(t, err)
}
func BenchmarkExport(b *testing.B) {
b.StopTimer()
tree := setupExportTreeSized(b, 4096)
b.StartTimer()
for n := 0; n < b.N; n++ {
exporter, err := tree.Export()
require.NoError(b, err)
for {
_, err := exporter.Next()
if err == ErrorExportDone {
break
} else if err != nil {
b.Error(err)
}
}
exporter.Close()
}
}