func NewBPTree32
NewBPTree32 creates a new empty B+ tree with fanout 32.
Package bptree provides a mutable B+ tree implementation for storing key-value data in Gno realms. It implements the ...
v0 - Unaudited This is an initial version of this package that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.
bptree - Mutable B+ treeA mutable, in-place B+ tree for storing key-value data in Gno realms. Exposes the same ITree interface as gno.land/p/nt/avl/v0 but uses a B+ tree internally — fewer pointer dereferences per operation and better cache locality, with a configurable fanout.
1package myrealm
2
3import "gno.land/p/nt/bptree/v0"
4
5// Zero value is usable (fanout 32). Persisted across transactions.
6var tree bptree.BPTree
7
8func Set(key string, value int) {
9 tree.Set(key, value)
10}
11
12func Get(key string) int {
13 raw := tree.Get(key)
14 if raw == nil {
15 panic("not found")
16 }
17 return raw.(int)
18}
19
20func RangeAsc(start, end string) {
21 tree.Iterate(start, end, func(key string, value any) bool {
22 // return true to stop early
23 return false
24 })
25}
For a different fanout, use a constructor:
1tree := bptree.NewBPTreeN(64) // fanout 64
1type BPTree struct{ /* unexported */ }
2
3func NewBPTree32() *BPTree // fanout 32
4func NewBPTreeN(fanout int) *BPTree // panics if fanout < 4
5
6// Read
7func (t *BPTree) Size() int
8func (t *BPTree) Has(key string) bool
9func (t *BPTree) Get(key string) (value any) // nil if the key is absent
10func (t *BPTree) GetByIndex(index int) (key string, value any)
11func (t *BPTree) Iterate(start, end string, cb IterCbFn) bool
12func (t *BPTree) ReverseIterate(start, end string, cb IterCbFn) bool
13func (t *BPTree) IterateByOffset(offset, count int, cb IterCbFn) bool
14func (t *BPTree) ReverseIterateByOffset(offset, count int, cb IterCbFn) bool
15
16// Write
17func (t *BPTree) Set(key string, value any) (updated bool)
18func (t *BPTree) Remove(key string) (value any, removed bool)
19
20type IterCbFn func(key string, value any) bool
21
22type ITree interface { /* same shape as BPTree's methods */ }
The zero value of BPTree is a usable empty tree (fanout 32). Iterate uses [start, end) (start inclusive, end exclusive); ReverseIterate uses [start, end] (both inclusive). Empty strings mean unbounded. Callbacks return true to stop early. GetByIndex panics on out-of-range indices.
The tree must not be modified during iteration (no Set or Remove from the callback).
gno.land/p/nt/bptree/v0/list - ordered list built on top of BPTree.gno.land/p/nt/bptree/v0/pager - pagination helper for trees and lists.gno.land/p/nt/bptree/v0/rotree - read-only view of a BPTree.gno.land/p/nt/avl/v0 exactly — "" is a valid key, Get returns nil for a missing key (use Has to distinguish a stored nil), and Remove returns (nil, false).*BPTree from a realm getter: a caller can then call Set/Remove on it under your realm's authority. Return values, copies, or a read-only rotree view.seqid (gno.land/p/nt/seqid/v0) pair well here: monotonic inserts hit the append-optimized split path.>= 4. Higher fanouts mean shallower trees and fewer object loads per lookup, at the cost of larger individual node objects.O(log n) nodes on the search path — same storage-efficiency benefit as avl.first/last shortcuts: iteration uses an ephemeral stack to keep every persisted node at ref-count 1 (avoids Gno's object-escape penalty).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:
The zero value is usable as an empty tree with fanout 32:
NewBPTree32 creates a new empty B+ tree with fanout 32.
NewBPTreeN creates a new empty B+ tree with the given fanout. It panics when fanout is lower than 4.
The zero value is usable as an empty tree with fanout 32.
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:
Use Has to distinguish a stored nil value from a missing key.
GetByIndex returns the key-value pair at the given 0-based index. Panics if index is out of range.
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).
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).
Remove deletes a key. Returns the old value and true if the key was found.
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).
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).
Set inserts or updates a key-value pair. Returns true if the key already existed.
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}