part

package
v0.3.5 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterKeyType

func RegisterKeyType[K any](bytesFromKey func(K) []byte)

RegisterKeyType registers a new key type to be used with the Map and Set types. Intended to be called from init() functions. For Set-only usage only the [bytesFromKey] function is needed.

func RootOnlyWatch

func RootOnlyWatch(o *options)

RootOnlyWatch sets the tree to only have a watch channel on the root node. This improves the speed at the cost of having a much more coarse grained notifications.

Types

type Iterator

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

Iterator for key and value pairs where value is of type T

func (*Iterator[T]) Clone added in v0.3.0

func (it *Iterator[T]) Clone() *Iterator[T]

Clone returns a copy of the iterator, allowing restarting the iterator from scratch.

func (*Iterator[T]) Next

func (it *Iterator[T]) Next() (key []byte, value T, ok bool)

Next returns the next key, value and true if the value exists, otherwise it returns false.

type Map

type Map[K, V any] struct {
	// contains filtered or unexported fields
}

Map of key-value pairs. The zero value is ready for use, provided that the key type has been registered with RegisterKeyType.

Map is a typed wrapper around Tree[T] for working with keys that are not []byte.

func FromMap

func FromMap[K comparable, V any](m Map[K, V], hm map[K]V) Map[K, V]

FromMap copies values from the hash map into the given Map. This is not implemented as a method on Map[K,V] as hash maps require the comparable constraint and we do not need to limit Map[K, V] to that.

func (Map[K, V]) All

func (m Map[K, V]) All() iter.Seq2[K, V]

All iterates every key-value in the map in order. The order is in bytewise order of the byte slice returned by bytesFromKey.

func (Map[K, V]) Delete

func (m Map[K, V]) Delete(key K) Map[K, V]

Delete a value from the map. Returns a new map without the element pointed to by the key (if found).

func (Map[K, V]) EqualKeys

func (m Map[K, V]) EqualKeys(other Map[K, V]) bool

EqualKeys returns true if both maps contain the same keys.

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) (value V, found bool)

Get a value from the map by its key.

func (Map[K, V]) Len

func (m Map[K, V]) Len() int

Len returns the number of elements in the map.

func (Map[K, V]) LowerBound

func (m Map[K, V]) LowerBound(from K) iter.Seq2[K, V]

LowerBound iterates over all keys in order with value equal to or greater than [from].

func (Map[K, V]) MarshalJSON

func (m Map[K, V]) MarshalJSON() ([]byte, error)

func (Map[K, V]) MarshalYAML added in v0.3.1

func (m Map[K, V]) MarshalYAML() (any, error)

func (Map[K, V]) Prefix

func (m Map[K, V]) Prefix(prefix K) iter.Seq2[K, V]

Prefix iterates in order over all keys that start with the given prefix.

func (Map[K, V]) Set

func (m Map[K, V]) Set(key K, value V) Map[K, V]

Set a value. Returns a new map with the value set. Original map is unchanged.

func (Map[K, V]) SlowEqual

func (m Map[K, V]) SlowEqual(other Map[K, V]) bool

SlowEqual returns true if the two maps contain the same keys and values. Value comparison is implemented with reflect.DeepEqual which makes this slow and mostly useful for testing.

func (*Map[K, V]) UnmarshalJSON

func (m *Map[K, V]) UnmarshalJSON(data []byte) error

func (*Map[K, V]) UnmarshalYAML added in v0.3.1

func (m *Map[K, V]) UnmarshalYAML(value *yaml.Node) error

type Ops

type Ops[T any] interface {
	// Len returns the number of objects in the tree.
	Len() int

	// Get fetches the value associated with the given key.
	// Returns the value, a watch channel (which is closed on
	// modification to the key) and boolean which is true if
	// value was found.
	Get(key []byte) (T, <-chan struct{}, bool)

	// Prefix returns an iterator for all objects that starts with the
	// given prefix, and a channel that closes when any objects matching
	// the given prefix are upserted or deleted.
	Prefix(key []byte) (*Iterator[T], <-chan struct{})

	// LowerBound returns an iterator for all objects that have a
	// key equal or higher than the given 'key'.
	LowerBound(key []byte) *Iterator[T]

	// RootWatch returns a watch channel for the root of the tree.
	// Since this is the channel associated with the root, this closes
	// when there are any changes to the tree.
	RootWatch() <-chan struct{}

	// Iterator returns an iterator for all objects.
	Iterator() *Iterator[T]

	// PrintTree to the standard output. For debugging.
	PrintTree()
}

