ghazali / universal-probabilistic-engine
Universal Probabilistic Engine (UPE) providing O(1) Bloom Filter shielding and high-speed CRUD operations for Laravel.
Package info
github.com/codeWithDevMA/universal-probabilistic-engine
pkg:composer/ghazali/universal-probabilistic-engine
Requires
- php: ^8.2
- illuminate/console: ^10.0|^11.0|^12.0|^13.0
- illuminate/contracts: ^10.0|^11.0|^12.0|^13.0
- illuminate/database: ^10.0|^11.0|^12.0|^13.0
- illuminate/redis: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- fakerphp/faker: ^1.23
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
- laravel/pint: ^1.0
- laravel/tinker: ^2.9|^3.0
- mockery/mockery: ^1.6
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.0|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The Universal Probabilistic Engine (UPE) is a high-performance, developer-friendly Laravel package that provides O(1) Bloom Filter shielding and high-speed CRUD operations. It works universally across databases that lack native Bloom filters (MySQL, SQLite, PostgreSQL) using an optimized bitstring fallback storage system, while seamlessly scaling to native Bloom-capable databases (like Redis) via a unified Driver Manager.
Key Features
- โก O(1) Bloom Filter Shielding: Fast-reject non-existent lookup requests (e.g. searching for invalid emails, usernames, or API keys) before querying the database, preventing DB resource exhaustion.
- โ๏ธ Write-Through Synchronization: All CRUD operations update both the primary database and the Bloom filter synchronously, ensuring zero read-latency penalty.
- ๐ก๏ธ Zero Data Loss & Collision Safety: Handles Bloom filter false positives elegantly. If a key is reported as "might be present" (due to hash collisions), UPE queries the database and verifies existence. Requires a
UNIQUEorINDEXkey on the target column for instant collision resolution. - ๐๏ธ Optimized Bitstring Driver: For MySQL/SQLite/PostgreSQL, UPE manages a memory-efficient bitstring inside a
MEDIUMTEXTcolumn. - ๐ด Redis Native Driver: Scales instantly to Redis utilizing native Bloom filter module commands (
BF.RESERVE,BF.ADD,BF.EXISTS). - ๐งน Drift Self-Healing: Automatically handles database deletions. Since Bloom filters do not support deletion, UPE intercepts "deleted" hits, queries the DB, yields a null result, and logs the occurrence without database corruptions.
- ๐ Artisan Warmup Command: Seed your Bloom filters in chunks from existing database tables with a single command.
How It Works
Mathematical Tuning
A Bloom filter is parameterized by:
- $n$: The expected number of items to insert.
-
$p$: The desired false positive rate (e.g.,
0.01for $1%$).
UPE dynamically calculates the optimal size of the bit array ($m$) and the number of hash functions ($k$): [m = \left\lceil - \frac{n \ln(p)}{(\ln 2)^2} \right\rceil] [k = \max\left(1, \text{round}\left(\frac{m}{n} \ln 2\right)\right)]
These parameters are configured in config/upe.php and applied dynamically by the drivers.
Hashing Strategy
To distribute bits uniformly without the overhead of heavy cryptographic algorithms, UPE uses salted CRC32 hash values combined with modulo arithmetic:
[\text{BitIndex}_i = (\text{crc32}(value \cdot i \cdot salt) \pmod m)]
This produces $k$ distinct, highly uniform bit indices to set/check in the bitstring.
Installation
1. Install via Composer
Install the package directly from Packagist:
composer require ghazali/universal-probabilistic-engine
2. Publish Configuration & Migrations
Publish the package configuration and database migration file to your Laravel application:
php artisan vendor:publish --provider="Ghazali\UniversalProbabilisticEngine\UpeServiceProvider"
3. Run Migrations
Execute the migration to create the bloom_filters table used by the fallback driver:
php artisan migrate
Configuration
The configuration file is published at config/upe.php. You can customize the driver, expected elements, and false positive rates:
return [ // Default driver: 'mysql' or 'redis' 'default_driver' => env('UPE_DRIVER', 'mysql'), // Expected number of records in the Bloom filter 'expected_elements' => (int) env('UPE_EXPECTED_ELEMENTS', 100000), // Allowed False Positive Rate (0.01 = 1% false positive probability) 'false_positive_rate' => (float) env('UPE_FALSE_POSITIVE_RATE', 0.01), // Redis connection configuration 'redis' => [ 'connection' => 'default', ], ];
Seeding the Filter (Warmup Command)
If you already have existing data in a database table, you can seed the Bloom filter using the upe:warmup command. It uses chunkById under the hood to handle millions of records without running out of memory:
php artisan upe:warmup --table=users --column=email
Basic Usage
Instantiate the FastCrudEngine by passing the target database table name and the indexed column name:
use Ghazali\UniversalProbabilisticEngine\FastCrudEngine; // Create engine instance $engine = new FastCrudEngine('users', 'email'); // 1. Create a record (Write-Through) $user = $engine->create([ 'email' => 'alice@example.com', 'name' => 'Alice Smith', 'created_at' => now(), 'updated_at' => now(), ]); // 2. Find a record (O(1) Shielded) // - If the email is NOT in the Bloom Filter, it returns NULL instantly without hitting the DB. // - If the email IS in the Bloom Filter, it queries the DB to fetch the row and resolve false positives. $user = $engine->find('alice@example.com'); if ($user) { echo "User found: " . $user['name']; } // 3. Update a record $engine->update('alice@example.com', [ 'email' => 'alice.new@example.com', 'name' => 'Alice Cooper', ]); // 4. Delete a record // - Deletes from the database. // - Subsequent find() queries for 'alice.new@example.com' will hit the Bloom filter (which still has set bits), // but when UPE queries the database, it receives NULL. UPE safely handles this drift. $engine->delete('alice.new@example.com');
License
This package is open-source software licensed under the MIT license.