iblt.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 #ifndef PSYNC_DETAIL_IBLT_HPP
47 #define PSYNC_DETAIL_IBLT_HPP
48 
49 #include "PSync/common.hpp"
50 
51 #include <ndn-cxx/name.hpp>
52 
53 #include <boost/operators.hpp>
54 
55 #include <set>
56 #include <string>
57 
58 namespace psync::detail {
59 
60 class HashTableEntry : private boost::equality_comparable<HashTableEntry>
61 {
62 public:
63  bool
64  isPure() const;
65 
66  bool
67  isEmpty() const;
68 
69 private: // non-member operators
70  // NOTE: the following "hidden friend" operators are available via
71  // argument-dependent lookup only and must be defined inline.
72  // boost::equality_comparable provides != operator.
73 
74  friend bool
75  operator==(const HashTableEntry& lhs, const HashTableEntry& rhs) noexcept
76  {
77  return lhs.count == rhs.count && lhs.keySum == rhs.keySum && lhs.keyCheck == rhs.keyCheck;
78  }
79 
80 public:
81  int32_t count = 0;
82  uint32_t keySum = 0;
83  uint32_t keyCheck = 0;
84 };
85 
86 inline constexpr size_t N_HASH = 3;
87 inline constexpr size_t N_HASHCHECK = 11;
88 
94 class IBLT : private boost::equality_comparable<IBLT>
95 {
96 public:
97  class Error : public std::runtime_error
98  {
99  public:
100  using std::runtime_error::runtime_error;
101  };
102 
109  explicit
110  IBLT(size_t expectedNumEntries, CompressionScheme scheme);
111 
118  void
119  initialize(const ndn::name::Component& ibltName);
120 
121  void
122  insert(uint32_t key);
123 
124  void
125  erase(uint32_t key);
126 
127  const std::vector<HashTableEntry>&
128  getHashTable() const
129  {
130  return m_hashTable;
131  }
132 
142  void
143  appendToName(ndn::Name& name) const;
144 
145 private: // non-member operators
146  // NOTE: the following "hidden friend" operators are available via
147  // argument-dependent lookup only and must be defined inline.
148  // boost::equality_comparable provides != operator.
149 
150  friend bool
151  operator==(const IBLT& lhs, const IBLT& rhs)
152  {
153  return lhs.m_hashTable == rhs.m_hashTable;
154  }
155 
156 private:
157  std::vector<HashTableEntry> m_hashTable;
158  CompressionScheme m_compressionScheme;
159 };
160 
161 std::ostream&
162 operator<<(std::ostream& os, const IBLT& iblt);
163 
165 struct IBLTDiff
166 {
168  bool canDecode = false;
169 
171  std::set<uint32_t> positive;
172 
174  std::set<uint32_t> negative;
175 };
176 
183 IBLTDiff
184 operator-(const IBLT& lhs, const IBLT& rhs);
185 
186 } // namespace psync::detail
187 
188 #endif // PSYNC_DETAIL_IBLT_HPP
friend bool operator==(const HashTableEntry &lhs, const HashTableEntry &rhs) noexcept
Definition: iblt.hpp:75
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:95
friend bool operator==(const IBLT &lhs, const IBLT &rhs)
Definition: iblt.hpp:151
IBLT(size_t expectedNumEntries, CompressionScheme scheme)
constructor
Definition: iblt.cpp:75
void insert(uint32_t key)
Definition: iblt.cpp:126
const std::vector< HashTableEntry > & getHashTable() const
Definition: iblt.hpp:128
void initialize(const ndn::name::Component &ibltName)
Populate the hash table using the vector representation of IBLT.
Definition: iblt.cpp:90
void appendToName(ndn::Name &name) const
Appends self to name.
Definition: iblt.cpp:138
void erase(uint32_t key)
Definition: iblt.cpp:132
constexpr size_t N_HASH
Definition: iblt.hpp:86
constexpr size_t N_HASHCHECK
Definition: iblt.hpp:87
std::ostream & operator<<(std::ostream &os, const IBLT &iblt)
Definition: iblt.cpp:158
IBLTDiff operator-(const IBLT &lhs, const IBLT &rhs)
Compute the difference between two IBLTs.
Definition: iblt.cpp:170
CompressionScheme
Definition: common.hpp:43
Represent the difference between two IBLTs,.
Definition: iblt.hpp:166
std::set< uint32_t > negative
Entries in rhs but not lhs.
Definition: iblt.hpp:174
bool canDecode
Whether decoding completed successfully.
Definition: iblt.hpp:168
std::set< uint32_t > positive
Entries in lhs but not rhs.
Definition: iblt.hpp:171