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
|
package bolt
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
// Ensure that a branch can replace a key.
func TestBranchPutReplace(t *testing.T) {
b := &branch{
items: branchItems{
branchItem{pgid: 1, key: []byte("bar")},
branchItem{pgid: 2, key: []byte("baz")},
branchItem{pgid: 3, key: []byte("foo")},
},
}
b.put(1, 4, []byte("bar"), true)
b.put(2, 5, []byte("boo"), true)
assert.Equal(t, len(b.items), 3)
assert.Equal(t, b.items[0].pgid, pgid(4))
assert.Equal(t, string(b.items[0].key), "bar")
assert.Equal(t, b.items[1].pgid, pgid(5))
assert.Equal(t, string(b.items[1].key), "boo")
assert.Equal(t, b.items[2].pgid, pgid(3))
assert.Equal(t, string(b.items[2].key), "foo")
}
// Ensure that a branch can insert a key.
func TestBranchPutInsert(t *testing.T) {
b := &branch{
items: branchItems{
branchItem{pgid: 1, key: []byte("bar")},
branchItem{pgid: 2, key: []byte("foo")},
},
}
b.put(1, 4, []byte("baz"), false)
b.put(2, 5, []byte("zzz"), false)
assert.Equal(t, len(b.items), 4)
assert.Equal(t, b.items[0].pgid, pgid(1))
assert.Equal(t, string(b.items[0].key), "bar")
assert.Equal(t, b.items[1].pgid, pgid(4))
assert.Equal(t, string(b.items[1].key), "baz")
assert.Equal(t, b.items[2].pgid, pgid(2))
assert.Equal(t, string(b.items[2].key), "foo")
assert.Equal(t, b.items[3].pgid, pgid(5))
assert.Equal(t, string(b.items[3].key), "zzz")
}
// Ensure that a branch can deserialize from a page.
func TestBranchRead(t *testing.T) {
// Create a page.
var buf [4096]byte
page := (*page)(unsafe.Pointer(&buf[0]))
page.count = 2
// Insert 2 items at the beginning. sizeof(bnode) == 16
nodes := (*[3]bnode)(unsafe.Pointer(&page.ptr))
nodes[0] = bnode{pos: 32, ksize: 3, pgid: 100} // pos = sizeof(bnode) * 2
nodes[1] = bnode{pos: 19, ksize: 10, pgid: 101} // pos = sizeof(bnode) + 3
// Write data for the nodes at the end.
data := (*[4096]byte)(unsafe.Pointer(&nodes[2]))
copy(data[:], []byte("bar"))
copy(data[3:], []byte("helloworld"))
// Deserialize page into a branch.
b := &branch{}
b.read(page)
// Check that there are two items with correct data.
assert.Equal(t, len(b.items), 2)
assert.Equal(t, b.items[0].key, []byte("bar"))
assert.Equal(t, b.items[1].key, []byte("helloworld"))
}
// Ensure that a branch can serialize itself.
func TestBranchWrite(t *testing.T) {
b := &branch{
items: branchItems{
branchItem{pgid: 1, key: []byte("susy")},
branchItem{pgid: 2, key: []byte("ricki")},
branchItem{pgid: 3, key: []byte("john")},
},
}
// Write it to a page.
var buf [4096]byte
p := (*page)(unsafe.Pointer(&buf[0]))
b.write(p)
// Read the page back in.
b2 := &branch{}
b2.read(p)
// Check that the two pages are the same.
assert.Equal(t, len(b2.items), 3)
assert.Equal(t, b2.items[0].pgid, pgid(1))
assert.Equal(t, b2.items[0].key, []byte("susy"))
assert.Equal(t, b2.items[1].pgid, pgid(2))
assert.Equal(t, b2.items[1].key, []byte("ricki"))
assert.Equal(t, b2.items[2].pgid, pgid(3))
assert.Equal(t, b2.items[2].key, []byte("john"))
}
// Ensure that a branch can split into appropriate subgroups.
func TestBranchSplit(t *testing.T) {
// Create a branch.
b := &branch{
items: branchItems{
branchItem{pgid: 1, key: []byte("00000001")},
branchItem{pgid: 2, key: []byte("00000002")},
branchItem{pgid: 3, key: []byte("00000003")},
branchItem{pgid: 4, key: []byte("00000004")},
branchItem{pgid: 5, key: []byte("00000005")},
},
}
// Split between 3 & 4.
branches := b.split(100)
assert.Equal(t, len(branches), 2)
assert.Equal(t, len(branches[0].items), 2)
assert.Equal(t, len(branches[1].items), 3)
}
// Ensure that a branch with the minimum number of items just returns a single branch.
func TestBranchSplitWithMinKeys(t *testing.T) {
// Create a branch.
b := &branch{
items: branchItems{
branchItem{pgid: 1, key: []byte("00000001")},
branchItem{pgid: 2, key: []byte("00000002")},
},
}
// Split.
branches := b.split(20)
assert.Equal(t, len(branches), 1)
assert.Equal(t, len(branches[0].items), 2)
}
// Ensure that a branch that has keys that all fit on a page just returns one branch.
func TestBranchSplitFitsInPage(t *testing.T) {
// Create a branch.
b := &branch{
items: branchItems{
branchItem{pgid: 1, key: []byte("00000001")},
branchItem{pgid: 2, key: []byte("00000002")},
branchItem{pgid: 3, key: []byte("00000003")},
branchItem{pgid: 4, key: []byte("00000004")},
branchItem{pgid: 5, key: []byte("00000005")},
},
}
// Split.
branches := b.split(4096)
assert.Equal(t, len(branches), 1)
assert.Equal(t, len(branches[0].items), 5)
}
|