Btree 集合索引

0.1.0 2022-02-09 12:23 UTC

This package is auto-updated.

Last update: 2024-09-09 18:38:54 UTC


README

Latest Version Software License codecov Codecov Total Downloads

为对象集合提供 btree-indexation。提供排序、排序和复合索引。支持 PSR12 写入

安装

通过 Composer

$ composer require assassin215k/btree

创建集合和索引

使用具有相同公共属性的同一对象。

use Btree\IndexedCollection;

$collection = new IndexedCollection($data);
$collection->addIndex(['name', 'age']);

可以使用多个索引用于不同属性

$collection = new IndexedCollection(data: $data);
$collection->addIndex(['name', 'age']);
$collection->addIndex(['name']);
$collection->addIndex('age');

使用自己的 Builder 和/或 Index,实现 BuilderInterface 和/或 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
]);

配置默认 btree 索引的度数,默认为 100

use Btree\Index\Btree\Index;

Index::$nodeSize = 10;
$collection = new IndexedCollection();

使用实现 IndexInterface 的自定义索引类

class OwnIndex implements IndexInterface {}

$collection = new IndexedCollection(data: []);
$collection->addIndex('name', new OwnIndex());

删除索引

$collection->dropIndex(['name', 'age']);
$collection->dropIndex('name');

添加到集合

在创建集合后添加项目到集合中

$collection = new IndexedCollection();
$collection->addIndex(['name', 'age']);
$collection->add(new SomeClass('Sofia', 18));

从集合中删除

在创建集合后添加项目到集合中

$collection->delete(['name' => 'Sofia", 'age' => 18]);

$person = new SomeClass('Sofia', 18);
$collection = new IndexedCollection();
$collection->add($person);
..
$collection->delete($person);

Builder

每个 Builder 用于一个查询

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

如果集合有多个索引,Builder 仅使用其中最适合搜索的一个

使用 andWhere 添加新的比较或使用 where 从头开始。

$builder = $collection->createBuilder();

$builder->andWhere('name', EnumOperator::IsNull);

$builder->where('country', 'name', EnumOperator::Between, ['A','Z']);
// will search all in A..Z

类似地,使用 addOrder 添加下一个排序或使用 order 替换所有之前的排序为一个新排序。

测试

$ phpunit

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件 info@iceorb.com.ua 而不是使用问题跟踪器。

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件