iblt.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, The University of Memphis
4  *
5  * This file is part of PSync.
6  * See AUTHORS.md for complete list of PSync authors and contributors.
7  *
8  * PSync is free software: you can redistribute it and/or modify it under the terms
9  * of the GNU Lesser General Public License as published by the Free Software Foundation,
10  * either version 3 of the License, or (at your option) any later version.
11  *
12  * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  * PURPOSE. See the GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License along with
17  * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18  *
19 
20  * This file incorporates work covered by the following copyright and
21  * permission notice:
22 
23  * The MIT License (MIT)
24 
25  * Copyright (c) 2014 Gavin Andresen
26 
27  * Permission is hereby granted, free of charge, to any person obtaining a copy
28  * of this software and associated documentation files (the "Software"), to deal
29  * in the Software without restriction, including without limitation the rights
30  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31  * copies of the Software, and to permit persons to whom the Software is
32  * furnished to do so, subject to the following conditions:
33 
34  * The above copyright notice and this permission notice shall be included in all
35  * copies or substantial portions of the Software.
36 
37  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43  * SOFTWARE.
44 */
45 
46 #include "PSync/detail/iblt.hpp"
47 
48 namespace psync {
49 
50 const size_t N_HASH(3);
51 const size_t N_HASHCHECK(11);
52 
53 bool
55 {
56  if (count == 1 || count == -1) {
57  uint32_t check = murmurHash3(N_HASHCHECK, keySum);
58  return keyCheck == check;
59  }
60 
61  return false;
62 }
63 
64 bool
66 {
67  return count == 0 && keySum == 0 && keyCheck == 0;
68 }
69 
70 IBLT::IBLT(size_t expectedNumEntries, CompressionScheme scheme)
71  : m_compressionScheme(scheme)
72 {
73  // 1.5x expectedNumEntries gives very low probability of decoding failure
74  size_t nEntries = expectedNumEntries + expectedNumEntries / 2;
75  // make nEntries exactly divisible by N_HASH
76  size_t remainder = nEntries % N_HASH;
77  if (remainder != 0) {
78  nEntries += (N_HASH - remainder);
79  }
80 
81  m_hashTable.resize(nEntries);
82 }
83 
84 void
85 IBLT::initialize(const ndn::name::Component& ibltName)
86 {
87  const auto& values = extractValueFromName(ibltName);
88 
89  if (3 * m_hashTable.size() != values.size()) {
90  BOOST_THROW_EXCEPTION(Error("Received IBF cannot be decoded!"));
91  }
92 
93  for (size_t i = 0; i < m_hashTable.size(); i++) {
94  HashTableEntry& entry = m_hashTable.at(i);
95  if (values[i * 3] != 0) {
96  entry.count = values[i * 3];
97  entry.keySum = values[(i * 3) + 1];
98  entry.keyCheck = values[(i * 3) + 2];
99  }
100  }
101 }
102 
103 void
104 IBLT::update(int plusOrMinus, uint32_t key)
105 {
106  size_t bucketsPerHash = m_hashTable.size() / N_HASH;
107 
108  for (size_t i = 0; i < N_HASH; i++) {
109  size_t startEntry = i * bucketsPerHash;
110  uint32_t h = murmurHash3(i, key);
111  HashTableEntry& entry = m_hashTable.at(startEntry + (h % bucketsPerHash));
112  entry.count += plusOrMinus;
113  entry.keySum ^= key;
114  entry.keyCheck ^= murmurHash3(N_HASHCHECK, key);
115  }
116 }
117 
118 void
119 IBLT::insert(uint32_t key)
120 {
121  update(INSERT, key);
122 }
123 
124 void
125 IBLT::erase(uint32_t key)
126 {
127  update(ERASE, key);
128 }
129 
130 bool
131 IBLT::listEntries(std::set<uint32_t>& positive, std::set<uint32_t>& negative) const
132 {
133  IBLT peeled = *this;
134 
135  size_t nErased = 0;
136  do {
137  nErased = 0;
138  for (const auto& entry : peeled.m_hashTable) {
139  if (entry.isPure()) {
140  if (entry.count == 1) {
141  positive.insert(entry.keySum);
142  }
143  else {
144  negative.insert(entry.keySum);
145  }
146  peeled.update(-entry.count, entry.keySum);
147  ++nErased;
148  }
149  }
150  } while (nErased > 0);
151 
152  // If any buckets for one of the hash functions is not empty,
153  // then we didn't peel them all:
154  for (const auto& entry : peeled.m_hashTable) {
155  if (entry.isEmpty() != true) {
156  return false;
157  }
158  }
159 
160  return true;
161 }
162 
163 IBLT
164 IBLT::operator-(const IBLT& other) const
165 {
166  BOOST_ASSERT(m_hashTable.size() == other.m_hashTable.size());
167 
168  IBLT result(*this);
169  for (size_t i = 0; i < m_hashTable.size(); i++) {
170  HashTableEntry& e1 = result.m_hashTable.at(i);
171  const HashTableEntry& e2 = other.m_hashTable.at(i);
172  e1.count -= e2.count;
173  e1.keySum ^= e2.keySum;
174  e1.keyCheck ^= e2.keyCheck;
175  }
176 
177  return result;
178 }
179 
180 bool
181 operator==(const IBLT& iblt1, const IBLT& iblt2)
182 {
183  auto iblt1HashTable = iblt1.getHashTable();
184  auto iblt2HashTable = iblt2.getHashTable();
185  if (iblt1HashTable.size() != iblt2HashTable.size()) {
186  return false;
187  }
188 
189  size_t N = iblt1HashTable.size();
190 
191  for (size_t i = 0; i < N; i++) {
192  if (iblt1HashTable[i].count != iblt2HashTable[i].count ||
193  iblt1HashTable[i].keySum != iblt2HashTable[i].keySum ||
194  iblt1HashTable[i].keyCheck != iblt2HashTable[i].keyCheck)
195  return false;
196  }
197 
198  return true;
199 }
200 
201 bool
202 operator!=(const IBLT& iblt1, const IBLT& iblt2)
203 {
204  return !(iblt1 == iblt2);
205 }
206 
207 std::ostream&
208 operator<<(std::ostream& out, const IBLT& iblt)
209 {
210  out << "count keySum keyCheckMatch\n";
211  for (const auto& entry : iblt.getHashTable()) {
212  out << entry.count << " " << entry.keySum << " ";
213  out << ((murmurHash3(N_HASHCHECK, entry.keySum) == entry.keyCheck) ||
214  (entry.isEmpty())? "true" : "false");
215  out << "\n";
216  }
217 
218  return out;
219 }
220 
221 void
222 IBLT::appendToName(ndn::Name& name) const
223 {
224  constexpr size_t unitSize = (sizeof(m_hashTable[0].count) +
225  sizeof(m_hashTable[0].keySum) +
226  sizeof(m_hashTable[0].keyCheck));
227 
228  size_t tableSize = unitSize * m_hashTable.size();
229  std::vector<uint8_t> table(tableSize);
230 
231  for (size_t i = 0; i < m_hashTable.size(); i++) {
232  // table[i*12], table[i*12+1], table[i*12+2], table[i*12+3] --> hashTable[i].count
233 
234  table[(i * unitSize)] = 0xFF & m_hashTable[i].count;
235  table[(i * unitSize) + 1] = 0xFF & (m_hashTable[i].count >> 8);
236  table[(i * unitSize) + 2] = 0xFF & (m_hashTable[i].count >> 16);
237  table[(i * unitSize) + 3] = 0xFF & (m_hashTable[i].count >> 24);
238 
239  // table[i*12+4], table[i*12+5], table[i*12+6], table[i*12+7] --> hashTable[i].keySum
240 
241  table[(i * unitSize) + 4] = 0xFF & m_hashTable[i].keySum;
242  table[(i * unitSize) + 5] = 0xFF & (m_hashTable[i].keySum >> 8);
243  table[(i * unitSize) + 6] = 0xFF & (m_hashTable[i].keySum >> 16);
244  table[(i * unitSize) + 7] = 0xFF & (m_hashTable[i].keySum >> 24);
245 
246  // table[i*12+8], table[i*12+9], table[i*12+10], table[i*12+11] --> hashTable[i].keyCheck
247 
248  table[(i * unitSize) + 8] = 0xFF & m_hashTable[i].keyCheck;
249  table[(i * unitSize) + 9] = 0xFF & (m_hashTable[i].keyCheck >> 8);
250  table[(i * unitSize) + 10] = 0xFF & (m_hashTable[i].keyCheck >> 16);
251  table[(i * unitSize) + 11] = 0xFF & (m_hashTable[i].keyCheck >> 24);
252  }
253 
254  auto compressed = compress(m_compressionScheme, table.data(), table.size());
255  name.append(ndn::name::Component(std::move(compressed)));
256 }
257 
258 std::vector<uint32_t>
259 IBLT::extractValueFromName(const ndn::name::Component& ibltName) const
260 {
261  auto decompressedBuf = decompress(m_compressionScheme, ibltName.value(), ibltName.value_size());
262 
263  size_t n = decompressedBuf->size() / 4;
264 
265  std::vector<uint32_t> values(n, 0);
266 
267  for (size_t i = 0; i < 4 * n; i += 4) {
268  uint32_t t = ((*decompressedBuf)[i + 3] << 24) +
269  ((*decompressedBuf)[i + 2] << 16) +
270  ((*decompressedBuf)[i + 1] << 8) +
271  (*decompressedBuf)[i];
272  values[i / 4] = t;
273  }
274 
275  return values;
276 }
277 
278 } // namespace psync
uint32_t keyCheck
Definition: iblt.hpp:65
void initialize(const ndn::name::Component &ibltName)
Populate the hash table using the vector representation of IBLT.
Definition: iblt.cpp:85
uint32_t murmurHash3(uint32_t nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: util.cpp:61
const size_t N_HASH
IBLT(size_t expectedNumEntries, CompressionScheme scheme=CompressionScheme::ZLIB)
constructor
Definition: iblt.cpp:70
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:82
const size_t N_HASHCHECK
bool operator==(const BloomFilter &bf1, const BloomFilter &bf2)
uint32_t keySum
Definition: iblt.hpp:64
bool isEmpty() const
Definition: iblt.cpp:65
const std::vector< HashTableEntry > & getHashTable() const
Definition: iblt.hpp:133
std::shared_ptr< ndn::Buffer > decompress(CompressionScheme scheme, const uint8_t *buffer, size_t bufferSize)
Definition: util.cpp:187
bool operator!=(const IBLT &iblt1, const IBLT &iblt2)
Definition: iblt.cpp:202
void erase(uint32_t key)
Definition: iblt.cpp:125
void insert(uint32_t key)
Definition: iblt.cpp:119
CompressionScheme
Definition: util.hpp:51
std::ostream & operator<<(std::ostream &out, const BloomFilter &bf)
std::shared_ptr< ndn::Buffer > compress(CompressionScheme scheme, const uint8_t *buffer, size_t bufferSize)
Definition: util.cpp:132
bool isPure() const
Definition: iblt.cpp:54
bool listEntries(std::set< uint32_t > &positive, std::set< uint32_t > &negative) const
List all the entries in the IBLT.
Definition: iblt.cpp:131
IBLT operator-(const IBLT &other) const
Definition: iblt.cpp:164
std::vector< uint32_t > extractValueFromName(const ndn::name::Component &ibltName) const
Extracts IBLT from name component.
Definition: iblt.cpp:259
void appendToName(ndn::Name &name) const
Appends self to name.
Definition: iblt.cpp:222