Noramlize Store interface

Instead of exposing a key/value get/set style interface, design the
interface around the actions that are actually required.

This is a first step towards supporting adding a Store to support
Google Cloud's Datastore.
This commit is contained in:
2016-12-04 19:16:57 +00:00
parent d544df0b6d
commit 1486110d2e
5 changed files with 413 additions and 163 deletions

View File

@@ -1,17 +1,16 @@
package inmemorystore
import (
"errors"
"sync"
)
// ErrNotFound is returned when Get() tries to fetch a non-existent key.
var ErrNotFound = errors.New("not found")
"github.com/jimeh/ozu.io/storage"
)
// New creates a new Store using given path to persist data.
func New() (*Store, error) {
store := &Store{
Data: map[string][]byte{},
UIDMap: map[string][]byte{},
URLMap: map[string][]byte{},
Sequence: 0,
}
return store, nil
@@ -20,43 +19,79 @@ func New() (*Store, error) {
// Store allows storing data into a in-memory map.
type Store struct {
sync.RWMutex
Data map[string][]byte
UIDMap map[string][]byte
URLMap map[string][]byte
Sequence int
Closed bool
}
// Close database.
func (s *Store) Close() error {
s.Data = make(map[string][]byte)
s.Lock()
s.UIDMap = make(map[string][]byte)
s.URLMap = make(map[string][]byte)
s.Sequence = 0
s.Unlock()
return nil
}
// Get a given key's value.
func (s *Store) Get(key []byte) ([]byte, error) {
// Create a given Record
func (s *Store) Create(uid []byte, url []byte) (*storage.Record, error) {
s.Lock()
s.UIDMap[string(uid)] = url
s.URLMap[string(url)] = uid
s.Unlock()
return &storage.Record{UID: uid, URL: url}, nil
}
// FindByUID looks up records based on their UID.
func (s *Store) FindByUID(uid []byte) (*storage.Record, error) {
s.RLock()
value := s.Data[string(key)]
value := s.UIDMap[string(uid)]
s.RUnlock()
if value == nil {
return nil, ErrNotFound
return &storage.Record{}, storage.ErrNotFound
}
return value, nil
return &storage.Record{UID: uid, URL: value}, nil
}
// Set a given key's to the specified value.
func (s *Store) Set(key []byte, value []byte) error {
s.Lock()
s.Data[string(key)] = value
s.Unlock()
return nil
// FindByURL looks up records based on their URL.
func (s *Store) FindByURL(url []byte) (*storage.Record, error) {
s.RLock()
value := s.URLMap[string(url)]
s.RUnlock()
if value == nil {
return &storage.Record{}, storage.ErrNotFound
}
return &storage.Record{UID: value, URL: url}, nil
}
// Delete a given key.
func (s *Store) Delete(key []byte) error {
// DeleteByUID deletes records based on their UID.
func (s *Store) DeleteByUID(uid []byte) (*storage.Record, error) {
record, err := s.FindByUID(uid)
if err != nil {
return &storage.Record{}, err
}
s.delete(record)
return record, nil
}
// DeleteByURL deletes records based on their URL.
func (s *Store) DeleteByURL(url []byte) (*storage.Record, error) {
record, err := s.FindByURL(url)
if err != nil {
return &storage.Record{}, err
}
s.delete(record)
return record, nil
}
func (s *Store) delete(r *storage.Record) {
s.Lock()
delete(s.Data, string(key))
delete(s.UIDMap, string(r.UID))
delete(s.URLMap, string(r.URL))
s.Unlock()
return nil
}
// NextSequence returns a auto-incrementing int.