blob: f03797a0c2e30b1728cb489daa0f04ce640bad07 [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#ifndef PSYNC_IBLT_HPP
47#define PSYNC_IBLT_HPP
48
49#include <ndn-cxx/name.hpp>
50
51#include <inttypes.h>
52#include <set>
53#include <vector>
54#include <string>
55
56namespace psync {
57
58class HashTableEntry
59{
60public:
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
72extern const size_t N_HASH;
73extern const size_t N_HASHCHECK;
74
75/**
76 * @brief Invertible Bloom Lookup Table (Invertible Bloom Filter)
77 *
78 * Used by Partial Sync (PartialProducer) and Full Sync (Full Producer)
79 */
80class IBLT
81{
82public:
83 class Error : public std::runtime_error
84 {
85 public:
86 using std::runtime_error::runtime_error;
87 };
88
89 /**
90 * @brief constructor
91 *
92 * @param expectedNumEntries the expected number of entries in the IBLT
93 */
94 explicit IBLT(size_t expectedNumEntries);
95
96 /**
97 * @brief Populate the hash table using the vector representation of IBLT
98 *
99 * @param ibltName the Component representation of IBLT
100 * @throws Error if size of values is not compatible with this IBF
101 */
102 void
103 initialize(const ndn::name::Component& ibltName);
104
105 void
106 insert(uint32_t key);
107
108 void
109 erase(uint32_t key);
110
111 /**
112 * @brief List all the entries in the IBLT
113 *
114 * This is called on a difference of two IBLTs: ownIBLT - rcvdIBLT
115 * Entries listed in positive are in ownIBLT but not in rcvdIBLT
116 * Entries listed in negative are in rcvdIBLT but not in ownIBLT
117 *
118 * @param positive
119 * @param negative
120 * @return true if decoding is complete successfully
121 */
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 std::vector<HashTableEntry>
129 getHashTable() const
130 {
131 return m_hashTable;
132 }
133
134 /**
135 * @brief Appends self to name
136 *
137 * Encodes our hash table from uint32_t vector to uint8_t vector
138 * We create a uin8_t vector 12 times the size of uint32_t vector
139 * We put the first count in first 4 cells, keySum in next 4, and keyCheck in next 4.
140 * Repeat for all the other cells of the hash table.
141 * Then we append this uint8_t vector to the name.
142 *
143 * @param name
144 */
145 void
146 appendToName(ndn::Name& name) const;
147
148 /**
149 * @brief Extracts IBLT from name component
150 *
151 * Converts the name into a uint8_t vector which is then decoded to a
152 * a uint32_t vector.
153 *
154 * @param ibltName IBLT represented as a Name Component
155 * @return a uint32_t vector representing the hash table of the IBLT
156 */
157 std::vector<uint32_t>
158 extractValueFromName(const ndn::name::Component& ibltName) const;
159
160private:
161 void
162 update(int plusOrMinus, uint32_t key);
163
164private:
165 std::vector<HashTableEntry> m_hashTable;
166 static const int INSERT = 1;
167 static const int ERASE = -1;
168};
169
170bool
171operator==(const IBLT& iblt1, const IBLT& iblt2);
172
173bool
174operator!=(const IBLT& iblt1, const IBLT& iblt2);
175
176std::ostream&
177operator<<(std::ostream& out, const IBLT& iblt);
178
179} // namespace psync
180
181#endif // PSYNC_IBLT_HPP