kvtx

package
v0.57.2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDiscarded is returned if the transaction was already discarded or committed.
	ErrDiscarded = tx.ErrDiscarded
	// ErrNotWrite is returned if Commit is called on a non-write transaction.
	ErrNotWrite = tx.ErrNotWrite
	// ErrEmptyKey is returned if the key was empty.
	ErrEmptyKey = errors.New("key cannot be empty")
	// ErrBlockTxOpsUnimplemented is returned if the interface does not support BlockTxOps.
	ErrBlockTxOpsUnimplemented = errors.New("kvtx store does not implement block tx operations")
	// ErrKvtxSizeUnimplemented is returned if the store does not support Size.
	ErrKvtxSizeUnimplemented = errors.New("kvtx store does not support size lookup")
	// ErrNotFound is returned if the key was not found.
	ErrNotFound = errors.New("key was not found")
	// ErrInvalidSnapshot is returned when a transaction's storage snapshot can
	// no longer be trusted and the caller must reopen at a fresh generation.
	ErrInvalidSnapshot = errors.New("kvtx snapshot is invalid")
	// ErrWatchUnsupported is returned when a store cannot stream committed changes.
	ErrWatchUnsupported = errors.New("kvtx store does not support watch")
)

Functions

func GetBatch added in v0.53.0

func GetBatch(ctx context.Context, ops TxOps, keys [][]byte) ([][]byte, []bool, error)

GetBatch reads multiple keys with BatchTxOps.GetBatch when ops implements it and with GetBatchFallback when it does not.

func GetBatchFallback added in v0.53.0

func GetBatchFallback(ctx context.Context, ops TxOps, keys [][]byte) ([][]byte, []bool, error)

GetBatchFallback reads keys with the scalar transaction operation.

func MustGet

func MustGet(ctx context.Context, o TxOps, key []byte) ([]byte, error)

MustGet performs Get against a kvtx store and returns ErrNotFound if not found.

func PrefixSuccessor added in v0.57.0

func PrefixSuccessor(prefix []byte) ([]byte, bool)

PrefixSuccessor returns the smallest key above the contiguous prefix range. The boolean is false when prefix is empty or has no finite successor.

func RetryInvalidSnapshot added in v0.57.0

func RetryInvalidSnapshot(err error) bool

RetryInvalidSnapshot reports whether err requires a fresh storage snapshot.

func RunOperation added in v0.57.2

func RunOperation(ctx context.Context, body func(context.Context) error) error

RunOperation executes a complete logical operation again when its storage snapshot becomes invalid. The body must be safe to replay.

func RunOperationWithRetry added in v0.57.2

func RunOperationWithRetry(
	ctx context.Context,
	body func(context.Context) error,
	retry RetryPredicate,
) error

RunOperationWithRetry executes a complete logical operation with the supplied typed retry policy. The body must not perform non-idempotent external effects. Per-attempt mutable state must be created or reset inside the body; immutable inputs may be captured from outside it.

func RunTransaction added in v0.57.0

func RunTransaction[T TransactionLifecycle](
	ctx context.Context,
	write bool,
	open func(context.Context) (T, error),
	body func(context.Context, T) error,
) error

RunTransaction executes body against a fresh transaction for each retryable failure and retries only ErrInvalidSnapshot. A successful read body is discarded without committing; a successful write body is committed before the attempt is discarded.

The body must be safe to replay. It may use the transaction and perform deterministic local computation, but it must not perform non-idempotent external effects. Per-attempt mutable state must be reset inside the body.

func RunTransactionWithRetry added in v0.57.0

func RunTransactionWithRetry[T TransactionLifecycle](
	ctx context.Context,
	write bool,
	open func(context.Context) (T, error),
	body func(context.Context, T) error,
	retry RetryPredicate,
) error

RunTransactionWithRetry executes body with the supplied typed retry policy. Every opened attempt is discarded, including attempts whose body or commit fails. The next attempt opens only after the failed attempt has returned.

Types

type BatchTxOps added in v0.53.0

