NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
name-tree-hashtable.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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"
27 #include "common/city-hash.hpp"
28 #include "common/logger.hpp"
29 
30 namespace nfd {
31 namespace name_tree {
32 
34 
35 class Hash32
36 {
37 public:
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 
45 class Hash64
46 {
47 public:
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 
57 using HashFunc = std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32>::type;
58 
60 computeHash(const Name& name, size_t prefixLen)
61 {
62  name.wireEncode(); // ensure wire buffer exists
63 
64  HashValue h = 0;
65  for (size_t i = 0, last = std::min(prefixLen, name.size()); i < last; ++i) {
66  const name::Component& comp = name[i];
67  h ^= HashFunc::compute(comp.wire(), comp.size());
68  }
69  return h;
70 }
71 
73 computeHashes(const Name& name, size_t prefixLen)
74 {
75  name.wireEncode(); // ensure wire buffer exists
76 
77  size_t last = std::min(prefixLen, name.size());
78  HashSequence seq;
79  seq.reserve(last + 1);
80 
81  HashValue h = 0;
82  seq.push_back(h);
83 
84  for (size_t i = 0; i < last; ++i) {
85  const name::Component& comp = name[i];
86  h ^= HashFunc::compute(comp.wire(), comp.size());
87  seq.push_back(h);
88  }
89  return seq;
90 }
91 
93  : hash(h)
94  , prev(nullptr)
95  , next(nullptr)
96  , entry(name, this)
97 {
98 }
99 
101 {
102  BOOST_ASSERT(prev == nullptr);
103  BOOST_ASSERT(next == nullptr);
104 }
105 
106 Node*
107 getNode(const Entry& entry)
108 {
109  return entry.m_node;
110 }
111 
113  : initialSize(size)
114  , minSize(size)
115 {
116 }
117 
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 
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 
146 void
147 Hashtable::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 
160 void
161 Hashtable::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 
180 std::pair<const Node*, bool>
181 Hashtable::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) {
186  if (node->hash == h && name.compare(0, prefixLen, node->entry.getName()) == 0) {
187  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);
199  NFD_LOG_TRACE("insert " << node->entry.getName() << " hash=" << h << " bucket=" << bucket);
200  ++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 
209 const Node*
210 Hashtable::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 
216 const Node*
217 Hashtable::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 
223 std::pair<const Node*, bool>
224 Hashtable::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 
230 void
232 {
233  BOOST_ASSERT(node != nullptr);
234  BOOST_ASSERT(node->entry.getParent() == nullptr);
235 
236  size_t bucket = this->computeBucketIndex(node->hash);
237  NFD_LOG_TRACE("erase " << node->entry.getName() << " hash=" << node->hash << " bucket=" << bucket);
238 
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 
250 void
251 Hashtable::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 
258 void
259 Hashtable::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
NFD_LOG_TRACE
#define NFD_LOG_TRACE
Definition: logger.hpp:37
nfd::name_tree::Entry
An entry in the name tree.
Definition: name-tree-entry.hpp:42
nfd::name_tree::HashtableOptions::initialSize
size_t initialSize
initial number of buckets
Definition: name-tree-hashtable.hpp:119
nfd::name_tree::HashFunc
std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32 >::type HashFunc
a type with compute static method to compute hash value from a raw buffer
Definition: name-tree-hashtable.cpp:57
nfd::name_tree::Hash32
Definition: name-tree-hashtable.cpp:36
name-tree-hashtable.hpp
nfd::name_tree::Hashtable::getNBuckets
size_t getNBuckets() const
Definition: name-tree-hashtable.hpp:172
CityHash32
uint32 CityHash32(const char *s, size_t len)
Definition: city-hash.cpp:182
nfd::name_tree::computeHashes
HashSequence computeHashes(const Name &name, size_t prefixLen)
computes hash values for each prefix of name.getPrefix(prefixLen)
Definition: name-tree-hashtable.cpp:73
CityHash64
uint64 CityHash64(const char *s, size_t len)
Definition: city-hash.cpp:359
nfd::name_tree::getNode
Node * getNode(const Entry &entry)
Definition: name-tree-hashtable.cpp:107
nfd::name_tree::foreachNode
void foreachNode(N *head, const F &func)
invoke a function for each node in a doubly linked list
Definition: name-tree-hashtable.hpp:94
nfd::name_tree::Node::prev
Node * prev
Definition: name-tree-hashtable.hpp:76
nfd::name_tree::HashtableOptions::minSize
size_t minSize
minimal number of buckets
Definition: name-tree-hashtable.hpp:123
nfd::name_tree::Hashtable::Hashtable
Hashtable(const Options &options)
Definition: name-tree-hashtable.cpp:118
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd::name_tree::Entry::getName
const Name & getName() const
Definition: name-tree-entry.hpp:47
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::name_tree::Hash64::compute
static HashValue compute(const void *buffer, size_t length)
Definition: name-tree-hashtable.cpp:49
nfd::name_tree::Node::Node
Node(HashValue h, const Name &name)
Definition: name-tree-hashtable.cpp:92
city-hash.hpp
nfd::name_tree::Node
a hashtable node
Definition: name-tree-hashtable.hpp:62
nfd::name_tree::Node::next
Node * next
Definition: name-tree-hashtable.hpp:77
nfd::name_tree::HashtableOptions::HashtableOptions
HashtableOptions(size_t size=16)
constructor
Definition: name-tree-hashtable.cpp:112
nfd::name_tree::HashtableOptions::shrinkLoadFactor
float shrinkLoadFactor
if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
Definition: name-tree-hashtable.hpp:135
nfd::name_tree::HashValue
size_t HashValue
a single hash value
Definition: name-tree-hashtable.hpp:38
nfd::name_tree::Hash64
Definition: name-tree-hashtable.cpp:46
nfd::name_tree::Node::hash
const HashValue hash
Definition: name-tree-hashtable.hpp:75
nfd::name_tree::Hashtable
a hashtable for fast exact name lookup
Definition: name-tree-hashtable.hpp:150
nfd::name_tree::Node::entry
Entry entry
Definition: name-tree-hashtable.hpp:78
NFD_LOG_DEBUG
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
nfd::name_tree::Hash32::compute
static HashValue compute(const void *buffer, size_t length)
Definition: name-tree-hashtable.cpp:39
nfd::name_tree::computeHash
HashValue computeHash(const Name &name, size_t prefixLen)
computes hash value of name.getPrefix(prefixLen)
Definition: name-tree-hashtable.cpp:60
nfd::name_tree::NameTreeHashtable
NameTreeHashtable
Definition: name-tree-hashtable.cpp:33
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
nfd::name_tree::Hashtable::find
const Node * find(const Name &name, size_t prefixLen) const
find node for name.getPrefix(prefixLen)
Definition: name-tree-hashtable.cpp:210
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
nfd::name_tree::Node::~Node
~Node()
Definition: name-tree-hashtable.cpp:100
nfd::name_tree::HashtableOptions::expandFactor
float expandFactor
when hashtable is expanded, its new size is nBuckets*expandFactor
Definition: name-tree-hashtable.hpp:131
ndn::name
Definition: name-component-types.hpp:33
nfd::name_tree::HashSequence
std::vector< HashValue > HashSequence
a sequence of hash values
Definition: name-tree-hashtable.hpp:43
nfd::name_tree::HashtableOptions::shrinkFactor
float shrinkFactor
when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
Definition: name-tree-hashtable.hpp:139
nfd::name_tree::Hashtable::~Hashtable
~Hashtable()
deallocates all nodes
Definition: name-tree-hashtable.cpp:136
nfd::name_tree::HashtableOptions::expandLoadFactor
float expandLoadFactor
if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
Definition: name-tree-hashtable.hpp:127
nfd::name_tree::HashtableOptions
provides options for Hashtable
Definition: name-tree-hashtable.hpp:107
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:281
nfd::name_tree::Hashtable::erase
void erase(Node *node)
delete node
Definition: name-tree-hashtable.cpp:231
nfd::name_tree::Hashtable::computeBucketIndex
size_t computeBucketIndex(HashValue h) const
Definition: name-tree-hashtable.hpp:180
nfd::name_tree::Hashtable::insert
std::pair< const Node *, bool > insert(const Name &name, size_t prefixLen, const HashSequence &hashes)
find or insert node for name.getPrefix(prefixLen)
Definition: name-tree-hashtable.cpp:224
nfd::name_tree::Entry::getParent
Entry * getParent() const
Definition: name-tree-entry.hpp:56
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
logger.hpp