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-2022, 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 #include "PSync/detail/util.hpp"
48 
49 #include <ndn-cxx/util/exception.hpp>
50 
51 namespace psync::detail {
52 
53 namespace be = boost::endian;
54 
55 bool
57 {
58  if (count == 1 || count == -1) {
59  uint32_t check = murmurHash3(N_HASHCHECK, keySum);
60  return keyCheck == check;
61  }
62 
63  return false;
64 }
65 
66 bool
68 {
69  return count == 0 && keySum == 0 && keyCheck == 0;
70 }
71 
72 IBLT::IBLT(size_t expectedNumEntries, CompressionScheme scheme)
73  : m_compressionScheme(scheme)
74 {
75  // 1.5x expectedNumEntries gives very low probability of decoding failure
76  size_t nEntries = expectedNumEntries + expectedNumEntries / 2;
77  // make nEntries exactly divisible by N_HASH
78  size_t remainder = nEntries % N_HASH;
79  if (remainder != 0) {
80  nEntries += (N_HASH - remainder);
81  }
82 
83  m_hashTable.resize(nEntries);
84 }
85 
86 void
87 IBLT::initialize(const ndn::name::Component& ibltName)
88 {
89  const auto& values = extractValueFromName(ibltName);
90 
91  if (3 * m_hashTable.size() != values.size()) {
92  NDN_THROW(Error("Received IBF cannot be decoded!"));
93  }
94 
95  for (size_t i = 0; i < m_hashTable.size(); i++) {
96  HashTableEntry& entry = m_hashTable.at(i);
97  if (values[i * 3] != 0) {
98  entry.count = values[i * 3];
99  entry.keySum = values[(i * 3) + 1];
100  entry.keyCheck = values[(i * 3) + 2];
101  }
102  }
103 }
104 
105 void
106 IBLT::update(int plusOrMinus, uint32_t key)
107 {
108  size_t bucketsPerHash = m_hashTable.size() / N_HASH;
109 
110  for (size_t i = 0; i < N_HASH; i++) {
111  size_t startEntry = i * bucketsPerHash;
112  uint32_t h = murmurHash3(i, key);
113  HashTableEntry& entry = m_hashTable.at(startEntry + (h % bucketsPerHash));
114  entry.count += plusOrMinus;
115  entry.keySum ^= key;
116  entry.keyCheck ^= murmurHash3(N_HASHCHECK, key);
117  }
118 }
119 
120 void
121 IBLT::insert(uint32_t key)
122 {
123  update(INSERT, key);
124 }
125 
126 void
127 IBLT::erase(uint32_t key)
128 {
129  update(ERASE, key);
130 }
131 
132 bool
133 IBLT::listEntries(std::set<uint32_t>& positive, std::set<uint32_t>& negative) const
134 {
135  IBLT peeled = *this;
136 
137  size_t nErased = 0;
138  do {
139  nErased = 0;
140  for (const auto& entry : peeled.m_hashTable) {
141  if (entry.isPure()) {
142  if (entry.count == 1) {
143  positive.insert(entry.keySum);
144  }
145  else {
146  negative.insert(entry.keySum);
147  }
148  peeled.update(-entry.count, entry.keySum);
149  ++nErased;
150  }
151  }
152  } while (nErased > 0);
153 
154  // If any buckets for one of the hash functions is not empty,
155  // then we didn't peel them all:
156  for (const auto& entry : peeled.m_hashTable) {
157  if (!entry.isEmpty()) {
158  return false;
159  }
160  }
161 
162  return true;
163 }
164 
165 IBLT
166 IBLT::operator-(const IBLT& other) const
167 {
168  BOOST_ASSERT(m_hashTable.size() == other.m_hashTable.size());
169 
170  IBLT result(*this);
171  for (size_t i = 0; i < m_hashTable.size(); i++) {
172  HashTableEntry& e1 = result.m_hashTable.at(i);
173  const HashTableEntry& e2 = other.m_hashTable.at(i);
174  e1.count -= e2.count;
175  e1.keySum ^= e2.keySum;
176  e1.keyCheck ^= e2.keyCheck;
177  }
178 
179  return result;
180 }
181 
182 void
183 IBLT::appendToName(ndn::Name& name) const
184 {
185  constexpr size_t unitSize = sizeof(m_hashTable[0].count) +
186  sizeof(m_hashTable[0].keySum) +
187  sizeof(m_hashTable[0].keyCheck);
188 
189  size_t tableSize = unitSize * m_hashTable.size();
190  std::vector<uint8_t> table(tableSize);
191 
192  for (size_t i = 0; i < m_hashTable.size(); i++) {
193  uint32_t count = be::native_to_big(static_cast<uint32_t>(m_hashTable[i].count));
194  uint32_t keySum = be::native_to_big(static_cast<uint32_t>(m_hashTable[i].keySum));
195  uint32_t keyCheck = be::native_to_big(static_cast<uint32_t>(m_hashTable[i].keyCheck));
196 
197  std::memcpy(&table[i * unitSize], &count, sizeof(count));
198  std::memcpy(&table[(i * unitSize) + 4], &keySum, sizeof(keySum));
199  std::memcpy(&table[(i * unitSize) + 8], &keyCheck, sizeof(keyCheck));
200  }
201 
202  auto compressed = compress(m_compressionScheme, table);
203  name.append(ndn::name::Component(std::move(compressed)));
204 }
205 
206 std::vector<uint32_t>
207 IBLT::extractValueFromName(const ndn::name::Component& ibltName) const
208 {
209  auto decompressedBuf = decompress(m_compressionScheme, ibltName.value_bytes());
210 
211  if (decompressedBuf->size() % 4 != 0) {
212  NDN_THROW(Error("Received IBF cannot be decompressed correctly!"));
213  }
214 
215  size_t n = decompressedBuf->size() / 4;
216  std::vector<uint32_t> values(n, 0);
217 
218  for (size_t i = 0; i < n; i++) {
219  uint32_t t;
220  std::memcpy(&t, &(*decompressedBuf)[i * 4], sizeof(t));
221  values[i] = be::big_to_native(t);
222  }
223 
224  return values;
225 }
226 
227 bool
228 operator==(const IBLT& iblt1, const IBLT& iblt2)
229 {
230  auto iblt1HashTable = iblt1.getHashTable();
231  auto iblt2HashTable = iblt2.getHashTable();
232  if (iblt1HashTable.size() != iblt2HashTable.size()) {
233  return false;
234  }
235 
236  size_t N = iblt1HashTable.size();
237 
238  for (size_t i = 0; i < N; i++) {
239  if (iblt1HashTable[i].count != iblt2HashTable[i].count ||
240  iblt1HashTable[i].keySum != iblt2HashTable[i].keySum ||
241  iblt1HashTable[i].keyCheck != iblt2HashTable[i].keyCheck)
242  return false;
243  }
244 
245  return true;
246 }
247 
248 bool
249 operator!=(const IBLT& iblt1, const IBLT& iblt2)
250 {
251  return !(iblt1 == iblt2);
252 }
253 
254 std::ostream&
255 operator<<(std::ostream& os, const IBLT& iblt)
256 {
257  os << "count keySum keyCheckMatch\n";
258  for (const auto& entry : iblt.getHashTable()) {
259  os << entry.count << " " << entry.keySum << " "
260  << ((entry.isEmpty() || murmurHash3(N_HASHCHECK, entry.keySum) == entry.keyCheck) ? "true" : "false")
261  << "\n";
262  }
263  return os;
264 }
265 
266 } // namespace psync::detail
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:81
bool listEntries(std::set< uint32_t > &positive, std::set< uint32_t > &negative) const
List all the entries in the IBLT.
Definition: iblt.cpp:133
std::vector< uint32_t > extractValueFromName(const ndn::name::Component &ibltName) const
Extracts IBLT from name component.
Definition: iblt.cpp:207
IBLT operator-(const IBLT &other) const
Definition: iblt.cpp:166
IBLT(size_t expectedNumEntries, CompressionScheme scheme)
constructor
Definition: iblt.cpp:72
void insert(uint32_t key)
Definition: iblt.cpp:121
const std::vector< HashTableEntry > & getHashTable() const
Definition: iblt.hpp:129
void initialize(const ndn::name::Component &ibltName)
Populate the hash table using the vector representation of IBLT.
Definition: iblt.cpp:87
void appendToName(ndn::Name &name) const
Appends self to name.
Definition: iblt.cpp:183
void erase(uint32_t key)
Definition: iblt.cpp:127
bool operator!=(const IBLT &iblt1, const IBLT &iblt2)
Definition: iblt.cpp:249
constexpr size_t N_HASH
Definition: iblt.hpp:72
uint32_t murmurHash3(const void *key, size_t len, uint32_t seed)
Definition: util.cpp:58
constexpr size_t N_HASHCHECK
Definition: iblt.hpp:73
std::ostream & operator<<(std::ostream &os, const IBLT &iblt)
Definition: iblt.cpp:255
std::shared_ptr< ndn::Buffer > compress(CompressionScheme scheme, ndn::span< const uint8_t > buffer)
Definition: util.cpp:124
bool operator==(const IBLT &iblt1, const IBLT &iblt2)
Definition: iblt.cpp:228
std::shared_ptr< ndn::Buffer > decompress(CompressionScheme scheme, ndn::span< const uint8_t > buffer)
Definition: util.cpp:183
CompressionScheme
Definition: common.hpp:43