forked from eileen-code4fun/LSM-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_test.go
106 lines (102 loc) · 2.65 KB
/
lstm_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
package lsmt
import (
"fmt"
"reflect"
"sync"
"testing"
"time"
)
func TestInMemoryOnly(t *testing.T) {
var wg sync.WaitGroup
var expected []Element
total := 10
tree := NewLSMTree(total+1 /* flush threshold larger than total */)
for i := 0; i < total; i ++ {
e := Element{Key: fmt.Sprintf("%d", i), Value: fmt.Sprintf("%d", i)}
expected = append(expected, e)
wg.Add(1)
go func(){
tree.Put(e.Key, e.Value)
wg.Done()
}()
}
wg.Wait()
if tree.tree.Size != total {
t.Errorf("got tree size %d; want %d", tree.tree.Size, total)
}
for i := 0; i < total; i ++ {
wg.Add(1)
e := fmt.Sprintf("%d", i)
go func() {
v, err := tree.Get(e)
if err != nil {
t.Errorf("key %s not found", e)
}
if v != e {
t.Errorf("got %s for key %s; want %s", v, e, e)
}
wg.Done()
}()
}
wg.Wait()
got := Traverse(tree.tree)
if !reflect.DeepEqual(expected, got) {
t.Errorf("got result %v; want %v", got, expected)
}
}
func TestFlushedToDisk(t *testing.T) {
t.Parallel()
tree := NewLSMTree(2)
tree.Put("1", "One")
tree.Put("2", "Two")
// Wait for flush.
time.Sleep(1 * time.Second)
if tree.tree != nil {
t.Errorf("got tree size %d; want empty", tree.tree.Size)
}
if len(tree.diskFiles) != 1 {
t.Errorf("got disk file size %d; want 1", len(tree.diskFiles))
}
if _, err := tree.Get("1"); err != nil {
t.Error("key 1 not found")
}
if _, err := tree.Get("2"); err != nil {
t.Error("key 2 not found")
}
tree.Put("3", "Three")
if _, err := tree.Get("3"); err != nil {
t.Error("key 3 not found")
}
tree.Put("4", "Four")
// Wait for flush and compaction.
time.Sleep(3 * time.Second)
if len(tree.diskFiles) != 1 {
t.Errorf("got disk file size %d; want 1", len(tree.diskFiles))
}
if len(tree.diskFiles) == 1 {
got := tree.diskFiles[0].AllElements()
want := []Element{{Key: "1", Value: "One"}, {Key: "2", Value: "Two"}, {Key: "3", Value: "Three"}, {Key: "4", Value: "Four"}}
if !reflect.DeepEqual(want, got) {
t.Errorf("got result %v; want %v", got, want)
}
}
}
func TestCompactionCollapse(t *testing.T) {
t.Parallel()
tree := NewLSMTree(1)
tree.Put("1", "One")
time.Sleep(time.Second)
tree.Put("1", "ONE")
// Wait for flush and compaction.
time.Sleep(3 * time.Second)
if len(tree.diskFiles) != 1 {
t.Errorf("got disk file size %d; want 1", len(tree.diskFiles))
}
if len(tree.diskFiles) == 1 {
got := tree.diskFiles[0].AllElements()
want := []Element{{Key: "1", Value: "ONE"}}
if !reflect.DeepEqual(want, got) {
t.Errorf("got result %v; want %v", got, want)
}
}
}