Skip to content
Append-only B+Tree implemented purely in PHP.
PHP
Latest commit 14719f7 Mar 29, 2016 @jakubkulhan Merge pull request #2 from yuloh/patch-1
Fix typo
Failed to load latest commit information.
examples Initial commit Aug 21, 2009
README.md License year change Feb 21, 2010
btree.php Fix typo Mar 29, 2016
composer.json Added composer.json Mar 30, 2014

README.md

btree

Append-only B+Tree implemented purely in PHP. Intended to be an efficient key-value storage. B+Tree is a very popular in storage systems (databases) because of its fast retrieval even with millions of records.

API

Get the instance:

$btree = btree::open('my.tree');

btree::open() takes one argument – path to file with B+Tree. If file does not exists, it is created for you. btree::open() returns FALSE in case that anything went wrong, newly created otherwise.

Set (insert, update) a value for some key:

$btree->set('key', 'value');

set() returns FALSE on failure, TRUE otherwise.

Get the value:

$btree->get('key');

If key does not exist or anything goes wrong, get() returns NULL. Value under the key is returned otherwise.

Delete key:

$btree->set('key', NULL);

NULLs (deletes) the key. Same return values as in case of inserting/updating value.

That is all you need.

Advanced

Range searches

$btree->range("startkey", "endkey");

And you'll get array of values whose key is greater than or equal to "startkey" and less than "endkey".

Get all values

range() (assuming you do not have keys like "\xff\xff...") should do it:

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

Or get pointers to all leaf nodes:

$leaves = $btree->leaves();

And then process nodes:

$values = array();
foreach ($leaves as $p) {
    list(,$leaf) = $btree->node($leaf);
    $values += $leaf;
}

Using leaves() is depracted.

Compaction

Does your B+Tree consume a lot of space? Then compact it:

$btree->compact();

License

The MIT license:

Copyright (c) 2009-2010 Jakub Kulhan <jakub.kulhan@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.