Provides btree-indexation for an object collection. Provide sorting, ordering and composite indexes. Writes with PSR12 support
Via Composer
$ composer require assassin215k/btree
Use objects with same public properties.
use Btree\IndexedCollection;
$collection = new IndexedCollection($data);
$collection->addIndex(['name', 'age']);
Can be used multiple indexes for different properties
$collection = new IndexedCollection(data: $data);
$collection->addIndex(['name', 'age']);
$collection->addIndex(['name']);
$collection->addIndex('age');
Use own Builder and/or Index, that implements BuilderInterface and/or IndexInterface
use Btree\Builder\BuilderInterface;
use Btree\Index\IndexInterface;
class OwnBuilder implements BuilderInterface{};
class OwnIndex implements IndexInterface{};
$collection = new IndexedCollection(options: [
'builderClass' => OwnBuilder:class
'indexClass' => OwnIndex:class
]);
Configure degree of default btree index, 100 by default
use Btree\Index\Btree\Index;
Index::$nodeSize = 10;
$collection = new IndexedCollection();
Use custom index class that implements IndexInterface
class OwnIndex implements IndexInterface {}
$collection = new IndexedCollection(data: []);
$collection->addIndex('name', new OwnIndex());
$collection->dropIndex(['name', 'age']);
$collection->dropIndex('name');
Add items into collection after creating the one
$collection = new IndexedCollection();
$collection->addIndex(['name', 'age']);
$collection->add(new SomeClass('Sofia', 18));
Add items into collection after creating the one
$collection->delete(['name' => 'Sofia", 'age' => 18]);
$person = new SomeClass('Sofia', 18);
$collection = new IndexedCollection();
$collection->add($person);
..
$collection->delete($person);
Each builder use for one query
use Btree\Builder\Enum\EnumOperator;
use Btree\Builder\Enum\EnumSort;
$builder = $collection->createBuilder();
$builder->andWhere('name', EnumOperator::Equal, 'Lisa');
$builder->andWhere('country', EnumOperator::IsNull);
$builder->andWhere('age', EnumOperator::LessThen, 50);
$builder->andWhere('age', EnumOperator::LessThenOrEqual, 50);
$builder->andWhere('age', EnumOperator::GreaterThen, 10);
$builder->andWhere('name', EnumOperator::GreaterThenOrEqual, 'A');
$builder->andWhere('age', EnumOperator::Between, [45, 15]);
$builder->andWhere('name', EnumOperator::Between, ['A','Z']);
$builder->order('age', EnumSort::DESC);
$builder->addOrder('name', EnumSort::ASC);
$builder->run();
// Will return an array of added objects
If a collection has multiple indexes, builder use only the one of them that is better to search
Use andWhere
to add new comparison or where
to use from scratch.
$builder = $collection->createBuilder();
$builder->andWhere('name', EnumOperator::IsNull);
$builder->where('country', 'name', EnumOperator::Between, ['A','Z']);
// will search all in A..Z
Similar with order addOrder
to add next order or order
to replace all previous with a new one.
$ phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email info@iceorb.com.ua instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.