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 = 12VERSION_STRING
private const VERSION_STRING = '1.0'Properties
version
private Version $versionslotSize
private int $slotSize = 16keySize
private int $keySize = 32filePath
private string $filePathfile
private mixed $filerootNode
private Node $rootNodereadOnly
private bool $readOnlyMethods
__construct
Constructs a new BTree instance.
public __construct(string $filePath, bool $readOnly, int $keySize = 32, int $slotSize = 16): voidInitializes 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(): voidEnsures that the BTree file resource is closed when the object is destroyed.
reset
Resets the B-Tree by reloading the root node from the file.
public reset(): boolset
Sets the value for the specified key in the B-Tree.
public set(string $key, mixed $value): boolParameters
| 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): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key to get from the B-Tree |
has
public has(string $key): boolParameters
| Parameter | Type | Description |
|---|---|---|
$key | string |
remove
Removes the specified key from the B-Tree.
public remove(string $key): boolParameters
| 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): boolParameters
| 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(): intgetIterator
Returns a generator that yields all records in the B-Tree.
public getIterator(): voidThis method traverses the B-Tree and yields each record's key and value.
Empties the B-Tree by creating a new root node.
public (): booltoArray
Return the entire B-Tree as an array.
public toArray(): voidverify
Verifies the integrity of the B-Tree.
public verify(): booladdLeafToTree
Adds a new leaf node to the B-Tree.
private addLeafToTree(array $nodeTree, mixed $file, Node $newLeafNode): voidThis 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): voidThis 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(): boolwriteHeader
Writes the header to the B-Tree file.
private writeHeader(mixed $file, ?Node $rootNode): boolParameters
| Parameter | Type | Description |
|---|---|---|
$file | mixed | |
$rootNode | Node |
Generated by Hazaar API Doc Generator on Mon, 27 Oct 2025 13:01:29 +0000