pouya1364/probabilistic-bundle
Composer 安装命令:
composer require pouya1364/probabilistic-bundle
包简介
Symfony bundle integration for probabilistic-php — Bloom Filter, Counting Bloom Filter, Cuckoo Filter, Count-Min Sketch, and HyperLogLog as configured services.
README 文档
README
Symfony integration for pouya1364/probabilistic-php. Define Bloom filters, Counting Bloom filters, Cuckoo filters, Count-Min sketches, and HyperLogLogs once in configuration, then access them anywhere in your application as named, pre-configured services.
For the algorithms themselves — accuracy guarantees, memory characteristics, and the maths behind each structure — see the core probabilistic-php library. This bundle is only the framework wiring.
Requirements
- PHP 8.2+
- Symfony 6.4, 7.x, or 8.x
Installation
composer require pouya1364/probabilistic-bundle
If you use Symfony Flex the bundle is registered automatically. Otherwise enable it in config/bundles.php:
return [ // ... ProbabilisticBundle\ProbabilisticBundle::class => ['all' => true], ];
Configuration
Create config/packages/probabilistic.yaml. Every structure type holds any number of independently-sized, named instances — a real app typically needs several Bloom filters for different purposes, each configured differently.
probabilistic: bloom_filters: emails_seen: expected_items: 100000 false_positive_rate: 0.01 counting_bloom_filters: active_sessions: expected_items: 50000 false_positive_rate: 0.01 cuckoo_filters: rate_limited_ips: expected_items: 100000 count_min_sketches: page_view_counts: width: 2000 depth: 5 hyperloglogs: unique_visitors: precision: 14
Configuration reference
| Group | Per-instance keys | Required | Notes |
|---|---|---|---|
bloom_filters |
expected_items (int ≥ 1), false_positive_rate (float) |
both | |
counting_bloom_filters |
expected_items (int ≥ 1), false_positive_rate (float) |
both | supports deletion |
cuckoo_filters |
expected_items (int ≥ 1) |
yes | |
count_min_sketches |
width (int ≥ 1), depth (int ≥ 1) |
both | frequency estimation |
hyperloglogs |
precision (int, 4–18) |
no | defaults to 14 |
Why expected_items and not expectedItems?
The configuration keys are written in snake_case to match Symfony's configuration conventions (the same style as false_positive_rate, framework.http_method_override, and so on). The underlying probabilistic-php factories, however, take camelCase named arguments — BloomFilter::create(expectedItems: …, falsePositiveRate: …).
The bundle bridges the two: it accepts your snake_case YAML and translates each key to its camelCase equivalent internally, right before constructing the structure. You write idiomatic Symfony config; the library still receives exactly the argument names it expects. You never have to think about the conversion — this note exists only so the difference isn't surprising if you compare your YAML against the library's method signatures.
Usage
Option 1 — the registry
Inject ProbabilisticBundle\Registry\ProbabilisticRegistry (autowired) and ask it for instances by name. Each instance is built lazily on first access and reused for the rest of the request.
use ProbabilisticBundle\Registry\ProbabilisticRegistry; final class SignupService { public function __construct( private readonly ProbabilisticRegistry $probabilistic, ) { } public function isProbablyDuplicate(string $email): bool { $seen = $this->probabilistic->bloomFilter('emails_seen'); if ($seen->mightContain($email)) { return true; } $seen->add($email); return false; } }
Available registry methods, each taking the instance name (defaulting to 'default'):
| Method | Returns |
|---|---|
bloomFilter(string $name) |
Probabilistic\BloomFilter |
countingBloomFilter(string $name) |
Probabilistic\CountingBloomFilter |
cuckooFilter(string $name) |
Probabilistic\CuckooFilter |
countMinSketch(string $name) |
Probabilistic\CountMinSketch |
hyperLogLog(string $name) |
Probabilistic\HyperLogLog |
Asking for a name that isn't configured throws ProbabilisticBundle\Registry\UnknownInstanceException.
Option 2 — direct named-service autowiring
Every configured instance is also registered as its own service, named probabilistic.<type>.<name>, so you can wire a single specific structure straight into a service without going through the registry:
use Probabilistic\HyperLogLog; use Symfony\Component\DependencyInjection\Attribute\Autowire; final class VisitorStats { public function __construct( #[Autowire(service: 'probabilistic.hyperloglog.unique_visitors')] private readonly HyperLogLog $uniqueVisitors, ) { } public function record(string $visitorId): void { $this->uniqueVisitors->add($visitorId); } public function estimatedUniqueVisitors(): int { return $this->uniqueVisitors->estimate(); } }
The service ids follow the singular form of each group:
| Configured under | Service id |
|---|---|
bloom_filters |
probabilistic.bloom_filter.<name> |
counting_bloom_filters |
probabilistic.counting_bloom_filter.<name> |
cuckoo_filters |
probabilistic.cuckoo_filter.<name> |
count_min_sketches |
probabilistic.count_min_sketch.<name> |
hyperloglogs |
probabilistic.hyperloglog.<name> |
Both approaches return the same shared instance for a given name within a request, so mixing them is fine.
Console
List everything that is configured, grouped by structure type:
php bin/console probabilistic:list
License
MIT. See LICENSE.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-20