2019-06-12 09:46:20 +03:00
|
|
|
package store
|
2019-06-11 19:20:52 +03:00
|
|
|
|
|
|
|
import (
|
2019-12-16 17:38:51 +03:00
|
|
|
"context"
|
2021-01-26 02:08:22 +03:00
|
|
|
"crypto/tls"
|
2020-03-12 16:41:30 +03:00
|
|
|
"time"
|
2020-08-29 17:44:49 +03:00
|
|
|
|
2020-12-10 22:08:56 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-12-10 22:08:56 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2019-06-11 19:20:52 +03:00
|
|
|
)
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// Options contains configuration for the Store
|
2019-12-16 15:13:18 +03:00
|
|
|
type Options struct {
|
2021-02-14 11:28:50 +03:00
|
|
|
// Name specifies store name
|
2021-01-29 13:17:32 +03:00
|
|
|
Name string
|
2020-03-12 16:41:30 +03: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 15:13:18 +03:00
|
|
|
Nodes []string
|
2020-04-06 18:45:55 +03:00
|
|
|
// Database allows multiple isolated stores to be kept in one backend, if supported.
|
|
|
|
Database string
|
2020-08-25 14:33:36 +03:00
|
|
|
// Table is analag for a table in database backends or a key prefix in KV backends
|
2020-04-06 18:45:55 +03:00
|
|
|
Table string
|
2020-12-10 22:08:56 +03:00
|
|
|
// Codec that used for marshal/unmarshal value
|
|
|
|
Codec codec.Codec
|
2020-12-09 12:10:25 +03:00
|
|
|
// Logger the logger
|
2020-08-29 17:44:49 +03:00
|
|
|
Logger logger.Logger
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter the meter
|
|
|
|
Meter meter.Meter
|
|
|
|
// Tracer the tacer
|
|
|
|
Tracer tracer.Tracer
|
2021-01-26 02:08:22 +03:00
|
|
|
// TLSConfig specifies tls.Config for secure
|
|
|
|
TLSConfig *tls.Config
|
2021-02-14 11:28:50 +03:00
|
|
|
// Context should contain all implementation specific options
|
2019-12-16 17:38:51 +03:00
|
|
|
Context context.Context
|
2019-12-16 15:13:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewOptions creates options struct
|
2020-09-05 02:11:29 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2020-09-03 15:11:05 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
2020-12-11 00:21:53 +03:00
|
|
|
Codec: codec.DefaultCodec,
|
2021-01-22 23:32:33 +03:00
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
Meter: meter.DefaultMeter,
|
2020-09-03 15:11:05 +03:00
|
|
|
}
|
2020-09-05 02:11:29 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2020-09-03 15:11:05 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// Option sets values in Options
|
2019-12-16 17:38:51 +03:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2021-01-26 02:08:22 +03:00
|
|
|
// TLSConfig specifies a *tls.Config
|
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Context pass context to store
|
2020-10-16 09:38:57 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:08:56 +03:00
|
|
|
// Codec sets the codec
|
|
|
|
func Codec(c codec.Codec) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Codec = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:02:54 +03:00
|
|
|
// Name the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Tracer sets the tracer
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03: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 17:38:51 +03:00
|
|
|
func Nodes(a ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Nodes = a
|
|
|
|
}
|
2019-06-11 19:20:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-06 18:45:55 +03:00
|
|
|
// Database allows multiple isolated stores to be kept in one backend, if supported.
|
|
|
|
func Database(db string) Option {
|
2020-03-12 16:41:30 +03:00
|
|
|
return func(o *Options) {
|
2020-04-06 18:45:55 +03:00
|
|
|
o.Database = db
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:33:36 +03:00
|
|
|
// Table is analag for a table in database backends or a key prefix in KV backends
|
2020-04-06 18:45:55 +03:00
|
|
|
func Table(t string) Option {
|
2019-12-16 17:38:51 +03:00
|
|
|
return func(o *Options) {
|
2020-04-06 18:45:55 +03:00
|
|
|
o.Table = t
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:37:40 +03:00
|
|
|
// NewReadOptions fills ReadOptions struct with opts slice
|
|
|
|
func NewReadOptions(opts ...ReadOption) ReadOptions {
|
|
|
|
options := ReadOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// ReadOptions configures an individual Read operation
|
|
|
|
type ReadOptions struct {
|
2021-01-26 02:08:22 +03:00
|
|
|
Database string
|
|
|
|
Table string
|
|
|
|
Namespace string
|
|
|
|
Context context.Context
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadOption sets values in ReadOptions
|
|
|
|
type ReadOption func(r *ReadOptions)
|
|
|
|
|
2020-04-09 19:56:13 +03:00
|
|
|
// ReadFrom the database and table
|
|
|
|
func ReadFrom(database, table string) ReadOption {
|
|
|
|
return func(r *ReadOptions) {
|
|
|
|
r.Database = database
|
|
|
|
r.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:37:40 +03:00
|
|
|
// NewWriteOptions fills WriteOptions struct with opts slice
|
|
|
|
func NewWriteOptions(opts ...WriteOption) WriteOptions {
|
|
|
|
options := WriteOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// WriteOptions configures an individual Write operation
|
|
|
|
type WriteOptions struct {
|
2021-01-26 02:08:22 +03:00
|
|
|
Database string
|
|
|
|
Table string
|
|
|
|
TTL time.Duration
|
|
|
|
Metadata metadata.Metadata
|
|
|
|
Namespace string
|
|
|
|
Context context.Context
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOption sets values in WriteOptions
|
|
|
|
type WriteOption func(w *WriteOptions)
|
|
|
|
|
2020-04-09 19:56:13 +03: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 16:41:30 +03:00
|
|
|
// WriteTTL is the time the record expires
|
|
|
|
func WriteTTL(d time.Duration) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.TTL = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:08:56 +03:00
|
|
|
// WriteMetadata add metadata.Metadata
|
|
|
|
func WriteMetadata(md metadata.Metadata) WriteOption {
|
|
|
|
return func(w *WriteOptions) {
|
|
|
|
w.Metadata = metadata.Copy(md)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:37:40 +03:00
|
|
|
// NewDeleteOptions fills DeleteOptions struct with opts slice
|
|
|
|
func NewDeleteOptions(opts ...DeleteOption) DeleteOptions {
|
|
|
|
options := DeleteOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// DeleteOptions configures an individual Delete operation
|
2020-04-09 19:56:13 +03:00
|
|
|
type DeleteOptions struct {
|
2021-01-26 02:08:22 +03:00
|
|
|
Database string
|
|
|
|
Table string
|
|
|
|
Namespace string
|
|
|
|
Context context.Context
|
2020-04-09 19:56:13 +03:00
|
|
|
}
|
2020-03-12 16:41:30 +03:00
|
|
|
|
|
|
|
// DeleteOption sets values in DeleteOptions
|
|
|
|
type DeleteOption func(d *DeleteOptions)
|
|
|
|
|
2020-04-09 19:56:13 +03:00
|
|
|
// DeleteFrom the database and table
|
|
|
|
func DeleteFrom(database, table string) DeleteOption {
|
|
|
|
return func(d *DeleteOptions) {
|
|
|
|
d.Database = database
|
|
|
|
d.Table = table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:37:40 +03:00
|
|
|
// NewListOptions fills ListOptions struct with opts slice
|
|
|
|
func NewListOptions(opts ...ListOption) ListOptions {
|
|
|
|
options := ListOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// ListOptions configures an individual List operation
|
|
|
|
type ListOptions struct {
|
2020-04-09 19:56:13 +03:00
|
|
|
// List from the following
|
|
|
|
Database, Table string
|
2020-03-12 16:41:30 +03: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
|
2021-01-26 02:08:22 +03:00
|
|
|
Offset uint
|
|
|
|
Namespace string
|
|
|
|
Context context.Context
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListOption sets values in ListOptions
|
|
|
|
type ListOption func(l *ListOptions)
|
|
|
|
|
2020-04-09 19:56:13 +03: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 16:41:30 +03: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 11:16:02 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 02:08:22 +03:00
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// ExistsOption specifies Exists call options
|
2021-01-26 02:08:22 +03:00
|
|
|
type ExistsOption func(*ExistsOptions)
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// ExistsOptions holds options for Exists method
|
2021-01-26 02:08:22 +03:00
|
|
|
type ExistsOptions struct {
|
|
|
|
Namespace string
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// NewExistsOptions helper for Exists method
|
2021-01-26 02:08:22 +03:00
|
|
|
func NewExistsOptions(opts ...ExistsOption) ExistsOptions {
|
|
|
|
options := ExistsOptions{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|