布隆过滤器 (Bloom Filter) 是1970年由布隆提出的。 它实际上是一个很长的二进制向量和一系列随机映射函数。 布隆过滤器可以用于检索一个元素是否在一个集合中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| <?php
namespace App\Support\BloomFilter;
class BloomFilterHash {
public function JSHash($string, $len = null) { $hash = 1315423911; $len || $len = strlen($string); for ($i = 0; $i < $len; $i++) { $hash ^= (($hash << 5) + ord($string[$i]) + ($hash >> 2)); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function PJWHash($string, $len = null) { $bitsInUnsignedInt = 4 * 8; $threeQuarters = ($bitsInUnsignedInt * 3) / 4; $oneEighth = $bitsInUnsignedInt / 8; $highBits = 0x7FFFFFFF << (int) ($bitsInUnsignedInt - $oneEighth); $hash = 0; $test = 0; $len || $len = strlen($string); for ($i = 0; $i < $len; $i++) { $hash = ($hash << (int) ($oneEighth)) + ord($string[$i]); } $test = $hash & $highBits; if ($test != 0) { $hash = (($hash ^ ($test >> (int)($threeQuarters))) & (~$highBits)); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function ELFHash($string, $len = null) { $hash = 0; $len || $len = strlen($string); for ($i = 0; $i < $len; $i++) { $hash = ($hash << 4) + ord($string[$i]); $x = $hash & 0xF0000000; if ($x != 0) { $hash ^= ($x >> 24); } $hash &= ~$x; }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function BKDRHash($string, $len = null) { $seed = 131; $hash = 0; $len || $len = strlen($string); for ($i=0; $i<$len; $i++) { $hash = (int) (($hash * $seed) + ord($string[$i])); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function SDBMHash($string, $len = null) { $hash = 0; $len || $len = strlen($string); for ($i = 0; $i < $len; $i++) { $hash = (int) (ord($string[$i]) + ($hash << 6) + ($hash << 16) - $hash); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function DJBHash($string, $len = null) { $hash = 5381; $len || $len = strlen($string); for ($i = 0; $i < $len; $i++) { $hash = (int) (($hash << 5) + $hash) + ord($string[$i]); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function DEKHash($string, $len = null) { $len || $len = strlen($string); $hash = $len; for ($i = 0; $i < $len; $i++) { $hash = (($hash << 5) ^ ($hash >> 27)) ^ ord($string[$i]); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; }
public function FNVHash($string, $len = null) { $prime = 16777619; $hash = 2166136261; $len || $len = strlen($string); for ($i = 0; $i < $len; $i++) { $hash = (int) ($hash * $prime) % 0x7FFFFFFF; $hash ^= ord($string[$i]); }
return ($hash % 0x7FFFFFFF) & 0x7FFFFFFF; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <?php
namespace App\Support\BloomFilter;
use App\Support\RedisTool;
abstract class BloomFilterRedis { protected $redis; protected $hash; protected $bucket; protected $hashFunc;
public function __construct() { if (!$this->bucket || !$this->hashFunc) { throw new \Exception("需要定义bucket和hashFunction", 4000); }
$this->hash = new BloomFilterHash(); $this->redis = RedisTool::getInstance(); }
public function add($string) { $pipe = $this->redis->multi(); foreach ($this->hashFunc as $func) { $hash = $this->hash->$func($string); $pipe->setBit($this->bucket, $hash, 1); } return $pipe->exec(); }
public function exists($string) { $pipe = $this->redis->multi(); $len = strlen($string); foreach ($this->hashFunc as $func) { $hash = $this->hash->$func($string, $len); $pipe = $pipe->getBit($this->bucket, $hash); } $res = $pipe->exec(); foreach ($res as $bit) { if ($bit == 0) return false; }
return true; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?php
namespace App\Support\BloomFilter;
class BloomFilter extends BloomFilterRedis {
protected $bucket = 'bfr';
protected $hashFunc = ['BKDRHash', 'SDBMHash', 'JSHAsh']; }
|
1 2 3 4 5 6 7 8 9 10 11
|
public function test_bloom(BloomFilter $filter) { $filter->add('bloom'); dd($filter->exists('bloom')); }
|