-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
259 lines (223 loc) · 5.13 KB
/
db.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
// Copyright Turing Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package db 数据库操作底层接口定义以及实现包括:leveldb、
// memdb、mvcc、badgerdb、pegasus、ssdb
package db
import (
"bytes"
"fmt"
"github.com/turingchain2020/turingchain/types"
lru "github.com/hashicorp/golang-lru"
)
//ErrNotFoundInDb error
var ErrNotFoundInDb = types.ErrNotFound
//Lister 列表接口
type Lister interface {
List(prefix, key []byte, count, direction int32) ([][]byte, error)
PrefixCount(prefix []byte) int64
}
//TxKV transaction Key Value
type TxKV interface {
KV
IteratorDB
}
//KV kv
type KV interface {
Get(key []byte) ([]byte, error)
Set(key []byte, value []byte) (err error)
Begin()
Commit() error
Rollback()
}
//KVDB kvdb
type KVDB interface {
KV
Lister
}
//DB db
type DB interface {
KV
IteratorDB
SetSync([]byte, []byte) error
Delete([]byte) error
DeleteSync([]byte) error
Close()
NewBatch(sync bool) Batch
BeginTx() (TxKV, error)
CompactRange(start, limit []byte) error
// For debugging
Print()
Stats() map[string]string
SetCacheSize(size int)
GetCache() *lru.ARCCache
}
//KVDBList list
type KVDBList struct {
DB
list *ListHelper
}
//List 列表
func (l *KVDBList) List(prefix, key []byte, count, direction int32) ([][]byte, error) {
vals := l.list.List(prefix, key, count, direction)
if vals == nil {
return nil, types.ErrNotFound
}
return vals, nil
}
//PrefixCount 前缀长度
func (l *KVDBList) PrefixCount(prefix []byte) int64 {
return l.list.PrefixCount(prefix)
}
//NewKVDB new
func NewKVDB(db DB) KVDB {
return &KVDBList{DB: db, list: NewListHelper(db)}
}
//Batch batch
type Batch interface {
Set(key, value []byte)
Delete(key []byte)
Write() error
ValueSize() int // size of data in the batch
ValueLen() int // amount of data in the batch
Reset() // Reset resets the batch for reuse
UpdateWriteSync(sync bool) // update write sync
}
// MustWrite must write correct
func MustWrite(batch Batch) {
err := batch.Write()
if err != nil {
panic(fmt.Sprint("batch write err", err))
}
}
//IteratorSeeker ...
type IteratorSeeker interface {
Rewind() bool
// 返回false, 表示系统中没有指定的key,Iterator会指向key附近
Seek(key []byte) bool
Next() bool
}
//Iterator 迭代器
type Iterator interface {
IteratorSeeker
Valid() bool
Key() []byte
Value() []byte
ValueCopy() []byte
Error() error
Prefix() []byte
IsReverse() bool
Close()
}
type itBase struct {
start []byte
end []byte
reverse bool
}
func (it *itBase) checkKey(key []byte) bool {
//key must in start and end
var startok = true
var endok = true
if it.start != nil {
startok = bytes.Compare(key, it.start) >= 0
}
if it.end != nil {
endok = bytes.Compare(key, it.end) <= 0
}
ok := startok && endok
return ok
}
//Prefix 前缀
func (it *itBase) Prefix() []byte {
return nil
}
func (it *itBase) IsReverse() bool {
return it.reverse
}
//IteratorDB 迭代
type IteratorDB interface {
Iterator(start []byte, end []byte, reserver bool) Iterator
}
func bytesPrefix(prefix []byte) []byte {
var limit []byte
for i := len(prefix) - 1; i >= 0; i-- {
c := prefix[i]
if c < 0xff {
limit = make([]byte, i+1)
copy(limit, prefix)
limit[i] = c + 1
break
}
}
return limit
}
const (
levelDBBackendStr = "leveldb" // legacy, defaults to goleveldb.
goLevelDBBackendStr = "goleveldb"
memDBBackendStr = "memdb"
goBadgerDBBackendStr = "gobadgerdb"
ssDBBackendStr = "ssdb"
goPegasusDbBackendStr = "pegasus"
)
type dbCreator func(name string, dir string, cache int) (DB, error)
var backends = map[string]dbCreator{}
func registerDBCreator(backend string, creator dbCreator, force bool) {
_, ok := backends[backend]
if !force && ok {
return
}
backends[backend] = creator
}
//NewDB new
func NewDB(name string, backend string, dir string, cache int32) DB {
dbCreator, ok := backends[backend]
if !ok {
fmt.Printf("Error initializing DB: %v\n", backend)
panic("initializing DB error")
}
db, err := dbCreator(name, dir, int(cache))
if err != nil {
fmt.Printf("Error initializing DB: %v\n", err)
panic("initializing DB error")
}
return db
}
//BaseDB 交易缓存
type BaseDB struct {
cache *lru.ARCCache
}
//GetCache 获取缓存
func (db *BaseDB) GetCache() *lru.ARCCache {
return db.cache
}
//SetCacheSize 设置缓存大小
func (db *BaseDB) SetCacheSize(size int) {
if db.cache != nil {
return
}
var err error
db.cache, err = lru.NewARC(size)
if err != nil {
panic(err)
}
}
//Begin call panic when Begin not rewrite
func (db *BaseDB) Begin() {
panic("Begin not impl")
}
//Commit call panic when Commit not rewrite
func (db *BaseDB) Commit() error {
panic("Commit not impl")
}
//Rollback call panic when Rollback not rewrite
func (db *BaseDB) Rollback() {
panic("Rollback not impl")
}
//BeginTx call panic when BeginTx not rewrite
func (db *BaseDB) BeginTx() (TxKV, error) {
panic("BeginTx not impl")
}
//CompactRange call panic when CompactRange not rewrite
func (db *BaseDB) CompactRange(start, limit []byte) error {
panic("CompactRange not impl")
}