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
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/metadata"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/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-03-06 19:45:13 +03:00
|
|
|
// Meter used for metrics
|
|
|
|
Meter meter.Meter
|
|
|
|
// Tracer used for tracing
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// Codec used to marshal/unmarshal
|
|
|
|
Codec codec.Codec
|
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
|
|
|
// TLSConfig holds tls.TLSConfig options
|
|
|
|
TLSConfig *tls.Config
|
2021-02-14 11:28:50 +03:00
|
|
|
// Name specifies store name
|
2021-01-29 13:17:32 +03:00
|
|
|
Name string
|
2021-07-14 17:11:37 +03:00
|
|
|
// Namespace of the records
|
|
|
|
Namespace string
|
2022-11-07 14:53:58 +03:00
|
|
|
// Separator used as key parts separator
|
|
|
|
Separator string
|
2023-07-29 00:40:58 +03:00
|
|
|
// Address contains store address
|
|
|
|
Address []string
|
2022-07-08 22:16:33 +03:00
|
|
|
// Timeout specifies timeout duration for all operations
|
|
|
|
Timeout time.Duration
|
2019-12-16 15:13:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewOptions creates options struct
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2020-09-05 02:11:29 +03:00
|
|
|
options := Options{
|
2022-11-07 14:53:58 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
|
|
|
Codec: codec.DefaultCodec,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Separator: DefaultSeparator,
|
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
|
|
|
}
|
|
|
|
|
2022-11-07 14:53:58 +03:00
|
|
|
// Separator the value used as key parts separator
|
2023-07-29 00:40:58 +03:00
|
|
|
func Separator(s string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, s, "Separator")
|
2021-01-22 23:32:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 22:16:33 +03:00
|
|
|
// Timeout sets the timeout
|
2023-07-29 00:40:58 +03:00
|
|
|
func Timeout(td time.Duration) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, td, ".Timeout")
|
2019-12-16 17:38:51 +03:00
|
|
|
}
|
2019-06-11 19:20:52 +03:00
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// ReadOptions configures an individual Read operation
|
|
|
|
type ReadOptions struct {
|
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// Namespace holds namespace
|
|
|
|
Namespace string
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 22:37:40 +03:00
|
|
|
// NewReadOptions fills ReadOptions struct with opts slice
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewReadOptions(opts ...options.Option) ReadOptions {
|
2020-12-10 22:37:40 +03:00
|
|
|
options := ReadOptions{}
|
|
|
|
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-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// Metadata contains additional metadata
|
|
|
|
Metadata metadata.Metadata
|
|
|
|
// Namespace holds namespace
|
2021-01-26 02:08:22 +03:00
|
|
|
Namespace string
|
2021-03-06 19:45:13 +03:00
|
|
|
// TTL specifies key TTL
|
|
|
|
TTL time.Duration
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// NewWriteOptions fills WriteOptions struct with opts slice
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewWriteOptions(opts ...options.Option) WriteOptions {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := WriteOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMetadata add metadata.Metadata
|
2023-07-29 00:40:58 +03:00
|
|
|
func WriteMetadata(md metadata.Metadata) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, metadata.Copy(md), ".Metadata")
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// WriteTTL is the time the record expires
|
2023-07-29 00:40:58 +03:00
|
|
|
func WriteTTL(td time.Duration) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, td, ".TTL")
|
2020-12-10 22:37:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// Namespace holds namespace
|
2021-01-26 02:08:22 +03:00
|
|
|
Namespace string
|
2020-04-09 19:56:13 +03:00
|
|
|
}
|
2020-03-12 16:41:30 +03:00
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// NewDeleteOptions fills DeleteOptions struct with opts slice
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewDeleteOptions(opts ...options.Option) DeleteOptions {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := DeleteOptions{}
|
|
|
|
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 {
|
2021-01-26 02:08:22 +03:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
Prefix string
|
|
|
|
Suffix string
|
|
|
|
Namespace string
|
|
|
|
Limit uint
|
|
|
|
Offset uint
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// NewListOptions fills ListOptions struct with opts slice
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewListOptions(opts ...options.Option) ListOptions {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := ListOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// ListPrefix returns all keys that are prefixed with key
|
2023-07-29 00:40:58 +03:00
|
|
|
func ListPrefix(s string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, s, ".Prefix")
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSuffix returns all keys that end with key
|
2023-07-29 00:40:58 +03:00
|
|
|
func ListSuffix(s string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, s, ".Prefix")
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// ListLimit limits the number of returned keys
|
2023-07-29 00:40:58 +03:00
|
|
|
func ListLimit(n uint) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, n, ".Limit")
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
// ListOffset use with Limit for pagination
|
2023-07-29 00:40:58 +03:00
|
|
|
func ListOffset(n uint) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, n, ".Offset")
|
2021-07-14 17:11:37 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 02:08:22 +03:00
|
|
|
|
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 {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// Namespace contains namespace
|
2021-01-26 02:08:22 +03:00
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// NewExistsOptions helper for Exists method
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewExistsOptions(opts ...options.Option) ExistsOptions {
|
2021-01-26 02:08:22 +03:00
|
|
|
options := ExistsOptions{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|