virtual bool KeyMayMatch(const Slice& key, const Slice& bloom_filter) const { const size_t len = bloom_filter.size(); if (len < 2) return false; const char* array = bloom_filter.data(); const size_t bits = (len - 1) * 8; // Use the encoded k so that we can read filters generated by // bloom filters created using different parameters. const size_t k = array[len-1]; if (k > 30) { // Reserved for potentially new encodings for short bloom filters. // Consider it a match. return true; } uint32_t h = BloomHash(key); const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits for (size_t j = 0; j < k; j++) { const uint32_t bitpos = h % bits; if ((array[bitpos/8] & (1 << (bitpos % 8))) == 0) return false; h += delta; } return true; }
bool BloomFilter::Match(const string &key) { uint32_t h = BloomHash(key); const uint32_t delta = (h>>17) | (h<<15); for (size_t i = 0; i < bits_per_key_; i ++) { const uint32_t bitpos = h % bits_; if ((array_[bitpos/8] & (1<<(bitpos%8))) == 0) return false; h += delta; } return true; }
int BloomFilter::AddKey(const string &key) { // Use double-hashing to generate a sequence of hash values. // See analysis in [Kirsch,Mitzenmacher 2006]. uint32_t h = BloomHash(key); const uint32_t delta = (h>>17) | (h<<15); for (size_t i = 0; i < bits_per_key_; i ++) { const uint32_t bitpos = h % bits_; array_[bitpos/8] |= (1<<(bitpos%8)); h += delta; } return 0; }