btree

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package btree implements a disk-backed B+Tree primitive.

Index

Constants

View Source
const (
	MaxKeySize   = 128
	MaxValueSize = 1024
)
View Source
const (
	// PageSize is the standard size for a B+Tree page (4KB)
	PageSize = 4096
)

Variables

View Source
var (
	ErrPageNotFound = errors.New("page not found")
)

Functions

This section is empty.

Types

type Cursor

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

Cursor represents an iterator over B+Tree keys

func (*Cursor) Next

func (c *Cursor) Next() ([]byte, []byte, bool)

Next moves to the next key-value pair

type Node

type Node struct {
	ID       uint64
	IsLeaf   bool
	Keys     [][]byte
	Children []uint64 // For internal nodes
	Values   [][]byte // For leaf nodes
	NextPage uint64   // For leaf nodes (linked list)
	// contains filtered or unexported fields
}

Node represents a B+Tree node

func DeserializeNode

func DeserializeNode(page *Page) (*Node, error)

DeserializeNode deserializes a node from a page

func NewNode

func NewNode(pageID uint64, isLeaf bool) *Node

NewNode creates a new B+Tree node

func (*Node) Serialize

func (n *Node) Serialize() error

Serialize serializes the node into its page data

type Page

type Page struct {
	ID   uint64
	Data []byte
}

Page represents a fixed-size block of data on disk

type Pager

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

Pager manages reading and writing pages to a file

func NewPager

func NewPager(path string) (*Pager, error)

NewPager creates a new pager for the given file path

func (*Pager) AllocatePage

func (p *Pager) AllocatePage() (*Page, error)

AllocatePage creates a new page at the end of the file

func (*Pager) Close

func (p *Pager) Close() error

Close closes the pager file

func (*Pager) Flush

func (p *Pager) Flush() error

Flush writes all cached pages to disk and flushes the file

func (*Pager) ReadPage

func (p *Pager) ReadPage(pageID uint64) (*Page, error)

ReadPage reads a page from disk or cache

func (*Pager) WritePage

func (p *Pager) WritePage(page *Page) error

WritePage writes a page to disk and cache

type Tree

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

Tree represents a B+Tree

func Open

func Open(path string) (*Tree, error)

Open opens or creates a B+Tree at the given path

func (*Tree) Close

func (t *Tree) Close() error

Close closes the tree

func (*Tree) Cursor

func (t *Tree) Cursor(startKey []byte) *Cursor

Cursor returns a cursor for range scans starting at key

func (*Tree) Delete

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

Delete marks a key as deleted by writing a zero-length value as a tombstone. Get and Cursor.Next treat zero-length values as absent, so reads behave as expected.

Tombstone semantics are the intended contract — they trade space for simplicity, deferring the leaf-underflow-rebalance complexity of a real B+Tree delete. A re-Put of a tombstoned key overwrites in place, so the delete-and-rewrite pattern reclaims space; pure deletes do not. A periodic compaction pass would reclaim leaked tombstone slots — see the TODO at the top of pager.go.

Callers that need real-removal semantics (e.g. for storage-tier compaction) should layer that on top, not expect Delete to do it.

func (*Tree) Flush

func (t *Tree) Flush() error

Flush flushes the tree to disk

func (*Tree) Get

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

Get retrieves a value by key

func (*Tree) Put

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

Put inserts a key-value pair

Jump to

Keyboard shortcuts

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