Skip to content

Commit 811e4a9

Browse files
bysomeone33cn
authored andcommitted
[[FIX]]add statedb nil value setting(#1203)
1 parent f973f98 commit 811e4a9

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

executor/statedb.go

+18-32
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
// StateDB state db for store mavl
1616
type StateDB struct {
17-
cache map[string][]byte
18-
txcache map[string][]byte
17+
cache *cacheDB
18+
txcache *cacheDB
1919
keys []string
2020
intx bool
2121
client queue.Client
@@ -39,8 +39,8 @@ func NewStateDB(client queue.Client, stateHash []byte, localdb db.KVDB, opt *Sta
3939
}
4040
db := &StateDB{
4141
//预分配一个单位
42-
cache: make(map[string][]byte, 1),
43-
txcache: make(map[string][]byte),
42+
cache: newcacheDB(),
43+
txcache: newcacheDB(),
4444
intx: false,
4545
client: client,
4646
stateHash: stateHash,
@@ -86,9 +86,7 @@ func (s *StateDB) Rollback() {
8686

8787
// Commit canche tx
8888
func (s *StateDB) Commit() error {
89-
for k, v := range s.txcache {
90-
s.cache[k] = v
91-
}
89+
s.cache.Merge(s.txcache)
9290
s.intx = false
9391
s.keys = nil
9492
types.AssertConfig(s.client)
@@ -101,26 +99,26 @@ func (s *StateDB) Commit() error {
10199

102100
func (s *StateDB) resetTx() {
103101
s.intx = false
104-
s.txcache = nil
102+
s.txcache.Reset()
105103
s.keys = nil
106104
}
107105

108106
// Get get value from state db
109107
func (s *StateDB) Get(key []byte) ([]byte, error) {
110108
v, err := s.get(key)
111-
debugAccount("==get==", key, v)
109+
//debugAccount("==get==", key, v)
112110
return v, err
113111
}
114112

115113
func (s *StateDB) get(key []byte) ([]byte, error) {
116-
skey := types.Bytes2Str(key)
117-
if s.intx && s.txcache != nil {
118-
if value, ok := s.txcache[skey]; ok {
119-
return value, nil
114+
115+
if s.intx {
116+
if value, exist, err := s.txcache.Get(key); exist {
117+
return value, err
120118
}
121119
}
122-
if value, ok := s.cache[skey]; ok {
123-
return value, nil
120+
if value, exist, err := s.cache.Get(key); exist {
121+
return value, err
124122
}
125123
//mvcc 是有效的情况下,直接从mvcc中获取
126124
if s.version >= 0 {
@@ -151,7 +149,7 @@ func (s *StateDB) get(key []byte) ([]byte, error) {
151149
return nil, types.ErrNotFound
152150
}
153151
//get 的值可以写入cache,因为没有对系统的值做修改
154-
s.cache[string(key)] = value
152+
s.cache.Set(key, value)
155153
return value, nil
156154
}
157155

@@ -181,28 +179,16 @@ func (s *StateDB) GetSetKeys() (keys []string) {
181179

182180
// Set set key value to state db
183181
func (s *StateDB) Set(key []byte, value []byte) error {
184-
debugAccount("==set==", key, value)
185-
skey := string(key)
182+
//debugAccount("==set==", key, value)
186183
if s.intx {
187-
if s.txcache == nil {
188-
s.txcache = make(map[string][]byte)
189-
}
190-
s.keys = append(s.keys, skey)
191-
setmap(s.txcache, skey, value)
184+
s.keys = append(s.keys, string(key))
185+
s.txcache.Set(key, value)
192186
} else {
193-
setmap(s.cache, skey, value)
187+
s.cache.Set(key, value)
194188
}
195189
return nil
196190
}
197191

198-
func setmap(data map[string][]byte, key string, value []byte) {
199-
if value == nil {
200-
delete(data, key)
201-
return
202-
}
203-
data[key] = value
204-
}
205-
206192
// BatchGet batch get keys from state db
207193
func (s *StateDB) BatchGet(keys [][]byte) (values [][]byte, err error) {
208194
for _, key := range keys {

0 commit comments

Comments
 (0)