type BatchTxOps interface {
	// GetBatch returns values and found flags aligned with keys.
	GetBatch(ctx context.Context, keys [][]byte) (values [][]byte, found []bool, err error)
}

BatchTxOps reads multiple keys in one transaction operation.

type BlockIterator

type BlockIterator interface {
	// Iterator is the kvtx iterator interface.
	Iterator
	// ValueCursor returns a cursor located at the "value" sub-block.
	// Returns nil if the iterator is not at a valid location.
	ValueCursor() *block.Cursor
}

BlockIterator is a kvtx iterator backed by a block graph.

type BlockTx

type BlockTx interface {
	// TxOps contains the transaction operations.
	TxOps
	// BlockTxOps contains the block graph transaction operations.
	BlockTxOps

	// Tx contains the transaction confirm.
	tx.Tx
}

BlockTx is a database transaction backed by a block graph. Concurrent calls are not safe on a single transaction.

func CastBlockTx

func CastBlockTx(tx Tx) (BlockTx, error)

CastBlockTx casts a Tx to a BlockTx or returns ErrBlockTxOpsUnimplemented.

type BlockTxOps

type BlockTxOps interface {
	// GetCursor returns the block cursor at the root of the tree.
	GetCursor() *block.Cursor
	// GetCursorAtKey returns the cursor referenced by the key.
	//
	// Returns nil, nil if not found.
	GetCursorAtKey(ctx context.Context, key []byte) (*block.Cursor, error)
	// SetCursorAtKey sets the key to a reference to the object at bcs.
	// if isBlob is set, the object must be a *blob.Blob (for reading with Get).
	// if bcs == nil, the key is set with a empty block ref.
	// bcs must not point to a sub-block.
	SetCursorAtKey(ctx context.Context, key []byte, bcs *block.Cursor, isBlob bool) error
	// DeleteCursorAtKey deletes the key and returns the cursor to the value.
	// returns nil, nil if not found.
	DeleteCursorAtKey(ctx context.Context, key []byte) (*block.Cursor, error)
	// BlockIterate returns the block iterator.
	BlockIterate(ctx context.Context, prefix []byte, sort, reverse bool) BlockIterator
}

BlockTxOps contains extra tx ops for a block-backed store.

func CastBlockTxOps

func CastBlockTxOps(ops TxOps) (BlockTxOps, error)

CastBlockTxOps casts a TxOps to a BlockTxOps or returns ErrBlockTxOpsUnimplemented.

type CoordinationRefreshStore added in v0.52.0

type CoordinationRefreshStore interface {
	RefreshForCoordinationLock() error
}

CoordinationRefreshStore refreshes a store at an external coordination boundary before callers open transactions derived from that boundary.

type Iterator

type Iterator interface {
	// Err returns any error that has closed the iterator.
	// May return context.Canceled or ErrDiscarded if closed.
	Err() error
	// Valid returns if the iterator points to a valid entry.
	//
	// If err is set, returns false.
	Valid() bool
	// Key returns the current entry key, or nil if not valid.
	//
	// NOTE: even if prefix is set this does not trim the prefix.
	Key() []byte
	// Value returns the current entry value, or nil if not valid.
	//
	// May cache the value between calls, copy if modifying.
	Value() ([]byte, error)
	// ValueCopy copies the value to the given byte slice and returns it.
	// If the slice is not big enough (cap), it must create a new one and return it.
	// May use the value cached from Value() call as the source of the data.
	// May return nil if !Valid().
	ValueCopy([]byte) ([]byte, error)
	// Next advances to the next entry and returns Valid.
	Next() bool
	// Seek moves the iterator to the first key >= the provided key (or <= in reverse mode).
	// Pass nil to seek to the beginning (or end if reversed).
	// It is not necessary to call Next() after seek.
	// If prefix is set, k should have the prefix or be nil. prefix is not prepended automatically.
	// Seek has three possible failure modes:
	//  - return an error without modifying the iterator
	//  - set the iterator Err to the error and return nil
	//  - set the iterator Err to the error and return the error
	Seek(k []byte) error
	// Close closes the iterator.
	// Note: it is not necessary to close all iterators before Discard().
	Close()
}

