-
Notifications
You must be signed in to change notification settings - Fork 2
/
testlib.go
135 lines (118 loc) · 3 KB
/
testlib.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
// Copyright (c) 2013 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
// Common functions used across test cases.
package btree
import (
"bufio"
"bytes"
"fmt"
"math/rand"
"os"
"time"
)
var _ = fmt.Sprintln(time.Now())
var testconf1 = Config{
Idxfile: "./data/index_datafile.dat",
Kvfile: "./data/appendkv_datafile.dat",
IndexConfig: IndexConfig{
Sectorsize: 512,
Flistsize: 1000 * OFFSET_SIZE,
Blocksize: 4 * 1024,
},
Maxlevel: 6,
RebalanceThrs: 6,
AppendRatio: 0.7,
DrainRate: 10,
MaxLeafCache: 1000,
Sync: false,
Nocache: false,
}
type TestKey struct {
K string
Id int64
}
type TestValue struct {
V string
}
func (tk *TestKey) Bytes() []byte {
return []byte(tk.K)
}
func (tk *TestKey) Docid() []byte {
return []byte(fmt.Sprintf("%020v", tk.Id))
}
func (tk *TestKey) CompareLess(s *Store, kfp, dfp int64, isD bool) (int, int64, int64) {
var otherk, otherd []byte
var cmp int
otherk = s.fetchKey(kfp)
// Compare
if cmp = bytes.Compare(tk.Bytes(), otherk); cmp == 0 && isD {
otherd = s.fetchKey(dfp)
cmp = bytes.Compare(tk.Docid(), otherd)
if cmp == 0 {
return cmp, kfp, dfp
} else {
return cmp, kfp, -1
}
} else if cmp == 0 {
return cmp, kfp, -1
} else {
return cmp, -1, -1
}
}
func (tk *TestKey) Equal(otherk []byte, otherd []byte) (bool, bool) {
var keyeq, doceq bool
if otherk == nil {
keyeq = false
} else {
keyeq = bytes.Equal(tk.Bytes(), otherk)
}
if otherd == nil {
doceq = false
} else {
doceq = bytes.Equal(tk.Docid(), otherd)
}
return keyeq, doceq
}
func (tv *TestValue) Bytes() []byte {
return []byte(tv.V)
}
func TestData(count int, seed int64) ([]*TestKey, []*TestValue) {
if seed < 0 {
seed = int64(time.Now().Nanosecond())
}
rnd := rand.New(rand.NewSource(seed))
keys := make([]*TestKey, 0, count)
values := make([]*TestValue, 0, count)
for i := 0; i < count; i++ {
keys = append(keys, &TestKey{RandomKey(rnd), int64(i)})
values = append(values, &TestValue{RandomValue(rnd) + "Value"})
}
return keys, values
}
func testStore(remove bool) *Store {
if remove {
os.Remove("./data/index_datafile.dat")
os.Remove("./data/appendkv_datafile.dat")
}
return NewStore(testconf1)
}
var keys = make([]string, 0)
func RandomKey(rnd *rand.Rand) string {
if len(keys) == 0 {
fd, _ := os.Open("./data/words")
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
keys = append(keys, scanner.Text())
}
}
return keys[rnd.Intn(len(keys))]
}
func RandomValue(rnd *rand.Rand) string {
return RandomKey(rnd)
}