\Hazaar
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.

Tags

Since

2.3.17

Summary
Methods Properties Constants
get
has
set
Constants
HEADER
HEADER
Header that has to be at end of every file
KPNODE
KPNODE
This is key-pointer node
KVNODE
KVNODE
This is key-value node
NODECHACHE_SIZE
NODECHACHE_SIZE
Size of node chache
NODE_SLOTS
NODE_SLOTS
Maximum number of keys per node (do not even think about to change it)
SIZEOF_HEADER
SIZEOF_HEADER
Size of header
SIZEOF_INT
SIZEOF_INT
Size of integer (pack type N)
Properties
$file
$file
The file resource
$nodecache
$nodecache
Methods
__construct()
__construct($file)
Use static method open() to get instance

Parameters

$file

No description

compact()
compact() : bool
Remove old nodes from BTree file

Returns

\Hazaar\bool

true if everything went well; false otherwise

copyto()
copyto($to, $node_type = null, $node = null) : int
Copy node from opened file to another

Parameters

$to\Hazaar\File

No description

$node_typestring

No description

$nodearray

No description

Returns

int

new pointer to copied node;

dropCache()
dropCache()
get()
get($key) : mixed
Get value by key

Parameters

$keystring

The key to return data for

Returns

mixed
has()
has($key) : boolean
Check if a given key exists in the database

Parameters

$keymixed

The key to check

Returns

boolean
Static
header()
header($file, $root) : bool
Writes header to file

Parameters

$file\Hazaar\File

The file to write the header ro

$rootint

root position

Returns

\Hazaar\bool
keys()
keys() : array
Get a list of all available keys in the database

Warning: Unlike a search this will hit almost every part of the database file and can be a bit slow.

Returns

array

An array of available keys

leafhunt()
leafhunt($p) : array
Find positions of all leaves

Parameters

$pint

pointer to node

Returns

array

pairs of (leaf, pointer); null on failure

leaves()
leaves() : array
Get positions of all leaves

Returns

array

pointers to leaves; null on failure

lookup()
lookup($key, $node_type = null, $node = null) : array
Look up a key

Parameters

$keystring

The key to lookup

$node_typestring

No description

$nodearray

No description

Returns

array

traversed nodes

node()
node($p)
Get node

Tags

Retrun

array 0 => node type, 1 => node; array(null, null) on failure

Parameters

$pint

Pointer to node (offset in file)

open()
open($file = null) : boolean
Open the file for access

Tags

Throws

\Exception

Parameters

$filemixed

Either a string file name, or a Hazaar\File object.

Returns

boolean
range()
range($startkey, $endkey) : array
Get all values where startkey <= key < endkey

To get all data, use:

$values = $btree->range("\x00", "\xff");

Parameters

$startkeystring

No description

$endkeystring

No description

Returns

array
remove()
remove($key)

Parameters

$key

No description

reset_btree_file()
reset_btree_file() : boolean
The the B-Tree source file.

Returns

boolean
root()
root() : array
Get root node

Returns

array

0 => node type, 1 => node; array(null, null) on failure

roothunt()
roothunt() : int
Try to get position of root

Returns

int

pointer to root; null on failure

Static
serialize()
serialize($type, $node) : string
Serialize node

Parameters

$typestring

node type

$nodearray

No description

Returns

string
set()
set($key, $value) : boolean
Set value under given key

Parameters

$keystring

The key to store the value under.

$valuemixed

The value to store. A NULL value deletes given key.

Returns

boolean
Static
unserialize()
unserialize($str) : array
Unserialize node

Parameters

$strstring

serialized node

Returns

array