blob: 1328c2e7718eb01faaa8a67d9b40bccbca4bbe5e [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 Pesaventob7bfcb92022-05-22 23:55:23 -04003 * 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#ifndef NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP
27#define NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP
28
Junxiao Shi340d5532016-08-13 04:00:35 +000029#include "name-tree-entry.hpp"
Junxiao Shib660b4c2016-08-06 20:47:44 +000030
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::name_tree {
Junxiao Shib660b4c2016-08-06 20:47:44 +000032
33class Entry;
34
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040035/** \brief A single hash value.
Junxiao Shib660b4c2016-08-06 20:47:44 +000036 */
Junxiao Shif54d0302017-09-07 18:16:38 +000037using HashValue = size_t;
Junxiao Shib660b4c2016-08-06 20:47:44 +000038
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040039/** \brief A sequence of hash values.
Junxiao Shib660b4c2016-08-06 20:47:44 +000040 * \sa computeHashes
41 */
Junxiao Shif54d0302017-09-07 18:16:38 +000042using HashSequence = std::vector<HashValue>;
Junxiao Shib660b4c2016-08-06 20:47:44 +000043
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040044/** \brief Computes hash value of \p name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +000045 */
46HashValue
Junxiao Shi042a3312017-09-15 02:51:20 +000047computeHash(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max());
Junxiao Shib660b4c2016-08-06 20:47:44 +000048
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040049/** \brief Computes hash values for each prefix of \p name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +000050 * \return a hash sequence, where the i-th hash value equals computeHash(name, i)
51 */
52HashSequence
Junxiao Shi042a3312017-09-15 02:51:20 +000053computeHashes(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max());
Junxiao Shib660b4c2016-08-06 20:47:44 +000054
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040055/** \brief A hashtable node.
Junxiao Shib660b4c2016-08-06 20:47:44 +000056 *
57 * Zero or more nodes can be added to a hashtable bucket. They are organized as
58 * a doubly linked list through prev and next pointers.
59 */
Junxiao Shi340d5532016-08-13 04:00:35 +000060class Node : noncopyable
Junxiao Shib660b4c2016-08-06 20:47:44 +000061{
62public:
Junxiao Shi340d5532016-08-13 04:00:35 +000063 /** \post entry.getName() == name
64 * \post getNode(entry) == this
Junxiao Shib660b4c2016-08-06 20:47:44 +000065 */
66 Node(HashValue h, const Name& name);
67
68 /** \pre prev == nullptr
69 * \pre next == nullptr
70 */
71 ~Node();
72
73public:
Junxiao Shi340d5532016-08-13 04:00:35 +000074 const HashValue hash;
Junxiao Shib660b4c2016-08-06 20:47:44 +000075 Node* prev;
76 Node* next;
Junxiao Shi340d5532016-08-13 04:00:35 +000077 mutable Entry entry;
Junxiao Shib660b4c2016-08-06 20:47:44 +000078};
79
80/** \return node associated with entry
81 * \note This function is for NameTree internal use.
82 */
83Node*
84getNode(const Entry& entry);
85
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040086/** \brief Invoke a function for each node in a doubly linked list.
Junxiao Shib660b4c2016-08-06 20:47:44 +000087 * \tparam N either Node or const Node
88 * \tparam F a functor with signature void F(N*)
89 * \note It's safe to delete the node in the function.
90 */
91template<typename N, typename F>
92void
93foreachNode(N* head, const F& func)
94{
95 N* node = head;
96 while (node != nullptr) {
97 N* next = node->next;
98 func(node);
99 node = next;
100 }
101}
102
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400103/**
104 * \brief Provides options for Hashtable.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000105 */
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400106struct HashtableOptions
Junxiao Shib660b4c2016-08-06 20:47:44 +0000107{
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400108 /** \brief Constructor.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000109 * \post initialSize == size
110 * \post minSize == size
111 */
112 explicit
113 HashtableOptions(size_t size = 16);
114
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400115 /** \brief Initial number of buckets.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000116 */
117 size_t initialSize;
118
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400119 /** \brief Minimal number of buckets.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000120 */
121 size_t minSize;
122
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400123 /** \brief If the hashtable has more than `nBuckets*expandLoadFactor` nodes, it will be expanded.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000124 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400125 float expandLoadFactor = 0.5f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000126
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400127 /** \brief When the hashtable is expanded, its new size will be `nBuckets*expandFactor`.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000128 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400129 float expandFactor = 2.0f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000130
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400131 /** \brief If the hashtable has less than `nBuckets*shrinkLoadFactor` nodes, it will be shrunk.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000132 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400133 float shrinkLoadFactor = 0.1f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000134
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400135 /** \brief When the hashtable is shrunk, its new size will be `max(nBuckets*shrinkFactor, minSize)`.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000136 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400137 float shrinkFactor = 0.5f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000138};
139
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400140/**
141 * \brief A hashtable for fast exact name lookup.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000142 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400143 * The Hashtable contains a number of buckets.
144 * Each node is placed into a bucket determined by a hash value computed from its name.
145 * Hash collision is resolved through a doubly linked list in each bucket.
146 * The number of buckets is adjusted according to how many nodes are stored.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000147 */
148class Hashtable
149{
150public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400151 using Options = HashtableOptions;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000152
153 explicit
154 Hashtable(const Options& options);
155
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400156 /** \brief Deallocates all nodes.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000157 */
158 ~Hashtable();
159
160 /** \return number of nodes
161 */
162 size_t
163 size() const
164 {
165 return m_size;
166 }
167
168 /** \return number of buckets
169 */
170 size_t
171 getNBuckets() const
172 {
173 return m_buckets.size();
174 }
175
176 /** \return bucket index for hash value h
177 */
178 size_t
179 computeBucketIndex(HashValue h) const
180 {
181 return h % this->getNBuckets();
182 }
183
184 /** \return i-th bucket
185 * \pre bucket < getNBuckets()
186 */
187 const Node*
188 getBucket(size_t bucket) const
189 {
190 BOOST_ASSERT(bucket < this->getNBuckets());
191 return m_buckets[bucket]; // don't use m_bucket.at() for better performance
192 }
193
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400194 /** \brief Find node for name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +0000195 * \pre name.size() > prefixLen
196 */
197 const Node*
198 find(const Name& name, size_t prefixLen) const;
199
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400200 /** \brief Find node for name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +0000201 * \pre name.size() > prefixLen
202 * \pre hashes == computeHashes(name)
203 */
204 const Node*
205 find(const Name& name, size_t prefixLen, const HashSequence& hashes) const;
206
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400207 /** \brief Find or insert node for name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +0000208 * \pre name.size() > prefixLen
209 * \pre hashes == computeHashes(name)
210 */
211 std::pair<const Node*, bool>
212 insert(const Name& name, size_t prefixLen, const HashSequence& hashes);
213
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400214 /** \brief Delete node.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000215 * \pre node exists in this hashtable
216 */
217 void
218 erase(Node* node);
219
220private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400221 /** \brief Attach node to bucket.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000222 */
223 void
224 attach(size_t bucket, Node* node);
225
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400226 /** \brief Detach node from bucket.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000227 */
228 void
229 detach(size_t bucket, Node* node);
230
231 std::pair<const Node*, bool>
232 findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert);
233
234 void
235 computeThresholds();
236
237 void
238 resize(size_t newNBuckets);
239
240private:
241 std::vector<Node*> m_buckets;
242 Options m_options;
243 size_t m_size;
244 size_t m_expandThreshold;
245 size_t m_shrinkThreshold;
246};
247
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400248} // namespace nfd::name_tree
Junxiao Shib660b4c2016-08-06 20:47:44 +0000249
250#endif // NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP