-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtidesdb_test.go
451 lines (381 loc) · 12.1 KB
/
tidesdb_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Package tidesdb_go
// Copyright (C) TidesDB
//
// Original Author: Alex Gaetano Padula
//
// Licensed under the Mozilla Public License, v. 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
//
// https://www.mozilla.org/en-US/MPL/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.
package tidesdb_go
import (
"bytes"
"encoding/gob"
"fmt"
"os"
"strings"
"testing"
)
func TestOpenClose(t *testing.T) {
defer os.RemoveAll("testdb")
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
if err := db.Close(); err != nil {
t.Fatalf("Failed to close database: %v", err)
}
}
func TestCreateDropColumnFamily(t *testing.T) {
defer os.RemoveAll("testdb")
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
err = db.CreateColumnFamily("test_cf", 1024*1024*64, 12, 0.24, true, int(TDB_COMPRESS_SNAPPY), true)
if err != nil {
t.Fatalf("Failed to create column family: %v", err)
}
err = db.DropColumnFamily("test_cf")
if err != nil {
t.Fatalf("Failed to drop column family: %v", err)
}
}
type TestStruct struct {
Name string
Age int
}
func TestPutGetDelete(t *testing.T) {
defer os.RemoveAll("testdb")
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
err = db.CreateColumnFamily("test_cf", 1024*1024*64, 12, 0.24, true, int(TDB_COMPRESS_SNAPPY), true)
if err != nil {
t.Fatalf("Failed to create column family: %v", err)
}
defer func(db *TidesDB, name string) {
err := db.DropColumnFamily(name)
if err != nil {
}
}(db, "test_cf")
s := &TestStruct{
Name: "John Doe",
Age: 30,
}
b := make([]byte, 0)
buff := bytes.NewBuffer(b)
err = gob.NewEncoder(buff).Encode(s)
if err != nil {
return
}
key := []byte("key")
err = db.Put("test_cf", key, buff.Bytes(), -1)
if err != nil {
t.Fatalf("Failed to put key-value pair: %v", err)
}
gotValue, err := db.Get("test_cf", key)
if err != nil {
t.Fatalf("Failed to get value: %v", err)
}
// decode the value
var ts TestStruct
err = gob.NewDecoder(bytes.NewBuffer(gotValue)).Decode(&ts)
if ts.Name != s.Name || ts.Age != s.Age {
t.Fatalf("Expected value %v, got %v", s, ts)
}
err = db.Delete("test_cf", key)
if err != nil {
t.Fatalf("Failed to delete key: %v", err)
}
}
func TestCompactSSTables(t *testing.T) {
// @TODO
}
func TestTransaction(t *testing.T) {
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
err = db.CreateColumnFamily("test_cf", 1024*1024*64, 12, 0.24, true, int(TDB_COMPRESS_SNAPPY), true)
if err != nil {
t.Fatalf("Failed to create column family: %v", err)
}
defer func(db *TidesDB, name string) {
err := db.DropColumnFamily(name)
if err != nil {
t.Fatalf("Failed to drop column family: %v", err)
}
}(db, "test_cf")
txn, err := db.BeginTxn("test_cf")
if err != nil {
t.Fatalf("Failed to begin transaction: %v", err)
}
defer txn.Free()
key := []byte("key")
value := []byte("value")
err = txn.Put(key, value, -1)
if err != nil {
t.Fatalf("Failed to put key-value pair in transaction: %v", err)
}
err = txn.Commit()
if err != nil {
t.Fatalf("Failed to commit transaction: %v", err)
}
// Check if the key-value pair was added to the database
gotValue, err := db.Get("test_cf", key)
if err != nil {
t.Fatalf("Failed to get value: %v", err)
}
if string(gotValue) != string(value) {
t.Fatalf("Expected value %s, got %s", value, gotValue)
}
}
func TestRange(t *testing.T) {
defer os.RemoveAll("testdb")
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
err = db.CreateColumnFamily("test_cf", 1024*1024*64, 12, 0.24, true, int(TDB_COMPRESS_SNAPPY), true)
if err != nil {
t.Fatalf("Failed to create column family: %v", err)
}
defer db.DropColumnFamily("test_cf")
// Insert several key-value pairs with clear min/max boundaries
pairs := map[string]string{
"a_key1": "value1",
"b_key2": "value2",
"c_key3": "value3",
"d_key4": "value4",
"e_key5": "value5",
}
// Insert in sorted order to ensure predictable range results
keys := []string{"a_key1", "b_key2", "c_key3", "d_key4", "e_key5"}
for _, key := range keys {
err = db.Put("test_cf", []byte(key), []byte(pairs[key]), -1)
if err != nil {
t.Fatalf("Failed to put key-value pair: %v", err)
}
}
// Test specific range query (b_key2 to d_key4)
rangePairs, err := db.Range("test_cf", []byte("b_key2"), []byte("d_key4"))
if err != nil {
t.Fatalf("Failed to get range: %v", err)
}
// We expect b_key2, c_key3, d_key4 to be returned
expectedCount := 3
if len(rangePairs) != expectedCount {
t.Fatalf("Expected %d pairs, got %d", expectedCount, len(rangePairs))
}
// Verify the returned key-value pairs
for i, pair := range rangePairs {
key := string(pair[0])
value := string(pair[1])
expectedKey := keys[i+1] // Start from b_key2
expectedValue := pairs[expectedKey]
if key != expectedKey || value != expectedValue {
t.Fatalf("Expected (%s, %s), got (%s, %s)", expectedKey, expectedValue, key, value)
}
}
// Test range query for a prefix (all keys starting with "c_")
rangePairs, err = db.Range("test_cf", []byte("c_"), []byte("c`")) // '`' is just after '_' in ASCII
if err != nil {
t.Fatalf("Failed to get prefix range: %v", err)
}
// We expect only c_key3 to be returned
expectedCount = 1
if len(rangePairs) != expectedCount {
t.Fatalf("Expected %d pairs for prefix query, got %d", expectedCount, len(rangePairs))
}
// Test range query with minimum key to a specific key
rangePairs, err = db.Range("test_cf", []byte("a_key1"), []byte("c_key3"))
if err != nil {
t.Fatalf("Failed to get range from minimum: %v", err)
}
// We expect a_key1, b_key2, c_key3 to be returned
expectedCount = 3
if len(rangePairs) != expectedCount {
t.Fatalf("Expected %d pairs for min range, got %d", expectedCount, len(rangePairs))
}
// Test range query with a specific key to maximum key
rangePairs, err = db.Range("test_cf", []byte("d_key4"), []byte("e_key5"))
if err != nil {
t.Fatalf("Failed to get range to maximum: %v", err)
}
// We expect d_key4, e_key5 to be returned
expectedCount = 2
if len(rangePairs) != expectedCount {
t.Fatalf("Expected %d pairs for max range, got %d", expectedCount, len(rangePairs))
}
// Test full range query (first to last key)
rangePairs, err = db.Range("test_cf", []byte("a_key1"), []byte("e_key5"))
if err != nil {
t.Fatalf("Failed to get full range: %v", err)
}
// We expect all 5 keys to be returned
expectedCount = 5
if len(rangePairs) != expectedCount {
t.Fatalf("Expected %d pairs for full range, got %d", expectedCount, len(rangePairs))
}
}
func TestDeleteByRange(t *testing.T) {
defer os.RemoveAll("testdb")
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
err = db.CreateColumnFamily("test_cf", 1024*1024*64, 12, 0.24, true, int(TDB_COMPRESS_SNAPPY), true)
if err != nil {
t.Fatalf("Failed to create column family: %v", err)
}
defer db.DropColumnFamily("test_cf")
// Insert several key-value pairs with clear min/max boundaries
pairs := map[string]string{
"a_key1": "value1",
"b_key2": "value2",
"c_key3": "value3",
"d_key4": "value4",
"e_key5": "value5",
}
// Insert in sorted order
keys := []string{"a_key1", "b_key2", "c_key3", "d_key4", "e_key5"}
for _, key := range keys {
err = db.Put("test_cf", []byte(key), []byte(pairs[key]), -1)
if err != nil {
t.Fatalf("Failed to put key-value pair: %v", err)
}
}
// Delete a range of keys (b_key2 to d_key4)
err = db.DeleteByRange("test_cf", []byte("b_key2"), []byte("d_key4"))
if err != nil {
t.Fatalf("Failed to delete range: %v", err)
}
// Verify that the keys in the range were deleted
for _, key := range []string{"b_key2", "c_key3", "d_key4"} {
_, err := db.Get("test_cf", []byte(key))
if err == nil {
t.Fatalf("Key %s should be deleted but still exists", key)
}
if !strings.Contains(err.Error(), "not found") {
t.Fatalf("Unexpected error when getting deleted key: %v", err)
}
}
// Verify that keys outside the range still exist
for _, key := range []string{"a_key1", "e_key5"} {
val, err := db.Get("test_cf", []byte(key))
if err != nil {
t.Fatalf("Key %s should exist but got error: %v", key, err)
}
if string(val) != pairs[key] {
t.Fatalf("Expected value %s for key %s, got %s", pairs[key], key, string(val))
}
}
// Test deleting a non-existent range (should succeed without error)
err = db.DeleteByRange("test_cf", []byte("x_key"), []byte("z_key"))
if err != nil {
t.Fatalf("DeleteByRange for non-existent range should succeed, got error: %v", err)
}
// Test deleting a range in a non-existent column family (should fail)
err = db.DeleteByRange("nonexistent_cf", []byte("a_key"), []byte("b_key"))
if err == nil {
t.Fatalf("DeleteByRange in non-existent column family should fail")
}
}
func TestGetColumnFamilyStat(t *testing.T) {
defer os.RemoveAll("testdb")
db, err := Open("testdb")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
// Create a column family with specific settings
err = db.CreateColumnFamily("test_cf", (1024*1024)*2, 12, 0.24, true, int(TDB_COMPRESS_SNAPPY), true)
if err != nil {
t.Fatalf("Failed to create column family: %v", err)
}
// Add some data to create SSTables
for i := 0; i < 100; i++ {
key := []byte(fmt.Sprintf("key%d", i))
value := []byte(fmt.Sprintf("value%d", i))
for len(value) < 1024*256 {
value = append(value, 0)
}
err = db.Put("test_cf", key, value, -1)
if err != nil {
t.Fatalf("Failed to put key-value pair: %v", err)
}
}
// Get column family statistics
stat, err := db.GetColumnFamilyStat("test_cf")
if err != nil {
t.Fatalf("Failed to get column family statistics: %v", err)
}
// Verify column family config
if stat.Config.Name != "test_cf" {
t.Errorf("Expected column family name 'test_cf', got '%s'", stat.Config.Name)
}
if stat.Config.FlushThreshold != (1024*1024)*2 {
t.Errorf("Expected flush threshold %d, got %d", (1024*1024)*2, stat.Config.FlushThreshold)
}
if stat.Config.MaxLevel != 12 {
t.Errorf("Expected max level 12, got %d", stat.Config.MaxLevel)
}
if !stat.Config.Compressed {
t.Errorf("Expected compressed to be true, got false")
}
if stat.Config.CompressAlgo != TDB_COMPRESS_SNAPPY {
t.Errorf("Expected compression algorithm %d, got %d", TDB_COMPRESS_SNAPPY, stat.Config.CompressAlgo)
}
if !stat.Config.BloomFilter {
t.Errorf("Expected bloom filter to be true, got false")
}
// Verify column family name
if stat.Name != "test_cf" {
t.Errorf("Expected column family name 'test_cf', got '%s'", stat.Name)
}
// Verify memtable stats (values will vary, just check existence)
if stat.MemtableSize <= 0 {
t.Logf("Memtable size: %d", stat.MemtableSize)
}
if stat.MemtableEntryCount <= 0 {
t.Logf("Memtable entries count: %d", stat.MemtableEntryCount)
}
// Check if there are SSTables (may not be any if memtable hasn't been flushed)
t.Logf("Number of SSTables: %d", stat.NumSSTables)
// If there are SSTables, verify their stats
if stat.NumSSTables > 0 {
for i, sstStat := range stat.SSTableStats {
if sstStat.Path == "" {
t.Errorf("SSTable %d has empty path", i)
}
if sstStat.Size <= 0 {
t.Errorf("SSTable %d has invalid size: %d", i, sstStat.Size)
}
if sstStat.NumBlocks <= 0 {
t.Errorf("SSTable %d has invalid block count: %d", i, sstStat.NumBlocks)
}
t.Logf("SSTable %d: Path=%s, Size=%d, NumBlocks=%d",
i, sstStat.Path, sstStat.Size, sstStat.NumBlocks)
}
}
// Test non-existent column family
_, err = db.GetColumnFamilyStat("nonexistent_cf")
if err == nil {
t.Fatalf("Expected error when getting stats for non-existent column family")
}
}
// More tests to be added...