kv

package
v0.0.0-...-f9b9731 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const NoLeaseId = 0

Variables

View Source
var (
	ErrAlreadyExists = fmt.Errorf("The key already exists")
	ErrNotFound      = fmt.Errorf("The value is not found")
	ErrWrongVersion  = fmt.Errorf("Wrong version is provided")
	ErrWrongLeaseId  = fmt.Errorf("Wrong leaseId")
)

Functions

This section is empty.

Types

type Key

type Key string

Key is the type for storing keys

type Lease

type Lease interface {

	// Id returns the lease Id
	Id() LeaseId

	// TTL is time to the lease expiration
	TTL() time.Duration

	// Release terminates (releases) the lease immediately
	Release() error
}

Lease allows to control a lease. Lease is an abstraction which identifies a time interval where the lease is valid - ttl. Storage records could be associated with the lease by its Id.

type LeaseId

type LeaseId int64

LeaseId is a record lease identifier. Every record in the storage can have a lease id or has NoLeaseId. Records that have a lease will be automatically deleted if the lease is expired.

type Lessor

type Lessor interface {

	// NewLease creates a new lease with ttl provided. The keepAlive flag
	// indicates that the the lease must be kept alive until the current process
	// is done.
	NewLease(ctx context.Context, ttl time.Duration, keepAlive bool) (Lease, error)

	// GetLease returns a Lease object by it's Id
	GetLease(lid LeaseId) (Lease, error)
}

Lessor is an interface which allows to manage leases in conjuction with the Storage

type Record

type Record struct {
	// Key is a key for the record
	Key Key
	// Value is a value for the record
	Value Value

	// A version that identifies the record. It is managed by the Storage and
	// it is ignored in Create and update operations
	Version Version

	// Lease contains the record leaseId. If the lease is not empty
	// (Lease != NoLeaseId), the record availability is defined by the
	// lease TTL. As soon as the lease becomes invalid, the record is
	// Removed from the storage permanently
	//
	// Records with NoLeaseId should be deleted explicitly. The value
	// can be set only when the record is created and cannot be changed
	// (it will be ignored) during updates.
	Lease LeaseId
}

A record that can be stored in a KV

func (Record) Copy

func (r Record) Copy() Record

Copy returns copy of the record r

type Records

type Records []Record

func (Records) Len

func (rcs Records) Len() int

Len is part of sort.Interface.

func (Records) Less

func (rcs Records) Less(i, j int) bool

Less is part of sort.Interface.

func (Records) Swap

func (rcs Records) Swap(i, j int)

Swap is part of sort.Interface.

type Storage

type Storage interface {
	// Lessor returns Lessor implementation for the storage
	Lessor() Lessor

	// Create adds a new record into the storage. It returns existing record with
	// ErrAlreadyExists error if it already exists in the storage.
	// Create returns version of the new record with error=nil
	Create(ctx context.Context, record Record) (Version, error)

	// Get retrieves the record by its key. It will return nil and an error,
	// which will indicate the reason why the operation was not succesful.
	// ErrNotFound is returned if the key is not found in the storage
	Get(ctx context.Context, key Key) (Record, error)

	// GetRange returns the list of records for the range of keys (inclusively)
	// if there is no keys found, it returns empty slice with no error.
	GetRange(ctx context.Context, startKey, endKey Key) (Records, error)

	// CasByVersion compares-and-sets the record Value if the record stored
	// version is same as in the provided record. The record version will be updated
	// too and returned in the result.
	//
	// the record lease WILL NOT BE CHANGED, so the record will be associated
	// with the lease, that it has been done before.
	//
	// an error will contain the reason if the operation was not successful, or
	// the new version will be returned otherwise
	//   ErrWrongVersion - indicates that the version is changed
	//   ErrNotFound - indicates that the record does not exist
	CasByVersion(ctx context.Context, record Record) (Version, error)

	// Delete removes the record from the storage by its key. It returns
	// an error if the operation was not successful.
	Delete(ctx context.Context, key Key) error

	// WaitForVersionChange will wait for the record version change. The
	// version param contans an expected record version. The call returns
	// immediately if the record is not found together with ErrNotFound, or
	// if the record version is different than expected (no error this case
	// is returned).
	//
	// the call will block the current go-routine until one of the following
	// things happens:
	// - context is closed
	// - the record version is changed
	// - the record is deleted
	//
	// If the record is deleted during the call (nil, nil) is returned
	WaitForVersionChange(ctx context.Context, key Key, version Version) (Record, error)
}

Storage interface defines some operations over the record storage. The record storage allows to keep key-value pairs, and supports a set of operations that allow to implement some distributed (if supported) primitives

type Value

type Value []byte

Value type is for storing Values

type Version

type Version int64

A record version identifier

Directories

Path Synopsis
Package inmem contains in-memory implementation of kv.Storage and other objects to work with key-value storage
Package inmem contains in-memory implementation of kv.Storage and other objects to work with key-value storage

Jump to

Keyboard shortcuts

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