versioned

package
v1.17.0-pre.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidVersion   = errors.New("invalid version")
	ErrStaleTransaction = errors.New("stale transaction")
	ErrStaleVersion     = errors.New("stale version")
	ErrVersionNotFound  = errors.New("version not found")
)
View Source
var LatestTx = &Tx{nextVersion: maxVersion}

LatestTx refers to maxVersion without having a coordinator, should only be used for testing.

Functions

This section is empty.

Types

type Coordinator

type Coordinator struct {
	// Logger supplied to NewCoordinator. Should be set if logging is desired.
	Logger *logrus.Entry

	// Cleaner is called with the earliest version that must be kept.
	// Must be set to clean up resources held for old versions.
	// Cleaner function may be called concurrently, the function must synchronize
	// use of any shared resources.
	Cleaner func(KeepVersion)
	// contains filtered or unexported fields
}

Coordinator defines a common version number space for multiple versioned.Values, and provides facilities for cleaning out stale versions. The Value's are not directly managed by the Coordinator, but all the values under coordination should be cleaned by the 'cleaner' function given to the Coordinator.

func (*Coordinator) GetVersionHandle

func (v *Coordinator) GetVersionHandle() *VersionHandle

GetVersionHandle returns a VersionHandle for the current version, so that it can not be cleaned off before the returned VersionHandle is closed.

func (*Coordinator) PrepareNextVersion

func (v *Coordinator) PrepareNextVersion() *Tx

PrepareNextVersion returns a transaction to be used when adding or removing values.

Callers need to coordinate so that a single goroutine is performing modifications at any one time, consisting of the following operations:

- tx := coordinator.PrepareNextVersion()

  • value.SetAt(... , tx)
  • value.RemoveAt(..., tx)
  • ...

- tx.Commit()

type KeepVersion

type KeepVersion version

KeepVersion is an exported version type used when releasing memory held for old versions.

type Tx

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

All values in Tx are constants

func (*Tx) After

func (tx *Tx) After(v KeepVersion) bool

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit makes a new version of values available to readers Commit call may be omitted if no changes were actually made.

func (*Tx) GetVersionHandle

func (tx *Tx) GetVersionHandle() *VersionHandle

GetVersionHandle returns a VersionHandle for the given transaction.

func (*Tx) String

func (tx *Tx) String() string

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value is a container for versioned values, implemented as a lock-free linked list.

func (*Value[T]) At

func (v *Value[T]) At(handle *VersionHandle) T

At returns value of type 'T' valid for the given version, or an empty value if none is found.

func (*Value[T]) RemoveAt

func (v *Value[T]) RemoveAt(tx *Tx) error

RemoveAt changes the validity of the stored value previously valid at version 'next' to have ended at version 'next'. 'next' must be later than any the current version visible to the readers. Returns an error if this is not the case. Callers must coordinate for mutual exclusion.

func (*Value[T]) RemoveBefore

func (v *Value[T]) RemoveBefore(keepVersion KeepVersion)

RemoveBefore removes all values whose validity ends before 'keepVersion'. Caller must coordinate for mutual exclusion.

func (*Value[T]) SetAt

func (v *Value[T]) SetAt(value T, tx *Tx) error

SetAt adds the value with validity starting from 'version'. All values are added with "infinite" validity, which is then truncated when an overlapping entry is added, or the value is removed. 'next' version must be later than any the current version visible to the readers. Returns an error if this is not the case. Callers must coordinate for mutual exclusion.

type VersionHandle

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

VersionHandle is used to keep values valid for a specific version from being released, so that they are available for use for as long as the VersionHandle is not closed.

A special form with a nil coordinator, which is returned by Latest(), always finds the latest versions and does not keep any versions from being released.

func Latest

func Latest() *VersionHandle

Latest returns a VersionHandle for the latest version of current/non-removed values Only to be used in cases where the latest values are needed and no transactionality is required.

func (*VersionHandle) Close

func (h *VersionHandle) Close() error

Close releases the held version for removal once no handle for it are no longer held. This may not be called while holding any locks that the 'closer' function passed to the coordinator may take!

func (*VersionHandle) IsValid

func (h *VersionHandle) IsValid() bool

func (*VersionHandle) String

func (h *VersionHandle) String() string

func (*VersionHandle) Version

func (h *VersionHandle) Version() KeepVersion

type Versioned

type Versioned[T any] struct {
	// contains filtered or unexported fields
}

Versioned is a pair of a version and any type T

type VersionedSlice

type VersionedSlice[T any] []Versioned[T]

func (VersionedSlice[T]) Append

func (s VersionedSlice[T]) Append(value T, tx *Tx) VersionedSlice[T]

Append appends a pair of 'nextVersion' and 'value' to VersionedSlice 's', returning updated 's'. Needed to keep members private. Should only be called with monotonically increasing 'nextVersion's, so that the slice remains sorted by version in ascending order.

func (VersionedSlice[T]) Before

func (s VersionedSlice[T]) Before(keepVersion KeepVersion) iter.Seq[T]

Before returns an iterator over the elements in VersionedSlice 's' having a version earlier than 'keepVersion'. The slice is assumed to be sorted by version in ascending order.

Jump to

Keyboard shortcuts

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