-
Notifications
You must be signed in to change notification settings - Fork 336
/
bucket_manager_test.go
107 lines (93 loc) · 3.36 KB
/
bucket_manager_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
package nutsdb
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestBucketManager_NewBucketAndDeleteBucket(t *testing.T) {
bucket1 := "bucket_1"
bucket2 := "bucket_2"
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
txNewBucket(t, db, bucket1, DataStructureBTree, nil, nil)
exist := db.bm.ExistBucket(DataStructureBTree, bucket1)
assert.Equal(t, true, exist)
txNewBucket(t, db, bucket2, DataStructureBTree, nil, nil)
exist = db.bm.ExistBucket(DataStructureBTree, bucket2)
assert.Equal(t, true, exist)
})
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
txNewBucket(t, db, bucket1, DataStructureBTree, nil, nil)
exist := db.bm.ExistBucket(DataStructureBTree, bucket1)
assert.Equal(t, true, exist)
txDeleteBucketFunc(t, db, bucket1, DataStructureBTree, nil, nil)
exist = db.bm.ExistBucket(DataStructureBTree, bucket1)
assert.Equal(t, false, exist)
})
}
func TestBucketManager_ExistBucket(t *testing.T) {
bucket1 := "bucket_1"
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
exist := db.bm.ExistBucket(DataStructureBTree, bucket1)
assert.Equal(t, false, exist)
txNewBucket(t, db, bucket1, DataStructureBTree, nil, nil)
exist = db.bm.ExistBucket(DataStructureBTree, bucket1)
assert.Equal(t, true, exist)
})
}
func TestBucketManager_Recovery(t *testing.T) {
dir := "/tmp/nutsdb_test_data"
const bucket1 = "bucket_1"
const bucket2 = "bucket_2"
db, err := Open(DefaultOptions, WithDir(dir))
defer removeDir(dir)
assert.NotNil(t, db)
assert.Nil(t, err)
txNewBucket(t, db, bucket1, DataStructureBTree, nil, nil)
txNewBucket(t, db, bucket2, DataStructureBTree, nil, nil)
txDeleteBucketFunc(t, db, bucket1, DataStructureBTree, nil, nil)
db.Close()
db, err = Open(DefaultOptions, WithDir(dir))
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.View(func(tx *Tx) error {
exist := tx.ExistBucket(DataStructureBTree, bucket2)
assert.Equal(t, true, exist)
exist = tx.ExistBucket(DataStructureBTree, bucket1)
assert.Equal(t, false, exist)
return nil
})
assert.Nil(t, err)
}
func TestBucketManager_DataStructureIsolation(t *testing.T) {
const bucket1 = "bucket_1"
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
txCreateBucket(t, db, DataStructureBTree, bucket1, nil)
assert.Equal(t, false, db.bm.ExistBucket(DataStructureList, bucket1))
assert.Equal(t, false, db.bm.ExistBucket(DataStructureSortedSet, bucket1))
assert.Equal(t, false, db.bm.ExistBucket(DataStructureSet, bucket1))
})
}
func TestBucketManager_DeleteBucketIsolation(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
const bucket1 = "bucket_1"
txCreateBucket(t, db, DataStructureBTree, bucket1, nil)
txPut(t, db, bucket1, []byte("key_1"), []byte("value_1"), Persistent, nil, nil)
txDeleteBucket(t, db, DataStructureBTree, bucket1, nil)
txGet(t, db, bucket1, []byte("key_1"), nil, ErrBucketNotExist)
})
}
func txNewBucket(t *testing.T, db *DB, bucket string, ds uint16, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
err2 := tx.NewBucket(ds, bucket)
assertErr(t, expectErr, err2)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txDeleteBucketFunc(t *testing.T, db *DB, bucket string, ds uint16, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
err2 := tx.DeleteBucket(ds, bucket)
assertErr(t, expectErr, err2)
return nil
})
assertErr(t, err, finalExpectErr)
}