Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package bptree provides a mutable B+ tree implementation for storing key-value data in Gno realms. It implements the ...

Overview

Package bptree provides a mutable B+ tree implementation for storing key-value data in Gno realms. It implements the same ITree interface as the avl package but uses a B+ tree internally for better cache locality and fewer pointer dereferences per operation.

The fanout (maximum number of children per inner node, and maximum number of entries per leaf node) is configurable:

Example
1tree := bptree.NewBPTree32()    // fanout 32
2tree := bptree.NewBPTreeN(64)   // fanout 64

The zero value is usable as an empty tree with fanout 32:

Example
1var tree bptree.BPTree
2tree.Set("key", "value")

Functions 2

func NewBPTree32

1func NewBPTree32() *BPTree
source

NewBPTree32 creates a new empty B+ tree with fanout 32.

func NewBPTreeN

1func NewBPTreeN(fanout int) *BPTree
source

NewBPTreeN creates a new empty B+ tree with the given fanout. It panics when fanout is lower than 4.

Types 3

type BPTree

struct
1type BPTree struct {
2	root   node
3	size   int
4	fanout int
5}
source

The zero value is usable as an empty tree with fanout 32.

Methods on BPTree

func Get

method on BPTree
1func (t *BPTree) Get(key string) any
source

Get retrieves the value associated with the given key. It returns the value if the key exists, or nil if it doesn't. This allows for a simpler usage pattern with type assertions:

Example
1if value, ok := tree.Get("key").(MyType); ok {
2    // use value
3}

Use Has to distinguish a stored nil value from a missing key.

func GetByIndex

method on BPTree
1func (t *BPTree) GetByIndex(index int) (key string, value any)
source

GetByIndex returns the key-value pair at the given 0-based index. Panics if index is out of range.

func Has

method on BPTree
1func (t *BPTree) Has(key string) bool
source

func Iterate

method on BPTree
1func (t *BPTree) Iterate(start, end string, cb IterCbFn) bool
source

Iterate calls cb for each key-value pair in [start, end) ascending order. Empty start/end means no bound. Returns true if stopped early by cb. The tree must not be modified during iteration (no Set or Remove from the callback).

func IterateByOffset

method on BPTree
1func (t *BPTree) IterateByOffset(offset int, count int, cb IterCbFn) bool
source

IterateByOffset calls cb for count entries starting at the offset-th entry in ascending order. Returns true if stopped early by cb. The tree must not be modified during iteration (no Set or Remove from the callback).

func Remove

method on BPTree
1func (t *BPTree) Remove(key string) (value any, removed bool)
source

Remove deletes a key. Returns the old value and true if the key was found.

func ReverseIterate

method on BPTree
1func (t *BPTree) ReverseIterate(start, end string, cb IterCbFn) bool
source

ReverseIterate calls cb for each key-value pair in [start, end] descending order. Empty start/end means no bound. Returns true if stopped early by cb. The tree must not be modified during iteration (no Set or Remove from the callback).

func ReverseIterateByOffset

method on BPTree
1func (t *BPTree) ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool
source

ReverseIterateByOffset calls cb for count entries starting at the offset-th entry from the end, in descending order. Returns true if stopped early by cb. The tree must not be modified during iteration (no Set or Remove from the callback).

func Set

method on BPTree
1func (t *BPTree) Set(key string, value any) (updated bool)
source

Set inserts or updates a key-value pair. Returns true if the key already existed.

func Size

method on BPTree
1func (t *BPTree) Size() int
source

type ITree

interface
 1type ITree interface {
 2	Size() int
 3	Has(key string) bool
 4	Get(key string) any
 5	GetByIndex(index int) (key string, value any)
 6	Iterate(start, end string, cb IterCbFn) bool
 7	ReverseIterate(start, end string, cb IterCbFn) bool
 8	IterateByOffset(offset int, count int, cb IterCbFn) bool
 9	ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool
10	Set(key string, value any) (updated bool)
11	Remove(key string) (value any, removed bool)
12}
source

type IterCbFn

func
1type IterCbFn func(key string, value any) bool
source

Source Files 7

Directories 3