micro/store/store.go

49 lines
1.9 KiB
Go
Raw Normal View History

// Package store is an interface for distributed data storage.
// The design document is located at https://github.com/micro/development/blob/master/design/framework/store.md
2019-06-12 09:46:20 +03:00
package store
2019-05-31 02:43:23 +03:00
import (
"context"
2019-05-31 02:43:23 +03:00
"errors"
"time"
)
var (
// ErrNotFound is returned when a key doesn't exist
ErrNotFound = errors.New("not found")
DefaultStore Store = &NoopStore{opts: NewOptions()}
2019-05-31 02:43:23 +03:00
)
2019-06-12 09:46:20 +03:00
// Store is a data storage interface
type Store interface {
// Init initialises the store. It must perform any required setup on the backing storage implementation and check that it is ready for use, returning any errors.
Init(opts ...Option) error
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
// 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.
List(ctx context.Context, opts ...ListOption) ([]string, error)
// Disconnect the store
Disconnect(ctx context.Context) error
2020-04-02 01:27:15 +03:00
// String returns the name of the implementation.
String() string
2019-05-31 02:43:23 +03:00
}
// Record is an item stored or retrieved from a Store
2019-05-31 02:43:23 +03:00
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
2020-04-09 21:38:43 +03:00
Expiry time.Duration `json:"expiry,omitempty"`
2019-05-31 02:43:23 +03:00
}