part

package
v0.0.0-...-7cd6fd4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BytesSet = NewBytesSet()

BytesSet is an empty set of byte slices. Short form for "part.NewBytesSet()"

View Source
var StringSet = NewStringSet()

StringSet is an empty set of strings. Short form for "part.NewStringSet()"

Functions

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]) 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.

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 NewBytesMap

func NewBytesMap[V any]() Map[[]byte, V]

func NewMap

func NewMap[K, V any](toBytes func(K) []byte, fromBytes func([]byte) K) Map[K, V]

NewMap creates a new persistent map. The toBytes function maps the key into bytes, and fromBytes does the reverse.

func NewStringMap

func NewStringMap[V any]() Map[string, V]

func NewUint64Map

func NewUint64Map[V any]() Map[uint64, V]

func (Map[K, V]) All

func (m Map[K, V]) All() MapIterator[K, V]

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

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]) 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) MapIterator[K, V]

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

func (Map[K, V]) Prefix

func (m Map[K, V]) Prefix(prefix K) MapIterator[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.

type MapIterator

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

MapIterator iterates over key and value pairs.

func (MapIterator[K, V]) Next

func (it MapIterator[K, V]) Next() (k K, v V, ok bool)

Next returns the next key and value. If the iterator is exhausted it returns false.

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.

func NewBytesSet

func NewBytesSet(values ...[]byte) Set[[]byte]

NewBytesSet creates a new set of byte slices.

func NewSet

func NewSet[T any](toBytes func(T) []byte, values ...T) Set[T]

NewSet creates a new set of T when given a function to convert T into a byte slice key.

func NewStringSet

func NewStringSet(values ...string) Set[string]

NewStringSet creates a new set of strings.

func (Set[T]) All

func (s Set[T]) All() SetIterator[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 removes the values in the second set from the first set. Returns a new set, the original sets are unchanged.

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]) Set

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

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

func (Set[T]) Slice

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

Slice converts the set into a slice. Note that this allocates a new slice and appends all values into it. If you just want to iterate over the set use All() instead.

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 combines the values in the two sets. Returns a new set.

type SetIterator

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

SetIterator iterates over values in a set.

func (SetIterator[T]) Next

func (it SetIterator[T]) Next() (v T, ok bool)

Next returns the next value or false if all have been iterated over.

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]) 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]) 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