b

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: BSD-3-Clause Imports: 2 Imported by: 5

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmp

type Cmp[K any] func(a, b K) int

Cmp compares a and b. Return value is:

< 0 if a <  b
  0 if a == b
> 0 if a >  b

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.

func (*Enumerator[K, V]) Next

func (e *Enumerator[K, V]) Next() (k K, v V, err error)

Next returns the currently enumerated item, if it exists and moves to the next item in the key collation order. If there is no item to return, err == io.EOF is returned.

func (*Enumerator[K, V]) Prev

func (e *Enumerator[K, V]) Prev() (k K, v V, err error)

Prev returns the currently enumerated item, if it exists and moves to the previous item in the key collation order. If there is no item to return, err == io.EOF is returned.

type Tree

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

Tree is a B+tree.

func TreeNew

func TreeNew[K, V any](cmp Cmp[K]) *Tree[K, V]

TreeNew returns a newly created, empty Tree. The compare function is used for key collation.

func (*Tree[K, V]) Clear

func (t *Tree[K, V]) Clear()

Clear removes all K/V pairs from the tree.

func (*Tree[K, V]) Close

func (t *Tree[K, V]) Close()

Close performs Clear and zeroes *t.

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

func (t *Tree[K, V]) Put(k K, upd Updater[V]) (oldV V, written bool)

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.

func (*Tree[K, V]) SeekLast

func (t *Tree[K, V]) SeekLast() (e *Enumerator[K, V], err error)

SeekLast returns an enumerator positioned on the last KV pair in the tree, if any. For an empty tree, err == io.EOF is returned and e will be nil.

func (*Tree[K, V]) Set

func (t *Tree[K, V]) Set(k K, v V)

Set sets the value associated with k.

type Updater

type Updater[V any] func(oldV V, exists bool) (newV V, write bool)

Updater is the function of the Tree.Put upd argument.

Jump to

Keyboard shortcuts

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