-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst_table_test.go
193 lines (170 loc) · 4.27 KB
/
sst_table_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
package mitkv
import (
"fmt"
"os"
"strconv"
"testing"
avl "github.com/emirpasic/gods/trees/avltree"
)
func TestWriteFile(t *testing.T) {
sstMeta := SSTTableMetaInfo{
Version: 1001,
DataStart: 1002,
DataLen: 1003,
IndexStart: 1004,
IndexLen: 1005,
PartSize: 1006,
}
// 可写方式打开文件
file, err := os.OpenFile(
"test.txt",
os.O_APPEND|os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
0666,
)
if err != nil {
t.Fatal(err)
}
defer file.Close() //nolint:errcheck
// 写字节到文件中
versionByte := Int64ToBytes(sstMeta.Version)
// byteSlice := []byte("Bytes!\n")
bytesWritten, err := file.Write(versionByte)
if err != nil {
t.Fatal(err)
}
t.Log(bytesWritten)
dataStartByte := Int64ToBytes(sstMeta.DataStart)
bytesWritten, err = file.Write(dataStartByte)
if err != nil {
t.Fatal(err)
}
t.Log(bytesWritten)
dataLenByte := Int64ToBytes(sstMeta.DataLen)
bytesWritten, err = file.Write(dataLenByte)
if err != nil {
t.Fatal(err)
}
t.Log(bytesWritten)
indexStartByte := Int64ToBytes(sstMeta.IndexStart)
bytesWritten, err = file.Write(indexStartByte)
if err != nil {
t.Fatal(err)
}
t.Log(bytesWritten)
indexLenByte := Int64ToBytes(sstMeta.IndexLen)
bytesWritten, err = file.Write(indexLenByte)
if err != nil {
t.Fatal(err)
}
t.Log(bytesWritten)
partSizeByte := Int64ToBytes(sstMeta.PartSize)
bytesWritten, err = file.Write(partSizeByte)
if err != nil {
t.Fatal(err)
}
t.Log(bytesWritten)
}
func TestReadFile(t *testing.T) {
sstMeta := SSTTableMetaInfo{}
// Open file for reading
file, err := os.Open("test.txt")
if err != nil {
t.Fatal(err)
}
defer file.Close()
// file.Read()可以读取一个小文件到大的byte slice中,
// 但是io.ReadFull()在文件的字节数小于byte slice字节数的时候会返回错误
// 从文件中读取len(b)字节的文件。
// 返回0字节意味着读取到文件尾了
// 读取到文件会返回io.EOF的error
byteSlice := make([]byte, 8)
bytesRead, err := file.Read(byteSlice)
if err != nil {
t.Fatal(err)
}
sstMeta.Version = BytesToInt64(byteSlice)
t.Log(sstMeta.Version)
t.Log(bytesRead)
file.Seek(8, 0)
byteSlice = make([]byte, 8)
bytesRead, err = file.Read(byteSlice)
if err != nil {
t.Fatal(err)
}
sstMeta.DataStart = BytesToInt64(byteSlice)
t.Log(sstMeta.DataStart)
t.Log(bytesRead)
file.Seek(16, 0)
byteSlice = make([]byte, 8)
bytesRead, err = file.Read(byteSlice)
if err != nil {
t.Fatal(err)
}
sstMeta.DataLen = BytesToInt64(byteSlice)
t.Log(sstMeta.DataLen)
t.Log(bytesRead)
file.Seek(24, 0)
byteSlice = make([]byte, 8)
bytesRead, err = file.Read(byteSlice)
if err != nil {
t.Fatal(err)
}
sstMeta.IndexStart = BytesToInt64(byteSlice)
t.Log(sstMeta.IndexStart)
t.Log(bytesRead)
file.Seek(32, 0)
byteSlice = make([]byte, 8)
bytesRead, err = file.Read(byteSlice)
if err != nil {
t.Fatal(err)
}
sstMeta.IndexLen = BytesToInt64(byteSlice)
t.Log(sstMeta.IndexLen)
t.Log(bytesRead)
file.Seek(40, 0) //nolint:errcheck
byteSlice = make([]byte, 8)
bytesRead, err = file.Read(byteSlice)
if err != nil {
t.Fatal(err)
}
sstMeta.PartSize = BytesToInt64(byteSlice)
t.Log(sstMeta.PartSize)
t.Log(bytesRead)
}
func TestAvlTree(t *testing.T) {
tree := avl.NewWithStringComparator() // empty(keys are of type int)
tree.Put("1", "x") // 1->x
tree.Put("2", "b") // 1->x, 2->b (in order)
tree.Put("1", "a") // 1->a, 2->b (in order, replacement)
tree.Put("3", "c") // 1->a, 2->b, 3->c (in order)
tree.Put("4", "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put("5", "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put("6", "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
}
func TestCreateFromIndex(t *testing.T) {
indexTree := avl.NewWithStringComparator()
for i := 0; i < 10; i++ {
cmd := Cmd{
Key: "key" + strconv.Itoa(i),
Val: "value" + strconv.Itoa(i),
CmdType: SET,
}
indexTree.Put(cmd.Key, cmd)
}
CreateFromIndex("test.txt", 3, *indexTree) //nolint:errcheck
}
func TestCreateFromFile(t *testing.T) {
CreateFromFile("test.txt") //nolint:errcheck
}
func TestQuery(t *testing.T) {
sst, err := CreateFromFile("test.txt")
if err != nil {
t.Fatal(err)
}
t.Log(sst.query("key0"))
t.Log(sst.query("key1"))
t.Log(sst.query("key2"))
t.Log(sst.query("key3"))
t.Log(sst.query("key11111"))
}