blob: de96d0a0910112ffa15bd657bbce71c878a223bc [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
35/** \brief a single hash value
36 */
Junxiao Shif54d0302017-09-07 18:16:38 +000037using HashValue = size_t;
Junxiao Shib660b4c2016-08-06 20:47:44 +000038
39/** \brief a sequence of hash values
40 * \sa computeHashes
41 */
Junxiao Shif54d0302017-09-07 18:16:38 +000042using HashSequence = std::vector<HashValue>;
Junxiao Shib660b4c2016-08-06 20:47:44 +000043
Junxiao Shi042a3312017-09-15 02:51:20 +000044/** \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
Junxiao Shi042a3312017-09-15 02:51:20 +000049/** \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
55/** \brief a hashtable node
56 *
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
86/** \brief invoke a function for each node in a doubly linked list
87 * \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
103/** \brief provides options for Hashtable
104 */
105class HashtableOptions
106{
107public:
108 /** \brief constructor
109 * \post initialSize == size
110 * \post minSize == size
111 */
112 explicit
113 HashtableOptions(size_t size = 16);
114
115public:
116 /** \brief initial number of buckets
117 */
118 size_t initialSize;
119
120 /** \brief minimal number of buckets
121 */
122 size_t minSize;
123
124 /** \brief if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
125 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400126 float expandLoadFactor = 0.5f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000127
128 /** \brief when hashtable is expanded, its new size is nBuckets*expandFactor
129 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400130 float expandFactor = 2.0f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000131
132 /** \brief if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
133 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400134 float shrinkLoadFactor = 0.1f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000135
136 /** \brief when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
137 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400138 float shrinkFactor = 0.5f;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000139};
140
141/** \brief a hashtable for fast exact name lookup
142 *
143 * 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.
147 */
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
156 /** \brief deallocates all nodes
157 */
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
194 /** \brief find node for name.getPrefix(prefixLen)
195 * \pre name.size() > prefixLen
196 */
197 const Node*
198 find(const Name& name, size_t prefixLen) const;
199
200 /** \brief find node for name.getPrefix(prefixLen)
201 * \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
207 /** \brief find or insert node for name.getPrefix(prefixLen)
208 * \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
214 /** \brief delete node
215 * \pre node exists in this hashtable
216 */
217 void
218 erase(Node* node);
219
220private:
221 /** \brief attach node to bucket
222 */
223 void
224 attach(size_t bucket, Node* node);
225
226 /** \brief detach node from bucket
227 */
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