-
Notifications
You must be signed in to change notification settings - Fork 7
/
merge_test.go
85 lines (71 loc) · 1.74 KB
/
merge_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
package lsmtree
import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"testing"
)
func TestMergeDiskTables(t *testing.T) {
dbDir, err := ioutil.TempDir(os.TempDir(), "example")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := os.RemoveAll(dbDir); err != nil {
panic(fmt.Errorf("failed to remove %s: %w", dbDir, err))
}
}()
if err := createDiskTable(prepareMemTable1(), dbDir, 0, 3); err != nil {
t.Fatal(err)
}
if err := createDiskTable(prepareMemTable2(), dbDir, 1, 3); err != nil {
t.Fatal(err)
}
if err := mergeDiskTables(dbDir, 0, 1, 3); err != nil {
t.Fatal(err)
}
it, err := newDataFileIterator(path.Join(dbDir, "1-data.db"))
if err != nil {
t.Fatal(err)
}
actual := make([][]byte, 0)
for it.hasNext() {
key, value, err := it.next()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
actual = append(actual, key, value)
}
expected := [][]byte{
[]byte("b"), []byte("vb2"),
[]byte("c"), []byte("vc"),
[]byte("d"), nil,
[]byte("e"), []byte("ve"),
[]byte("f"), []byte("vf2"),
[]byte("g"), []byte("vg"),
[]byte("h"), []byte("vh"),
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("%s != %s", expected, actual)
}
}
func prepareMemTable1() *memTable {
memTable := newMemTable()
memTable.put([]byte("b"), []byte("vb1"))
memTable.put([]byte("c"), []byte("vc"))
memTable.put([]byte("d"), []byte("vd"))
memTable.put([]byte("e"), []byte("ve"))
memTable.put([]byte("f"), []byte("vf1"))
memTable.put([]byte("g"), []byte("vg"))
memTable.put([]byte("h"), []byte("vh"))
return memTable
}
func prepareMemTable2() *memTable {
memTable := newMemTable()
memTable.put([]byte("b"), []byte("vb2"))
memTable.delete([]byte("d"))
memTable.put([]byte("f"), []byte("vf2"))
return memTable
}