diff --git a/pkg/core/storage/redis_store.go b/pkg/core/storage/redis_store.go index a401ce08d..069581956 100644 --- a/pkg/core/storage/redis_store.go +++ b/pkg/core/storage/redis_store.go @@ -18,28 +18,6 @@ type RedisStore struct { client *redis.Client } -// RedisBatch simple batch implementation to satisfy the Store interface. -type RedisBatch struct { - mem map[string]string -} - -// Len implements the Batch interface. -func (b *RedisBatch) Len() int { - return len(b.mem) -} - -// Put implements the Batch interface. -func (b *RedisBatch) Put(k, v []byte) { - b.mem[string(k)] = string(v) -} - -// NewRedisBatch returns a new ready to use RedisBatch. -func NewRedisBatch() *RedisBatch { - return &RedisBatch{ - mem: make(map[string]string), - } -} - // NewRedisStore returns an new initialized - ready to use RedisStore object. func NewRedisStore(cfg RedisDBOptions) (*RedisStore, error) { c := redis.NewClient(&redis.Options{ @@ -55,7 +33,7 @@ func NewRedisStore(cfg RedisDBOptions) (*RedisStore, error) { // Batch implements the Store interface. func (s *RedisStore) Batch() Batch { - return NewRedisBatch() + return newMemoryBatch() } // Get implements the Store interface. @@ -76,8 +54,8 @@ func (s *RedisStore) Put(k, v []byte) error { // PutBatch implements the Store interface. func (s *RedisStore) PutBatch(b Batch) error { pipe := s.client.Pipeline() - for k, v := range b.(*RedisBatch).mem { - pipe.Set(k, v, 0) + for k, v := range b.(*MemoryBatch).m { + pipe.Set(string(*k), v, 0) } _, err := pipe.Exec() return err diff --git a/pkg/core/storage/redis_store_test.go b/pkg/core/storage/redis_store_test.go index 12403b974..f60f76922 100644 --- a/pkg/core/storage/redis_store_test.go +++ b/pkg/core/storage/redis_store_test.go @@ -1,7 +1,6 @@ package storage import ( - "reflect" "testing" "github.com/alicebob/miniredis" @@ -9,13 +8,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestNewRedisBatch(t *testing.T) { - want := &RedisBatch{mem: map[string]string{}} - if got := NewRedisBatch(); !reflect.DeepEqual(got, want) { - t.Errorf("NewRedisBatch() = %v, want %v", got, want) - } -} - func TestNewRedisStore(t *testing.T) { redisMock, redisStore := prepareRedisMock(t) key := []byte("testKey") @@ -33,50 +25,10 @@ func TestNewRedisStore(t *testing.T) { func TestRedisBatch_Len(t *testing.T) { want := len(map[string]string{}) - b := &RedisBatch{ - mem: map[string]string{}, + b := &MemoryBatch{ + m: map[*[]byte][]byte{}, } - assert.Equal(t, len(b.mem), want) -} - -func TestRedisBatch_Put(t *testing.T) { - type args struct { - k []byte - v []byte - } - tests := []struct { - name string - args args - want *RedisBatch - }{ - {"TestRedisBatch_Put_Strings", - args{ - k: []byte("foo"), - v: []byte("bar"), - }, - &RedisBatch{mem: map[string]string{"foo": "bar"}}, - }, - {"TestRedisBatch_Put_Numbers", - args{ - k: []byte("123"), - v: []byte("456"), - }, - &RedisBatch{mem: map[string]string{"123": "456"}}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - actual := &RedisBatch{mem: map[string]string{}} - actual.Put(tt.args.k, tt.args.v) - assert.Equal(t, tt.want, actual) - }) - } -} - -func TestRedisStore_Batch(t *testing.T) { - want := &RedisBatch{mem: map[string]string{}} - actual := NewRedisBatch() - assert.Equal(t, want, actual) + assert.Equal(t, len(b.m), want) } func TestRedisStore_GetAndPut(t *testing.T) { @@ -130,7 +82,7 @@ func TestRedisStore_GetAndPut(t *testing.T) { } func TestRedisStore_PutBatch(t *testing.T) { - batch := &RedisBatch{mem: map[string]string{"foo1": "bar1"}} + batch := &MemoryBatch{m: map[*[]byte][]byte{&[]byte{'f', 'o', 'o', '1'}: []byte("bar1")}} mock, redisStore := prepareRedisMock(t) err := redisStore.PutBatch(batch) assert.Nil(t, err, "Error while PutBatch")