storage

package
v0.8.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: PostgreSQL Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyExists = errors.New("key already exists")

ErrAlreadyExists indicates that a value could not be created because the key already exists.

View Source
var ErrDuplicateKeysInTransaction = errors.New("duplicate keys in transaction")

ErrDuplicateKeysInTransaction indicates that the transaction contained duplicate keys.

View Source
var ErrNotFound = errors.New("key not found")

ErrNotFound indicates that no values were found for the given key.

View Source
var ErrOperationConstraintViolated = errors.New("operation constraint violated")

ErrOperationConstraintViolated indicates that one of the constraints on the operation, such as 'version = 0', was violated.

View Source
var ErrValueVersionMismatch = errors.New("value version mismatch")

ErrValueVersionMismatch indicates that the operation failed because the stored value's version didn't match the given value's version.

View Source
var ErrWatchAlreadyInProgress = errors.New("watch already in progress")

ErrWatchAlreadyInProgress indicates that the WatchOp has already been started and cannot be started again until it's closed.

Functions

func DecodeGetResponse

func DecodeGetResponse[V Value](resp *clientv3.GetResponse) ([]V, error)

DecodeGetResponse is a helper function to extract typed values from a clientv3.GetResponse

func Key

func Key(elem ...string) string

Key returns a slash-separated key.

func Prefix

func Prefix(elem ...string) string

Prefix returns a slash-separated key prefix with a trailing slash to ensure that its safe for use in all storage operations.

Types

type Cache added in v0.8.0

type Cache[V Value] interface {
	// Start starts the cache. This must be called before the cache can be used.
	Start(ctx context.Context) error
	// Put returns an operation that puts a key-value pair into storage.
	Put(item V, options ...clientv3.OpOption) PutOp[V]
	// Create returns an operation that creates a key value pair with an
	// optional time-to-live. This operation will fail with ErrAlreadyExists if
	// the given key already exists.
	Create(item V, options ...clientv3.OpOption) PutOp[V]
	// Update returns an operation updates an existing key value pair with a new
	// value and an optional time-to-live. This operation will fail with
	// ErrValueVersionMismatch if the stored value's version does not match the
	// given value's version. Note that this operation is equivalent to a create
	// when the item version is 0.
	Update(item V, options ...clientv3.OpOption) PutOp[V]
	// DeleteByKey returns an operation that deletes a single value by key.
	DeleteByKey(key string, options ...clientv3.OpOption) DeleteOp
	// DeleteValue returns an operation that deletes a single value if its
	// version matches the given value's version. Its Exec method will return an
	// ErrValueVersionMismatch if the stored value version did not match the
	// given value version.
	DeleteValue(item V, options ...clientv3.OpOption) DeleteValueOp[V]
	// DeletePrefix returns an operation that deletes a multiple values by
	// prefix.
	DeletePrefix(prefix string, options ...clientv3.OpOption) DeleteOp
	// Get returns an operation that returns a single value by key.
	Get(key string) GetOp[V]
	// GetPrefix returns an operation that returns multiple values by prefix.
	GetPrefix(prefix string) GetMultipleOp[V]
	// Stop stops the cache.
	Stop()
	// Error reports errors that originate from the cache's watch.
	Error() <-chan error
	// PropagateErrors will propagate errors from the cache's Error() channel to
	// the given error channel in a goroutine until the given context is
	// complete.
	PropagateErrors(ctx context.Context, ch chan error)
}

Cache is a write-through cache that uses Watch operations to stay updated. It is most suitable for making range operations on small numbers of small values more efficient. Note that since reads are executed from the in-memory cache, these caches are not suitable for complex Get operations that require OpOptions.

func NewCache added in v0.8.0

func NewCache[V Value](client *clientv3.Client, prefix string, key func(v V) string) Cache[V]

type CachedTxnOp added in v0.8.0

type CachedTxnOp interface {
	UpdateCache()
}

type DeleteOp

