Skip to content

Commit ecde994

Browse files
committed
[workingset] value in cache takes precedence
1 parent 884ab14 commit ecde994

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

db/kvstorewithbuffer.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/pkg/errors"
99

1010
"github.com/iotexproject/iotex-core/v2/db/batch"
11+
"github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
1112
"github.com/iotexproject/iotex-core/v2/pkg/log"
1213
)
1314

@@ -23,7 +24,12 @@ type (
2324
// KVStoreWithBuffer defines a KVStore with a buffer, which enables snapshot, revert,
2425
// and transaction with multiple writes
2526
KVStoreWithBuffer interface {
26-
KVStore
27+
lifecycle.StartStopper
28+
Put(string, []byte, []byte) error
29+
Get(string, []byte) ([]byte, bool, error)
30+
Delete(string, []byte) error
31+
WriteBatch(batch.KVStoreBatch) error
32+
Filter(string, Condition, []byte, []byte) ([][]byte, [][]byte, error)
2733
withBuffer
2834
}
2935

@@ -165,15 +171,18 @@ func (kvb *kvStoreWithBuffer) Size() int {
165171
return kvb.buffer.Size()
166172
}
167173

168-
func (kvb *kvStoreWithBuffer) Get(ns string, key []byte) ([]byte, error) {
174+
func (kvb *kvStoreWithBuffer) Get(ns string, key []byte) ([]byte, bool, error) {
169175
value, err := kvb.buffer.Get(ns, key)
176+
if err == nil {
177+
return value, true, nil
178+
}
170179
if errors.Cause(err) == batch.ErrNotExist {
171180
value, err = kvb.store.Get(ns, key)
172181
}
173182
if errors.Cause(err) == batch.ErrAlreadyDeleted {
174183
err = errors.Wrapf(ErrNotExist, "failed to get key %x in %s, deleted in buffer level", key, ns)
175184
}
176-
return value, err
185+
return value, false, err
177186
}
178187

179188
func (kvb *kvStoreWithBuffer) Put(ns string, key, value []byte) error {

state/factory/workingset.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,16 @@ func (ws *workingSet) State(s interface{}, opts ...protocol.StateOption) (uint64
345345
if cfg.Keys != nil {
346346
return 0, errors.Wrap(ErrNotSupported, "Read state with keys option has not been implemented yet")
347347
}
348+
value, inCache, err := ws.store.Get(cfg.Namespace, cfg.Key)
349+
if inCache && err == nil {
350+
return ws.height, state.Deserialize(s, value)
351+
}
348352
if ws.parent != nil {
349-
if value, err := ws.parent.store.Get(cfg.Namespace, cfg.Key); err == nil {
353+
value, inCache, err = ws.parent.store.Get(cfg.Namespace, cfg.Key)
354+
if inCache && err == nil {
350355
return ws.height, state.Deserialize(s, value)
351356
}
352357
}
353-
value, err := ws.store.Get(cfg.Namespace, cfg.Key)
354358
if err != nil {
355359
return ws.height, err
356360
}

state/factory/workingsetstore.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ import (
1010

1111
"github.com/iotexproject/iotex-core/v2/action/protocol"
1212
"github.com/iotexproject/iotex-core/v2/db"
13+
"github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
1314
)
1415

1516
type (
1617
workingSetStore interface {
17-
db.KVStoreBasic
18+
lifecycle.StartStopper
19+
Put(string, []byte, []byte) error
20+
Get(string, []byte) ([]byte, bool, error)
21+
Delete(string, []byte) error
1822
Commit() error
1923
States(string, [][]byte) ([][]byte, [][]byte, error)
2024
Digest() hash.Hash256

state/factory/workingsetstore_statedb.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ func (store *stateDBWorkingSetStore) Stop(context.Context) error {
3939
return nil
4040
}
4141

42-
func (store *stateDBWorkingSetStore) Get(ns string, key []byte) ([]byte, error) {
43-
data, err := store.flusher.KVStoreWithBuffer().Get(ns, key)
42+
func (store *stateDBWorkingSetStore) Get(ns string, key []byte) ([]byte, bool, error) {
43+
data, inCache, err := store.flusher.KVStoreWithBuffer().Get(ns, key)
4444
if err != nil {
4545
if errors.Cause(err) == db.ErrNotExist {
46-
return nil, errors.Wrapf(state.ErrStateNotExist, "failed to get state of ns = %x and key = %x", ns, key)
46+
return nil, inCache, errors.Wrapf(state.ErrStateNotExist, "failed to get state of ns = %x and key = %x", ns, key)
4747
}
48-
return nil, err
48+
return nil, inCache, err
4949
}
50-
return data, nil
50+
return data, inCache, nil
5151
}
5252

5353
func (store *stateDBWorkingSetStore) States(ns string, keys [][]byte) ([][]byte, [][]byte, error) {

0 commit comments

Comments
 (0)