forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
140 lines (121 loc) · 2.68 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
package rosedb
import (
"bytes"
"fmt"
"github.com/flower-corp/rosedb/logger"
"github.com/stretchr/testify/assert"
"math/rand"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
func TestOpen(t *testing.T) {
path := filepath.Join("/tmp", "rosedb")
t.Run("default", func(t *testing.T) {
opts := DefaultOptions(path)
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
})
t.Run("mmap", func(t *testing.T) {
opts := DefaultOptions(path)
opts.IoType = MMap
db, err := Open(opts)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
})
}
func TestLogFileGC(t *testing.T) {
path := filepath.Join("/tmp", "rosedb")
opts := DefaultOptions(path)
opts.LogFileGCInterval = time.Second * 7
opts.LogFileGCRatio = 0.00001
db, err := Open(opts)
defer destroyDB(db)
if err != nil {
t.Error("open db err ", err)
}
writeCount := 800000
for i := 0; i < writeCount; i++ {
err := db.Set(GetKey(i), GetValue16B())
assert.Nil(t, err)
}
var deleted [][]byte
rand.Seed(time.Now().Unix())
for i := 0; i < 100000; i++ {
k := rand.Intn(writeCount)
key := GetKey(k)
err := db.Delete(key)
assert.Nil(t, err)
deleted = append(deleted, key)
}
time.Sleep(time.Second * 12)
for _, key := range deleted {
_, err := db.Get(key)
assert.Equal(t, err, ErrKeyNotFound)
}
}
func TestRoseDB_Backup(t *testing.T) {
path := filepath.Join("/tmp", "rosedb")
opts := DefaultOptions(path)
db, err := Open(opts)
defer destroyDB(db)
if err != nil {
t.Error("open db err ", err)
}
for i := 0; i < 10; i++ {
err := db.Set(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
backupPath := filepath.Join("/tmp", "rosedb-backup")
err = db.Backup(backupPath)
assert.Nil(t, err)
// open the backup database
opts2 := DefaultOptions(backupPath)
db2, err := Open(opts2)
assert.Nil(t, err)
defer destroyDB(db2)
val, err := db2.Get(GetKey(4))
assert.Nil(t, err)
assert.NotNil(t, val)
}
func destroyDB(db *RoseDB) {
if db != nil {
_ = db.Close()
if runtime.GOOS == "windows" {
time.Sleep(time.Millisecond * 100)
}
err := os.RemoveAll(db.opts.DBPath)
if err != nil {
logger.Errorf("destroy db err: %v", err)
}
}
}
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
func init() {
rand.Seed(time.Now().Unix())
}
// GetKey length: 32 Bytes
func GetKey(n int) []byte {
return []byte("kvstore-bench-key------" + fmt.Sprintf("%09d", n))
}
func GetValue16B() []byte {
return GetValue(16)
}
func GetValue128B() []byte {
return GetValue(128)
}
func GetValue4K() []byte {
return GetValue(4096)
}
func GetValue(n int) []byte {
var str bytes.Buffer
for i := 0; i < n; i++ {
str.WriteByte(alphabet[rand.Int()%36])
}
return str.Bytes()
}