Documentation ¶
Index ¶
- func RegisterKeyType[K any](bytesFromKey func(K) []byte)
- func RootOnlyWatch(o *options)
- type Iterator
- type Map
- func (m Map[K, V]) All() iter.Seq2[K, V]
- func (m Map[K, V]) Delete(key K) Map[K, V]
- func (m Map[K, V]) EqualKeys(other Map[K, V]) bool
- func (m Map[K, V]) Get(key K) (value V, found bool)
- func (m Map[K, V]) Len() int
- func (m Map[K, V]) LowerBound(from K) iter.Seq2[K, V]
- func (m Map[K, V]) MarshalJSON() ([]byte, error)
- func (m Map[K, V]) MarshalYAML() (any, error)
- func (m Map[K, V]) Prefix(prefix K) iter.Seq2[K, V]
- func (m Map[K, V]) Set(key K, value V) Map[K, V]
- func (m Map[K, V]) SlowEqual(other Map[K, V]) bool
- func (m *Map[K, V]) UnmarshalJSON(data []byte) error
- func (m *Map[K, V]) UnmarshalYAML(value *yaml.Node) error
- type Ops
- type Option
- type Set
- func (s Set[T]) All() iter.Seq[T]
- func (s Set[T]) Delete(v T) Set[T]
- func (s Set[T]) Difference(s2 Set[T]) Set[T]
- func (s Set[T]) Equal(other Set[T]) bool
- func (s Set[T]) Has(v T) bool
- func (s Set[T]) Len() int
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s Set[T]) MarshalYAML() (any, error)
- func (s Set[T]) Set(v T) Set[T]
- func (s Set[T]) ToBytesFunc() func(T) []byte
- func (s Set[T]) Union(s2 Set[T]) Set[T]
- func (s *Set[T]) UnmarshalJSON(data []byte) error
- func (s *Set[T]) UnmarshalYAML(value *yaml.Node) error
- type Tree
- func (t *Tree[T]) Delete(key []byte) (old T, hadOld bool, tree *Tree[T])
- func (t *Tree[T]) Get(key []byte) (T, <-chan struct{}, bool)
- func (t *Tree[T]) Insert(key []byte, value T) (old T, hadOld bool, tree *Tree[T])
- func (t *Tree[T]) Iterator() *Iterator[T]
- func (t *Tree[T]) Len() int
- func (t *Tree[T]) LowerBound(key []byte) *Iterator[T]
- func (t *Tree[T]) Modify(key []byte, mod func(T) T) (old T, hadOld bool, tree *Tree[T])
- func (t *Tree[T]) Prefix(prefix []byte) (*Iterator[T], <-chan struct{})
- func (t *Tree[T]) PrintTree()
- func (t *Tree[T]) RootWatch() <-chan struct{}
- func (t *Tree[T]) Txn() *Txn[T]
- type Txn
- func (txn *Txn[T]) Clone() *Txn[T]
- func (txn *Txn[T]) Commit() *Tree[T]
- func (txn *Txn[T]) CommitOnly() *Tree[T]
- func (txn *Txn[T]) Delete(key []byte) (old T, hadOld bool)
- func (txn *Txn[T]) Get(key []byte) (T, <-chan struct{}, bool)
- func (txn *Txn[T]) Insert(key []byte, value T) (old T, hadOld bool)
- func (txn *Txn[T]) Iterator() *Iterator[T]
- func (txn *Txn[T]) Len() int
- func (txn *Txn[T]) LowerBound(key []byte) *Iterator[T]
- func (txn *Txn[T]) Modify(key []byte, mod func(T) T) (old T, hadOld bool)
- func (txn *Txn[T]) Notify()
- func (txn *Txn[T]) Prefix(key []byte) (*Iterator[T], <-chan struct{})
- func (txn *Txn[T]) PrintTree()
- func (txn *Txn[T]) RootWatch() <-chan struct{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterKeyType ¶
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
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 ¶
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 ¶
Delete a value from the map. Returns a new map without the element pointed to by the key (if found).
func (Map[K, V]) LowerBound ¶
LowerBound iterates over all keys in order with value equal to or greater than [from].
func (Map[K, V]) MarshalJSON ¶
func (Map[K, V]) MarshalYAML ¶ added in v0.3.1
func (Map[K, V]) Set ¶
Set a value. Returns a new map with the value set. Original map is unchanged.
func (Map[K, V]) SlowEqual ¶
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 (*Map[K, V]) UnmarshalYAML ¶ added in v0.3.1
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 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 ¶
NewSet creates a new set of T. The value type T must be registered with RegisterKeyType.
func (Set[T]) Difference ¶
Difference returns a set with values that only appear in the first set.
func (Set[T]) MarshalJSON ¶
func (Set[T]) MarshalYAML ¶ added in v0.3.1
func (Set[T]) ToBytesFunc ¶
ToBytesFunc returns the function to extract the key from the element type. Useful for utilities that are interested in the key.
func (*Set[T]) UnmarshalJSON ¶
func (*Set[T]) UnmarshalYAML ¶ added in v0.3.1
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 (*Tree[T]) Delete ¶
Delete the given key from the tree. Returns the old value if it exists and the new tree.
func (*Tree[T]) Get ¶
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 ¶
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]) LowerBound ¶
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
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 ¶
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.
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 ¶
Clone returns a clone of the transaction. The clone is unaffected by any future changes done with the original transaction.
func (*Txn[T]) CommitOnly ¶
CommitOnly the transaction, but do not close the watch channels. Returns the new tree. To close the watch channels call Notify().
func (*Txn[T]) Get ¶
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 ¶
Insert or update the tree with the given key and value. Returns the old value if it exists.
func (*Txn[T]) LowerBound ¶
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
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 ¶
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.