BTree
BTree
BTree class provides a B-Tree implementation for storing key-value pairs.
It supports basic operations like insert, delete, and search.
Constants
BTREE_HEADER_SIZE
private const BTREE_HEADER_SIZE = 12
VERSION_STRING
private const VERSION_STRING = '1.0'
Properties
version
private Version $version
slotSize
private int $slotSize = 16
keySize
private int $keySize = 32
filePath
private string $filePath
file
private mixed $file
rootNode
private Node $rootNode
readOnly
private bool $readOnly
Methods
__construct
Constructs a new BTree instance.
public __construct(string $filePath, bool $readOnly, int $keySize = 32, int $slotSize = 16): void
Initializes the BTree with the specified file path, key size, and slot size. If the BTree file does not exist, it will be created. Loads the root node from the file.
The header will be written to the file in the format:
- Major version (2 bytes)
- Minor version (2 bytes)
- Slot size (2 bytes)
- Key size (2 bytes)
- Pointer to the root node (4 bytes)
If the file already exists, it will read the header to determine the BTree's configuration.
Parameters
Parameter | Type | Description |
---|---|---|
$filePath | string | path to the BTree file |
$readOnly | bool | if true, the BTree will be opened in read-only mode; otherwise, it will be opened in read-write mode |
$keySize | int | Size of the key in bytes. Default is 32. |
$slotSize | int | Size of the slot in bytes. Default is 16. |
__destruct
Destructor for the BTree class.
public __destruct(): void
Ensures that the BTree file resource is closed when the object is destroyed.
close
Closes the BTree file resource.
public close(): void
reset
Resets the B-Tree by reloading the root node from the file.
public reset(): bool
set
Sets the value for the specified key in the B-Tree.
public set(string $key, mixed $value): bool
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key to set in the B-Tree |
$value | mixed | the value to associate with the key |
get
Gets the value for the specified key from the B-Tree.
public get(string $key): mixed
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key to get from the B-Tree |
has
public has(string $key): bool
Parameters
Parameter | Type | Description |
---|---|---|
$key | string |
remove
Removes the specified key from the B-Tree.
public remove(string $key): bool
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key to remove from the B-Tree |
compact
Compacts the B-Tree file to reduce its size.
public compact(float $fillFactor = 0.5): bool
Parameters
Parameter | Type | Description |
---|---|---|
$fillFactor | float | the fill factor for compacting the B-Tree, default is 0.5 This is used to determine how full the nodes should be after compaction. * A fill factor of 0.5 means that nodes will be 50% full after compaction. * A fill factor of 0.8 means that nodes will be 80% full after compaction. |
count
Returns the total number of elements in the B-tree.
public count(): int
getIterator
Returns a generator that yields all records in the B-Tree.
public getIterator(): void
This method traverses the B-Tree and yields each record's key and value.
Empties the B-Tree by creating a new root node.
public (): bool
toArray
Return the entire B-Tree as an array.
public toArray(): void
verify
Verifies the integrity of the B-Tree.
public verify(): bool
addLeafToTree
Adds a new leaf node to the B-Tree.
private addLeafToTree(array $nodeTree, mixed $file, Node $newLeafNode): void
This method is used internally to add a new leaf node to the B-Tree structure. It checks if the new leaf node is full and adds it to the tree if necessary.
Parameters
Parameter | Type | Description |
---|---|---|
$nodeTree | array | The current node tree |
$file | mixed | The file resource for the B-Tree |
$newLeafNode | Node | The new leaf node to be added |
propagateNodeTree
Adds a node to the B-Tree.
private propagateNodeTree(array $nodeTree, mixed $file, int $fillCount): void
This method is used internally to add a new node to the B-Tree structure. It checks if the new node is full and propagates it upwards in the tree if necessary.
Parameters
Parameter | Type | Description |
---|---|---|
$nodeTree | array | The current node tree |
$file | mixed | The file resource for the B-Tree |
$fillCount | int | The fill count for the nodes |
readHeader
Loads the root node from the B-Tree file.
private readHeader(): bool
writeHeader
Writes the header to the B-Tree file.
private writeHeader(mixed $file, ?Node $rootNode): bool
Parameters
Parameter | Type | Description |
---|---|---|
$file | mixed | |
$rootNode | Node |
Generated by Hazaar API Doc Generator on Sat, 23 Aug 2025 22:23:42 +0000