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:
Anna Shaleva 2021-10-05 09:54:03 +03:00
parent af9e39ced2
commit 8d8071f97e
2 changed files with 18 additions and 12 deletions

View file

@ -18,14 +18,20 @@ type (
KeyValue struct { KeyValue struct {
Key []byte Key []byte
Value []byte Value []byte
}
// KeyValueExists represents key-value pair with indicator whether the item
// exists in the persistent storage.
KeyValueExists struct {
KeyValue
Exists bool Exists bool
} }
// MemBatch represents a changeset to be persisted. // MemBatch represents a changeset to be persisted.
MemBatch struct { MemBatch struct {
Put []KeyValue Put []KeyValueExists
Deleted []KeyValue Deleted []KeyValueExists
} }
) )
@ -58,18 +64,18 @@ func (s *MemCachedStore) GetBatch() *MemBatch {
var b 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 { for k, v := range s.mem {
key := []byte(k) key := []byte(k)
_, err := s.ps.Get(key) _, 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 { for k := range s.del {
key := []byte(k) key := []byte(k)
_, err := s.ps.Get(key) _, 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 return &b

View file

@ -16,7 +16,7 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
assert.Equal(t, 0, c) assert.Equal(t, 0, c)
// persisting one key should result in one key in ps and nothing in ts // persisting one key should result in one key in ps and nothing in ts
assert.NoError(t, ts.Put([]byte("key"), []byte("value"))) 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() c, err = ts.Persist()
checkBatch(t, ts, nil, nil) checkBatch(t, ts, nil, nil)
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
@ -35,9 +35,9 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
v, err = ps.Get([]byte("key2")) v, err = ps.Get([]byte("key2"))
assert.Equal(t, ErrKeyNotFound, err) assert.Equal(t, ErrKeyNotFound, err)
assert.Equal(t, []byte(nil), v) assert.Equal(t, []byte(nil), v)
checkBatch(t, ts, []KeyValue{ checkBatch(t, ts, []KeyValueExists{
{Key: []byte("key"), Value: []byte("newvalue"), Exists: true}, {KeyValue: KeyValue{Key: []byte("key"), Value: []byte("newvalue")}, Exists: true},
{Key: []byte("key2"), Value: []byte("value2")}, {KeyValue: KeyValue{Key: []byte("key2"), Value: []byte("value2")}},
}, nil) }, nil)
// two keys should be persisted (one overwritten and one new) and // two keys should be persisted (one overwritten and one new) and
// available in the ps // available in the ps
@ -65,7 +65,7 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
// test persisting deletions // test persisting deletions
err = ts.Delete([]byte("key")) err = ts.Delete([]byte("key"))
assert.Equal(t, nil, err) 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() c, err = ts.Persist()
checkBatch(t, ts, nil, nil) checkBatch(t, ts, nil, nil)
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
@ -78,7 +78,7 @@ func testMemCachedStorePersist(t *testing.T, ps Store) {
assert.Equal(t, []byte("value2"), v) 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() b := ts.GetBatch()
assert.Equal(t, len(put), len(b.Put), "wrong number of put elements in a batch") 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") assert.Equal(t, len(del), len(b.Deleted), "wrong number of deleted elements in a batch")