micro/store/store.go

73 lines
1.3 KiB
Go
Raw Normal View History

2019-06-12 07:46:20 +01:00
// Package store is an interface for distribute data storage.
package store
2019-05-31 00:43:23 +01:00
import (
"errors"
"time"
)
var (
2019-11-01 14:13:21 +00:00
// ErrNotFound is returned when a Read key doesn't exist
2019-05-31 00:43:23 +01:00
ErrNotFound = errors.New("not found")
2020-01-06 17:44:32 +00:00
// Default store
DefaultStore Store = new(noop)
2019-05-31 00:43:23 +01:00
)
2019-06-12 07:46:20 +01:00
// Store is a data storage interface
type Store interface {
// Initialise store options
Init(...Option) error
2019-10-23 22:05:39 +01:00
// List all the known records
List() ([]*Record, error)
2019-11-01 14:13:21 +00:00
// Read records with keys
Read(key string, opts ...ReadOption) ([]*Record, error)
2019-11-01 14:13:21 +00:00
// Write records
Write(*Record) error
2019-11-01 14:13:21 +00:00
// Delete records with keys
Delete(key string) error
// Name of the store
String() string
2019-05-31 00:43:23 +01:00
}
// Record represents a data record
type Record struct {
2019-06-11 17:49:34 +01:00
Key string
Value []byte
Expiry time.Duration
2019-05-31 00:43:23 +01:00
}
2020-01-06 17:44:32 +00:00
type ReadOptions struct {
// Read key as a prefix
Prefix bool
// Read key as a suffix
Suffix bool
}
type ReadOption func(o *ReadOptions)
2020-01-06 17:44:32 +00:00
type noop struct{}
func (n *noop) Init(...Option) error {
return nil
}
2020-01-06 17:44:32 +00:00
func (n *noop) List() ([]*Record, error) {
return nil, nil
}
func (n *noop) Read(key string, opts ...ReadOption) ([]*Record, error) {
2020-01-06 17:44:32 +00:00
return nil, nil
}
func (n *noop) Write(rec *Record) error {
2020-01-06 17:44:32 +00:00
return nil
}
func (n *noop) Delete(key string) error {
2020-01-06 17:44:32 +00:00
return nil
}
func (n *noop) String() string {
return "noop"
}