2019-06-12 08:46:20 +02:00
|
|
|
package store
|
2019-06-11 18:20:52 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-16 16:38:51 +02:00
|
|
|
"context"
|
2020-03-12 15:41:30 +02:00
|
|
|
"time"
|
2020-05-11 18:57:39 +02:00
|
|
|
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/client"
|
2022-09-29 16:44:53 +02:00
|
|
|
"go-micro.dev/v4/logger"
|
2019-06-11 18:20:52 +02:00
|
|
|
)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Options contains configuration for the Store.
|
2019-12-16 14:13:18 +02:00
|
|
|
type Options struct {
|
2020-03-12 15:41:30 +02: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:13:18 +02:00
|
|
|
Nodes []string
|
2020-04-06 17:45:55 +02:00
|
|
|
// Database allows multiple isolated stores to be kept in one backend, if supported.
|
|
|
|
Database string
|
2022-09-30 16:27:07 +02:00
|
|
|
// Table is analogous to a table in database backends or a key prefix in KV backends
|
2020-04-06 17:45:55 +02:00
|
|
|
Table string
|
2020-03-12 15:41:30 +02:00
|
|
|
// Context should contain all implementation specific options, using context.WithValue.
|
2019-12-16 16:38:51 +02:00
|
|
|
Context context.Context
|
2020-05-11 18:57:39 +02:00
|
|
|
// Client to use for RPC
|
|
|
|
Client client.Client
|
2022-09-29 16:44:53 +02:00
|
|
|
// Logger is the underline logger
|
|
|
|
Logger logger.Logger
|
2019-12-16 14:13:18 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Option sets values in Options.
|
2019-12-16 16:38:51 +02:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-03-12 15:41:30 +02: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 16:38:51 +02:00
|
|
|
func Nodes(a ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Nodes = a
|
|
|
|
}
|
2019-06-11 18:20:52 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 17:45:55 +02:00
|
|
|
// Database allows multiple isolated stores to be kept in one backend, if supported.
|
|
|
|
func Database(db string) Option {
|
2020-03-12 15:41:30 +02:00
|
|
|
return func(o *Options) {
|
2020-04-06 17:45:55 +02:00
|
|
|
o.Database = db
|
2020-03-12 15:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:45:55 +02:00
|
|
|
func Table(t string) Option {
|
2019-12-16 16:38:51 +02:00
|
|
|
return func(o *Options) {
|
2020-04-06 17:45:55 +02:00
|
|
|
o.Table = t
|
2020-03-12 15:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithContext sets the stores context, for any extra configuration.
|
2020-03-12 15:41:30 +02:00
|
|
|
func WithContext(c context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = c
|
2019-12-16 16:38:51 +02:00
|
|
|
}
|
2019-11-01 16:13:21 +02:00
|
|
|
}
|
2020-01-09 00:23:14 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithClient sets the stores client to use for RPC.
|
2020-05-11 18:57:39 +02:00
|
|
|
func WithClient(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithLogger sets the underline logger.
|
2022-09-29 16:44:53 +02:00
|
|
|
func WithLogger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadOptions configures an individual Read operation.
|
2020-03-12 15:41:30 +02:00
|
|
|
type ReadOptions struct {
|
2020-04-09 18:56:13 +02:00
|
|
|
Database, Table string
|
2020-03-12 15:41:30 +02: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
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadOption sets values in ReadOptions.
|
2020-03-12 15:41:30 +02:00
|
|
|
type ReadOption func(r *ReadOptions)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadFrom the database and table.
|
2020-04-09 18:56:13 +02:00
|
|
|
func ReadFrom(database, table string) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Database = database
|
|
|
|
r.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadPrefix returns all records that are prefixed with key.
|
2020-01-09 00:23:14 +02:00
|
|
|
func ReadPrefix() ReadOption {
|
2020-03-12 15:41:30 +02:00
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Prefix = true
|
2020-01-09 00:23:14 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 10:16:02 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadSuffix returns all records that have the suffix key.
|
2020-02-03 10:16:02 +02:00
|
|
|
func ReadSuffix() ReadOption {
|
2020-03-12 15:41:30 +02:00
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Suffix = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadLimit limits the number of responses to l.
|
2020-03-12 15:41:30 +02:00
|
|
|
func ReadLimit(l uint) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Limit = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ReadOffset starts returning responses from o. Use in conjunction with Limit for pagination.
|
2020-03-12 15:41:30 +02:00
|
|
|
func ReadOffset(o uint) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Offset = o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOptions configures an individual Write operation
|
2022-09-30 16:27:07 +02:00
|
|
|
// If Expiry and TTL are set TTL takes precedence.
|
2020-03-12 15:41:30 +02:00
|
|
|
type WriteOptions struct {
|
2020-04-09 18:56:13 +02:00
|
|
|
Database, Table string
|
2020-03-12 15:41:30 +02:00
|
|
|
// Expiry is the time the record expires
|
|
|
|
Expiry time.Time
|
|
|
|
// TTL is the time until the record expires
|
|
|
|
TTL time.Duration
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WriteOption sets values in WriteOptions.
|
2020-03-12 15:41:30 +02:00
|
|
|
type WriteOption func(w *WriteOptions)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WriteTo the database and table.
|
2020-04-09 18:56:13 +02:00
|
|
|
func WriteTo(database, table string) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.Database = database
|
|
|
|
w.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WriteExpiry is the time the record expires.
|
2020-03-12 15:41:30 +02:00
|
|
|
func WriteExpiry(t time.Time) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.Expiry = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WriteTTL is the time the record expires.
|
2020-03-12 15:41:30 +02:00
|
|
|
func WriteTTL(d time.Duration) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.TTL = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// DeleteOptions configures an individual Delete operation.
|
2020-04-09 18:56:13 +02:00
|
|
|
type DeleteOptions struct {
|
|
|
|
Database, Table string
|
|
|
|
}
|
2020-03-12 15:41:30 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// DeleteOption sets values in DeleteOptions.
|
2020-03-12 15:41:30 +02:00
|
|
|
type DeleteOption func(d *DeleteOptions)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// DeleteFrom the database and table.
|
2020-04-09 18:56:13 +02:00
|
|
|
func DeleteFrom(database, table string) DeleteOption {
|
|
|
|
return func(d *DeleteOptions) {
|
|
|
|
d.Database = database
|
|
|
|
d.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ListOptions configures an individual List operation.
|
2020-03-12 15:41:30 +02:00
|
|
|
type ListOptions struct {
|
2020-04-09 18:56:13 +02:00
|
|
|
// List from the following
|
|
|
|
Database, Table string
|
2020-03-12 15:41:30 +02: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
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ListOption sets values in ListOptions.
|
2020-03-12 15:41:30 +02:00
|
|
|
type ListOption func(l *ListOptions)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ListFrom the database and table.
|
2020-04-09 18:56:13 +02:00
|
|
|
func ListFrom(database, table string) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Database = database
|
|
|
|
l.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ListPrefix returns all keys that are prefixed with key.
|
2020-03-12 15:41:30 +02:00
|
|
|
func ListPrefix(p string) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Prefix = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ListSuffix returns all keys that end with key.
|
2020-03-12 15:41:30 +02:00
|
|
|
func ListSuffix(s string) ListOption {
|
|
|
|
return func(l *ListOptions) {
|
|
|
|
l.Suffix = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// ListLimit limits the number of returned keys to l.
|
2020-03-12 15:41:30 +02:00
|
|
|
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 10:16:02 +02:00
|
|
|
}
|
|
|
|
}
|