blob: fc175237c346985566e9e852416a54783d4bcf9b [file] [log] [blame]
Junxiao Shib660b4c2016-08-06 20:47:44 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif54d0302017-09-07 18:16:38 +00002/*
Davide Pesaventodeb54272022-03-11 18:51:05 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shib660b4c2016-08-06 20:47:44 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "name-tree-hashtable.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/city-hash.hpp"
28#include "common/logger.hpp"
Junxiao Shib660b4c2016-08-06 20:47:44 +000029
30namespace nfd {
31namespace name_tree {
32
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(NameTreeHashtable);
Junxiao Shib660b4c2016-08-06 20:47:44 +000034
35class Hash32
36{
37public:
38 static HashValue
39 compute(const void* buffer, size_t length)
40 {
41 return static_cast<HashValue>(CityHash32(reinterpret_cast<const char*>(buffer), length));
42 }
43};
44
45class Hash64
46{
47public:
48 static HashValue
49 compute(const void* buffer, size_t length)
50 {
51 return static_cast<HashValue>(CityHash64(reinterpret_cast<const char*>(buffer), length));
52 }
53};
54
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040055/**
56 * \brief A type with a `compute()` static method to compute the hash value from a raw buffer
Junxiao Shib660b4c2016-08-06 20:47:44 +000057 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040058using HashFunc = std::conditional_t<(sizeof(HashValue) > 4), Hash64, Hash32>;
Junxiao Shib660b4c2016-08-06 20:47:44 +000059
60HashValue
Junxiao Shi042a3312017-09-15 02:51:20 +000061computeHash(const Name& name, size_t prefixLen)
Junxiao Shib660b4c2016-08-06 20:47:44 +000062{
63 name.wireEncode(); // ensure wire buffer exists
64
65 HashValue h = 0;
Junxiao Shi042a3312017-09-15 02:51:20 +000066 for (size_t i = 0, last = std::min(prefixLen, name.size()); i < last; ++i) {
Junxiao Shib660b4c2016-08-06 20:47:44 +000067 const name::Component& comp = name[i];
Davide Pesaventodeb54272022-03-11 18:51:05 -050068 h ^= HashFunc::compute(comp.data(), comp.size());
Junxiao Shib660b4c2016-08-06 20:47:44 +000069 }
70 return h;
71}
72
73HashSequence
Junxiao Shi042a3312017-09-15 02:51:20 +000074computeHashes(const Name& name, size_t prefixLen)
Junxiao Shib660b4c2016-08-06 20:47:44 +000075{
76 name.wireEncode(); // ensure wire buffer exists
77
Junxiao Shi042a3312017-09-15 02:51:20 +000078 size_t last = std::min(prefixLen, name.size());
Junxiao Shib660b4c2016-08-06 20:47:44 +000079 HashSequence seq;
Junxiao Shif54d0302017-09-07 18:16:38 +000080 seq.reserve(last + 1);
Junxiao Shib660b4c2016-08-06 20:47:44 +000081
82 HashValue h = 0;
83 seq.push_back(h);
84
Junxiao Shif54d0302017-09-07 18:16:38 +000085 for (size_t i = 0; i < last; ++i) {
86 const name::Component& comp = name[i];
Davide Pesaventodeb54272022-03-11 18:51:05 -050087 h ^= HashFunc::compute(comp.data(), comp.size());
Junxiao Shib660b4c2016-08-06 20:47:44 +000088 seq.push_back(h);
89 }
90 return seq;
91}
92
93Node::Node(HashValue h, const Name& name)
94 : hash(h)
95 , prev(nullptr)
96 , next(nullptr)
Junxiao Shi340d5532016-08-13 04:00:35 +000097 , entry(name, this)
Junxiao Shib660b4c2016-08-06 20:47:44 +000098{
99}
100
101Node::~Node()
102{
103 BOOST_ASSERT(prev == nullptr);
104 BOOST_ASSERT(next == nullptr);
105}
106
107Node*
108getNode(const Entry& entry)
109{
110 return entry.m_node;
111}
112
113HashtableOptions::HashtableOptions(size_t size)
114 : initialSize(size)
115 , minSize(size)
116{
117}
118
119Hashtable::Hashtable(const Options& options)
120 : m_options(options)
121 , m_size(0)
122{
123 BOOST_ASSERT(m_options.minSize > 0);
124 BOOST_ASSERT(m_options.initialSize >= m_options.minSize);
125 BOOST_ASSERT(m_options.expandLoadFactor > 0.0);
126 BOOST_ASSERT(m_options.expandLoadFactor <= 1.0);
127 BOOST_ASSERT(m_options.expandFactor > 1.0);
128 BOOST_ASSERT(m_options.shrinkLoadFactor >= 0.0);
129 BOOST_ASSERT(m_options.shrinkLoadFactor < 1.0);
130 BOOST_ASSERT(m_options.shrinkFactor > 0.0);
131 BOOST_ASSERT(m_options.shrinkFactor < 1.0);
132
133 m_buckets.resize(options.initialSize);
134 this->computeThresholds();
135}
136
137Hashtable::~Hashtable()
138{
139 for (size_t i = 0; i < m_buckets.size(); ++i) {
140 foreachNode(m_buckets[i], [] (Node* node) {
141 node->prev = node->next = nullptr;
142 delete node;
143 });
144 }
145}
146
147void
148Hashtable::attach(size_t bucket, Node* node)
149{
150 node->prev = nullptr;
151 node->next = m_buckets[bucket];
152
153 if (node->next != nullptr) {
154 BOOST_ASSERT(node->next->prev == nullptr);
155 node->next->prev = node;
156 }
157
158 m_buckets[bucket] = node;
159}
160
161void
162Hashtable::detach(size_t bucket, Node* node)
163{
164 if (node->prev != nullptr) {
165 BOOST_ASSERT(node->prev->next == node);
166 node->prev->next = node->next;
167 }
168 else {
169 BOOST_ASSERT(m_buckets[bucket] == node);
170 m_buckets[bucket] = node->next;
171 }
172
173 if (node->next != nullptr) {
174 BOOST_ASSERT(node->next->prev == node);
175 node->next->prev = node->prev;
176 }
177
178 node->prev = node->next = nullptr;
179}
180
181std::pair<const Node*, bool>
182Hashtable::findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert)
183{
184 size_t bucket = this->computeBucketIndex(h);
185
186 for (const Node* node = m_buckets[bucket]; node != nullptr; node = node->next) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000187 if (node->hash == h && name.compare(0, prefixLen, node->entry.getName()) == 0) {
Junxiao Shib660b4c2016-08-06 20:47:44 +0000188 NFD_LOG_TRACE("found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket);
189 return {node, false};
190 }
191 }
192
193 if (!allowInsert) {
194 NFD_LOG_TRACE("not-found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket);
195 return {nullptr, false};
196 }
197
198 Node* node = new Node(h, name.getPrefix(prefixLen));
199 this->attach(bucket, node);
Junxiao Shi340d5532016-08-13 04:00:35 +0000200 NFD_LOG_TRACE("insert " << node->entry.getName() << " hash=" << h << " bucket=" << bucket);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000201 ++m_size;
202
203 if (m_size > m_expandThreshold) {
204 this->resize(static_cast<size_t>(m_options.expandFactor * this->getNBuckets()));
205 }
206
207 return {node, true};
208}
209
210const Node*
211Hashtable::find(const Name& name, size_t prefixLen) const
212{
213 HashValue h = computeHash(name, prefixLen);
214 return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, h, false).first;
215}
216
217const Node*
218Hashtable::find(const Name& name, size_t prefixLen, const HashSequence& hashes) const
219{
220 BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen));
221 return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, hashes[prefixLen], false).first;
222}
223
224std::pair<const Node*, bool>
225Hashtable::insert(const Name& name, size_t prefixLen, const HashSequence& hashes)
226{
227 BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen));
228 return this->findOrInsert(name, prefixLen, hashes[prefixLen], true);
229}
230
231void
232Hashtable::erase(Node* node)
233{
234 BOOST_ASSERT(node != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000235 BOOST_ASSERT(node->entry.getParent() == nullptr);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000236
237 size_t bucket = this->computeBucketIndex(node->hash);
Junxiao Shi340d5532016-08-13 04:00:35 +0000238 NFD_LOG_TRACE("erase " << node->entry.getName() << " hash=" << node->hash << " bucket=" << bucket);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000239
240 this->detach(bucket, node);
241 delete node;
242 --m_size;
243
244 if (m_size < m_shrinkThreshold) {
245 size_t newNBuckets = std::max(m_options.minSize,
246 static_cast<size_t>(m_options.shrinkFactor * this->getNBuckets()));
247 this->resize(newNBuckets);
248 }
249}
250
251void
252Hashtable::computeThresholds()
253{
254 m_expandThreshold = static_cast<size_t>(m_options.expandLoadFactor * this->getNBuckets());
255 m_shrinkThreshold = static_cast<size_t>(m_options.shrinkLoadFactor * this->getNBuckets());
256 NFD_LOG_TRACE("thresholds expand=" << m_expandThreshold << " shrink=" << m_shrinkThreshold);
257}
258
259void
260Hashtable::resize(size_t newNBuckets)
261{
262 if (this->getNBuckets() == newNBuckets) {
263 return;
264 }
265 NFD_LOG_DEBUG("resize from=" << this->getNBuckets() << " to=" << newNBuckets);
266
267 std::vector<Node*> oldBuckets;
268 oldBuckets.swap(m_buckets);
269 m_buckets.resize(newNBuckets);
270
271 for (Node* head : oldBuckets) {
272 foreachNode(head, [this] (Node* node) {
273 size_t bucket = this->computeBucketIndex(node->hash);
274 this->attach(bucket, node);
275 });
276 }
277
278 this->computeThresholds();
279}
280
281} // namespace name_tree
282} // namespace nfd