-
Notifications
You must be signed in to change notification settings - Fork 7
/
table_test.go
222 lines (194 loc) · 5.09 KB
/
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package diskhash
import (
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func destroyTable(t *Table) {
_ = t.Close()
_ = os.RemoveAll(t.options.DirPath)
}
func GetTestKey(i int) []byte {
return []byte(fmt.Sprintf("diskhash-test-key-%09d", i))
}
func TestOpen(t *testing.T) {
dir, err := os.MkdirTemp("", "diskhash-test-open")
assert.Nil(t, err)
options := DefaultOptions
options.DirPath = dir
options.SlotValueLength = 10
table, err := Open(options)
assert.Nil(t, err)
defer destroyTable(table)
err = table.Close()
assert.Nil(t, err)
}
func TestTable_Put(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, false, false)
})
t.Run("20B-100000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, false, false)
})
t.Run("1K-50000", func(t *testing.T) {
testTableBaisc(t, 1024, 50000, false, false, false)
})
t.Run("4K-50000", func(t *testing.T) {
testTableBaisc(t, 4*1024, 50000, false, false, false)
})
}
func TestTable_Get(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, true, false, false)
})
t.Run("20B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, true, false, false)
})
t.Run("1K-50000", func(t *testing.T) {
testTableBaisc(t, 1024, 50000, true, false, false)
})
t.Run("4K-50000", func(t *testing.T) {
testTableBaisc(t, 4*1024, 50000, true, false, false)
})
}
func TestTable_Delete(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, true, false)
})
t.Run("20B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, true, false)
})
t.Run("1K-50000", func(t *testing.T) {
testTableBaisc(t, 1024, 50000, false, true, false)
})
t.Run("4K-50000", func(t *testing.T) {
testTableBaisc(t, 4*1024, 50000, false, true, false)
})
}
func TestTable_Update(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, false, true)
})
t.Run("20B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, false, true)
})
t.Run("1K-50000", func(t *testing.T) {
testTableBaisc(t, 1024, 50000, false, false, true)
})
t.Run("4K-50000", func(t *testing.T) {
testTableBaisc(t, 4*1024, 50000, false, false, true)
})
}
func testTableBaisc(t *testing.T, valueLen uint32, count int, needGet, needDelete, needUpdate bool) {
dir, err := os.MkdirTemp("", "diskhash-test")
assert.Nil(t, err)
options := DefaultOptions
options.DirPath = dir
options.SlotValueLength = valueLen
table, err := Open(options)
assert.Nil(t, err)
defer destroyTable(table)
value := []byte(strings.Repeat("D", int(valueLen)))
for i := 0; i < count; i++ {
key := GetTestKey(i)
err = table.Put(key, value, func(slot Slot) (bool, error) {
return false, nil
})
assert.Nil(t, err)
}
getValue := func(target []byte) {
for i := 0; i < count; i++ {
key := GetTestKey(i)
var res []byte
matchKey := func(slot Slot) (bool, error) {
if getKeyHash(key) == slot.Hash {
res = make([]byte, len(slot.Value))
copy(res, slot.Value)
return true, nil
}
return false, nil
}
err := table.Get(key, matchKey)
assert.Equal(t, target, res)
assert.Nil(t, err)
}
}
if needGet {
getValue(value)
}
if needDelete {
assert.Equal(t, uint32(count), table.Size())
for i := 0; i < count; i++ {
key := GetTestKey(i)
matchKey := func(slot Slot) (bool, error) {
if getKeyHash(key) == slot.Hash {
return true, nil
}
return false, nil
}
err := table.Delete(key, matchKey)
assert.Nil(t, err)
}
assert.Equal(t, uint32(0), table.Size())
getValue(nil)
}
if needUpdate {
assert.Equal(t, uint32(count), table.Size())
newValue := []byte(strings.Repeat("H", int(valueLen)))
for i := 0; i < count; i++ {
key := GetTestKey(i)
matchKey := func(slot Slot) (bool, error) {
if getKeyHash(key) == slot.Hash {
return true, nil
}
return false, nil
}
err := table.Put(key, newValue, matchKey)
assert.Nil(t, err)
}
getValue(newValue)
assert.Equal(t, uint32(count), table.Size())
}
}
func TestTableCRUD(t *testing.T) {
dir, err := os.MkdirTemp("", "diskhash-test-crud")
assert.Nil(t, err)
options := DefaultOptions
options.DirPath = dir
options.SlotValueLength = 32
table, err := Open(options)
assert.Nil(t, err)
defer destroyTable(table)
for i := 0; i < 100; i++ {
var cur []byte
getFunc := func(slot Slot) (bool, error) {
cur = slot.Value
return false, nil
}
updateFunc := func(slot Slot) (bool, error) {
return false, nil
}
key := GetTestKey(i)
value := []byte(strings.Repeat("D", 32))
// put
err = table.Put(key, value, updateFunc)
assert.Nil(t, err)
// get
err = table.Get(key, getFunc)
assert.Nil(t, err)
assert.Equal(t, value, cur)
// put different value
value = []byte(strings.Repeat("A", 32))
err = table.Put(key, value, updateFunc)
assert.Nil(t, err)
// get after put different value
err = table.Get(key, getFunc)
assert.Nil(t, err)
assert.Equal(t, value, cur)
// delete
err = table.Delete(key, updateFunc)
assert.Nil(t, err)
}
}