nmarniesse / phindexer
PHP 数据索引器
v1.0.0
2019-05-05 15:28 UTC
Requires
- php: >=7.1
- ext-json: *
- doctrine/inflector: ^1.3
- psr/log: ~1.0
- ramsey/uuid: ^3.8
- symfony/validator: ^4.2
Requires (Dev)
- atoum/atoum: *
- fzaninotto/faker: ^1.8
- symfony/console: ^4.2
This package is auto-updated.
Last update: 2024-09-06 02:36:05 UTC
README
PHP 数据索引器,可加快您的数据操作速度。
当您需要从集合中过滤或搜索项目时,操作可能需要时间和精力来检索数据。除非您手动索引数据(例如使用哈希策略),否则操作可能会大幅降低您的程序性能。
该项目帮助您轻松快速地索引和检索数据。
要求
- php 7.1
- ext-json
安装
composer require nmarniesse/phindexer
性能
以下是性能测试。首先我们创建了一个包含 10,000 个项目的集合,然后对其执行了 1000 次搜索。PhindexerJob 使用这个库来索引项目,而 ClassicJob 在每次搜索时迭代项目,不进行索引。结果取决于计算机。
$ php ./tests/Performance/console performance:array:launch 10000 1000
Create fixtures... OK
Start tests with data size [10000] and searches repetition [1000]...
Launch tests... OK
Results
-------
Strategy : NMarniesse\Phindexer\Test\Performance\Job\PhindexerJob
Memory used: 2.147781 MB
Time : 0.042447 seconds
Launch tests... OK
Results
-------
Strategy : NMarniesse\Phindexer\Test\Performance\Job\ClassicJob
Memory used: 0.000359 MB
Time : 11.652977 seconds
文档
在集合上索引和搜索
use NMarniesse\Phindexer\Collection\Collection; $list = [ ['name' => 'A', 'color' => 'green', 'price' => 60], ['name' => 'B', 'color' => 'green', 'price' => 80], ['name' => 'C', 'color' => 'blue', 'price' => 10], ['name' => 'D', 'color' => 'green', 'price' => 40], ['name' => 'E', 'color' => 'red', 'price' => 50], ]; $collection = new Collection($list); $collection->addKeyIndex('color'); $results = $collection->findWhere('color', 'green'); // Results is a new instance of Collection that contains the results foreach ($results as $result) { print_r($result); }
使用自定义索引
如果您想使用更复杂的条件来索引数据,您可以通过 ExpressionIndex 传递您想要的函数。
// Create the custom index $expression_index = new ExpressionIndex(function (array $item) { return $item['color'] === 'green' && $item['price'] <= 50; }); // Add the index in your collection $collection->addExpressionIndex($expression_index); // Search using the index $results = $collection->findWhereExpression($expression_index, true); // Search the items that do not satisfy the expression index $results = $collection->findWhereExpression($expression_index, false);
使用对象集合
Collection 类也可以包含一组对象。您甚至可以混合对象和关联数组。
当通过键索引时,系统会尝试索引属性。它可以是公共属性,也可以是受保护的/private属性,如果存在getter函数。
use NMarniesse\Phindexer\Collection\Collection; use NMarniesse\Phindexer\IndexType\ExpressionIndex; // $list can be an array or an iterator $list = [ new Planet('Earth', 'Solar system'), new Planet('Mars', 'Solar system'), new Planet('Kepler 186-f', 'Kepler 186 system'), ]; $collection = new Collection($list); // Index with property $collection->addKeyIndex('system'); $results = $collection->findWhere('system', 'Solar system'); // Index with custom expression $expression_index = new ExpressionIndex(function (Planet $planet) { return strpos(strtolower($planet->getSystem()), 'solar') !== false; }); $collection->addExpressionIndex($expression_index); $results = $collection->findWhereExpression($expression_index, true);
在集合中添加项目
一旦初始化了集合,您就可以在其中添加项目。您甚至可以在一边创建一个空的集合,在另一边添加项目。添加的项目将以相同的方式进行索引。
以下是一个使用 Collection 的示例,与 Collection 的行为相同。
use NMarniesse\Phindexer\Collection\Collection; $collection = new Collection([]); $collection->addKeyIndex('system'); $collection->addItem(new Planet('Earth', 'Solar system')); $collection->addItem(new Planet('Mars', 'Solar system')); $collection->addItem(new Planet('Kepler 186-f', 'Kepler 186 system')); $results = $collection->findWhere('system', 'Solar system');
验证约束
如果您需要确保项目始终具有正确的结构或相同的类型,您可以在您的集合中指定一些约束。
验证在创建集合时以及添加每个项目时都会进行检查。
use NMarniesse\Phindexer\Collection\Collection; use Symfony\Component\Validator\Constraints as Assert; // Valiation ok. $constraint = new Assert\Type(['type' => Planet::class]); $collection = new Collection([new Planet('Earth', 'Solar system')], $constraint); // Valiation ok. $collection->addItem(new Planet('Mars', 'Solar system')); $collection->addItem(new Planet('Kepler 186-f', 'Kepler 186 system')); // Valiation fails. $collection->addItem(new Star('Sun'));