@@ -14,8 +14,8 @@ import (
14
14
15
15
// StateDB state db for store mavl
16
16
type StateDB struct {
17
- cache map [ string ][] byte
18
- txcache map [ string ][] byte
17
+ cache * cacheDB
18
+ txcache * cacheDB
19
19
keys []string
20
20
intx bool
21
21
client queue.Client
@@ -39,8 +39,8 @@ func NewStateDB(client queue.Client, stateHash []byte, localdb db.KVDB, opt *Sta
39
39
}
40
40
db := & StateDB {
41
41
//预分配一个单位
42
- cache : make ( map [ string ][] byte , 1 ),
43
- txcache : make ( map [ string ][] byte ),
42
+ cache : newcacheDB ( ),
43
+ txcache : newcacheDB ( ),
44
44
intx : false ,
45
45
client : client ,
46
46
stateHash : stateHash ,
@@ -86,9 +86,7 @@ func (s *StateDB) Rollback() {
86
86
87
87
// Commit canche tx
88
88
func (s * StateDB ) Commit () error {
89
- for k , v := range s .txcache {
90
- s .cache [k ] = v
91
- }
89
+ s .cache .Merge (s .txcache )
92
90
s .intx = false
93
91
s .keys = nil
94
92
types .AssertConfig (s .client )
@@ -101,26 +99,26 @@ func (s *StateDB) Commit() error {
101
99
102
100
func (s * StateDB ) resetTx () {
103
101
s .intx = false
104
- s .txcache = nil
102
+ s .txcache . Reset ()
105
103
s .keys = nil
106
104
}
107
105
108
106
// Get get value from state db
109
107
func (s * StateDB ) Get (key []byte ) ([]byte , error ) {
110
108
v , err := s .get (key )
111
- debugAccount ("==get==" , key , v )
109
+ // debugAccount("==get==", key, v)
112
110
return v , err
113
111
}
114
112
115
113
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
120
118
}
121
119
}
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
124
122
}
125
123
//mvcc 是有效的情况下,直接从mvcc中获取
126
124
if s .version >= 0 {
@@ -151,7 +149,7 @@ func (s *StateDB) get(key []byte) ([]byte, error) {
151
149
return nil , types .ErrNotFound
152
150
}
153
151
//get 的值可以写入cache,因为没有对系统的值做修改
154
- s .cache [ string (key )] = value
152
+ s .cache . Set (key , value )
155
153
return value , nil
156
154
}
157
155
@@ -181,28 +179,16 @@ func (s *StateDB) GetSetKeys() (keys []string) {
181
179
182
180
// Set set key value to state db
183
181
func (s * StateDB ) Set (key []byte , value []byte ) error {
184
- debugAccount ("==set==" , key , value )
185
- skey := string (key )
182
+ //debugAccount("==set==", key, value)
186
183
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 )
192
186
} else {
193
- setmap ( s .cache , skey , value )
187
+ s .cache . Set ( key , value )
194
188
}
195
189
return nil
196
190
}
197
191
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
-
206
192
// BatchGet batch get keys from state db
207
193
func (s * StateDB ) BatchGet (keys [][]byte ) (values [][]byte , err error ) {
208
194
for _ , key := range keys {
0 commit comments