-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
121 lines (109 loc) · 2.5 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
package levelkv
import (
"context"
"encoding/json"
iflog "github.com/IceFireDB/icefiredb-ipfs-log"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
"go.uber.org/zap"
)
type LevelKV struct {
ev *iflog.IpfsLog
db *leveldb.DB
log *zap.Logger
}
func NewLevelKVDB(ctx context.Context, ev *iflog.IpfsLog, log *zap.Logger) (*LevelKV, error) {
db, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
return nil, err
}
kv := LevelKV{
ev: ev,
db: db,
log: log,
}
if err := kv.syncData(ctx); err != nil {
return nil, err
}
return &kv, nil
}
func (kv *LevelKV) syncData(ctx context.Context) error {
ch, err := kv.ev.WatchWriteEvent(ctx)
if err != nil {
return err
}
go func() {
var data []byte
var opr operation
for {
select {
case <-ctx.Done():
return
case data = <-ch:
}
err = json.Unmarshal(data, &opr)
if err != nil {
kv.log.Error("json Unmarshal sync data err:", zap.Error(err))
continue
}
switch opr.Op {
case SetOperateType:
_ = kv.db.Put(opr.Key, opr.Value, nil)
case DeleteOperateType:
_ = kv.db.Delete(opr.Key, nil)
}
}
}()
return nil
}
func (kv *LevelKV) Get(key []byte) ([]byte, error) {
val, err := kv.db.Get(key, nil)
if err != nil && err == leveldb.ErrNotFound {
return nil, nil
}
return val, err
}
func (kv *LevelKV) Put(ctx context.Context, key, value []byte) error {
op := newOperation(key, value, SetOperateType)
_, err := kv.ev.Append(ctx, op.marshal())
if err != nil {
return err
}
return kv.db.Put(key, value, nil)
}
func (kv *LevelKV) Delete(ctx context.Context, key []byte) error {
op := newOperation(key, nil, DeleteOperateType)
_, err := kv.ev.Append(ctx, op.marshal())
if err != nil {
return err
}
return kv.db.Delete(key, nil)
}
func (kv *LevelKV) Has(key []byte) bool {
ok, _ := kv.db.Has(key, nil)
return ok
}
func (kv *LevelKV) List() map[string]string {
ite := kv.db.NewIterator(nil, &opt.ReadOptions{})
list := make(map[string]string)
for {
if !ite.Next() {
break
}
list[string(ite.Key())] = string(ite.Value())
}
return list
}
func (kv *LevelKV) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator {
return kv.db.NewIterator(slice, ro)
}
func (kv *LevelKV) GetSnapshot() (*leveldb.Snapshot, error) {
return kv.db.GetSnapshot()
}
func (kv *LevelKV) Close() {
kv.db.Close()
kv.ev.Close()
}