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-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 #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 <set>
54 #include <string>
55 
56 namespace psync::detail {
57 
59 {
60 public:
61  int32_t count;
62  uint32_t keySum;
63  uint32_t keyCheck;
64 
65  bool
66  isPure() const;
67 
68  bool
69  isEmpty() const;
70 };
71 
72 inline constexpr size_t N_HASH = 3;
73 inline constexpr size_t N_HASHCHECK = 11;
74 
80 class IBLT
81 {
82 public:
83  class Error : public std::runtime_error
84  {
85  public:
86  using std::runtime_error::runtime_error;
87  };
88 
95  explicit
96  IBLT(size_t expectedNumEntries, CompressionScheme scheme);
97 
104  void
105  initialize(const ndn::name::Component& ibltName);
106 
107  void
108  insert(uint32_t key);
109 
110  void
111  erase(uint32_t key);
112 
122  bool
123  listEntries(std::set<uint32_t>& positive, std::set<uint32_t>& negative) const;
124 
125  IBLT
126  operator-(const IBLT& other) const;
127 
128  const std::vector<HashTableEntry>&
129  getHashTable() const
130  {
131  return m_hashTable;
132  }
133 
143  void
144  appendToName(ndn::Name& name) const;
145 
155  std::vector<uint32_t>
156  extractValueFromName(const ndn::name::Component& ibltName) const;
157 
158 private:
159  void
160  update(int plusOrMinus, uint32_t key);
161 
162 private:
163  std::vector<HashTableEntry> m_hashTable;
164  static constexpr int INSERT = 1;
165  static constexpr int ERASE = -1;
166  CompressionScheme m_compressionScheme;
167 };
168 
169 bool
170 operator==(const IBLT& iblt1, const IBLT& iblt2);
171 
172 bool
173 operator!=(const IBLT& iblt1, const IBLT& iblt2);
174 
175 std::ostream&
176 operator<<(std::ostream& os, const IBLT& iblt);
177 
178 } // namespace psync::detail
179 
180 #endif // PSYNC_DETAIL_IBLT_HPP
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
constexpr size_t N_HASHCHECK
Definition: iblt.hpp:73
std::ostream & operator<<(std::ostream &os, const IBLT &iblt)
Definition: iblt.cpp:255
bool operator==(const IBLT &iblt1, const IBLT &iblt2)
Definition: iblt.cpp:228
CompressionScheme
Definition: common.hpp:43