mini\Table\Index\BTreeIndex final class

Documentation

Append-only B-tree index with on-disk persistence.

File layout (4KB aligned pages):

  • Page 0: Header (64 bytes used, rest reserved for metadata)
  • Page 1+: B-tree nodes (internal and leaf)

Design:

  • Copy-on-write: pages never modified once written (except header)
  • Variable-length keys with offset arrays for O(log n) binary search within pages

TODO: Fully append-only design with separate log file

  • Main file: pages only, never overwritten
  • Log file: append-only sequence of [seq, root, checksum] entries
  • Commit: append pages, fsync, append log entry, fsync
  • Open: scan log from end for latest valid root
  • Removes header overwrites for better crash safety

Inheritance

Implements: mini\Table\Index\IndexInterface mini\Table\Index\HashIndexInterface

Constants (10)

Name Value
MAGIC 1112822345
VERSION 1
HEADER_SIZE 64
PAGE_SIZE 4096
MAX_KEY_LENGTH 4077
PAGE_INTERNAL 1
PAGE_LEAF 2
PAGE_INTERNAL_ROOT 129
PAGE_LEAF_ROOT 130
CRC_OFFSET 4092

Properties (13)

private mixed $file
private mixed $lockFile
private string $path
private int $rootPage
private int $nextPage
private int $sequence
private bool $inTransaction
private array $unwrittenPages
private int $unwrittenPageCount
private array $pageCache
private float $maxReadLatency
private float $lastRootRefresh

Methods (39)

Documentation missing

Build index from a generator function.

Build index from array of [key, rowId] pairs.

Documentation missing

Begin a transaction for bulk operations.

Commit unwritten pages to disk.

Commit with bulk rebuild - use for initial loading only.

Rollback pending changes.

Split any oversized pages before writing to disk.

Set parent references on all pages in unwrittenPages by traversing from root.

Recursively set parent references on children.

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Write minimal file header (page 0) - just magic and version.

Write root page object with CRC marker (for new overlay approach).

Find the latest valid root page by scanning backwards from file end.

Documentation missing

Documentation missing

Read raw page from disk (no caching).

Load a page from unwritten overlay, cache, or disk.

Allocate a new page in the overlay. Returns the page number.

Get a page for modification. If from disk/cache, moves to overlay.

Release a leaf page back to pool, but only if it's not in the overlay.

Find child index for given key in internal node.

Find path from root to leaf for given key.

Documentation missing

Propagate a split up the tree.

Update parent pointers after a child was replaced.

Documentation missing

Documentation missing

Documentation missing

Recursively rewrite reachable pages to temp file.

Source

src/Table/Index/BTreeIndex.php:756-2294