2024-04-22 08:47:50 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-12-01 19:54:38 +03:00
|
|
|
"sync"
|
2024-11-26 12:18:17 +03:00
|
|
|
"sync/atomic"
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/options"
|
2024-12-01 19:54:38 +03:00
|
|
|
"go.unistack.org/micro/v3/util/id"
|
2024-04-22 08:47:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Store = (*noopStore)(nil)
|
|
|
|
|
|
|
|
type noopStore struct {
|
2024-12-09 16:23:25 +03:00
|
|
|
watchers map[string]Watcher
|
|
|
|
|
|
|
|
funcRead FuncRead
|
|
|
|
funcWrite FuncWrite
|
|
|
|
funcExists FuncExists
|
|
|
|
funcList FuncList
|
|
|
|
funcDelete FuncDelete
|
|
|
|
|
2024-11-26 12:18:17 +03:00
|
|
|
opts Options
|
|
|
|
isConnected atomic.Int32
|
2024-12-09 16:23:25 +03:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
|
|
|
|
2024-12-02 13:20:13 +03:00
|
|
|
func (n *noopStore) Live() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Ready() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStore) Health() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func NewStore(opts ...Option) Store {
|
2024-04-22 08:47:50 +03:00
|
|
|
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
|
|
|
|
|
2024-12-17 15:35:04 +03:00
|
|
|
n.opts.Hooks.EachPrev(func(hook options.Hook) {
|
2024-04-22 08:47:50 +03:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func (n *noopStore) fnRead(ctx context.Context, _ string, _ interface{}, _ ...ReadOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func (n *noopStore) fnDelete(ctx context.Context, _ string, _ ...DeleteOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func (n *noopStore) fnExists(ctx context.Context, _ string, _ ...ExistsOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func (n *noopStore) fnWrite(ctx context.Context, _ string, _ interface{}, _ ...WriteOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
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
|
|
|
|
}
|
2024-12-01 19:54:38 +03:00
|
|
|
|
|
|
|
type watcher struct {
|
2024-12-09 16:23:25 +03:00
|
|
|
opts WatchOptions
|
|
|
|
ch chan Event
|
2024-12-01 19:54:38 +03:00
|
|
|
exit chan bool
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
func (n *noopStore) Watch(_ context.Context, opts ...WatchOption) (Watcher, error) {
|
2024-12-01 19:54:38 +03:00
|
|
|
id, err := id.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wo, err := NewWatchOptions(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// construct the watcher
|
|
|
|
w := &watcher{
|
|
|
|
exit: make(chan bool),
|
|
|
|
ch: make(chan Event),
|
|
|
|
id: id,
|
|
|
|
opts: wo,
|
|
|
|
}
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
n.mu.Lock()
|
|
|
|
n.watchers[w.id] = w
|
|
|
|
n.mu.Unlock()
|
2024-12-01 19:54:38 +03:00
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Next() (Event, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Stop() {
|
|
|
|
}
|