2025-01-05 16:28:42 +00:00
|
|
|
package storage
|
|
|
|
|
2025-01-06 11:03:58 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2025-01-05 16:28:42 +00:00
|
|
|
|
|
|
|
func NewMemStore() Store {
|
|
|
|
s := &memStore{
|
|
|
|
elements: make(map[string][]byte, 0),
|
|
|
|
lock: sync.Mutex{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type memStore struct {
|
|
|
|
elements map[string][]byte
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memStore) Has(id string) error {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
_, found := s.elements[id]
|
|
|
|
if !found {
|
|
|
|
return ErrNotExists
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memStore) Get(id string) ([]byte, error) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
element, found := s.elements[id]
|
|
|
|
if !found {
|
|
|
|
return nil, ErrNotExists
|
|
|
|
}
|
|
|
|
|
|
|
|
return element, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memStore) Put(id string, element []byte) error {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
s.elements[id] = element
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *memStore) Delete(id string) error {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
_, found := s.elements[id]
|
|
|
|
if !found {
|
|
|
|
return ErrNotExists
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.elements, id)
|
|
|
|
return nil
|
|
|
|
}
|