Documentation
¶
Overview ¶
Package kvstore is an interface for distributed key-value data storage.
Index ¶
- Constants
- Variables
- func Register(name string, factory ProviderFunc) bool
- type Config
- type ConfigType
- type DeleteOption
- type DeleteOptions
- type Deprecated
- type GetOption
- type GetOptions
- type KVStore
- type KeysOption
- type KeysOptions
- type ListOption
- type ListOptions
- type Option
- type ProviderFunc
- type ReadOption
- type ReadOptions
- type Record
- type SetOption
- type SetOptions
- type Type
- type WatchEvent
- type WatchOp
- type WatchOption
- type WatchOptions
- type Watcher
- type WriteOption
- type WriteOptions
Constants ¶
const ComponentType = "kvstore"
ComponentType is the components name.
Variables ¶
var ( // DefaultConfigSection is the section in the config to use. DefaultConfigSection = "kvstore" // DefaultKVStore is the default kvstore to use. DefaultKVStore = "natsjs" )
var ( // ErrNotFound is returned when a key is not found. ErrNotFound = errors.New("not found") // ErrDatabaseNotFound is returned when a database is not found. ErrDatabaseNotFound = errors.New("database not found") // ErrTableNotFound is returned when a table is not found. ErrTableNotFound = errors.New("table not found") )
Functions ¶
func Register ¶
func Register(name string, factory ProviderFunc) bool
Register makes a plugin available by the provided name. If Register is called twice with the same name, it panics.
Types ¶
type Config ¶
type Config struct {
// Plugin selects the client implementation.
Plugin string `json:"plugin" yaml:"plugin"`
// Database allows multiple isolated stores to be kept in one backend, if supported.
Database string `json:"database,omitempty" yaml:"database,omitempty"`
// Table is analogous to a table in database backends or a key prefix in KV backends
Table string `json:"table,omitempty" yaml:"table,omitempty"`
}
Config are the Client options.
type ConfigType ¶
type ConfigType interface {
// contains filtered or unexported methods
}
ConfigType is used in the functional options as type to identify a registry option. It is used over a static *Config type as this way plugins can also easilty set functional options without the complication of contexts, as was done in v4. This is possible because plugins will nest the registry.Config type, and thus inherit the interface that is used to identify the registry config.
type DeleteOption ¶
type DeleteOption func(d *DeleteOptions)
DeleteOption sets values in DeleteOptions.
func DeleteFrom ¶
func DeleteFrom(database, table string) DeleteOption
DeleteFrom the database and table.
type DeleteOptions ¶
type DeleteOptions struct {
Database, Table string
}
DeleteOptions configures an individual Delete operation.
func NewDeleteOptions ¶
func NewDeleteOptions(opts ...DeleteOption) DeleteOptions
NewDeleteOptions creates a new DeleteOptions with the provided options applied.
type Deprecated ¶
type Deprecated interface {
// Read takes a single key and optional ReadOptions. It returns matching []*Record or an error.
// If no Record is found, ErrNotFound is returned.
// Deprecated: use Get instead.
Read(key string, opts ...ReadOption) ([]*Record, error)
// Write takes a single key and value, and optional WriteOptions.
// Deprecated: use Set instead.
Write(r *Record, opts ...WriteOption) error
// Delete purges the record with the corresponding key from the store.
// Deprecated: use Purge instead.
Delete(key string, opts ...DeleteOption) error
// List returns any keys that match, or an empty list with no error if none matched.
// Deprecated: use Keys instead.
List(opts ...ListOption) ([]string, error)
}
Deprecated is an interface for deprecated methods in KVStore. This is used to provide backwards compatibility for old code, it will be removed on go-orb v1.0.0.
type GetOption ¶
type GetOption func(g *GetOptions)
GetOption sets values in GetOptions.
func GetOffset ¶
GetOffset starts returning responses from o. Use in conjunction with Limit for pagination.
type GetOptions ¶
type GetOptions struct {
// Prefix returns all records that are prefixed with key
Prefix bool
// Suffix returns all records that have the suffix key
Suffix bool
// Limit limits the number of returned records
Limit uint
// Offset when combined with Limit supports pagination
Offset uint
}
GetOptions configures an individual Get operation.
func NewGetOptions ¶
func NewGetOptions(opts ...GetOption) GetOptions
NewGetOptions creates a new GetOptions with the provided options applied.
type KVStore ¶
type KVStore interface {
types.Component
Deprecated
// Get takes a key, database, table and optional GetOptions. It returns the Record or an error.
// Leave database and/or table empty to use the defaults.
Get(key, database, table string, opts ...GetOption) ([]Record, error)
// Set takes a key, database, table and data, and optional SetOptions.
// Leave database and/or table empty to use the defaults.
Set(key, database, table string, data []byte, opts ...SetOption) error
// Purge takes a key, database and table and purges it.
// Leave database and/or table empty to use the defaults.
Purge(key, database, table string) error
// Keys returns any keys that match, or an empty list with no error if none matched.
// Leave database and/or table empty to use the defaults.
Keys(database, table string, opts ...KeysOption) ([]string, error)
// DropTable drops the table.
// Leave database and/or table empty to use the defaults.
DropTable(database, table string) error
// DropDatabase drops the database.
// Leave database empty to use the default.
DropDatabase(database string) error
}
KVStore is an interface for distributed key-value data storage.
type KeysOption ¶
type KeysOption func(k *KeysOptions)
KeysOption sets values in KeysOptions.
func KeysOffset ¶
func KeysOffset(o uint) KeysOption
KeysOffset starts returning responses from o. Use in conjunction with Limit for pagination.
func KeysPrefix ¶
func KeysPrefix(p string) KeysOption
KeysPrefix returns all keys that are prefixed with key.
func KeysSuffix ¶
func KeysSuffix(s string) KeysOption
KeysSuffix returns all keys that have the suffix key.
type KeysOptions ¶
type KeysOptions struct {
// Prefix returns all keys that are prefixed with key
Prefix string
// Suffix returns all keys that end with key
Suffix string
// Limit limits the number of returned keys
Limit uint
// Offset when combined with Limit supports pagination
Offset uint
}
KeysOptions configures an individual Keys/List operation.
func NewKeysOptions ¶
func NewKeysOptions(opts ...KeysOption) KeysOptions
NewKeysOptions creates a new KeysOptions with the provided options applied.
type ListOption ¶
type ListOption func(l *ListOptions)
ListOption sets values in ListOptions.
func ListLimit ¶
func ListLimit(l uint) ListOption
ListLimit limits the number of returned keys to l.
func ListOffset ¶
func ListOffset(o uint) ListOption
ListOffset starts returning responses from o. Use in conjunction with Limit for pagination.
func ListPrefix ¶
func ListPrefix(p string) ListOption
ListPrefix returns all keys that are prefixed with key.
func ListSuffix ¶
func ListSuffix(s string) ListOption
ListSuffix returns all keys that end with key.
type ListOptions ¶
type ListOptions struct {
// List from the following
Database, Table string
// Prefix returns all keys that are prefixed with key
Prefix string
// Suffix returns all keys that end with key
Suffix string
// Limit limits the number of returned keys
Limit uint
// Offset when combined with Limit supports pagination
Offset uint
}
ListOptions configures an individual List operation.
func NewListOptions ¶
func NewListOptions(opts ...ListOption) ListOptions
NewListOptions creates a new ListOptions with the provided options applied.
type Option ¶
type Option func(ConfigType)
Option is a functional option type for the registry.
func WithDatabase ¶
WithDatabase allows multiple isolated stores to be kept in one backend, if supported.
type ProviderFunc ¶
type ProviderFunc func( configData map[string]any, logger log.Logger, opts ...Option, ) (Type, error)
ProviderFunc is provider function type used by plugins to create a new client.
type ReadOption ¶
type ReadOption func(r *ReadOptions)
ReadOption sets values in ReadOptions.
func ReadOffset ¶
func ReadOffset(o uint) ReadOption
ReadOffset starts returning responses from o. Use in conjunction with Limit for pagination.
func ReadPrefix ¶
func ReadPrefix(p string) ReadOption
ReadPrefix returns all records that are prefixed with key.
func ReadSuffix ¶
func ReadSuffix(s string) ReadOption
ReadSuffix returns all records that have the suffix key.
type ReadOptions ¶
type ReadOptions struct {
Database, Table string
// Prefix returns all keys that are prefixed with key
Prefix string
// Suffix returns all keys that end with key
Suffix string
// Limit limits the number of returned keys
Limit uint
// Offset when combined with Limit supports pagination
Offset uint
}
ReadOptions configures an individual Read operation.
func NewReadOptions ¶
func NewReadOptions(opts ...ReadOption) ReadOptions
NewReadOptions creates a new ReadOptions with the provided options applied.
type Record ¶
type Record struct {
// The key to store the record.
Key string `json:"key"`
// The value within the record.
Value []byte `json:"value"`
// Time when the record expires a nil value means no expiry.
Expiry *time.Time `json:"expiry,omitempty"`
}
Record is an item stored or retrieved from a Store.
type SetOptions ¶
type SetOptions struct {
// Expiry is the time the record expires
Expiry time.Time
// TTL is the time until the record expires
TTL time.Duration
}
SetOptions configures an individual Set operation If Expiry and TTL are set TTL takes precedence.
func NewSetOptions ¶
func NewSetOptions(opts ...SetOption) SetOptions
NewSetOptions creates a new SetOptions with the provided options applied.
type Type ¶
type Type struct {
KVStore
}
Type is the kvstore type it is returned when you use Provide which selects a kvstore to use based on the plugin configuration.
func Provide ¶
func Provide( svcCtx *cli.ServiceContextWithConfig, components *types.Components, logger log.Logger, opts ...Option, ) (Type, error)
Provide provides a new KVStore.
func ProvideNoOpts ¶
func ProvideNoOpts( svcCtx *cli.ServiceContextWithConfig, components *types.Components, logger log.Logger, ) (Type, error)
ProvideNoOpts provides a new KVStore without options.
type WatchEvent ¶
type WatchEvent struct {
Record
// Operation is the type of operation that occurred.
Operation WatchOp
}
WatchEvent is a change to a key-value store.
type WatchOp ¶
type WatchOp uint8
WatchOp represents the type of Watch operation (Update, Delete). It is a part of WatchUpdate.
type WatchOption ¶
type WatchOption func(o *WatchOptions)
WatchOption sets values in WatchOptions.
func WatchIgnoreDeletes ¶
func WatchIgnoreDeletes() WatchOption
WatchIgnoreDeletes do not send delete markers to the update channel.
func WatchIncludeHistory ¶
func WatchIncludeHistory() WatchOption
WatchIncludeHistory include all history per subject, not just last one.
func WatchMetaOnly ¶
func WatchMetaOnly() WatchOption
WatchMetaOnly retrieve only the meta data of the entry.
func WatchUpdatesOnly ¶
func WatchUpdatesOnly() WatchOption
WatchUpdatesOnly include only updates for keys.
type WatchOptions ¶
type WatchOptions struct {
// Do not send delete markers to the update channel.
IgnoreDeletes bool
// Include all history per subject, not just last one.
IncludeHistory bool
// Include only updates for keys.
UpdatesOnly bool
// retrieve only the meta data of the entry
MetaOnly bool
}
WatchOptions configures an individual Watch operation.
func NewWatchOptions ¶
func NewWatchOptions(opts ...WatchOption) WatchOptions
NewWatchOptions creates a new WatchOptions with the provided options applied.
type Watcher ¶
type Watcher interface {
// Watch starts a watcher for the given database and table.
// Returns a channel of WatchUpdate and a function to stop the watcher.
// If an error occurs, it is returned.
Watch(ctx context.Context, database, table string, opts ...WatchOption) (<-chan WatchEvent, func() error, error)
}
A Watcher is a component that can watch for changes to a key-value store. For example a registry component can watch for changes to a database.
type WriteOption ¶
type WriteOption func(w *WriteOptions)
WriteOption sets values in WriteOptions.
func WriteExpiry ¶
func WriteExpiry(t time.Time) WriteOption
WriteExpiry is the time the record expires.
type WriteOptions ¶
type WriteOptions struct {
// Expiry is the time the record expires
Expiry time.Time
Database, Table string
// TTL is the time until the record expires
TTL time.Duration
}
WriteOptions configures an individual Write operation If Expiry and TTL are set TTL takes precedence.
func NewWriteOptions ¶
func NewWriteOptions(opts ...WriteOption) WriteOptions
NewWriteOptions creates a new WriteOptions with the provided options applied.