-
Notifications
You must be signed in to change notification settings - Fork 1
/
kvdb.go
183 lines (161 loc) · 4.57 KB
/
kvdb.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
// Copyright Fuzamei 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 kvdb
import (
"github.com/assetcloud/chain/common"
clog "github.com/assetcloud/chain/common/log"
log "github.com/assetcloud/chain/common/log/log15"
"github.com/assetcloud/chain/queue"
drivers "github.com/assetcloud/chain/system/store"
"github.com/assetcloud/chain/types"
"github.com/golang/protobuf/proto"
)
var klog = log.New("module", "kvdb")
// SetLogLevel set log level
func SetLogLevel(level string) {
clog.SetLogLevel(level)
}
// DisableLog disable log output
func DisableLog() {
klog.SetHandler(log.DiscardHandler())
}
func init() {
drivers.Reg("kvdb", New)
}
// KVStore implementation
type KVStore struct {
*drivers.BaseStore
cache map[string]map[string]*types.KeyValue
}
// New KVStore module
func New(cfg *types.Store, sub []byte, chaincfg *types.ChainConfig) queue.Module {
bs := drivers.NewBaseStore(cfg)
kvs := &KVStore{bs, make(map[string]map[string]*types.KeyValue)}
bs.SetChild(kvs)
return kvs
}
// Close KVStore module
func (kvs *KVStore) Close() {
kvs.BaseStore.Close()
klog.Info("store kvdb closed")
}
// Set kvs with statehash to KVStore
func (kvs *KVStore) Set(datas *types.StoreSet, sync bool) ([]byte, error) {
hash := calcHash(datas)
kvmap := make(map[string]*types.KeyValue)
for _, kv := range datas.KV {
kvmap[string(kv.Key)] = kv
}
kvs.save(kvmap)
return hash, nil
}
// Get kvs with statehash from KVStore
func (kvs *KVStore) Get(datas *types.StoreGet) [][]byte {
values := make([][]byte, len(datas.Keys))
if kvmap, ok := kvs.cache[string(datas.StateHash)]; ok {
for i := 0; i < len(datas.Keys); i++ {
kv := kvmap[string(datas.Keys[i])]
if kv != nil {
values[i] = kv.Value
}
}
} else {
db := kvs.GetDB()
for i := 0; i < len(datas.Keys); i++ {
value, _ := db.Get(datas.Keys[i])
if value != nil {
values[i] = value
}
}
}
return values
}
// MemSet set kvs to the mem of KVStore
func (kvs *KVStore) MemSet(datas *types.StoreSet, sync bool) ([]byte, error) {
if len(datas.KV) == 0 {
klog.Info("store kv memset,use preStateHash as stateHash for kvset is null")
kvmap := make(map[string]*types.KeyValue)
kvs.cache[string(datas.StateHash)] = kvmap
return datas.StateHash, nil
}
hash := calcHash(datas)
kvmap := make(map[string]*types.KeyValue)
for _, kv := range datas.KV {
kvmap[string(kv.Key)] = kv
}
kvs.cache[string(hash)] = kvmap
if len(kvs.cache) > 100 {
klog.Error("too many items in cache")
}
return hash, nil
}
// Commit kvs in the mem of KVStore
func (kvs *KVStore) Commit(req *types.ReqHash) ([]byte, error) {
kvmap, ok := kvs.cache[string(req.Hash)]
if !ok {
klog.Error("store kvdb commit", "err", types.ErrHashNotFound)
return nil, types.ErrHashNotFound
}
if len(kvmap) == 0 {
klog.Info("store kvdb commit did nothing for kvset is nil")
delete(kvs.cache, string(req.Hash))
return req.Hash, nil
}
kvs.save(kvmap)
delete(kvs.cache, string(req.Hash))
return req.Hash, nil
}
// MemSetUpgrade set kvs to the mem of KVStore
func (kvs *KVStore) MemSetUpgrade(datas *types.StoreSet, sync bool) ([]byte, error) {
//not support
return nil, nil
}
// CommitUpgrade kvs in the mem of KVStore
func (kvs *KVStore) CommitUpgrade(req *types.ReqHash) ([]byte, error) {
//not support
return nil, nil
}
// Rollback kvs in the mem of KVStore
func (kvs *KVStore) Rollback(req *types.ReqHash) ([]byte, error) {
_, ok := kvs.cache[string(req.Hash)]
if !ok {
klog.Error("store kvdb rollback", "err", types.ErrHashNotFound)
return nil, types.ErrHashNotFound
}
delete(kvs.cache, string(req.Hash))
return req.Hash, nil
}
// IterateRangeByStateHash method
func (kvs *KVStore) IterateRangeByStateHash(statehash []byte, start []byte, end []byte, ascending bool, fn func(key, value []byte) bool) {
panic("empty")
//TODO:
//kvs.IterateRangeByStateHash(mavls.GetDB(), statehash, start, end, ascending, fn)
}
// ProcEvent handles supported events
func (kvs *KVStore) ProcEvent(msg *queue.Message) {
if msg == nil {
return
}
msg.ReplyErr("KVStore", types.ErrActionNotSupport)
}
// Del set kvs to nil with StateHash
func (kvs *KVStore) Del(req *types.StoreDel) ([]byte, error) {
//not support
return nil, nil
}
func (kvs *KVStore) save(kvmap map[string]*types.KeyValue) {
storeBatch := kvs.GetDB().NewBatch(true)
for _, kv := range kvmap {
if kv.Value == nil {
storeBatch.Delete(kv.Key)
} else {
storeBatch.Set(kv.Key, kv.Value)
}
}
storeBatch.Write()
}
func calcHash(datas proto.Message) []byte {
b := types.Encode(datas)
return common.Sha256(b)
}