From 8d8071f97e88d966575c2d2ff6a69cebda872e41 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 5 Oct 2021 09:54:03 +0300 Subject: [PATCH] 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. --- pkg/core/storage/memcached_store.go | 18 ++++++++++++------ pkg/core/storage/memcached_store_test.go | 12 ++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/core/storage/memcached_store.go b/pkg/core/storage/memcached_store.go index b21d3edaa..f2ebac2ba 100644 --- a/pkg/core/storage/memcached_store.go +++ b/pkg/core/storage/memcached_store.go @@ -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 diff --git a/pkg/core/storage/memcached_store_test.go b/pkg/core/storage/memcached_store_test.go index fe811f61d..83d8cc623 100644 --- a/pkg/core/storage/memcached_store_test.go +++ b/pkg/core/storage/memcached_store_test.go @@ -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")