2020-03-12 13:41:30 +00:00
|
|
|
// Package store is an interface for distributed data storage.
|
2024-09-20 17:54:17 +03:00
|
|
|
package store
|
2019-05-31 00:43:23 +01:00
|
|
|
|
|
|
|
import (
|
2020-09-17 15:15:42 +03:00
|
|
|
"context"
|
2019-05-31 00:43:23 +01:00
|
|
|
"errors"
|
2024-12-01 19:54:38 +03:00
|
|
|
"time"
|
2019-05-31 00:43:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-12-01 19:54:38 +03:00
|
|
|
ErrWatcherStopped = errors.New("watcher stopped")
|
2024-10-05 14:46:22 +03:00
|
|
|
// ErrNotConnected is returned when a store is not connected
|
2024-12-09 13:06:43 +03:00
|
|
|
ErrNotConnected = errors.New("not connected")
|
2020-03-12 13:41:30 +00:00
|
|
|
// ErrNotFound is returned when a key doesn't exist
|
2020-11-03 01:08:23 +03:00
|
|
|
ErrNotFound = errors.New("not found")
|
2021-01-26 02:08:22 +03:00
|
|
|
// ErrInvalidKey is returned when a key has empty or have invalid format
|
|
|
|
ErrInvalidKey = errors.New("invalid key")
|
2020-11-03 01:08:23 +03:00
|
|
|
// DefaultStore is the global default store
|
2024-12-09 13:06:43 +03:00
|
|
|
DefaultStore = NewStore()
|
2022-11-07 14:53:58 +03:00
|
|
|
// DefaultSeparator is the gloabal default key parts separator
|
|
|
|
DefaultSeparator = "/"
|
2019-05-31 00:43:23 +01:00
|
|
|
)
|
|
|
|
|
2019-06-12 07:46:20 +01:00
|
|
|
// Store is a data storage interface
|
|
|
|
type Store interface {
|
2024-04-22 08:47:50 +03:00
|
|
|
// Name returns store name
|
2021-01-29 13:17:32 +03:00
|
|
|
Name() string
|
2020-11-06 00:03:40 +03:00
|
|
|
// Init initialises the store
|
2020-10-16 09:38:57 +03:00
|
|
|
Init(opts ...Option) error
|
2020-11-03 01:08:23 +03:00
|
|
|
// Connect is used when store needs to be connected
|
2020-10-16 09:38:57 +03:00
|
|
|
Connect(ctx context.Context) error
|
2020-03-12 13:41:30 +00:00
|
|
|
// Options allows you to view the current options.
|
|
|
|
Options() Options
|
2020-12-10 22:08:56 +03:00
|
|
|
// Exists check that key exists in store
|
2021-01-26 02:08:22 +03:00
|
|
|
Exists(ctx context.Context, key string, opts ...ExistsOption) error
|
2020-12-10 22:08:56 +03:00
|
|
|
// Read reads a single key name to provided value with optional ReadOptions
|
|
|
|
Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error
|
|
|
|
// Write writes a value to key name to the store with optional WriteOption
|
|
|
|
Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error
|
2020-03-12 13:41:30 +00:00
|
|
|
// Delete removes the record with the corresponding key from the store.
|
2020-09-17 15:15:42 +03:00
|
|
|
Delete(ctx context.Context, key string, opts ...DeleteOption) error
|
2020-03-12 13:41:30 +00:00
|
|
|
// List returns any keys that match, or an empty list with no error if none matched.
|
2020-09-17 15:15:42 +03:00
|
|
|
List(ctx context.Context, opts ...ListOption) ([]string, error)
|
2020-10-16 09:38:57 +03:00
|
|
|
// Disconnect the store
|
|
|
|
Disconnect(ctx context.Context) error
|
2020-04-01 23:27:15 +01:00
|
|
|
// String returns the name of the implementation.
|
|
|
|
String() string
|
2024-12-02 13:20:13 +03:00
|
|
|
// Watch returns events watcher
|
2024-12-01 19:54:38 +03:00
|
|
|
Watch(ctx context.Context, opts ...WatchOption) (Watcher, error)
|
2024-12-02 13:20:13 +03:00
|
|
|
// Live returns store liveness
|
|
|
|
Live() bool
|
|
|
|
// Ready returns store readiness
|
|
|
|
Ready() bool
|
|
|
|
// Health returns store health
|
|
|
|
Health() bool
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
type (
|
|
|
|
FuncExists func(ctx context.Context, key string, opts ...ExistsOption) error
|
|
|
|
HookExists func(next FuncExists) FuncExists
|
|
|
|
FuncRead func(ctx context.Context, key string, val interface{}, opts ...ReadOption) error
|
|
|
|
HookRead func(next FuncRead) FuncRead
|
|
|
|
FuncWrite func(ctx context.Context, key string, val interface{}, opts ...WriteOption) error
|
|
|
|
HookWrite func(next FuncWrite) FuncWrite
|
|
|
|
FuncDelete func(ctx context.Context, key string, opts ...DeleteOption) error
|
|
|
|
HookDelete func(next FuncDelete) FuncDelete
|
|
|
|
FuncList func(ctx context.Context, opts ...ListOption) ([]string, error)
|
|
|
|
HookList func(next FuncList) FuncList
|
|
|
|
)
|
2024-12-01 19:54:38 +03:00
|
|
|
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
EventTypeUnknown = iota
|
|
|
|
EventTypeConnect
|
|
|
|
EventTypeDisconnect
|
|
|
|
EventTypeOpError
|
|
|
|
)
|
|
|
|
|
|
|
|
type Event interface {
|
|
|
|
Timestamp() time.Time
|
|
|
|
Error() error
|
|
|
|
Type() EventType
|
|
|
|
}
|
|
|
|
|
|
|
|
type Watcher interface {
|
|
|
|
// Next is a blocking call
|
|
|
|
Next() (Event, error)
|
|
|
|
// Stop stops the watcher
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type WatchOption func(*WatchOptions) error
|
|
|
|
|
|
|
|
type WatchOptions struct{}
|
|
|
|
|
|
|
|
func NewWatchOptions(opts ...WatchOption) (WatchOptions, error) {
|
|
|
|
options := WatchOptions{}
|
|
|
|
var err error
|
|
|
|
for _, o := range opts {
|
|
|
|
if err = o(&options); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options, err
|
|
|
|
}
|
2024-12-06 19:05:27 +03:00
|
|
|
|
|
|
|
func Watch(context.Context) (Watcher, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|