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