blob: 6c1b165dfd1b9512c2c8e7baf44001744d18683b [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 Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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 Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <limits>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::name_tree {
Junxiao Shib660b4c2016-08-06 20:47:44 +000034
35class Entry;
36
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040037/** \brief A single hash value.
Junxiao Shib660b4c2016-08-06 20:47:44 +000038 */
Junxiao Shif54d0302017-09-07 18:16:38 +000039using HashValue = size_t;
Junxiao Shib660b4c2016-08-06 20:47:44 +000040
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040041/** \brief A sequence of hash values.
Junxiao Shib660b4c2016-08-06 20:47:44 +000042 * \sa computeHashes
43 */
Junxiao Shif54d0302017-09-07 18:16:38 +000044using HashSequence = std::vector<HashValue>;
Junxiao Shib660b4c2016-08-06 20:47:44 +000045
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040046/** \brief Computes hash value of \p name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +000047 */
48HashValue
Junxiao Shi042a3312017-09-15 02:51:20 +000049computeHash(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max());
Junxiao Shib660b4c2016-08-06 20:47:44 +000050
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040051/** \brief Computes hash values for each prefix of \p name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +000052 * \return a hash sequence, where the i-th hash value equals computeHash(name, i)
53 */
54HashSequence
Junxiao Shi042a3312017-09-15 02:51:20 +000055computeHashes(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max());
Junxiao Shib660b4c2016-08-06 20:47:44 +000056
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040057/** \brief A hashtable node.
Junxiao Shib660b4c2016-08-06 20:47:44 +000058 *
59 * Zero or more nodes can be added to a hashtable bucket. They are organized as
60 * a doubly linked list through prev and next pointers.
61 */
Junxiao Shi340d5532016-08-13 04:00:35 +000062class Node : noncopyable
Junxiao Shib660b4c2016-08-06 20:47:44 +000063{
64public:
Junxiao Shi340d5532016-08-13 04:00:35 +000065 /** \post entry.getName() == name
66 * \post getNode(entry) == this
Junxiao Shib660b4c2016-08-06 20:47:44 +000067 */
68 Node(HashValue h, const Name& name);
69
70 /** \pre prev == nullptr
71 * \pre next == nullptr
72 */
73 ~Node();
74
75public:
Junxiao Shi340d5532016-08-13 04:00:35 +000076 const HashValue hash;
Junxiao Shib660b4c2016-08-06 20:47:44 +000077 Node* prev;
78 Node* next;
Junxiao Shi340d5532016-08-13 04:00:35 +000079 mutable Entry entry;
Junxiao Shib660b4c2016-08-06 20:47:44 +000080};
81
82/** \return node associated with entry
83 * \note This function is for NameTree internal use.
84 */
85Node*
86getNode(const Entry& entry);
87
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040088/** \brief Invoke a function for each node in a doubly linked list.
Junxiao Shib660b4c2016-08-06 20:47:44 +000089 * \tparam N either Node or const Node
90 * \tparam F a functor with signature void F(N*)
91 * \note It's safe to delete the node in the function.
92 */
93template<typename N, typename F>
94void
95foreachNode(N* head, const F& func)
96{
97 N* node = head;
98 while (node != nullptr) {
99 N* next = node->next;
100 func(node);
101 node = next;
102 }
103}
104
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400105/**
106 * \brief Provides options for Hashtable.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000107 */
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400108struct HashtableOptions
Junxiao Shib660b4c2016-08-06 20:47:44 +0000109{
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400110 /** \brief Constructor.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000111 * \post initialSize == size
112 * \post minSize == size
113 */
114 explicit
115 HashtableOptions(size_t size = 16);
116
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400117 /** \brief Initial number of buckets.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000118 */
119 size_t initialSize;
120
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400121 /** \brief Minimal number of buckets.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000122 */
123 size_t minSize;
124
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400125 /** \brief If the hashtable has more than `nBuckets*expandLoadFactor` nodes, it will be expanded.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000126 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400127 float expandLoadFactor = 0.5f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000128
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400129 /** \brief When the hashtable is expanded, its new size will be `nBuckets*expandFactor`.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000130 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400131 float expandFactor = 2.0f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000132
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400133 /** \brief If the hashtable has less than `nBuckets*shrinkLoadFactor` nodes, it will be shrunk.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000134 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400135 float shrinkLoadFactor = 0.1f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000136
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400137 /** \brief When the hashtable is shrunk, its new size will be `max(nBuckets*shrinkFactor, minSize)`.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000138 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400139 float shrinkFactor = 0.5f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000140};
141
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400142/**
143 * \brief A hashtable for fast exact name lookup.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000144 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400145 * The Hashtable contains a number of buckets.
146 * Each node is placed into a bucket determined by a hash value computed from its name.
147 * Hash collision is resolved through a doubly linked list in each bucket.
148 * The number of buckets is adjusted according to how many nodes are stored.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000149 */
150class Hashtable
151{
152public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400153 using Options = HashtableOptions;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000154
155 explicit
156 Hashtable(const Options& options);
157
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400158 /** \brief Deallocates all nodes.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000159 */
160 ~Hashtable();
161
162 /** \return number of nodes
163 */
164 size_t
165 size() const
166 {
167 return m_size;
168 }
169
170 /** \return number of buckets
171 */
172 size_t
173 getNBuckets() const
174 {
175 return m_buckets.size();
176 }
177
178 /** \return bucket index for hash value h
179 */
180 size_t
181 computeBucketIndex(HashValue h) const
182 {
183 return h % this->getNBuckets();
184 }
185
186 /** \return i-th bucket
187 * \pre bucket < getNBuckets()
188 */
189 const Node*
190 getBucket(size_t bucket) const
191 {
192 BOOST_ASSERT(bucket < this->getNBuckets());
193 return m_buckets[bucket]; // don't use m_bucket.at() for better performance
194 }
195
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400196 /** \brief Find node for name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +0000197 * \pre name.size() > prefixLen
198 */
199 const Node*
200 find(const Name& name, size_t prefixLen) const;
201
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400202 /** \brief Find node for name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +0000203 * \pre name.size() > prefixLen
204 * \pre hashes == computeHashes(name)
205 */
206 const Node*
207 find(const Name& name, size_t prefixLen, const HashSequence& hashes) const;
208
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400209 /** \brief Find or insert node for name.getPrefix(prefixLen).
Junxiao Shib660b4c2016-08-06 20:47:44 +0000210 * \pre name.size() > prefixLen
211 * \pre hashes == computeHashes(name)
212 */
213 std::pair<const Node*, bool>
214 insert(const Name& name, size_t prefixLen, const HashSequence& hashes);
215
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400216 /** \brief Delete node.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000217 * \pre node exists in this hashtable
218 */
219 void
220 erase(Node* node);
221
222private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400223 /** \brief Attach node to bucket.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000224 */
225 void
226 attach(size_t bucket, Node* node);
227
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400228 /** \brief Detach node from bucket.
Junxiao Shib660b4c2016-08-06 20:47:44 +0000229 */
230 void
231 detach(size_t bucket, Node* node);
232
233 std::pair<const Node*, bool>
234 findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert);
235
236 void
237 computeThresholds();
238
239 void
240 resize(size_t newNBuckets);
241
242private:
243 std::vector<Node*> m_buckets;
244 Options m_options;
245 size_t m_size;
246 size_t m_expandThreshold;
247 size_t m_shrinkThreshold;
248};
249
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400250} // namespace nfd::name_tree
Junxiao Shib660b4c2016-08-06 20:47:44 +0000251
252#endif // NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP