storage: unexport MemoryStore mutex

It shouldn't be available from outside.
This commit is contained in:
Roman Khimov 2019-09-26 20:49:02 +03:00
parent 9617a6f9e9
commit 5ff1399d54

View file

@ -9,7 +9,7 @@ import (
// MemoryStore is an in-memory implementation of a Store, mainly // MemoryStore is an in-memory implementation of a Store, mainly
// used for testing. Do not use MemoryStore in production. // used for testing. Do not use MemoryStore in production.
type MemoryStore struct { type MemoryStore struct {
*sync.RWMutex mut sync.RWMutex
mem map[string][]byte mem map[string][]byte
} }
@ -35,15 +35,14 @@ func (b *MemoryBatch) Len() int {
// NewMemoryStore creates a new MemoryStore object. // NewMemoryStore creates a new MemoryStore object.
func NewMemoryStore() *MemoryStore { func NewMemoryStore() *MemoryStore {
return &MemoryStore{ return &MemoryStore{
RWMutex: new(sync.RWMutex),
mem: make(map[string][]byte), mem: make(map[string][]byte),
} }
} }
// Get implements the Store interface. // Get implements the Store interface.
func (s *MemoryStore) Get(key []byte) ([]byte, error) { func (s *MemoryStore) Get(key []byte) ([]byte, error) {
s.RLock() s.mut.RLock()
defer s.RUnlock() defer s.mut.RUnlock()
if val, ok := s.mem[makeKey(key)]; ok { if val, ok := s.mem[makeKey(key)]; ok {
return val, nil return val, nil
} }
@ -52,9 +51,9 @@ func (s *MemoryStore) Get(key []byte) ([]byte, error) {
// Put implements the Store interface. Never returns an error. // Put implements the Store interface. Never returns an error.
func (s *MemoryStore) Put(key, value []byte) error { func (s *MemoryStore) Put(key, value []byte) error {
s.Lock() s.mut.Lock()
s.mem[makeKey(key)] = value s.mem[makeKey(key)] = value
s.Unlock() s.mut.Unlock()
return nil return nil
} }
@ -92,8 +91,8 @@ func newMemoryBatch() *MemoryBatch {
// Persist flushes all the MemoryStore contents into the (supposedly) persistent // Persist flushes all the MemoryStore contents into the (supposedly) persistent
// store provided via parameter. // store provided via parameter.
func (s *MemoryStore) Persist(ps Store) (int, error) { func (s *MemoryStore) Persist(ps Store) (int, error) {
s.Lock() s.mut.Lock()
defer s.Unlock() defer s.mut.Unlock()
batch := ps.Batch() batch := ps.Batch()
keys := 0 keys := 0
for k, v := range s.mem { for k, v := range s.mem {
@ -114,9 +113,9 @@ func (s *MemoryStore) Persist(ps Store) (int, error) {
// Close implements Store interface and clears up memory. Never returns an // Close implements Store interface and clears up memory. Never returns an
// error. // error.
func (s *MemoryStore) Close() error { func (s *MemoryStore) Close() error {
s.Lock() s.mut.Lock()
s.mem = nil s.mem = nil
s.Unlock() s.mut.Unlock()
return nil return nil
} }