type DeleteOp interface {
	TxnOperation
	Exec(ctx context.Context) (int64, error)
	// The revision returned by the server. Only populated after Exec() is
	// called.
	Revision() int64
}

DeleteOp is an operation that deletes one or more values from storage, and returns the number of values deleted.

func NewDeleteKeyOp

func NewDeleteKeyOp(client *clientv3.Client, key string, options ...clientv3.OpOption) DeleteOp

NewDeleteKeyOp returns an operation that deletes a single value by key.

func NewDeletePrefixOp

func NewDeletePrefixOp(client *clientv3.Client, prefix string, options ...clientv3.OpOption) DeleteOp

NewDeletePrefixOp returns an operation that deletes a multiple values by prefix.

type DeleteValueOp

type DeleteValueOp[V Value] interface {
	TxnOperation
	Exec(ctx context.Context) error
	// The revision returned by the server. Only populated after Exec is called.
	Revision() int64
}

DeleteValueOp is a delete operation that deletes a single value from storage and enforces value version constraints. Implementations should return an ErrValueVersionMismatch if the constraint fails.

func NewDeleteValueOp

func NewDeleteValueOp[V Value](client *clientv3.Client, key string, val V, options ...clientv3.OpOption) DeleteValueOp[V]

NewDeleteValueOp deletes a single value if its version matches the given value's version. Its Exec method will return an ErrValueVersionMismatch if the stored value version did not match the given value version.

type Event

type Event[V Value] struct {
	// Err will be non-nil when Type is "error".
	Err      error
	Type     EventType
	Key      string
	Value    V
	IsCreate bool
	IsModify bool
	Revision int64
}

Event is generated by a modification to the watched key.

type EventType

type EventType string
const (
	EventTypePut     EventType = "put"
	EventTypeDelete  EventType = "delete"
	EventTypeError   EventType = "error"
	EventTypeUnknown EventType = "unknown"
)

type ExistsOp

type ExistsOp interface {
	Exec(ctx context.Context) (bool, error)
}

ExistsOp is an operation that returns true if a the given key(s) exist.

func NewExistsOp

func NewExistsOp(client *clientv3.Client, key string) ExistsOp

NewExistsOp returns an operation that returns true if a key exists.

type GetMultipleOp

type GetMultipleOp[V Value] interface {
	Exec(ctx context.Context) ([]V, error)
}

GetMultipleOp is an operation that returns multiple values.

func NewGetMultipleOp

func NewGetMultipleOp[V Value](client *clientv3.Client, keys []string, options ...clientv3.OpOption) GetMultipleOp[V]

NewGetMultipleOp returns an operation that returns multiple values by key.

func NewGetPrefixOp

func NewGetPrefixOp[V Value](client *clientv3.Client, prefix string, options ...clientv3.OpOption) GetMultipleOp[V]

NewGetPrefixOp returns an operation that returns multiple values by prefix.

func NewGetRangeOp

func NewGetRangeOp[V Value](client *clientv3.Client, start, end string, options ...clientv3.OpOption) GetMultipleOp[V]

NewGetRangeOp returns an operation that returns values in the range [start, end).

type GetOp

type GetOp[V Value] interface {
	Exec(ctx context.Context) (V, error)
}

GetOp is an operation that returns a single value.

func NewGetOp

func NewGetOp[V Value](client *clientv3.Client, key string, options ...clientv3.OpOption) GetOp[V]

NewGetOp returns an operation that returns a single value by key.

type PutOp

type PutOp[V Value] interface {
	TxnOperation
	// WithTTL sets a time-to-live for this value. The value will automatically
	// be removed after the TTL has expired.
	WithTTL(ttl time.Duration) PutOp[V]
	// WithUpdatedVersion will update the version on the value after the put
	// operation completes.
	WithUpdatedVersion() PutOp[V]
	Exec(ctx context.Context) error
	// The revision returned by the server. Only populated after Exec is called.
	Revision() int64

	VersionUpdater
}

