btreeonhashdb

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const MaxKeys = 128

MaxKeys defines the maximum number of keys a node can hold before splitting. A fan-out of 128 keeps nodes compact while providing good branching.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashDBKV

type HashDBKV struct {
	Store *hashdb.HashDB
}

HashDBKV adapts HashDB to the KVStore interface.

func (*HashDBKV) Delete

func (g *HashDBKV) Delete(key []byte) error

func (*HashDBKV) Flush

func (g *HashDBKV) Flush() error

func (*HashDBKV) Get

func (g *HashDBKV) Get(key []byte) ([]byte, error)

func (*HashDBKV) Put

func (g *HashDBKV) Put(key, value []byte) error

func (*HashDBKV) PutMany

func (g *HashDBKV) PutMany(keys [][]byte, vals [][]byte) error

PutMany batches multiple puts when available.

type Iter

type Iter struct {
	// contains filtered or unexported fields
}

Iter iterates over the B+Tree in ascending key order. It holds a read lock on the tree for its lifetime; callers must Close().

func (*Iter) Close

func (it *Iter) Close()

Close releases the tree read lock.

func (*Iter) Error

func (it *Iter) Error() error

Error returns any error encountered during iteration.

func (*Iter) Key

func (it *Iter) Key() []byte

Key returns the current key or nil if invalid.

func (*Iter) Next

func (it *Iter) Next()

Next advances the iterator. Optimized for inlining: Keep this function small and simple.

func (*Iter) Valid

func (it *Iter) Valid() bool

Valid reports whether the iterator points to a valid key/value pair.

func (*Iter) Value

func (it *Iter) Value() []byte

Value returns the current value or nil if invalid.

type KVStore

type KVStore interface {
	Get(key []byte) ([]byte, error)
	Put(key, value []byte) error
	Delete(key []byte) error
}

KVStore is the minimal interface the B+Tree expects from the backing store.

type Meta

type Meta struct {
	RootNodeID NodeID
	Height     uint16
	NextNodeID NodeID
}

Meta is the durable header describing the tree layout.

type Node

type Node struct {
	ID   NodeID
	Type NodeType

	// Common
	Keys [][]byte

	// Internal
	Children []NodeID // len(Children) == len(Keys)+1 when Type == NodeInternal

	// Leaf
	Values   [][]byte // len(Values) == len(Keys) when Type == NodeLeaf
	NextLeaf NodeID
	PrevLeaf NodeID
}

Node represents an in-memory B+Tree node. Keys are kept sorted in ascending lexicographic order.

type NodeID

type NodeID uint64

NodeID uniquely identifies a node within a tree.

type NodeType

type NodeType uint8

NodeType distinguishes internal and leaf nodes.

const (
	NodeInternal NodeType = 1
	NodeLeaf     NodeType = 2
)

type Options

type Options struct {
	CacheSize int // Number of nodes to keep in memory. Default 128.
}

Options configures the BTree.

type RevIter

type RevIter struct {
	// contains filtered or unexported fields
}

RevIter iterates over the B+Tree in descending key order. It holds a read lock on the tree for its lifetime; callers must Close().

func (*RevIter) Close

func (it *RevIter) Close()

Close releases the tree read lock.

func (*RevIter) Error

func (it *RevIter) Error() error

Error returns any error encountered during iteration.

func (*RevIter) Key

func (it *RevIter) Key() []byte

Key returns the current key or nil if invalid.

func (*RevIter) Next

func (it *RevIter) Next()

Next advances the iterator backwards. Optimized for inlining.

func (*RevIter) Valid

func (it *RevIter) Valid() bool

Valid reports whether the iterator points to a valid key/value pair.

func (*RevIter) Value

func (it *RevIter) Value() []byte

Value returns the current value or nil if invalid.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree implements a B+Tree on top of an abstract KV store. Concurrency is handled with a coarse-grained RWMutex.

func NewTreeOnHashDB

func NewTreeOnHashDB(store *hashdb.HashDB, treeID string, opts *Options) (*Tree, error)

NewTreeOnHashDB constructs a Tree backed by HashDB.

func OpenTree

func OpenTree(kv KVStore, treeID string, opts *Options) (*Tree, error)

OpenTree loads an existing tree or initializes a new one if no metadata exists. Meta read errors are returned to the caller; only an absent meta entry triggers initialization.

func (*Tree) Delete

func (t *Tree) Delete(key []byte) error

Delete removes a key from the tree. It is a lazy delete and performs no rebalancing.

func (*Tree) Get

func (t *Tree) Get(key []byte) ([]byte, error)

Get returns the value for the given key or (nil, nil) if it does not exist.

func (*Tree) Put

func (t *Tree) Put(key, value []byte) error

Put inserts or updates the given key/value pair.

func (*Tree) PutMany

func (t *Tree) PutMany(keys, values [][]byte) error

PutMany inserts multiple key/value pairs in a batch.

func (*Tree) Range

func (t *Tree) Range(start, end []byte) (*Iter, error)

Range returns an iterator over [start, end). start == nil begins from the smallest key; end == nil iterates through the largest key.

func (*Tree) ReverseRange

func (t *Tree) ReverseRange(start, end []byte) (*RevIter, error)

ReverseRange returns a descending iterator over [start, end). start == nil iterates down to the smallest key; end == nil starts from the largest key.

func (*Tree) ScanAll

func (t *Tree) ScanAll() (*Iter, error)

ScanAll is a helper for iterating the entire keyspace.

Jump to

Keyboard shortcuts

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