Ops is the common operations that can be performed with a Tree or Txn.

type Option

type Option func(*options)

type Set

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

Set is a persistent (immutable) set of values. A Set can be defined for any type for which a byte slice key can be derived.

A zero value Set[T] can be used provided that the conversion function for T have been registered with RegisterKeyType. For Set-only use only [bytesFromKey] needs to be defined.

func NewSet

func NewSet[T any](values ...T) Set[T]

NewSet creates a new set of T. The value type T must be registered with RegisterKeyType.

func (Set[T]) All

func (s Set[T]) All() iter.Seq[T]

All returns an iterator for all values.

func (Set[T]) Delete

func (s Set[T]) Delete(v T) Set[T]

Delete returns a new set without the value. The original set is unchanged.

func (Set[T]) Difference

func (s Set[T]) Difference(s2 Set[T]) Set[T]

Difference returns a set with values that only appear in the first set.

func (Set[T]) Equal

func (s Set[T]) Equal(other Set[T]) bool

Equal returns true if the two sets contain the equal keys.

func (Set[T]) Has

func (s Set[T]) Has(v T) bool

Has returns true if the set has the value.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the number of values in the set.

func (Set[T]) MarshalJSON

func (s Set[T]) MarshalJSON() ([]byte, error)

func (Set[T]) MarshalYAML added in v0.3.1

func (s Set[T]) MarshalYAML() (any, error)

func (Set[T]) Set

func (s Set[T]) Set(v T) Set[T]

Set a value. Returns a new set. Original is unchanged.

func (Set[T]) ToBytesFunc

func (s Set[T]) ToBytesFunc() func(T) []byte

ToBytesFunc returns the function to extract the key from the element type. Useful for utilities that are interested in the key.

func (Set[T]) Union

func (s Set[T]) Union(s2 Set[T]) Set[T]

Union returns a set that is the union of the values in the input sets.

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) error

func (*Set[T]) UnmarshalYAML added in v0.3.1

func (s *Set[T]) UnmarshalYAML(value *yaml.Node) error

type Tree

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

Tree is a persistent (immutable) adaptive radix tree. It supports map-like operations on values keyed by []byte and additionally prefix searching and lower bound searching. Each node in the tree has an associated channel that is closed when that node is mutated. This allows watching any part of the tree (any prefix) for changes.

func New

func New[T any](opts ...Option) *Tree[T]

New constructs a new tree.

func (*Tree[T]) Delete

func (t *Tree[T]) Delete(key []byte) (old T, hadOld bool, tree *Tree[T])

Delete the given key from the tree. Returns the old value if it exists and the new tree.

func (*Tree[T]) Get

func (t *Tree[T]) Get(key []byte) (T, <-chan struct{}, bool)

Get fetches the value associated with the given key. Returns the value, a watch channel (which is closed on modification to the key) and boolean which is true if value was found.

func (*Tree[T]) Insert

func (t *Tree[T]) Insert(key []byte, value T) (old T, hadOld bool, tree *Tree[T])

Insert inserts the key into the tree with the given value. Returns the old value if it exists and a new tree.

func (*Tree[T]) Iterator

func (t *Tree[T]) Iterator() *Iterator[T]

Iterator returns an iterator for all objects.

func (*Tree[T]) Len

func (t *Tree[T]) Len() int

Len returns the number of objects in the tree.

func (*Tree[T]) LowerBound

func (t *Tree[T]) LowerBound(key []byte) *Iterator[T]

LowerBound returns an iterator for all keys that have a value equal to or higher than 'key'.

func (*Tree[T]) Modify added in v0.2.5

