-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
377 lines (332 loc) · 7.45 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
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
package main
import (
"crypto/rand"
"errors"
"os"
"path/filepath"
lvldb "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
)
const (
obfuscateKeyKey = "\000obfuscate_key"
obfuscateKeyLen = 8
)
const (
preallocKeySize = 64
preallocValueSize = 1024
)
type DBWrapper struct {
option opt.Options
readOption opt.ReadOptions
iterOption opt.ReadOptions
writeOption opt.WriteOptions
syncOption opt.WriteOptions
db *lvldb.DB
name string
obfuscateKey []byte
}
func genObfuscateKey() []byte {
buf := make([]byte, obfuscateKeyLen)
_, err := rand.Read(buf)
if err != nil {
panic("failed read random bytes")
}
return buf
}
func getOptions(cacheSize int) opt.Options {
var opts opt.Options
opts.BlockCacher = opt.LRUCacher
opts.BlockCacheCapacity = cacheSize / 2
opts.WriteBuffer = cacheSize / 4
opts.Filter = filter.NewBloomFilter(10)
opts.Compression = opt.NoCompression
opts.OpenFilesCacheCapacity = 64
return opts
}
func destroyDB(path string) error {
st, err := storage.OpenFile(path, false)
if err != nil {
return err
}
defer st.Close()
fds, err := st.List(storage.TypeAll)
if err != nil {
return err
}
for _, fd := range fds {
if err := st.Remove(fd); err != nil {
return err
}
}
for _, other := range []string{"CURRENT", "LOCK", "LOG", "LOG.old"} {
if err := os.Remove(filepath.Join(path, other)); err != nil && !os.IsNotExist(err) {
return err
}
}
return nil
}
type DBOption struct {
FilePath string
CacheSize int
Wipe bool
DontObfuscate bool
ForceCompactdb bool
}
func NewDBWrapper(do *DBOption) (*DBWrapper, error) {
if do == nil {
return nil, errors.New("DBWrapper: nil DBOption")
}
opts := getOptions(do.CacheSize)
if do.Wipe {
if err := destroyDB(do.FilePath); err != nil {
return nil, err
}
}
err := os.MkdirAll(do.FilePath, 0740)
if err != nil && !os.IsExist(err) {
return nil, err
}
db, err := lvldb.OpenFile(do.FilePath, &opts)
if err != nil {
return nil, err
}
if do.ForceCompactdb {
if err := db.CompactRange(util.Range{}); err != nil {
return nil, err
}
}
ro := opt.ReadOptions{
DontFillCache: false,
Strict: opt.StrictJournalChecksum | opt.StrictBlockChecksum,
}
io := opt.ReadOptions{
DontFillCache: true,
Strict: opt.StrictJournalChecksum | opt.StrictBlockChecksum,
}
wo := opt.WriteOptions{}
so := opt.WriteOptions{
Sync: true,
}
dbw := &DBWrapper{
option: opts,
readOption: ro,
iterOption: io,
writeOption: wo,
syncOption: so,
db: db,
name: filepath.Base(do.FilePath),
//obfuscateKey: make([]byte, 8),
}
exists := false
obk, err := dbw.Read([]byte(obfuscateKeyKey))
if err == nil {
dbw.obfuscateKey = obk
exists = true
}
if !exists && !do.DontObfuscate && dbw.IsEmpty() {
newKey := genObfuscateKey()
if err := dbw.Write([]byte(obfuscateKeyKey), newKey, false); err != nil {
return nil, err
}
dbw.obfuscateKey = newKey
}
return dbw, nil
}
func xor(val, key []byte) {
if len(key) == 0 {
return
}
for i, j := 0, 0; i < len(val); i++ {
val[i] ^= key[j]
j++
if j == len(key) {
j = 0
}
}
}
func (dbw *DBWrapper) Read(key []byte) ([]byte, error) {
value, err := dbw.db.Get(key, &dbw.readOption)
if err != nil {
return nil, err
}
xor(value, dbw.obfuscateKey)
return value, nil
}
func (dbw *DBWrapper) Write(key, val []byte, sync bool) error {
bw := NewBatchWrapper(dbw)
bw.Write(key, val)
return dbw.WriteBatch(bw, sync)
}
func (dbw *DBWrapper) WriteBatch(bw *BatchWrapper, sync bool) error {
var opts opt.WriteOptions
if sync {
opts = dbw.syncOption
} else {
opts = dbw.writeOption
}
return dbw.db.Write(&bw.bat, &opts)
}
func (dbw *DBWrapper) Exists(key []byte) bool {
_, err := dbw.db.Get(key, &dbw.readOption)
if err != nil {
if err == lvldb.ErrNotFound {
return false
}
panic("DBWrapper :" + err.Error())
}
return true
}
func (dbw *DBWrapper) Erase(key []byte, sync bool) error {
bw := NewBatchWrapper(dbw)
bw.Erase(key)
return dbw.WriteBatch(bw, sync)
}
func (dbw *DBWrapper) Sync() error {
bw := NewBatchWrapper(dbw)
return dbw.WriteBatch(bw, true)
}
func (dbw *DBWrapper) Iterator() *IterWrapper {
return NewIterWrapper(dbw, dbw.db.NewIterator(nil, &dbw.iterOption))
}
func (dbw *DBWrapper) IsEmpty() bool {
it := dbw.Iterator()
it.SeekToFirst()
return !it.Valid()
}
func (dbw *DBWrapper) EstimateSize(begin, end []byte) uint64 {
r := []util.Range{{Start: begin, Limit: end}}
sizes, err := dbw.db.SizeOf(r)
if err != nil {
return 0
}
return uint64(sizes.Sum())
}
func (dbw *DBWrapper) CompactRange(begin, end []byte) error {
return dbw.db.CompactRange(util.Range{Start: begin, Limit: end})
}
func (dbw *DBWrapper) GetObfuscateKey() []byte {
return dbw.obfuscateKey
}
func (dbw *DBWrapper) Close() {
if dbw.db != nil {
dbw.db.Close()
}
}
type BatchWrapper struct {
bat lvldb.Batch
parent *DBWrapper
bkey []byte
bval []byte
sizeEst int
}
func NewBatchWrapper(parent *DBWrapper) *BatchWrapper {
return &BatchWrapper{
parent: parent,
bkey: make([]byte, 0, preallocKeySize),
bval: make([]byte, 0, preallocValueSize),
}
}
func (bw *BatchWrapper) Clear() {
bw.bat.Reset()
bw.sizeEst = 0
}
func (bw *BatchWrapper) Write(key, val []byte) {
bw.bkey = append(bw.bkey, key...)
bw.bval = append(bw.bval, val...)
//log.Printf("key,val:%s,%s\n", bw.bkey, (bw.bval))
//log.Printf("bw.parent.GetObfuscateKey():%s\n", bw.parent.GetObfuscateKey())
xor(bw.bval, bw.parent.GetObfuscateKey())
bw.bat.Put(bw.bkey, bw.bval)
// LevelDB serializes writes as:
// - byte: header
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
// - byte[]: key
// - varint: value length
// - byte[]: value
// The formula below assumes the key and value are both less than 16k.
k := 0
v := 0
if len(bw.bkey) > 127 {
k = 1
}
if len(bw.bval) > 127 {
v = 1
}
bw.sizeEst += 3 + k + len(bw.bkey) + v + len(bw.bval)
bw.bkey = bw.bkey[:0]
bw.bval = bw.bkey[:0]
}
func (bw *BatchWrapper) SizeEstimate() int {
return bw.sizeEst
}
func (bw *BatchWrapper) Erase(key []byte) {
bw.bkey = append(bw.bkey, key...)
bw.bat.Delete(bw.bkey)
k := 0
if len(bw.bkey) > 127 {
k = 1
}
bw.sizeEst += 2 + k + len(bw.bkey)
bw.bkey = bw.bkey[:0]
}
type IterWrapper struct {
parent *DBWrapper
iter iterator.Iterator
}
func NewIterWrapper(parent *DBWrapper, iter iterator.Iterator) *IterWrapper {
return &IterWrapper{
parent: parent,
iter: iter,
}
}
func (iw *IterWrapper) Valid() bool {
if iw.iter == nil {
return false
}
return iw.iter.Valid()
}
func (iw *IterWrapper) SeekToFirst() {
iw.Seek(nil)
}
func (iw *IterWrapper) GetKey() []byte {
var key []byte
if iw.iter != nil {
k := iw.iter.Key()
key = append(key, k...)
}
return key
}
func (iw *IterWrapper) GetKeySize() int {
return len(iw.GetKey())
}
func (iw *IterWrapper) GetVal() []byte {
var val []byte
if iw.iter != nil {
v := iw.iter.Value()
val = append(val, v...)
}
xor(val, iw.parent.GetObfuscateKey())
return val
}
func (iw *IterWrapper) GetValSize() int {
return len(iw.GetVal())
}
func (iw *IterWrapper) Seek(key []byte) {
if iw.iter != nil {
iw.iter.Seek(key)
}
}
func (iw *IterWrapper) Next() {
if iw.iter != nil {
iw.iter.Next()
}
}
func (iw *IterWrapper) Close() {
if iw.iter != nil {
iw.iter.Release()
}
}