blob: 30e4ca356018e39a926ac8f9c0110896d23ce08f [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
55/** \brief a type with compute static method to compute hash value from a raw buffer
56 */
Junxiao Shif54d0302017-09-07 18:16:38 +000057using HashFunc = std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32>::type;
Junxiao Shib660b4c2016-08-06 20:47:44 +000058
59HashValue
Junxiao Shi042a3312017-09-15 02:51:20 +000060computeHash(const Name& name, size_t prefixLen)
Junxiao Shib660b4c2016-08-06 20:47:44 +000061{
62 name.wireEncode(); // ensure wire buffer exists
63
64 HashValue h = 0;
Junxiao Shi042a3312017-09-15 02:51:20 +000065 for (size_t i = 0, last = std::min(prefixLen, name.size()); i < last; ++i) {
Junxiao Shib660b4c2016-08-06 20:47:44 +000066 const name::Component& comp = name[i];
Davide Pesaventodeb54272022-03-11 18:51:05 -050067 h ^= HashFunc::compute(comp.data(), comp.size());
Junxiao Shib660b4c2016-08-06 20:47:44 +000068 }
69 return h;
70}
71
72HashSequence
Junxiao Shi042a3312017-09-15 02:51:20 +000073computeHashes(const Name& name, size_t prefixLen)
Junxiao Shib660b4c2016-08-06 20:47:44 +000074{
75 name.wireEncode(); // ensure wire buffer exists
76
Junxiao Shi042a3312017-09-15 02:51:20 +000077 size_t last = std::min(prefixLen, name.size());
Junxiao Shib660b4c2016-08-06 20:47:44 +000078 HashSequence seq;
Junxiao Shif54d0302017-09-07 18:16:38 +000079 seq.reserve(last + 1);
Junxiao Shib660b4c2016-08-06 20:47:44 +000080
81 HashValue h = 0;
82 seq.push_back(h);
83
Junxiao Shif54d0302017-09-07 18:16:38 +000084 for (size_t i = 0; i < last; ++i) {
85 const name::Component& comp = name[i];
Davide Pesaventodeb54272022-03-11 18:51:05 -050086 h ^= HashFunc::compute(comp.data(), comp.size());
Junxiao Shib660b4c2016-08-06 20:47:44 +000087 seq.push_back(h);
88 }
89 return seq;
90}
91
92Node::Node(HashValue h, const Name& name)
93 : hash(h)
94 , prev(nullptr)
95 , next(nullptr)
Junxiao Shi340d5532016-08-13 04:00:35 +000096 , entry(name, this)
Junxiao Shib660b4c2016-08-06 20:47:44 +000097{
98}
99
100Node::~Node()
101{
102 BOOST_ASSERT(prev == nullptr);
103 BOOST_ASSERT(next == nullptr);
104}
105
106Node*
107getNode(const Entry& entry)
108{
109 return entry.m_node;
110}
111
112HashtableOptions::HashtableOptions(size_t size)
113 : initialSize(size)
114 , minSize(size)
115{
116}
117
118Hashtable::Hashtable(const Options& options)
119 : m_options(options)
120 , m_size(0)
121{
122 BOOST_ASSERT(m_options.minSize > 0);
123 BOOST_ASSERT(m_options.initialSize >= m_options.minSize);
124 BOOST_ASSERT(m_options.expandLoadFactor > 0.0);
125 BOOST_ASSERT(m_options.expandLoadFactor <= 1.0);
126 BOOST_ASSERT(m_options.expandFactor > 1.0);
127 BOOST_ASSERT(m_options.shrinkLoadFactor >= 0.0);
128 BOOST_ASSERT(m_options.shrinkLoadFactor < 1.0);
129 BOOST_ASSERT(m_options.shrinkFactor > 0.0);
130 BOOST_ASSERT(m_options.shrinkFactor < 1.0);
131
132 m_buckets.resize(options.initialSize);
133 this->computeThresholds();
134}
135
136Hashtable::~Hashtable()
137{
138 for (size_t i = 0; i < m_buckets.size(); ++i) {
139 foreachNode(m_buckets[i], [] (Node* node) {
140 node->prev = node->next = nullptr;
141 delete node;
142 });
143 }
144}
145
146void
147Hashtable::attach(size_t bucket, Node* node)
148{
149 node->prev = nullptr;
150 node->next = m_buckets[bucket];
151
152 if (node->next != nullptr) {
153 BOOST_ASSERT(node->next->prev == nullptr);
154 node->next->prev = node;
155 }
156
157 m_buckets[bucket] = node;
158}
159
160void
161Hashtable::detach(size_t bucket, Node* node)
162{
163 if (node->prev != nullptr) {
164 BOOST_ASSERT(node->prev->next == node);
165 node->prev->next = node->next;
166 }
167 else {
168 BOOST_ASSERT(m_buckets[bucket] == node);
169 m_buckets[bucket] = node->next;
170 }
171
172 if (node->next != nullptr) {
173 BOOST_ASSERT(node->next->prev == node);
174 node->next->prev = node->prev;
175 }
176
177 node->prev = node->next = nullptr;
178}
179
180std::pair<const Node*, bool>
181Hashtable::findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert)
182{
183 size_t bucket = this->computeBucketIndex(h);
184
185 for (const Node* node = m_buckets[bucket]; node != nullptr; node = node->next) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000186 if (node->hash == h && name.compare(0, prefixLen, node->entry.getName()) == 0) {
Junxiao Shib660b4c2016-08-06 20:47:44 +0000187 NFD_LOG_TRACE("found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket);
188 return {node, false};
189 }
190 }
191
192 if (!allowInsert) {
193 NFD_LOG_TRACE("not-found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket);
194 return {nullptr, false};
195 }
196
197 Node* node = new Node(h, name.getPrefix(prefixLen));
198 this->attach(bucket, node);
Junxiao Shi340d5532016-08-13 04:00:35 +0000199 NFD_LOG_TRACE("insert " << node->entry.getName() << " hash=" << h << " bucket=" << bucket);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000200 ++m_size;
201
202 if (m_size > m_expandThreshold) {
203 this->resize(static_cast<size_t>(m_options.expandFactor * this->getNBuckets()));
204 }
205
206 return {node, true};
207}
208
209const Node*
210Hashtable::find(const Name& name, size_t prefixLen) const
211{
212 HashValue h = computeHash(name, prefixLen);
213 return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, h, false).first;
214}
215
216const Node*
217Hashtable::find(const Name& name, size_t prefixLen, const HashSequence& hashes) const
218{
219 BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen));
220 return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, hashes[prefixLen], false).first;
221}
222
223std::pair<const Node*, bool>
224Hashtable::insert(const Name& name, size_t prefixLen, const HashSequence& hashes)
225{
226 BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen));
227 return this->findOrInsert(name, prefixLen, hashes[prefixLen], true);
228}
229
230void
231Hashtable::erase(Node* node)
232{
233 BOOST_ASSERT(node != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000234 BOOST_ASSERT(node->entry.getParent() == nullptr);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000235
236 size_t bucket = this->computeBucketIndex(node->hash);
Junxiao Shi340d5532016-08-13 04:00:35 +0000237 NFD_LOG_TRACE("erase " << node->entry.getName() << " hash=" << node->hash << " bucket=" << bucket);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000238
239 this->detach(bucket, node);
240 delete node;
241 --m_size;
242
243 if (m_size < m_shrinkThreshold) {
244 size_t newNBuckets = std::max(m_options.minSize,
245 static_cast<size_t>(m_options.shrinkFactor * this->getNBuckets()));
246 this->resize(newNBuckets);
247 }
248}
249
250void
251Hashtable::computeThresholds()
252{
253 m_expandThreshold = static_cast<size_t>(m_options.expandLoadFactor * this->getNBuckets());
254 m_shrinkThreshold = static_cast<size_t>(m_options.shrinkLoadFactor * this->getNBuckets());
255 NFD_LOG_TRACE("thresholds expand=" << m_expandThreshold << " shrink=" << m_shrinkThreshold);
256}
257
258void
259Hashtable::resize(size_t newNBuckets)
260{
261 if (this->getNBuckets() == newNBuckets) {
262 return;
263 }
264 NFD_LOG_DEBUG("resize from=" << this->getNBuckets() << " to=" << newNBuckets);
265
266 std::vector<Node*> oldBuckets;
267 oldBuckets.swap(m_buckets);
268 m_buckets.resize(newNBuckets);
269
270 for (Node* head : oldBuckets) {
271 foreachNode(head, [this] (Node* node) {
272 size_t bucket = this->computeBucketIndex(node->hash);
273 this->attach(bucket, node);
274 });
275 }
276
277 this->computeThresholds();
278}
279
280} // namespace name_tree
281} // namespace nfd