2024-04-22 08:47:50 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-26 12:18:17 +03:00
|
|
|
"sync/atomic"
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Store = (*noopStore)(nil)
|
|
|
|
|
|
|
|
type noopStore struct {
|
2024-11-26 12:18:17 +03:00
|
|
|
funcRead FuncRead
|
|
|
|
funcWrite FuncWrite
|
|
|
|
funcExists FuncExists
|
|
|
|
funcList FuncList
|
|
|
|
funcDelete FuncDelete
|
|
|
|
opts Options
|
|
|
|
isConnected atomic.Int32
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStore(opts ...Option) *noopStore {
|
|
|
|
options := NewOptions(opts...)
|
|
|
|
return &noopStore{opts: options}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.funcRead = n.fnRead
|
|
|
|
n.funcWrite = n.fnWrite
|
|
|
|
n.funcExists = n.fnExists
|
|
|
|
n.funcList = n.fnList
|
|
|
|
n.funcDelete = n.fnDelete
|
|
|
|
|
|
|
|
n.opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
switch h := hook.(type) {
|
|
|
|
case HookRead:
|
|
|
|
n.funcRead = h(n.funcRead)
|
|
|
|
case HookWrite:
|
|
|
|
n.funcWrite = h(n.funcWrite)
|
|
|
|
case HookExists:
|
|
|
|
n.funcExists = h(n.funcExists)
|
|
|
|
case HookList:
|
|
|
|
n.funcList = h(n.funcList)
|
|
|
|
case HookDelete:
|
|
|
|
n.funcDelete = h(n.funcDelete)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Connect(ctx context.Context) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if n.opts.LazyConnect {
|
|
|
|
return nil
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
2024-11-26 12:18:17 +03:00
|
|
|
return n.connect(ctx)
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Disconnect(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if n.opts.LazyConnect {
|
|
|
|
if err := n.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcRead(ctx, key, val, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) fnRead(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if n.opts.LazyConnect {
|
|
|
|
if err := n.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcDelete(ctx, key, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) fnDelete(ctx context.Context, key string, opts ...DeleteOption) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Exists(ctx context.Context, key string, opts ...ExistsOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if n.opts.LazyConnect {
|
|
|
|
if err := n.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcExists(ctx, key, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) fnExists(ctx context.Context, key string, opts ...ExistsOption) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if n.opts.LazyConnect {
|
|
|
|
if err := n.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcWrite(ctx, key, val, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) fnWrite(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) List(ctx context.Context, opts ...ListOption) ([]string, error) {
|
2024-11-26 12:18:17 +03:00
|
|
|
if n.opts.LazyConnect {
|
|
|
|
if err := n.connect(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcList(ctx, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) fnList(ctx context.Context, opts ...ListOption) ([]string, error) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Name() string {
|
|
|
|
return n.opts.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) String() string {
|
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Options() Options {
|
|
|
|
return n.opts
|
|
|
|
}
|
2024-11-26 12:18:17 +03:00
|
|
|
|
|
|
|
func (n *noopStore) connect(ctx context.Context) error {
|
|
|
|
if n.isConnected.CompareAndSwap(0, 1) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|