func (t *Tree[T]) Modify(key []byte, mod func(T) T) (old T, hadOld bool, tree *Tree[T])

Modify a value in the tree. If the key does not exist the modify function is called with the zero value for T. It is up to the caller to not mutate the value in-place and to return a clone. Returns the old value if it exists.

func (*Tree[T]) Prefix

func (t *Tree[T]) Prefix(prefix []byte) (*Iterator[T], <-chan struct{})

Prefix returns an iterator for all objects that starts with the given prefix, and a channel that closes when any objects matching the given prefix are upserted or deleted.

func (*Tree[T]) PrintTree

func (t *Tree[T]) PrintTree()

PrintTree to the standard output. For debugging.

func (*Tree[T]) RootWatch

func (t *Tree[T]) RootWatch() <-chan struct{}

RootWatch returns a watch channel for the root of the tree. Since this is the channel associated with the root, this closes when there are any changes to the tree.

func (*Tree[T]) Txn

func (t *Tree[T]) Txn() *Txn[T]

Txn constructs a new transaction against the tree. Transactions enable efficient large mutations of the tree by caching cloned nodes.

type Txn

type Txn[T any] struct {
	// tree is the tree being modified
	Tree[T]
	// contains filtered or unexported fields
}

Txn is a transaction against a tree. It allows doing efficient modifications to a tree by caching and reusing cloned nodes.

func (*Txn[T]) Clone

func (txn *Txn[T]) Clone() *Txn[T]

Clone returns a clone of the transaction. The clone is unaffected by any future changes done with the original transaction.

func (*Txn[T]) Commit

func (txn *Txn[T]) Commit() *Tree[T]

Commit the transaction and produce the new tree.

func (*Txn[T]) CommitOnly

func (txn *Txn[T]) CommitOnly() *Tree[T]

CommitOnly the transaction, but do not close the watch channels. Returns the new tree. To close the watch channels call Notify().

func (*Txn[T]) Delete

func (txn *Txn[T]) Delete(key []byte) (old T, hadOld bool)

Delete the given key from the tree. Returns the old value if it exists.

func (*Txn[T]) Get

func (txn *Txn[T]) Get(key []byte) (T, <-chan struct{}, bool)

Get fetches the value associated with the given key. Returns the value, a watch channel (which is closed on modification to the key) and boolean which is true if value was found.

func (*Txn[T]) Insert

func (txn *Txn[T]) Insert(key []byte, value T) (old T, hadOld bool)

Insert or update the tree with the given key and value. Returns the old value if it exists.

func (*Txn[T]) Iterator

func (txn *Txn[T]) Iterator() *Iterator[T]

Iterator returns an iterator for all objects.

func (*Txn[T]) Len

func (txn *Txn[T]) Len() int

Len returns the number of objects in the tree.

func (*Txn[T]) LowerBound

func (txn *Txn[T]) LowerBound(key []byte) *Iterator[T]

LowerBound returns an iterator for all objects that have a key equal or higher than the given 'key'.

func (*Txn[T]) Modify added in v0.2.5

func (txn *Txn[T]) Modify(key []byte, mod func(T) T) (old T, hadOld bool)

Modify a value in the tree. If the key does not exist the modify function is called with the zero value for T. It is up to the caller to not mutate the value in-place and to return a clone. Returns the old value if it exists.

func (*Txn[T]) Notify

func (txn *Txn[T]) Notify()

Notify closes the watch channels of nodes that were mutated as part of this transaction.

func (*Txn[T]) Prefix

func (txn *Txn[T]) Prefix(key []byte) (*Iterator[T], <-chan struct{})

Prefix returns an iterator for all objects that starts with the given prefix, and a channel that closes when any objects matching the given prefix are upserted or deleted.

func (*Txn[T]) PrintTree

func (txn *Txn[T]) PrintTree()

PrintTree to the standard output. For debugging.

func (*Txn[T]) RootWatch

func (txn *Txn[T]) RootWatch() <-chan struct{}

RootWatch returns a watch channel for the root of the tree. Since this is the channel associated with the root, this closes when there are any changes to the tree.

Jump to

Keyboard shortcuts

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