Iterator iterates over a kvtx Tx store with a given prefix. Note: Next() or Seek() must be called before iterator is valid.

func NewErrIterator

func NewErrIterator(err error) Iterator

NewErrIterator returns an iterator that starts with an error.

type RetryPredicate added in v0.57.0

type RetryPredicate func(error) bool

RetryPredicate reports whether an error permits a fresh transaction attempt. Predicates must classify typed errors with errors.Is; diagnostic text is not part of the transaction retry contract.

type Store

type Store interface {
	// NewTransaction returns a new transaction against the store.
	// Always call Discard() after you are done with the transaction.
	// The transaction will be read-only unless write is set.
	NewTransaction(ctx context.Context, write bool) (Tx, error)
}

Store is a transactional key/value store.

type TransactionLifecycle added in v0.57.0

type TransactionLifecycle interface {
	Commit(ctx context.Context) error
	Discard()
}

TransactionLifecycle is the lifecycle a retryable transaction must expose.

type Tx

type Tx interface {
	// TxOps contains the transaction operations.
	TxOps

	// Tx contains the transaction confirm.
	tx.Tx
}

Tx is a database transaction. Concurrent calls are not safe on a single transaction.

type TxOps

type TxOps interface {
	// Size returns the number of keys in the store.
	Size(ctx context.Context) (uint64, error)
	// Get returns values for a key.
	Get(ctx context.Context, key []byte) (data []byte, found bool, err error)
	// Exists checks if a key exists.
	Exists(ctx context.Context, key []byte) (bool, error)
	// Set sets the value of a key.
	// This will not be committed until Commit is called.
	Set(ctx context.Context, key, value []byte) error
	// Delete deletes a key.
	// This will not be committed until Commit is called.
	// Not found should not return an error.
	Delete(ctx context.Context, key []byte) error
	// ScanPrefix iterates over keys with a prefix.
	//
	// Note: neither key nor value should be retained outside cb() without
	// copying.
	//
	// Note: the ordering of the scan is not necessarily sorted.
	ScanPrefix(ctx context.Context, prefix []byte, cb func(key, value []byte) error) error
	// ScanPrefixKeys iterates over keys only with a prefix.
	ScanPrefixKeys(ctx context.Context, prefix []byte, cb func(key []byte) error) error
	// Iterate returns an iterator with a given key prefix.
	//
	// Should always return non-nil, with error field filled if necessary.
	// If sort, iterates in sorted order, reverse reverses the key iteration.
	// The prefix is NOT clipped from the output keys.
	// If !sort, reverse MAY have no effect.
	// Must call Next() or Seek() before valid.
	// Some implementations return BlockIterator.
	// Context is used for the iterator and internally for iterator operations.
	// Return an ErrorIterator if anything goes wrong building the iterator.
	Iterate(ctx context.Context, prefix []byte, sort, reverse bool) Iterator
}

TxOps contains the database transaction operations.

type TxStore

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

TxStore implements the Store interface backed by a single Tx instance. This allows many transactions to be batched into one Tx.

Discard of a write tx after changes were made returns an error. Discard followed by Commit returns ErrDiscarded Commit followed by Discard returns nil. If tx committed or discarded, operations return ErrDiscarded. It's not possible to roll-back changes as we are proxying to 1 txops object.

func NewTxStore

func NewTxStore(ops TxOps) *TxStore

NewTxStore constructs a new tx store.

func (*TxStore) NewTransaction

func (t *TxStore) NewTransaction(ctx context.Context, write bool) (Tx, error)

NewTransaction returns a new transaction against the store. Indicate write if the transaction will not be read-only. Always call Discard() after you are done with the transaction.

type TxStoreTx

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

TxStoreTx implements the Tx interface backed by a TxOps See TxStore.

func NewTxStoreTx

func NewTxStoreTx(ops TxOps) (*TxStoreTx, error)

