blob: 495c232505047735cfe4cc808ab6c49421c6bc21 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, 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 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU 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 "iblt.hpp"
47#include "util.hpp"
48
49#include <sstream>
50
51namespace psync {
52
53const size_t N_HASH(3);
54const size_t N_HASHCHECK(11);
55
56bool
57HashTableEntry::isPure() const
58{
59 if (count == 1 || count == -1) {
60 uint32_t check = murmurHash3(N_HASHCHECK, keySum);
61 return keyCheck == check;
62 }
63
64 return false;
65}
66
67bool
68HashTableEntry::isEmpty() const
69{
70 return count == 0 && keySum == 0 && keyCheck == 0;
71}
72
73IBLT::IBLT(size_t expectedNumEntries)
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
86void
87IBLT::initialize(const ndn::name::Component& ibltName)
88{
89 const auto& values = extractValueFromName(ibltName);
90
91 if (3 * m_hashTable.size() != values.size()) {
92 BOOST_THROW_EXCEPTION(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
105void
106IBLT::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
120void
121IBLT::insert(uint32_t key)
122{
123 update(INSERT, key);
124}
125
126void
127IBLT::erase(uint32_t key)
128{
129 update(ERASE, key);
130}
131
132bool
133IBLT::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() != true) {
158 return false;
159 }
160 }
161
162 return true;
163}
164
165IBLT
166IBLT::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
182bool
183operator==(const IBLT& iblt1, const IBLT& iblt2)
184{
185 auto iblt1HashTable = iblt1.getHashTable();
186 auto iblt2HashTable = iblt2.getHashTable();
187 if (iblt1HashTable.size() != iblt2HashTable.size()) {
188 return false;
189 }
190
191 size_t N = iblt1HashTable.size();
192
193 for (size_t i = 0; i < N; i++) {
194 if (iblt1HashTable[i].count != iblt2HashTable[i].count ||
195 iblt1HashTable[i].keySum != iblt2HashTable[i].keySum ||
196 iblt1HashTable[i].keyCheck != iblt2HashTable[i].keyCheck)
197 return false;
198 }
199
200 return true;
201}
202
203bool
204operator!=(const IBLT& iblt1, const IBLT& iblt2)
205{
206 return !(iblt1 == iblt2);
207}
208
209std::ostream&
210operator<<(std::ostream& out, const IBLT& iblt)
211{
212 out << "count keySum keyCheckMatch\n";
213 for (const auto& entry : iblt.getHashTable()) {
214 out << entry.count << " " << entry.keySum << " ";
215 out << ((murmurHash3(N_HASHCHECK, entry.keySum) == entry.keyCheck) ||
216 (entry.isEmpty())? "true" : "false");
217 out << "\n";
218 }
219
220 return out;
221}
222
223void
224IBLT::appendToName(ndn::Name& name) const
225{
226 size_t n = m_hashTable.size();
227 size_t unitSize = (32 * 3) / 8; // hard coding
228 size_t tableSize = unitSize * n;
229
230 std::vector <uint8_t> table(tableSize);
231
232 for (size_t i = 0; i < n; i++) {
233 // table[i*12], table[i*12+1], table[i*12+2], table[i*12+3] --> hashTable[i].count
234
235 table[(i * unitSize)] = 0xFF & m_hashTable[i].count;
236 table[(i * unitSize) + 1] = 0xFF & (m_hashTable[i].count >> 8);
237 table[(i * unitSize) + 2] = 0xFF & (m_hashTable[i].count >> 16);
238 table[(i * unitSize) + 3] = 0xFF & (m_hashTable[i].count >> 24);
239
240 // table[i*12+4], table[i*12+5], table[i*12+6], table[i*12+7] --> hashTable[i].keySum
241
242 table[(i * unitSize) + 4] = 0xFF & m_hashTable[i].keySum;
243 table[(i * unitSize) + 5] = 0xFF & (m_hashTable[i].keySum >> 8);
244 table[(i * unitSize) + 6] = 0xFF & (m_hashTable[i].keySum >> 16);
245 table[(i * unitSize) + 7] = 0xFF & (m_hashTable[i].keySum >> 24);
246
247 // table[i*12+8], table[i*12+9], table[i*12+10], table[i*12+11] --> hashTable[i].keyCheck
248
249 table[(i * unitSize) + 8] = 0xFF & m_hashTable[i].keyCheck;
250 table[(i * unitSize) + 9] = 0xFF & (m_hashTable[i].keyCheck >> 8);
251 table[(i * unitSize) + 10] = 0xFF & (m_hashTable[i].keyCheck >> 16);
252 table[(i * unitSize) + 11] = 0xFF & (m_hashTable[i].keyCheck >> 24);
253 }
254
255 name.append(table.begin(), table.end());
256}
257
258std::vector<uint32_t>
259IBLT::extractValueFromName(const ndn::name::Component& ibltName) const
260{
261 std::vector<uint8_t> ibltValues(ibltName.value_begin(), ibltName.value_end());
262 size_t n = ibltValues.size() / 4;
263
264 std::vector<uint32_t> values(n, 0);
265
266 for (size_t i = 0; i < 4 * n; i += 4) {
267 uint32_t t = (ibltValues[i + 3] << 24) +
268 (ibltValues[i + 2] << 16) +
269 (ibltValues[i + 1] << 8) +
270 ibltValues[i];
271 values[i / 4] = t;
272 }
273
274 return values;
275}
276
277} // namespace psync