2019-06-12 07:46:20 +01:00
|
|
|
package store
|
2019-06-11 17:20:52 +01:00
|
|
|
|
|
|
|
import (
|
2019-12-16 14:38:51 +00:00
|
|
|
"context"
|
2020-03-12 13:41:30 +00:00
|
|
|
"time"
|
2019-06-11 17:20:52 +01:00
|
|
|
)
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// Options contains configuration for the Store
|
2019-12-16 12:13:18 +00:00
|
|
|
type Options struct {
|
2020-03-12 13:41:30 +00:00
|
|
|
// Nodes contains the addresses or other connection information of the backing storage.
|
|
|
|
// For example, an etcd implementation would contain the nodes of the cluster.
|
|
|
|
// A SQL implementation could contain one or more connection strings.
|
2019-12-16 12:13:18 +00:00
|
|
|
Nodes []string
|
2020-04-06 16:45:55 +01:00
|
|
|
// Database allows multiple isolated stores to be kept in one backend, if supported.
|
|
|
|
Database string
|
|
|
|
// Table is analagous to a table in database backends or a key prefix in KV backends
|
|
|
|
Table string
|
2020-03-12 13:41:30 +00:00
|
|
|
// Context should contain all implementation specific options, using context.WithValue.
|
2019-12-16 14:38:51 +00:00
|
|
|
Context context.Context
|
2019-12-16 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// Option sets values in Options
|
2019-12-16 14:38:51 +00:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// Nodes contains the addresses or other connection information of the backing storage.
|
|
|
|
// For example, an etcd implementation would contain the nodes of the cluster.
|
|
|
|
// A SQL implementation could contain one or more connection strings.
|
2019-12-16 14:38:51 +00:00
|
|
|
func Nodes(a ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Nodes = a
|
|
|
|
}
|
2019-06-11 17:20:52 +01:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:45:55 +01:00
|
|
|
// Database allows multiple isolated stores to be kept in one backend, if supported.
|
|
|
|
func Database(db string) Option {
|
2020-03-12 13:41:30 +00:00
|
|
|
return func(o *Options) {
|
2020-04-06 16:45:55 +01:00
|
|
|
o.Database = db
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:45:55 +01:00
|
|
|
// Table is analagous to a table in database backends or a key prefix in KV backends
|
|
|
|
func Table(t string) Option {
|
2019-12-16 14:38:51 +00:00
|
|
|
return func(o *Options) {
|
2020-04-06 16:45:55 +01:00
|
|
|
o.Table = t
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContext sets the stores context, for any extra configuration
|
|
|
|
func WithContext(c context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = c
|
2019-12-16 14:38:51 +00:00
|
|
|
}
|
2019-11-01 14:13:21 +00:00
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// ReadOptions configures an individual Read operation
|
|
|
|
type ReadOptions struct {
|
2020-04-09 17:56:13 +01:00
|
|
|
Database, Table string
|
2020-03-12 13:41:30 +00:00
|
|
|
// Prefix returns all records that are prefixed with key
|
|
|
|
Prefix bool
|
|
|
|
// Suffix returns all records that have the suffix key
|
|
|
|
Suffix bool
|
|
|
|
// Limit limits the number of returned records
|
|
|
|
Limit uint
|
|
|
|
// Offset when combined with Limit supports pagination
|
|
|
|
Offset uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadOption sets values in ReadOptions
|
|
|
|
type ReadOption func(r *ReadOptions)
|
|
|
|
|
2020-04-09 17:56:13 +01:00
|
|
|
// ReadFrom the database and table
|
|
|
|
func ReadFrom(database, table string) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Database = database
|
|
|
|
r.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// ReadPrefix returns all records that are prefixed with key
|
2020-01-08 22:23:14 +00:00
|
|
|
func ReadPrefix() ReadOption {
|
2020-03-12 13:41:30 +00:00
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Prefix = true
|
2020-01-08 22:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// ReadSuffix returns all records that have the suffix key
|
2020-02-03 08:16:02 +00:00
|
|
|
func ReadSuffix() ReadOption {
|
2020-03-12 13:41:30 +00:00
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Suffix = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLimit limits the number of responses to l
|
|
|
|
func ReadLimit(l uint) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Limit = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadOffset starts returning responses from o. Use in conjunction with Limit for pagination
|
|
|
|
func ReadOffset(o uint) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Offset = o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOptions configures an individual Write operation
|
|
|
|
// If Expiry and TTL are set TTL takes precedence
|
|
|
|
type WriteOptions struct {
|
2020-04-09 17:56:13 +01:00
|
|
|
Database, Table string
|
2020-03-12 13:41:30 +00:00
|
|
|
// Expiry is the time the record expires
|
|
|
|
Expiry time.Time
|
|
|
|
// TTL is the time until the record expires
|
|
|
|
TTL time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOption sets values in WriteOptions
|
|
|
|
type WriteOption func(w *WriteOptions)
|
|
|
|
|
2020-04-09 17:56:13 +01:00
|
|
|
// WriteTo the database and table
|
|
|
|
func WriteTo(database, table string) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.Database = database
|
|
|
|
w.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// WriteExpiry is the time the record expires
|
|
|
|
func WriteExpiry(t time.Time) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.Expiry = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTTL is the time the record expires
|
|
|
|
func WriteTTL(d time.Duration) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.TTL = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteOptions configures an individual Delete operation
|
2020-04-09 17:56:13 +01:00
|
|
|
type DeleteOptions struct {
|
|
|
|
Database, Table string
|
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
|
|
|
|
// DeleteOption sets values in DeleteOptions
|
|
|
|
type DeleteOption func(d *DeleteOptions)
|
|
|
|
|
2020-04-09 17:56:13 +01:00
|
|
|
// DeleteFrom the database and table
|
|
|
|
func DeleteFrom(database, table string) DeleteOption {
|
|
|
|
return func(d *DeleteOptions) {
|
|
|
|
d.Database = database
|
|
|
|
d.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// ListOptions configures an individual List operation
|
|
|
|
type ListOptions struct {
|
2020-04-09 17:56:13 +01:00
|
|
|
// List from the following
|
|
|
|
Database, Table string
|
2020-03-12 13:41:30 +00:00
|
|
|
// Prefix returns all keys that are prefixed with key
|
|
|
|
Prefix string
|
|
|
|
// Suffix returns all keys that end with key
|
|
|
|
Suffix string
|
|
|
|
// Limit limits the number of returned keys
|
|
|
|
Limit uint
|
|
|
|
// Offset when combined with Limit supports pagination
|
|
|
|
Offset uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListOption sets values in ListOptions
|
|
|
|
type ListOption func(l *ListOptions)
|
|
|
|
|
2020-04-09 17:56:13 +01:00
|
|
|
// ListFrom the database and table
|
|
|
|
func ListFrom(database, table string) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Database = database
|
|
|
|
l.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// ListPrefix returns all keys that are prefixed with key
|
|
|
|
func ListPrefix(p string) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Prefix = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSuffix returns all keys that end with key
|
|
|
|
func ListSuffix(s string) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Suffix = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListLimit limits the number of returned keys to l
|
|
|
|
func ListLimit(l uint) ListOption {
|
|
|
|
return func(lo *ListOptions) {
|
|
|
|
lo.Limit = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListOffset starts returning responses from o. Use in conjunction with Limit for pagination.
|
|
|
|
func ListOffset(o uint) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Offset = o
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
}
|