Documentation
¶
Overview ¶
Package b implements the B+tree flavor of a BTree.
This is a generic-enabled version of modernc.org/b.
Concurrency considerations ¶
Tree.{Clear,Delete,Put,Set} mutate the tree. One can use eg. a sync.Mutex.Lock/Unlock (or sync.RWMutex.Lock/Unlock) to wrap those calls if they are to be invoked concurrently.
Tree.{First,Get,Last,Len,Seek,SeekFirst,SekLast} read but do not mutate the tree. One can use eg. a sync.RWMutex.RLock/RUnlock to wrap those calls if they are to be invoked concurrently with any of the tree mutating methods.
Enumerator.{Next,Prev} mutate the enumerator and read but not mutate the tree. One can use eg. a sync.RWMutex.RLock/RUnlock to wrap those calls if they are to be invoked concurrently with any of the tree mutating methods. A separate mutex for the enumerator, or the whole tree in a simplified variant, is necessary if the enumerator's Next/Prev methods per se are to be invoked concurrently.
Index ¶
- type Cmp
- type Enumerator
- type Tree
- func (t *Tree[K, V]) Clear()
- func (t *Tree[K, V]) Close()
- func (t *Tree[K, V]) Delete(k K) (ok bool)
- func (t *Tree[K, V]) First() (k K, v V)
- func (t *Tree[K, V]) Get(k K) (v V, ok bool)
- func (t *Tree[K, V]) Last() (k K, v V)
- func (t *Tree[K, V]) Len() int
- func (t *Tree[K, V]) Put(k K, upd Updater[V]) (oldV V, written bool)
- func (t *Tree[K, V]) Seek(k K) (e *Enumerator[K, V], ok bool)
- func (t *Tree[K, V]) SeekFirst() (e *Enumerator[K, V], err error)
- func (t *Tree[K, V]) SeekLast() (e *Enumerator[K, V], err error)
- func (t *Tree[K, V]) Set(k K, v V)
- type Updater
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Enumerator ¶
type Enumerator[K, V any] struct { // contains filtered or unexported fields }
Enumerator captures the state of enumerating a tree. It is returned from the Seek* methods. The enumerator is aware of any mutations made to the tree in the process of enumerating it and automatically resumes the enumeration at the proper key, if possible.
However, once an Enumerator returns io.EOF to signal "no more items", it does no more attempt to "resync" on tree mutation(s). In other words, io.EOF from an Enumerator is "sticky" (idempotent).
func (*Enumerator[K, V]) Close ¶
func (e *Enumerator[K, V]) Close()
Close recycles e to a pool for possible later reuse. No references to e should exist or such references must not be used afterwards.
type Tree ¶
type Tree[K, V any] struct { // contains filtered or unexported fields }
Tree is a B+tree.
func TreeNew ¶
TreeNew returns a newly created, empty Tree. The compare function is used for key collation.
func (*Tree[K, V]) Delete ¶
func (t *Tree[K, V]) Delete(k K) (ok bool)
Delete removes the k's KV pair, if it exists, in which case Delete returns true.
func (*Tree[K, V]) First ¶
func (t *Tree[K, V]) First() (k K, v V)
First returns the first item of the tree in the key collating order, or (zero-value, zero-value) if the tree is empty.
func (*Tree[K, V]) Get ¶
func (t *Tree[K, V]) Get(k K) (v V, ok bool)
Get returns the value associated with k and true if it exists. Otherwise Get returns (zero-value, false).
func (*Tree[K, V]) Last ¶
func (t *Tree[K, V]) Last() (k K, v V)
Last returns the last item of the tree in the key collating order, or (zero-value, zero-value) if the tree is empty.
func (*Tree[K, V]) Len ¶
func (t *Tree[K, V]) Len() int
Len returns the number of items in the tree.
func (*Tree[K, V]) Put ¶
Put combines Get and Set in a more efficient way where the tree is walked only once. The upd(ater) receives (old-value, true) if a KV pair for k exists or (zero-value, false) otherwise. It can then return a (new-value, true) to create or overwrite the existing value in the KV pair, or (whatever, false) if it decides not to create or not to update the value of the KV pair.
tree.Set(k, v) call conceptually equals calling tree.Put(k, func(oldv V, exists bool){ return v, true })
modulo the differing return values.
func (*Tree[K, V]) Seek ¶
func (t *Tree[K, V]) Seek(k K) (e *Enumerator[K, V], ok bool)
Seek returns an Enumerator positioned on an item such that k >= item's key. ok reports if k == item.key The Enumerator's position is possibly after the last item in the tree.
func (*Tree[K, V]) SeekFirst ¶
func (t *Tree[K, V]) SeekFirst() (e *Enumerator[K, V], err error)
SeekFirst returns an enumerator positioned on the first KV pair in the tree, if any. For an empty tree, err == io.EOF is returned and e will be nil.