blob: 67eafcd5e116c803acc1ff0a27d56fc80ada6623 [file] [log] [blame]
Junxiao Shib660b4c2016-08-06 20:47:44 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California,
4 * 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"
Junxiao Shib660b4c2016-08-06 20:47:44 +000027#include "core/logger.hpp"
28#include "core/city-hash.hpp"
29
30namespace nfd {
31namespace name_tree {
32
33NFD_LOG_INIT("NameTreeHashtable");
34
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 */
57typedef std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32>::type HashFunc;
58
59HashValue
60computeHash(const Name& name, ssize_t prefixLen)
61{
62 name.wireEncode(); // ensure wire buffer exists
63
64 HashValue h = 0;
65 for (size_t i = 0, last = prefixLen < 0 ? name.size() : prefixLen; i < last; ++i) {
66 const name::Component& comp = name[i];
67 h ^= HashFunc::compute(comp.wire(), comp.size());
68 }
69 return h;
70}
71
72HashSequence
73computeHashes(const Name& name)
74{
75 name.wireEncode(); // ensure wire buffer exists
76
77 HashSequence seq;
78 seq.reserve(name.size() + 1);
79
80 HashValue h = 0;
81 seq.push_back(h);
82
83 for (const name::Component& comp : name) {
84 h ^= HashFunc::compute(comp.wire(), comp.size());
85 seq.push_back(h);
86 }
87 return seq;
88}
89
90Node::Node(HashValue h, const Name& name)
91 : hash(h)
92 , prev(nullptr)
93 , next(nullptr)
Junxiao Shi340d5532016-08-13 04:00:35 +000094 , entry(name, this)
Junxiao Shib660b4c2016-08-06 20:47:44 +000095{
96}
97
98Node::~Node()
99{
100 BOOST_ASSERT(prev == nullptr);
101 BOOST_ASSERT(next == nullptr);
102}
103
104Node*
105getNode(const Entry& entry)
106{
107 return entry.m_node;
108}
109
110HashtableOptions::HashtableOptions(size_t size)
111 : initialSize(size)
112 , minSize(size)
113{
114}
115
116Hashtable::Hashtable(const Options& options)
117 : m_options(options)
118 , m_size(0)
119{
120 BOOST_ASSERT(m_options.minSize > 0);
121 BOOST_ASSERT(m_options.initialSize >= m_options.minSize);
122 BOOST_ASSERT(m_options.expandLoadFactor > 0.0);
123 BOOST_ASSERT(m_options.expandLoadFactor <= 1.0);
124 BOOST_ASSERT(m_options.expandFactor > 1.0);
125 BOOST_ASSERT(m_options.shrinkLoadFactor >= 0.0);
126 BOOST_ASSERT(m_options.shrinkLoadFactor < 1.0);
127 BOOST_ASSERT(m_options.shrinkFactor > 0.0);
128 BOOST_ASSERT(m_options.shrinkFactor < 1.0);
129
130 m_buckets.resize(options.initialSize);
131 this->computeThresholds();
132}
133
134Hashtable::~Hashtable()
135{
136 for (size_t i = 0; i < m_buckets.size(); ++i) {
137 foreachNode(m_buckets[i], [] (Node* node) {
138 node->prev = node->next = nullptr;
139 delete node;
140 });
141 }
142}
143
144void
145Hashtable::attach(size_t bucket, Node* node)
146{
147 node->prev = nullptr;
148 node->next = m_buckets[bucket];
149
150 if (node->next != nullptr) {
151 BOOST_ASSERT(node->next->prev == nullptr);
152 node->next->prev = node;
153 }
154
155 m_buckets[bucket] = node;
156}
157
158void
159Hashtable::detach(size_t bucket, Node* node)
160{
161 if (node->prev != nullptr) {
162 BOOST_ASSERT(node->prev->next == node);
163 node->prev->next = node->next;
164 }
165 else {
166 BOOST_ASSERT(m_buckets[bucket] == node);
167 m_buckets[bucket] = node->next;
168 }
169
170 if (node->next != nullptr) {
171 BOOST_ASSERT(node->next->prev == node);
172 node->next->prev = node->prev;
173 }
174
175 node->prev = node->next = nullptr;
176}
177
178std::pair<const Node*, bool>
179Hashtable::findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert)
180{
181 size_t bucket = this->computeBucketIndex(h);
182
183 for (const Node* node = m_buckets[bucket]; node != nullptr; node = node->next) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000184 if (node->hash == h && name.compare(0, prefixLen, node->entry.getName()) == 0) {
Junxiao Shib660b4c2016-08-06 20:47:44 +0000185 NFD_LOG_TRACE("found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket);
186 return {node, false};
187 }
188 }
189
190 if (!allowInsert) {
191 NFD_LOG_TRACE("not-found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket);
192 return {nullptr, false};
193 }
194
195 Node* node = new Node(h, name.getPrefix(prefixLen));
196 this->attach(bucket, node);
Junxiao Shi340d5532016-08-13 04:00:35 +0000197 NFD_LOG_TRACE("insert " << node->entry.getName() << " hash=" << h << " bucket=" << bucket);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000198 ++m_size;
199
200 if (m_size > m_expandThreshold) {
201 this->resize(static_cast<size_t>(m_options.expandFactor * this->getNBuckets()));
202 }
203
204 return {node, true};
205}
206
207const Node*
208Hashtable::find(const Name& name, size_t prefixLen) const
209{
210 HashValue h = computeHash(name, prefixLen);
211 return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, h, false).first;
212}
213
214const Node*
215Hashtable::find(const Name& name, size_t prefixLen, const HashSequence& hashes) const
216{
217 BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen));
218 return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, hashes[prefixLen], false).first;
219}
220
221std::pair<const Node*, bool>
222Hashtable::insert(const Name& name, size_t prefixLen, const HashSequence& hashes)
223{
224 BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen));
225 return this->findOrInsert(name, prefixLen, hashes[prefixLen], true);
226}
227
228void
229Hashtable::erase(Node* node)
230{
231 BOOST_ASSERT(node != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000232 BOOST_ASSERT(node->entry.getParent() == nullptr);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000233
234 size_t bucket = this->computeBucketIndex(node->hash);
Junxiao Shi340d5532016-08-13 04:00:35 +0000235 NFD_LOG_TRACE("erase " << node->entry.getName() << " hash=" << node->hash << " bucket=" << bucket);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000236
237 this->detach(bucket, node);
238 delete node;
239 --m_size;
240
241 if (m_size < m_shrinkThreshold) {
242 size_t newNBuckets = std::max(m_options.minSize,
243 static_cast<size_t>(m_options.shrinkFactor * this->getNBuckets()));
244 this->resize(newNBuckets);
245 }
246}
247
248void
249Hashtable::computeThresholds()
250{
251 m_expandThreshold = static_cast<size_t>(m_options.expandLoadFactor * this->getNBuckets());
252 m_shrinkThreshold = static_cast<size_t>(m_options.shrinkLoadFactor * this->getNBuckets());
253 NFD_LOG_TRACE("thresholds expand=" << m_expandThreshold << " shrink=" << m_shrinkThreshold);
254}
255
256void
257Hashtable::resize(size_t newNBuckets)
258{
259 if (this->getNBuckets() == newNBuckets) {
260 return;
261 }
262 NFD_LOG_DEBUG("resize from=" << this->getNBuckets() << " to=" << newNBuckets);
263
264 std::vector<Node*> oldBuckets;
265 oldBuckets.swap(m_buckets);
266 m_buckets.resize(newNBuckets);
267
268 for (Node* head : oldBuckets) {
269 foreachNode(head, [this] (Node* node) {
270 size_t bucket = this->computeBucketIndex(node->hash);
271 this->attach(bucket, node);
272 });
273 }
274
275 this->computeThresholds();
276}
277
278} // namespace name_tree
279} // namespace nfd