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.
Constants
SIZEOF_HEADER
public const SIZEOF_HEADER = 6
HEADER
public const HEADER = '"\xffbtree"'
NODE_SLOTS
public const NODE_SLOTS = 16
SIZEOF_INT
public const SIZEOF_INT = 4
KVNODE
public const KVNODE = 'kv'
KPNODE
public const KPNODE = 'kp'
NODECHACHE_SIZE
public const NODECHACHE_SIZE = 64
Properties
file
public File $file
nodecache
private array $nodecache
readOnly
private bool $readOnly
Methods
__construct
Use static method open() to get instance.
public __construct(string $file, bool $readOnly = false): void
Parameters
Parameter | Type | Description |
---|---|---|
$file | string | |
$readOnly | bool |
open
Open the file for access.
public open(string $file, bool $readOnly = false): bool
Parameters
Parameter | Type | Description |
---|---|---|
$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): void
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): void
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(): void
Warning: Unlike a search this will hit almost every part of the database file and can be a bit slow.
leaves
Get positions of all leaves.
public leaves(): void
compact
Remove old nodes from BTree file.
public compact(): void
dropCache
public dropCache(): void
toArray
public toArray(): void
lookup
Look up a key.
private lookup(string $key, ?string $nodeType, ?array $node, bool $): void
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | |
$nodeType | string | |
$node | array | |
$ | bool |
leafHunt
Find positions of all leaves.
private leafHunt(int $p): void
Parameters
Parameter | Type | Description |
---|---|---|
$p | int | pointer to node |
copyTo
Copy node from opened file to another.
private copyTo(File $to, ?string $nodeType, ?array $node): ?int
Parameters
Parameter | Type | Description |
---|---|---|
$to | File | The file to copy everything to |
$nodeType | string | |
$node | array |
root
Get root node.
private root(): void
rootHunt
Try to get position of root.
private rootHunt(): ?int
node
Get node.
private node(int $p): void
Parameters
Parameter | Type | Description |
---|---|---|
$p | int | Pointer to node (offset in file) |
serialize
private serialize(string $type, array $node): string
Parameters
Parameter | Type | Description |
---|---|---|
$type | string | |
$node | array |
unserialize
private unserialize(string $str): void
Parameters
Parameter | Type | Description |
---|---|---|
$str | string |
header
private header(File $file, $root): void
Parameters
Parameter | Type | Description |
---|---|---|
$file | File | |
$root | `````` |
Generated by Hazaar API Doc Generator