mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-27 15:42:54 +00:00
core: distinguish storage.KeyValue and storage.KeyValueExists
We need Exists field for storage batch related code; other cases may go without Exists, so add new KeyValue structure and refactor related code.
This commit is contained in:
parent
af9e39ced2
commit
8d8071f97e
2 changed files with 18 additions and 12 deletions
|
@ -18,14 +18,20 @@ type (
|
|||
KeyValue struct {
|
||||
Key []byte
|
||||
Value []byte
|
||||
}
|
||||
|
||||
// KeyValueExists represents key-value pair with indicator whether the item
|
||||
// exists in the persistent storage.
|
||||
KeyValueExists struct {
|
||||
KeyValue
|
||||
|
||||
Exists bool
|
||||
}
|
||||
|
||||
// MemBatch represents a changeset to be persisted.
|
||||
MemBatch struct {
|
||||
Put []KeyValue
|
||||
Deleted []KeyValue
|
||||
Put []KeyValueExists
|
||||
Deleted []KeyValueExists
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -58,18 +64,18 @@ func (s *MemCachedStore) GetBatch() *MemBatch {
|
|||
|
||||
var b MemBatch
|
||||
|
||||
b.Put = make([]KeyValue, 0, len(s.mem))
|
||||
b.Put = make([]KeyValueExists, 0, len(s.mem))
|
||||
for k, v := range s.mem {
|
||||
key := []byte(k)
|
||||
_, err := s.ps.Get(key)
|
||||
b.Put = append(b.Put, KeyValue{Key: key, Value: v, Exists: err == nil})
|
||||
b.Put = append(b.Put, KeyValueExists{KeyValue: KeyValue{Key: key, Value: v}, Exists: err == nil})
|
||||
}
|
||||
|
||||
b.Deleted = make([]KeyValue, 0, len(s.del))
|
||||
b.Deleted = make([]KeyValueExists, 0, len(s.del))
|
||||
for k := range s.del {
|
||||
key := []byte(k)
|
||||
_, err := s.ps.Get(key)
|
||||
b.Deleted = append(b.Deleted, KeyValue{Key: key, Exists: err == nil})
|
||||
b.Deleted = append(b.Deleted, KeyValueExists{KeyValue: KeyValue{Key: key}, Exists: err == nil})
|
||||
}
|
||||
|
||||
return &b
|
||||
|
|
|
@ -16,7 +16,7 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
|
|||
assert.Equal(t, 0, c)
|
||||
// persisting one key should result in one key in ps and nothing in ts
|
||||
assert.NoError(t, ts.Put([]byte("key"), []byte("value")))
|
||||
checkBatch(t, ts, []KeyValue{{Key: []byte("key"), Value: []byte("value")}}, nil)
|
||||
checkBatch(t, ts, []KeyValueExists{{KeyValue: KeyValue{Key: []byte("key"), Value: []byte("value")}}}, nil)
|
||||
c, err = ts.Persist()
|
||||
checkBatch(t, ts, nil, nil)
|
||||
assert.Equal(t, nil, err)
|
||||
|
@ -35,9 +35,9 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
|
|||
v, err = ps.Get([]byte("key2"))
|
||||
assert.Equal(t, ErrKeyNotFound, err)
|
||||
assert.Equal(t, []byte(nil), v)
|
||||
checkBatch(t, ts, []KeyValue{
|
||||
{Key: []byte("key"), Value: []byte("newvalue"), Exists: true},
|
||||
{Key: []byte("key2"), Value: []byte("value2")},
|
||||
checkBatch(t, ts, []KeyValueExists{
|
||||
{KeyValue: KeyValue{Key: []byte("key"), Value: []byte("newvalue")}, Exists: true},
|
||||
{KeyValue: KeyValue{Key: []byte("key2"), Value: []byte("value2")}},
|
||||
}, nil)
|
||||
// two keys should be persisted (one overwritten and one new) and
|
||||
// available in the ps
|
||||
|
@ -65,7 +65,7 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
|
|||
// test persisting deletions
|
||||
err = ts.Delete([]byte("key"))
|
||||
assert.Equal(t, nil, err)
|
||||
checkBatch(t, ts, nil, []KeyValue{{Key: []byte("key"), Exists: true}})
|
||||
checkBatch(t, ts, nil, []KeyValueExists{{KeyValue: KeyValue{Key: []byte("key")}, Exists: true}})
|
||||
c, err = ts.Persist()
|
||||
checkBatch(t, ts, nil, nil)
|
||||
assert.Equal(t, nil, err)
|
||||
|
@ -78,7 +78,7 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
|
|||
assert.Equal(t, []byte("value2"), v)
|
||||
}
|
||||
|
||||
func checkBatch(t *testing.T, ts *MemCachedStore, put []KeyValue, del []KeyValue) {
|
||||
func checkBatch(t *testing.T, ts *MemCachedStore, put []KeyValueExists, del []KeyValueExists) {
|
||||
b := ts.GetBatch()
|
||||
assert.Equal(t, len(put), len(b.Put), "wrong number of put elements in a batch")
|
||||
assert.Equal(t, len(del), len(b.Deleted), "wrong number of deleted elements in a batch")
|
||||
|
|
Loading…
Reference in a new issue