-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
144 lines (119 loc) · 3.01 KB
/
db_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
package simpledb
import (
"bytes"
"fmt"
"testing"
"time"
)
// initialize DB
func initializeDB() (*DB, error) {
newMmapFile, err := OpenMmapFile(segmentName(0), 1)
if err != nil {
return nil, err
}
indexMmapFile, err := OpenMmapFile(indexName, 512 * (1 << 10))
if err != nil {
return nil, err
}
db := &DB{
index: &index{
MmapFile: indexMmapFile,
numBucket: BucketLen,
},
datalog: &datalog{
curSeg: &segment{
MmapFile: newMmapFile,
id: 0,
},
},
}
db.datalog.segments[0] = db.datalog.curSeg
return db, nil
}
// Test if the query results matches the data inserted
func TestSimple(t *testing.T) {
// initialize DB
db, err := initializeDB()
if err != nil {
t.Fatal(err)
}
db.Put([]byte("1"), []byte("simple1"))
db.Put([]byte("2"), []byte("这是simple2"))
db.Put([]byte("3"), []byte("玩的愉快"))
getFirst, err := db.Get([]byte("1"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(getFirst, []byte("simple1")) {
t.Fatal("Test for '1'")
}
getSecond, _ := db.Get([]byte("2"))
if !bytes.Equal(getSecond, []byte("这是simple2")) {
t.Fatal("Test for '2'")
}
getThird, _ := db.Get([]byte("3"))
if !bytes.Equal(getThird, []byte("玩的愉快")) {
t.Fatal("Test for '3'")
}
}
// Test if the query for an inexistent data works as expected
func TestInexistence(t *testing.T) {
// initialize DB
db, err := initializeDB()
if err != nil {
t.Fatal(err)
}
getData, _ := db.Get([]byte("0"))
if getData != nil {
t.Fatal("get an inexistent value, but it exists")
}
}
// Test if the segments and buckets are extended correctly,
// when the data is massive
// Watch if the 'Count of buckets' and 'Count of segments' increase as expected
func TestMassiveData(t *testing.T) {
// initialize DB
db, err := initializeDB()
if err != nil {
t.Fatal(err)
}
// suffix is used to enlarge the value,
// so as to test the creation of new segment more quickly
suffix := ""
for i:=0; i<10; i++ {
suffix += "fdhsjkhfjkshfjksahjkfshajkfhaskjfhkjsafjksahjkfajkfjnvnnasm"
}
for j:=0; j<1000; j++ {
for i := 0; i < 5000; i++ {
key := []byte(string(j*5000+i))
value := []byte(string(j*5000+i)+suffix)
db.Put(key, value)
}
fmt.Printf("Count of buckets: %d\n", db.index.numBucket)
fmt.Printf("Count of segments: %d\n", db.datalog.curSeg.id+1)
fmt.Printf("Size of curSeg: %d\n\n", db.datalog.curSeg.FileSize())
}
start := time.Now()
getData, _ := db.Get([]byte(string(10000)))
fmt.Printf("Elapsed time: %v\n", time.Now().Sub(start).String())
if !bytes.Equal(getData, []byte(string(10000)+suffix)) {
t.Fatal("Test for '10000'")
}
}
// Test if the data is updated correctly
func TestUpdateData(t *testing.T) {
// initialize DB
db, err := initializeDB()
if err != nil {
t.Fatal(err)
}
db.Put([]byte("1"), []byte("祝你早安"))
db.Put([]byte("1"), []byte("祝你晚安"))
getFirst, _ := db.Get([]byte("1"))
if bytes.Equal(getFirst, []byte("祝你早安")) {
t.Fatal("Update data")
}
if !bytes.Equal(getFirst, []byte("祝你晚安")) {
t.Fatal("Update data")
}
}