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-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 #ifndef PSYNC_IBLT_HPP
47 #define PSYNC_IBLT_HPP
48 
49 #include "PSync/detail/util.hpp"
50 
51 #include <ndn-cxx/name.hpp>
52 
53 #include <inttypes.h>
54 #include <set>
55 #include <vector>
56 #include <string>
57 
58 namespace psync {
59 
61 {
62 public:
63  int32_t count;
64  uint32_t keySum;
65  uint32_t keyCheck;
66 
67  bool
68  isPure() const;
69 
70  bool
71  isEmpty() const;
72 };
73 
74 extern const size_t N_HASH;
75 extern const size_t N_HASHCHECK;
76 
82 class IBLT
83 {
84 public:
85  class Error : public std::runtime_error
86  {
87  public:
88  using std::runtime_error::runtime_error;
89  };
90 
97  explicit
98  IBLT(size_t expectedNumEntries, CompressionScheme scheme = CompressionScheme::ZLIB);
99 
106  void
107  initialize(const ndn::name::Component& ibltName);
108 
109  void
110  insert(uint32_t key);
111 
112  void
113  erase(uint32_t key);
114 
126  bool
127  listEntries(std::set<uint32_t>& positive, std::set<uint32_t>& negative) const;
128 
129  IBLT
130  operator-(const IBLT& other) const;
131 
132  const std::vector<HashTableEntry>&
133  getHashTable() const
134  {
135  return m_hashTable;
136  }
137 
149  void
150  appendToName(ndn::Name& name) const;
151 
161  std::vector<uint32_t>
162  extractValueFromName(const ndn::name::Component& ibltName) const;
163 
164 private:
165  void
166  update(int plusOrMinus, uint32_t key);
167 
168 private:
169  std::vector<HashTableEntry> m_hashTable;
170  static const int INSERT = 1;
171  static const int ERASE = -1;
172  CompressionScheme m_compressionScheme;
173 };
174 
175 bool
176 operator==(const IBLT& iblt1, const IBLT& iblt2);
177 
178 bool
179 operator!=(const IBLT& iblt1, const IBLT& iblt2);
180 
181 std::ostream&
182 operator<<(std::ostream& out, const IBLT& iblt);
183 
184 } // namespace psync
185 
186 #endif // PSYNC_IBLT_HPP
uint32_t keyCheck
Definition: iblt.hpp:65
const size_t N_HASH
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
bool operator!=(const IBLT &iblt1, const IBLT &iblt2)
Definition: iblt.cpp:202
CompressionScheme
Definition: util.hpp:51
std::ostream & operator<<(std::ostream &out, const BloomFilter &bf)
bool isPure() const
Definition: iblt.cpp:54