Rewrite the store interface (#1335)
* WIP store rewrite * Fix memory store tests * Store hard expiry times rather than duration! * Clarify memory test * Add limit to store interface * Implement suffix option * Don't return nils from noop store * Fix syncmap * Start fixing store service * wip service and cache * Use _ for special characters in cockroachdb namespace * Improve cockroach namespace comment * Use service name as default store namespace * Fixes * Implement Store Scope * Start fixing etcd * implement read and write with expiry and prefix * Fix etcd tests * Fix cockroach store * Fix cloudflare interface * Fix certmagic / cloudflare store * comment lint * cache isn't implemented yet * Only prepare DB staements once Co-authored-by: Ben Toogood <ben@micro.mu> Co-authored-by: ben-toogood <bentoogood@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// Package store is an interface for distribute data storage.
|
||||
// Package store is an interface for distributed data storage.
|
||||
// The design document is located at https://github.com/micro/development/blob/master/design/store.md
|
||||
package store
|
||||
|
||||
import (
|
||||
@@ -7,66 +8,33 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNotFound is returned when a Read key doesn't exist
|
||||
// ErrNotFound is returned when a key doesn't exist
|
||||
ErrNotFound = errors.New("not found")
|
||||
// Default store
|
||||
DefaultStore Store = new(noop)
|
||||
// DefaultStore is the memory store.
|
||||
DefaultStore Store = new(noopStore)
|
||||
)
|
||||
|
||||
// Store is a data storage interface
|
||||
type Store interface {
|
||||
// Initialise store options
|
||||
// Init initialises the store. It must perform any required setup on the backing storage implementation and check that it is ready for use, returning any errors.
|
||||
Init(...Option) error
|
||||
// List all the known records
|
||||
List() ([]*Record, error)
|
||||
// Read records with keys
|
||||
Read(key string, opts ...ReadOption) ([]*Record, error)
|
||||
// Write records
|
||||
Write(*Record) error
|
||||
// Delete records with keys
|
||||
Delete(key string) error
|
||||
// Name of the store
|
||||
// Options allows you to view the current options.
|
||||
Options() Options
|
||||
// String returns the name of the implementation.
|
||||
String() string
|
||||
// Read takes a single key name and optional ReadOptions. It returns matching []*Record or an error.
|
||||
Read(key string, opts ...ReadOption) ([]*Record, error)
|
||||
// Write() writes a record to the store, and returns an error if the record was not written.
|
||||
Write(r *Record, opts ...WriteOption) error
|
||||
// Delete removes the record with the corresponding key from the store.
|
||||
Delete(key string, opts ...DeleteOption) error
|
||||
// List returns any keys that match, or an empty list with no error if none matched.
|
||||
List(opts ...ListOption) ([]string, error)
|
||||
}
|
||||
|
||||
// Record represents a data record
|
||||
// Record is an item stored or retrieved from a Store
|
||||
type Record struct {
|
||||
Key string
|
||||
Value []byte
|
||||
Expiry time.Duration
|
||||
}
|
||||
|
||||
type ReadOptions struct {
|
||||
// Read key as a prefix
|
||||
Prefix bool
|
||||
// Read key as a suffix
|
||||
Suffix bool
|
||||
}
|
||||
|
||||
type ReadOption func(o *ReadOptions)
|
||||
|
||||
type noop struct{}
|
||||
|
||||
func (n *noop) Init(...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noop) List() ([]*Record, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (n *noop) Read(key string, opts ...ReadOption) ([]*Record, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (n *noop) Write(rec *Record) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noop) Delete(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noop) String() string {
|
||||
return "noop"
|
||||
}
|
||||
|
Reference in New Issue
Block a user