storage: remove RedisBatch in favor of MemoryBatch

They're also almost the same.
This commit is contained in:
Roman Khimov 2019-09-26 19:05:20 +03:00
parent af557cea19
commit 0dbdbb9c2a
2 changed files with 7 additions and 77 deletions

View file

@ -18,28 +18,6 @@ type RedisStore struct {
client *redis.Client 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. // NewRedisStore returns an new initialized - ready to use RedisStore object.
func NewRedisStore(cfg RedisDBOptions) (*RedisStore, error) { func NewRedisStore(cfg RedisDBOptions) (*RedisStore, error) {
c := redis.NewClient(&redis.Options{ c := redis.NewClient(&redis.Options{
@ -55,7 +33,7 @@ func NewRedisStore(cfg RedisDBOptions) (*RedisStore, error) {
// Batch implements the Store interface. // Batch implements the Store interface.
func (s *RedisStore) Batch() Batch { func (s *RedisStore) Batch() Batch {
return NewRedisBatch() return newMemoryBatch()
} }
// Get implements the Store interface. // Get implements the Store interface.
@ -76,8 +54,8 @@ func (s *RedisStore) Put(k, v []byte) error {
// PutBatch implements the Store interface. // PutBatch implements the Store interface.
func (s *RedisStore) PutBatch(b Batch) error { func (s *RedisStore) PutBatch(b Batch) error {
pipe := s.client.Pipeline() pipe := s.client.Pipeline()
for k, v := range b.(*RedisBatch).mem { for k, v := range b.(*MemoryBatch).m {
pipe.Set(k, v, 0) pipe.Set(string(*k), v, 0)
} }
_, err := pipe.Exec() _, err := pipe.Exec()
return err return err

View file

@ -1,7 +1,6 @@
package storage package storage
import ( import (
"reflect"
"testing" "testing"
"github.com/alicebob/miniredis" "github.com/alicebob/miniredis"
@ -9,13 +8,6 @@ import (
"github.com/stretchr/testify/require" "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) { func TestNewRedisStore(t *testing.T) {
redisMock, redisStore := prepareRedisMock(t) redisMock, redisStore := prepareRedisMock(t)
key := []byte("testKey") key := []byte("testKey")
@ -33,50 +25,10 @@ func TestNewRedisStore(t *testing.T) {
func TestRedisBatch_Len(t *testing.T) { func TestRedisBatch_Len(t *testing.T) {
want := len(map[string]string{}) want := len(map[string]string{})
b := &RedisBatch{ b := &MemoryBatch{
mem: map[string]string{}, m: map[*[]byte][]byte{},
} }
assert.Equal(t, len(b.mem), want) assert.Equal(t, len(b.m), 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)
} }
func TestRedisStore_GetAndPut(t *testing.T) { func TestRedisStore_GetAndPut(t *testing.T) {
@ -130,7 +82,7 @@ func TestRedisStore_GetAndPut(t *testing.T) {
} }
func TestRedisStore_PutBatch(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) mock, redisStore := prepareRedisMock(t)
err := redisStore.PutBatch(batch) err := redisStore.PutBatch(batch)
assert.Nil(t, err, "Error while PutBatch") assert.Nil(t, err, "Error while PutBatch")