store: refactor interface

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-12-10 22:08:17 +03:00
parent a754ff7c0c
commit e6aa5b967b
8 changed files with 73 additions and 62 deletions

View File

@@ -29,12 +29,17 @@ func (n *noopStore) String() string {
}
// Read reads store value by key
func (n *noopStore) Read(ctx context.Context, key string, opts ...ReadOption) ([]*Record, error) {
return []*Record{}, nil
func (n *noopStore) Exists(ctx context.Context, key string) error {
return ErrNotFound
}
// Read reads store value by key
func (n *noopStore) Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
return ErrNotFound
}
// Write writes store record
func (n *noopStore) Write(ctx context.Context, r *Record, opts ...WriteOption) error {
func (n *noopStore) Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
return nil
}

View File

@@ -4,7 +4,9 @@ import (
"context"
"time"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/metadata"
)
// Options contains configuration for the Store
@@ -17,6 +19,8 @@ type Options struct {
Database string
// Table is analag for a table in database backends or a key prefix in KV backends
Table string
// Codec that used for marshal/unmarshal value
Codec codec.Codec
// Logger the logger
Logger logger.Logger
// Context should contain all implementation specific options, using context.WithValue.
@@ -45,6 +49,13 @@ func Context(ctx context.Context) Option {
}
}
// Codec sets the codec
func Codec(c codec.Codec) Option {
return func(o *Options) {
o.Codec = c
}
}
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
@@ -130,11 +141,13 @@ func ReadOffset(o uint) ReadOption {
// WriteOptions configures an individual Write operation
// If Expiry and TTL are set TTL takes precedence
type WriteOptions struct {
Database, Table string
Database string
Table string
// Expiry is the time the record expires
Expiry time.Time
// TTL is the time until the record expires
TTL time.Duration
TTL time.Duration
Metadata metadata.Metadata
}
// WriteOption sets values in WriteOptions
@@ -162,6 +175,13 @@ func WriteTTL(d time.Duration) WriteOption {
}
}
// WriteMetadata add metadata.Metadata
func WriteMetadata(md metadata.Metadata) WriteOption {
return func(w *WriteOptions) {
w.Metadata = metadata.Copy(md)
}
}
// DeleteOptions configures an individual Delete operation
type DeleteOptions struct {
Database, Table string

View File

@@ -5,7 +5,8 @@ package store
import (
"context"
"errors"
"time"
"github.com/unistack-org/micro/v3/metadata"
)
var (
@@ -23,10 +24,12 @@ type Store interface {
Connect(ctx context.Context) error
// Options allows you to view the current options.
Options() Options
// Read takes a single key name and optional ReadOptions. It returns matching []*Record or an error.
Read(ctx context.Context, key string, opts ...ReadOption) ([]*Record, error)
// Write() writes a record to the store, and returns an error if the record was not written.
Write(ctx context.Context, r *Record, opts ...WriteOption) error
// Exists check that key exists in store
Exists(ctx context.Context, key string) error
// 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
// Delete removes the record with the corresponding key from the store.
Delete(ctx context.Context, key string, opts ...DeleteOption) error
// List returns any keys that match, or an empty list with no error if none matched.
@@ -37,14 +40,11 @@ type Store interface {
String() string
}
// Record is an item stored or retrieved from a Store
type Record struct {
// The key to store the record
Key string `json:"key"`
// The value within the record
Value []byte `json:"value"`
// Any associated metadata for indexing
Metadata map[string]interface{} `json:"metadata"`
// Time to expire a record: TODO: change to timestamp
Expiry time.Duration `json:"expiry,omitempty"`
// Value is an item stored or retrieved from a Store
// may be used in store implementations to provide metadata
type Value struct {
// Data holds underline struct
Data interface{} `json:"data"`
// Metadata associated with data for indexing
Metadata metadata.Metadata `json:"metadata"`
}