bloom.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "jlib.hpp"
  15. #include "bloom.hpp"
  16. #include "math.h"
  17. BloomFilter::BloomFilter(unsigned _cardinality, double _probability)
  18. {
  19. unsigned cardinality = _cardinality ? _cardinality : 1;
  20. double probability = _probability >= 0.3 ? 0.3 : (_probability < 0.01 ? 0.01 : _probability);
  21. numBits = rtlRoundUp(-(cardinality*log(probability))/pow(log(2),2));
  22. unsigned tableSize = (numBits + 7) / 8;
  23. numBits = tableSize * 8;
  24. numHashes = round((numBits * log(2))/cardinality);
  25. table = (byte *) calloc(tableSize, 1);
  26. }
  27. BloomFilter::BloomFilter(unsigned _numHashes, unsigned _tableSize, byte *_table)
  28. {
  29. numBits = _tableSize * 8;
  30. numHashes = _numHashes;
  31. table = _table; // Note - takes ownership
  32. }
  33. BloomFilter::~BloomFilter()
  34. {
  35. free(table);
  36. }
  37. void BloomFilter::add(hash64_t hash)
  38. {
  39. uint32_t hash1 = hash >> 32;
  40. uint32_t hash2 = hash & 0xffffffff;
  41. for (unsigned i=0; i < numHashes; i++)
  42. {
  43. // Kirsch and Mitzenmacher technique (Harvard U)
  44. uint64_t bit = (hash1 + (i * hash2)) % numBits;
  45. uint64_t slot = bit / 8;
  46. unsigned shift = bit % 8;
  47. unsigned mask = 1 << shift;
  48. table[slot] |= mask;
  49. }
  50. }
  51. bool BloomFilter::test(hash64_t hash) const
  52. {
  53. uint32_t hash1 = hash >> 32;
  54. uint32_t hash2 = hash & 0xffffffff;
  55. for (unsigned i=0; i < numHashes; i++)
  56. {
  57. // Kirsch and Mitzenmacher technique (Harvard U)
  58. uint64_t bit = (hash1 + (i * hash2)) % numBits;
  59. uint64_t slot = bit / 8;
  60. unsigned shift = bit % 8;
  61. unsigned mask = 1 << shift;
  62. if (!(table[slot] & mask))
  63. return false;
  64. }
  65. return true;
  66. }
  67. BloomBuilder::BloomBuilder(unsigned _maxHashes) : maxHashes(_maxHashes)
  68. {
  69. isValid = true;
  70. }
  71. bool BloomBuilder::add(hash64_t val)
  72. {
  73. if (isValid)
  74. {
  75. if (hashes.length()==maxHashes)
  76. {
  77. isValid = false;
  78. hashes.kill();
  79. }
  80. else
  81. hashes.append(val);
  82. }
  83. return isValid;
  84. }
  85. bool BloomBuilder::valid() const
  86. {
  87. return isValid && hashes.length();
  88. }
  89. const BloomFilter * BloomBuilder::build(double probability) const
  90. {
  91. if (!valid())
  92. return nullptr;
  93. BloomFilter *b = new BloomFilter(hashes.length(), probability);
  94. ForEachItemIn(idx, hashes)
  95. {
  96. b->add(hashes.item(idx));
  97. }
  98. return b;
  99. }
  100. #ifdef _USE_CPPUNIT
  101. #include "unittests.hpp"
  102. class BloomTest : public CppUnit::TestFixture
  103. {
  104. CPPUNIT_TEST_SUITE(BloomTest);
  105. CPPUNIT_TEST(testBloom);
  106. CPPUNIT_TEST_SUITE_END();
  107. const unsigned count = 1000000;
  108. void testBloom()
  109. {
  110. BloomBuilder b;
  111. for (unsigned val = 0; val < count; val++)
  112. b.add(rtlHash64Data(sizeof(val), &val, HASH64_INIT));
  113. Owned<const BloomFilter> f = b.build(0.01);
  114. unsigned falsePositives = 0;
  115. unsigned falseNegatives = 0;
  116. unsigned start = usTick();
  117. for (unsigned val = 0; val < count; val++)
  118. {
  119. if (!f->test(rtlHash64Data(sizeof(val), &val, HASH64_INIT)))
  120. falseNegatives++;
  121. if (f->test(rtlHash64Data(sizeof(val), &val, HASH64_INIT+1)))
  122. falsePositives++;
  123. }
  124. unsigned end = usTick();
  125. ASSERT(falseNegatives==0);
  126. DBGLOG("Bloom filter (%d, %d) gave %d false positives (%.02f %%) in %d uSec", f->queryNumHashes(), f->queryTableSize(), falsePositives, (falsePositives * 100.0)/count, end-start);
  127. }
  128. };
  129. CPPUNIT_TEST_SUITE_REGISTRATION( BloomTest );
  130. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BloomTest, "BloomTest" );
  131. #endif