PutOp is an operation that puts a key-value pair into storage.

func NewCreateOp

func NewCreateOp[V Value](client *clientv3.Client, key string, val V, options ...clientv3.OpOption) PutOp[V]

NewCreateOp returns an operation that creates a key value pair with an optional time-to-live. This operation will fail with ErrAlreadyExists if the given key already exists.

func NewPutOp

func NewPutOp[V Value](client *clientv3.Client, key string, val V, options ...clientv3.OpOption) PutOp[V]

NewPutOp returns an operation that stores a key value pair with an optional time-to-live. This operation does not enforce any version constraints.

func NewUpdateOp

func NewUpdateOp[V Value](client *clientv3.Client, key string, val V, options ...clientv3.OpOption) PutOp[V]

NewUpdateOp returns an operation updates an existing key value pair with a new value and an optional time-to-live. This operation will fail with ErrValueVersionMismatch if the stored value's version does not match the given value's version. Note that this operation is equivalent to a create when the item version is 0.

type StoredValue

type StoredValue struct {
	// contains filtered or unexported fields
}

StoredValue is an embeddable struct that can be used to implement the Value interface on other structs.

func (*StoredValue) SetVersion

func (v *StoredValue) SetVersion(version int64)

func (*StoredValue) Version

func (v *StoredValue) Version() int64

type Txn

type Txn interface {
	AddOps(ops ...TxnOperation)
	Commit(ctx context.Context) error
}

Txn is a group of operations that will be executed together in a transaction. Similar to transactions in other systems, if any of the operations contains a condition, such as the CreateOp operation, and that condition fails, the entire transaction will fail. Each operation in the transaction must operate on a unique key.

func NewTxn

func NewTxn(client *clientv3.Client, ops ...TxnOperation) Txn

type TxnOperation

type TxnOperation interface {
	ClientOp(ctx context.Context) (clientv3.Op, error)
	Cmps() []clientv3.Cmp
	UpdateRevision(revision int64)
	Revision() int64
}

TxnOperation is a storage operation that can be used in a transaction.

type Value

type Value interface {
	Version() int64
	SetVersion(version int64)
}

Value is the interface that all stored values must adhere to. Values must be JSON-serializable and have a 'version' field that they expose through the methods on this interface. The 'version' field should be omitted from the JSON representation using a `json:"-"` tag.

type VersionUpdater added in v0.8.0

type VersionUpdater interface {
	// UpdateVersionEnabled should return true if this operation should update
	// the item's version.
	UpdateVersionEnabled() bool
	// UpdateVersion should read the previous KVs that it manages from prevKV
	// and update its item's versions.
	UpdateVersion(prevKVs map[string]*mvccpb.KeyValue)
}

VersionUpdater are the methods that an operation can implement to support updating the value version after an update. This exists as a separate interface to make it usable for runtime type checking.

type WatchOp

type WatchOp[V Value] interface {
	// Watch performs an initial Get of current items, calls handle for each,
	// then persistently watches for modifications, calling handle for each
	// event. The initial Get is blocking, and then it starts the watch in a
	// goroutine. Errors originating from the watch or handler are reported via
	// the Error() channel.
	Watch(ctx context.Context, handle func(e *Event[V]) error) error
	// Close cancels the active watch and enables callers to use Watch again.
	Close()
	// Error reports errors that originate from the watch or from the handler.
	Error() <-chan error
	// PropagateErrors will propagate errors from the watch's Error() channel to
	// the given error channel in a goroutine until the given context is
	// complete.
	PropagateErrors(ctx context.Context, ch chan error)
}

WatchOp watches one or more keys for modifications.

func NewWatchOp

func NewWatchOp[V Value](client *clientv3.Client, key string, options ...clientv3.OpOption) WatchOp[V]

func NewWatchPrefixOp

func NewWatchPrefixOp[V Value](client *clientv3.Client, key string, options ...clientv3.OpOption) WatchOp[V]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL