Skip to content

Commit 7ab294f

Browse files
committed
top-k implementation and tests
1 parent 516fdad commit 7ab294f

26 files changed

+897
-27
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ Then the tests will run inside `phpredis-bloom` docker service container and fin
203203

204204
## Examples
205205
- [Factory](https://github.com/averias/phpredis-bloom/blob/master/examples/factory.php)
206-
- [Scan](https://github.com/averias/phpredis-bloom/blob/master/examples/scan.php)
206+
- [BF copy](https://github.com/averias/phpredis-bloom/blob/master/examples/bloom-filter-copy.php)
207+
- [CMS increment](https://github.com/averias/phpredis-bloom/blob/master/examples/count-min-sketch-increment-by.php)
208+
- [CMS Merge](https://github.com/averias/phpredis-bloom/blob/master/examples/count-min-sketch-merge.php)
207209

208210
## License
209211
Phpredis-Bloom code is distributed under MIT license, see [LICENSE](https://github.com/averias/phpredis-bloom/blob/master/LICENSE)
File renamed without changes.

src/RedisBloomClient/Command/BloomCommandTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
use Averias\RedisBloom\Command\BloomFilter\BloomFilterCommandTrait;
1616
use Averias\RedisBloom\Command\CountMinSketch\CountMinSketchCommandTrait;
1717
use Averias\RedisBloom\Command\CuckooFilter\CuckooFilterCommandTrait;
18+
use Averias\RedisBloom\Command\TopK\TopKCommandTrait;
1819

1920
trait BloomCommandTrait
2021
{
2122
use BloomFilterCommandTrait;
2223
use CuckooFilterCommandTrait;
2324
use CountMinSketchCommandTrait;
25+
use TopKCommandTrait;
2426
}

src/RedisBloomClient/Command/BloomCommandTraitInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
use Averias\RedisBloom\Command\BloomFilter\BloomFilterCommandTraitInterface;
1616
use Averias\RedisBloom\Command\CountMinSketch\CountMinSketchCommandTraitInterface;
1717
use Averias\RedisBloom\Command\CuckooFilter\CuckooFilterCommandTraitInterface;
18+
use Averias\RedisBloom\Command\TopK\TopKCommandTraitInterface;
1819

1920
interface BloomCommandTraitInterface extends
2021
BloomFilterCommandTraitInterface,
2122
CuckooFilterCommandTraitInterface,
22-
CountMinSketchCommandTraitInterface
23+
CountMinSketchCommandTraitInterface,
24+
TopKCommandTraitInterface
2325
{
2426

2527
}

src/RedisBloomClient/Command/CountMinSketch/CountMinSketchCommandTrait.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,7 @@ public function countMinSketchInitByProb(string $key, float $errorRate, float $p
5555
*/
5656
public function countMinSketchIncrementBy(string $key, ...$itemsIncrease): bool
5757
{
58-
$this->validateEvenArrayDimension(
59-
$itemsIncrease,
60-
sprintf("item/increment params for %s", BloomCommands::CMS_INCRBY)
61-
);
62-
63-
$itemName = '';
64-
foreach ($itemsIncrease as $index => $item) {
65-
if ($index % 2 == 0) {
66-
$this->validateScalar($item, sprintf("%s params", BloomCommands::CMS_INCRBY));
67-
$itemName = $item;
68-
continue;
69-
}
70-
$this->validateInteger($item, $itemName);
71-
}
72-
58+
$this->validateIncrementByItemsIncrease($itemsIncrease, BloomCommands::CMS_INCRBY);
7359
return $this->executeBloomCommand(BloomCommands::CMS_INCRBY, $key, $itemsIncrease);
7460
}
7561

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <rafa.campoy@gmail.com>
5+
* @copyright 2019 Rafael Campoy <rafa.campoy@gmail.com>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\Command\TopK;
14+
15+
use Averias\RedisBloom\Enum\BloomCommands;
16+
17+
trait TopKCommandTrait
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function topKReserve(string $key, int $topK, int $width, int $depth, float $decay): bool
23+
{
24+
$arguments = [$topK, $width, $depth, $decay];
25+
return $this->executeBloomCommand(BloomCommands::TOPK_RESERVE, $key, $arguments);
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function topKAdd(string $key, ...$items): array
32+
{
33+
$this->validateArrayOfScalars($items, sprintf("%s params", BloomCommands::TOPK_ADD));
34+
return $this->executeBloomCommand(BloomCommands::TOPK_ADD, $key, $items);
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function topKIncrementBy(string $key, ...$itemsIncrease): array
41+
{
42+
$this->validateIncrementByItemsIncrease($itemsIncrease, BloomCommands::TOPK_INCRBY);
43+
return $this->executeBloomCommand(BloomCommands::TOPK_INCRBY, $key, $itemsIncrease);
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function topKQuery(string $key, ...$items): array
50+
{
51+
$this->validateArrayOfScalars($items, sprintf("%s params", BloomCommands::TOPK_QUERY));
52+
return $this->executeBloomCommand(BloomCommands::TOPK_QUERY, $key, $items);
53+
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
public function topKCount(string $key, ...$items): array
59+
{
60+
$this->validateArrayOfScalars($items, sprintf("%s params", BloomCommands::TOPK_COUNT));
61+
return $this->executeBloomCommand(BloomCommands::TOPK_COUNT, $key, $items);
62+
}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
public function topKList(string $key): array
68+
{
69+
return $this->executeBloomCommand(BloomCommands::TOPK_LIST, $key);
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public function topKInfo(string $key): array
76+
{
77+
return $this->executeBloomCommand(BloomCommands::TOPK_INFO, $key);
78+
}
79+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <rafa.campoy@gmail.com>
5+
* @copyright 2019 Rafael Campoy <rafa.campoy@gmail.com>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\Command\TopK;
14+
15+
interface TopKCommandTraitInterface
16+
{
17+
/**
18+
* @param string $key
19+
* @param int $topK
20+
* @param int $width
21+
* @param int $depth
22+
* @param float $decay
23+
* @return bool
24+
*/
25+
public function topKReserve(string $key, int $topK, int $width, int $depth, float $decay): bool;
26+
27+
/**
28+
* @param string $key
29+
* @param mixed ...$items
30+
* @return array
31+
*/
32+
public function topKAdd(string $key, ...$items): array;
33+
34+
/**
35+
* @param string $key
36+
* @param mixed ...$itemsIncrease
37+
* @return array
38+
*/
39+
public function topKIncrementBy(string $key, ...$itemsIncrease): array;
40+
41+
/**
42+
* @param string $key
43+
* @param mixed ...$items
44+
* @return array
45+
*/
46+
public function topKQuery(string $key, ...$items): array;
47+
48+
/**
49+
* @param string $key
50+
* @param mixed ...$items
51+
* @return array
52+
*/
53+
public function topKCount(string $key, ...$items): array;
54+
55+
/**
56+
* @param string $key
57+
* @return array
58+
*/
59+
public function topKList(string $key): array;
60+
61+
/**
62+
* @param string $key
63+
* @return array
64+
*/
65+
public function topKInfo(string $key): array;
66+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <rafa.campoy@gmail.com>
5+
* @copyright 2019 Rafael Campoy <rafa.campoy@gmail.com>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\DataTypes;
14+
15+
use Averias\RedisBloom\Command\TopK\TopKCommandTrait;
16+
17+
class TopK extends BaseDataType implements TopKInterface
18+
{
19+
use TopKCommandTrait;
20+
21+
/**
22+
* @inheritDoc
23+
*/
24+
public function reserve(int $topK, int $width, int $depth, float $decay): bool
25+
{
26+
return $this->topKReserve($this->name, $topK, $width, $depth, $decay);
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function add(...$items): array
33+
{
34+
return $this->topKAdd($this->name, ...$items);
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function incrementBy(...$itemsIncrease): array
41+
{
42+
return $this->topKIncrementBy($this->name, ...$itemsIncrease);
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function query(...$items): array
49+
{
50+
return $this->topKQuery($this->name, ...$items);
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function count(...$items): array
57+
{
58+
return $this->topKCount($this->name, ...$items);
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function list(): array
65+
{
66+
return $this->topKList($this->name);
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
public function info(): array
73+
{
74+
return $this->topKInfo($this->name);
75+
}
76+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <rafa.campoy@gmail.com>
5+
* @copyright 2019 Rafael Campoy <rafa.campoy@gmail.com>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\DataTypes;
14+
15+
interface TopKInterface extends DataTypeInterface
16+
{
17+
/**
18+
* @param int $topK
19+
* @param int $width
20+
* @param int $depth
21+
* @param float $decay
22+
* @return bool
23+
*/
24+
public function reserve(int $topK, int $width, int $depth, float $decay): bool;
25+
26+
/**
27+
* @param mixed ...$items
28+
* @return array
29+
*/
30+
public function add(...$items): array;
31+
32+
/**
33+
* @param mixed ...$itemsIncrease
34+
* @return array
35+
*/
36+
public function incrementBy(...$itemsIncrease): array;
37+
38+
/**
39+
* @param mixed ...$items
40+
* @return array
41+
*/
42+
public function query(...$items): array;
43+
44+
/**
45+
* @param mixed ...$items
46+
* @return array
47+
*/
48+
public function count(...$items): array;
49+
50+
/**
51+
* @return array
52+
*/
53+
public function list(): array;
54+
55+
/**
56+
* @return array
57+
*/
58+
public function info(): array;
59+
}

0 commit comments

Comments
 (0)