NewTxStoreTx constructs a new TxStoreTx from a TxOps

func (*TxStoreTx) Commit

func (t *TxStoreTx) Commit(ctx context.Context) error

Commit commits the transaction to storage. Can return an error to indicate tx failure. TxStore: does nothing internally TxStore: if called after Discard, returns ErrDiscarded TxStore: all ops will return ErrDiscarded if called after Commit or Discard

func (*TxStoreTx) Delete

func (t *TxStoreTx) Delete(ctx context.Context, key []byte) error

Delete deletes a key. This will not be committed until Commit is called. Not found should not return an error.

func (*TxStoreTx) Discard

func (t *TxStoreTx) Discard()

Discard cancels the transaction. If called after Commit, does nothing. Cannot return an error. Can be called unlimited times.

func (*TxStoreTx) Exists

func (t *TxStoreTx) Exists(ctx context.Context, key []byte) (bool, error)

Exists checks if a key exists.

func (*TxStoreTx) Get

func (t *TxStoreTx) Get(ctx context.Context, key []byte) (data []byte, found bool, err error)

Get returns values for a key.

func (*TxStoreTx) GetBatch added in v0.53.0

func (t *TxStoreTx) GetBatch(ctx context.Context, keys [][]byte) ([][]byte, []bool, error)

GetBatch returns values for multiple keys.

func (*TxStoreTx) GetTxOps

func (t *TxStoreTx) GetTxOps() TxOps

GetTxOps returns the transaction ops object.

func (*TxStoreTx) Iterate

func (t *TxStoreTx) Iterate(ctx context.Context, prefix []byte, sort, reverse bool) Iterator

Iterate returns an iterator with a given key prefix.

Should always return non-nil, with error field filled if necessary. If sort, iterates in sorted order, reverse reverses the key iteration. The prefix is NOT clipped from the output keys. If !sort, reverse MAY have no effect. Must call Next() or Seek() before valid. Some implementations return BlockIterator.

func (*TxStoreTx) ScanPrefix

func (t *TxStoreTx) ScanPrefix(ctx context.Context, prefix []byte, cb func(key, value []byte) error) error

ScanPrefix iterates over keys with a prefix.

Note: neither key nor value should be retained outside cb() without copying.

Note: the ordering of the scan is not necessarily sorted.

func (*TxStoreTx) ScanPrefixKeys

func (t *TxStoreTx) ScanPrefixKeys(ctx context.Context, prefix []byte, cb func(key []byte) error) error

ScanPrefixKeys iterates over keys only with a prefix.

func (*TxStoreTx) Set

func (t *TxStoreTx) Set(ctx context.Context, key, value []byte) error

Set sets the value of a key. This will not be committed until Commit is called.

func (*TxStoreTx) Size

func (t *TxStoreTx) Size(ctx context.Context) (uint64, error)

Size returns the number of keys in the store.

type WatchEntry added in v0.54.0

type WatchEntry struct {
	// Key is the entry key.
	Key []byte
	// Value is the entry value.
	Value []byte
}

WatchEntry is one key/value pair in a watched store snapshot.

type WatchStore added in v0.54.0

type WatchStore interface {
	// WatchPrefix calls cb with the current prefix snapshot and each changed snapshot.
	WatchPrefix(ctx context.Context, prefix []byte, cb func(entries []WatchEntry) error) error
}

WatchStore streams key/value snapshots after committed store changes.

Directories

Path Synopsis
iavl
Package iavl implements a iavl tree.
Package iavl implements a iavl tree.
Package hidalgo implements the high-level database abstractions for Go interfaces for kvtx.
Package hidalgo implements the high-level database abstractions for Go interfaces for kvtx.
Package kvtx_kvfile implements a key/value store backed by a file.
Package kvtx_kvfile implements a key/value store backed by a file.
Package kvtx_kvtest contains shared transaction behavior tests and fixtures.
Package kvtx_kvtest contains shared transaction behavior tests and fixtures.
rpc

Jump to

Keyboard shortcuts

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