BTree
BTree
B-Tree key/value database file access class.
This class provides a high performance key/value storage mechanism that stores data to file. A B-tree is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time.
- Full name:
\Hazaar\File\BTree
Constants
Constant | Visibility | Type | Value |
---|---|---|---|
SIZEOF_HEADER | public | 6 | |
HEADER | public | "\xffbtree" | |
NODE_SLOTS | public | 16 | |
SIZEOF_INT | public | 4 | |
KVNODE | public | 'kv' | |
KPNODE | public | 'kp' | |
NODECHACHE_SIZE | public | 64 |
Properties
file
The file resource.
private ?\Hazaar\File $file
nodecache
private array $nodecache
readOnly
private bool $readOnly
Methods
__construct
Use static method open() to get instance.
public __construct(\Hazaar\File|string $file, bool $readOnly = false): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$file | \Hazaar\File|string | |
$readOnly | bool |
open
Open the file for access.
public open(null|\Hazaar\File|string $file = null, bool $readOnly = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$file | null|\Hazaar\File|string | |
$readOnly | bool |
close
Close the file.
public close(): bool
resetBTreeFile
The the B-Tree source file.
public resetBTreeFile(): bool
get
Get value by key.
public get(string $key): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The key to return data for |
range
Get all values where startkey <= key < endkey.
public range(string $startkey, string $endkey): array
To get all data, use:
$values = $btree->range("\x00", "\xff");
Parameters:
Parameter | Type | Description |
---|---|---|
$startkey | string | |
$endkey | string |
set
Set value under given key.
public set(string $key, mixed $value): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key to store the value under |
$value | mixed | The value to store. A NULL value deletes given key. |
remove
public remove(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
has
Check if a given key exists in the database.
public has(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
keys
Get a list of all available keys in the database.
public keys(): string[]
Warning: Unlike a search this will hit almost every part of the database file and can be a bit slow.
Return Value:
An array of available keys
leaves
Get positions of all leaves.
public leaves(): int[]
Return Value:
pointers to leaves; null on failure
compact
Remove old nodes from BTree file.
public compact(): bool
Return Value:
true if everything went well; false otherwise
dropCache
public dropCache(): void
toArray
public toArray(): array
lookup
Look up a key.
private lookup(string $key, ?string $nodeType = null, array $node = null): array|bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | |
$nodeType | ?string | |
$node | array |
Return Value:
traversed nodes
leafHunt
Find positions of all leaves.
private leafHunt(int $p): int[]
Parameters:
Parameter | Type | Description |
---|---|---|
$p | int | pointer to node |
Return Value:
pairs of (leaf, pointer); null on failure
copyTo
Copy node from opened file to another.
private copyTo(\Hazaar\File $to, ?string $nodeType = null, array $node = null): int
Parameters:
Parameter | Type | Description |
---|---|---|
$to | \Hazaar\File | The file to copy everything to |
$nodeType | ?string | |
$node | array |
Return Value:
new pointer to copied node;
root
Get root node.
private root(): array
Return Value:
0 => node type, 1 => node; [null, null] on failure
rootHunt
Try to get position of root.
private rootHunt(): int
Return Value:
pointer to root; null on failure
node
Get node.
private node(int $p): array
Parameters:
Parameter | Type | Description |
---|---|---|
$p | int | Pointer to node (offset in file) |
Return Value:
0 => node type, 1 => node; [null, null] on failure
serialize
Serialize node.
private static serialize(string $type, array $node): string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | node type |
$node | array | node data |
unserialize
Unserialize node.
private static unserialize(string $str): array
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$str | string | serialized node |
header
Writes header to file.
private static header(\Hazaar\File $file, int $root): bool
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$file | \Hazaar\File | The file to write the header ro |
$root | int | root position |
Automatically generated on 2024-11-14