blob: d7ca6a6727f264f696cc3ca460e25e94efae19cd [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/*
3 * Copyright (c) 2014-2017, 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
31namespace nfd {
32namespace name_tree {
33
34class Entry;
35
36/** \brief a single hash value
37 */
Junxiao Shif54d0302017-09-07 18:16:38 +000038using HashValue = size_t;
Junxiao Shib660b4c2016-08-06 20:47:44 +000039
40/** \brief a sequence of hash values
41 * \sa computeHashes
42 */
Junxiao Shif54d0302017-09-07 18:16:38 +000043using HashSequence = std::vector<HashValue>;
Junxiao Shib660b4c2016-08-06 20:47:44 +000044
45/** \brief computes a single hash value
46 * \param name base name
47 * \param prefixLen if non-negative, compute hash value for name.getPrefix(prefixLen);
48 * if negative, compute hash value for complete name
49 */
50HashValue
51computeHash(const Name& name, ssize_t prefixLen = -1);
52
53/** \brief computes hash values for each prefix of name
Junxiao Shif54d0302017-09-07 18:16:38 +000054 * \param name base name
55 * \param prefixLen if non-negative, compute up to the first \p prefixLen name components;
56 * if negative, compute all hash values
Junxiao Shib660b4c2016-08-06 20:47:44 +000057 * \return a hash sequence, where the i-th hash value equals computeHash(name, i)
58 */
59HashSequence
Junxiao Shif54d0302017-09-07 18:16:38 +000060computeHashes(const Name& name, ssize_t prefixLen = -1);
Junxiao Shib660b4c2016-08-06 20:47:44 +000061
62/** \brief a hashtable node
63 *
64 * Zero or more nodes can be added to a hashtable bucket. They are organized as
65 * a doubly linked list through prev and next pointers.
66 */
Junxiao Shi340d5532016-08-13 04:00:35 +000067class Node : noncopyable
Junxiao Shib660b4c2016-08-06 20:47:44 +000068{
69public:
Junxiao Shi340d5532016-08-13 04:00:35 +000070 /** \post entry.getName() == name
71 * \post getNode(entry) == this
Junxiao Shib660b4c2016-08-06 20:47:44 +000072 */
73 Node(HashValue h, const Name& name);
74
75 /** \pre prev == nullptr
76 * \pre next == nullptr
77 */
78 ~Node();
79
80public:
Junxiao Shi340d5532016-08-13 04:00:35 +000081 const HashValue hash;
Junxiao Shib660b4c2016-08-06 20:47:44 +000082 Node* prev;
83 Node* next;
Junxiao Shi340d5532016-08-13 04:00:35 +000084 mutable Entry entry;
Junxiao Shib660b4c2016-08-06 20:47:44 +000085};
86
87/** \return node associated with entry
88 * \note This function is for NameTree internal use.
89 */
90Node*
91getNode(const Entry& entry);
92
93/** \brief invoke a function for each node in a doubly linked list
94 * \tparam N either Node or const Node
95 * \tparam F a functor with signature void F(N*)
96 * \note It's safe to delete the node in the function.
97 */
98template<typename N, typename F>
99void
100foreachNode(N* head, const F& func)
101{
102 N* node = head;
103 while (node != nullptr) {
104 N* next = node->next;
105 func(node);
106 node = next;
107 }
108}
109
110/** \brief provides options for Hashtable
111 */
112class HashtableOptions
113{
114public:
115 /** \brief constructor
116 * \post initialSize == size
117 * \post minSize == size
118 */
119 explicit
120 HashtableOptions(size_t size = 16);
121
122public:
123 /** \brief initial number of buckets
124 */
125 size_t initialSize;
126
127 /** \brief minimal number of buckets
128 */
129 size_t minSize;
130
131 /** \brief if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
132 */
133 float expandLoadFactor = 0.5;
134
135 /** \brief when hashtable is expanded, its new size is nBuckets*expandFactor
136 */
137 float expandFactor = 2.0;
138
139 /** \brief if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
140 */
141 float shrinkLoadFactor = 0.1;
142
143 /** \brief when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
144 */
145 float shrinkFactor = 0.5;
146};
147
148/** \brief a hashtable for fast exact name lookup
149 *
150 * The Hashtable contains a number of buckets.
151 * Each node is placed into a bucket determined by a hash value computed from its name.
152 * Hash collision is resolved through a doubly linked list in each bucket.
153 * The number of buckets is adjusted according to how many nodes are stored.
154 */
155class Hashtable
156{
157public:
158 typedef HashtableOptions Options;
159
160 explicit
161 Hashtable(const Options& options);
162
163 /** \brief deallocates all nodes
164 */
165 ~Hashtable();
166
167 /** \return number of nodes
168 */
169 size_t
170 size() const
171 {
172 return m_size;
173 }
174
175 /** \return number of buckets
176 */
177 size_t
178 getNBuckets() const
179 {
180 return m_buckets.size();
181 }
182
183 /** \return bucket index for hash value h
184 */
185 size_t
186 computeBucketIndex(HashValue h) const
187 {
188 return h % this->getNBuckets();
189 }
190
191 /** \return i-th bucket
192 * \pre bucket < getNBuckets()
193 */
194 const Node*
195 getBucket(size_t bucket) const
196 {
197 BOOST_ASSERT(bucket < this->getNBuckets());
198 return m_buckets[bucket]; // don't use m_bucket.at() for better performance
199 }
200
201 /** \brief find node for name.getPrefix(prefixLen)
202 * \pre name.size() > prefixLen
203 */
204 const Node*
205 find(const Name& name, size_t prefixLen) const;
206
207 /** \brief find node for name.getPrefix(prefixLen)
208 * \pre name.size() > prefixLen
209 * \pre hashes == computeHashes(name)
210 */
211 const Node*
212 find(const Name& name, size_t prefixLen, const HashSequence& hashes) const;
213
214 /** \brief find or insert node for name.getPrefix(prefixLen)
215 * \pre name.size() > prefixLen
216 * \pre hashes == computeHashes(name)
217 */
218 std::pair<const Node*, bool>
219 insert(const Name& name, size_t prefixLen, const HashSequence& hashes);
220
221 /** \brief delete node
222 * \pre node exists in this hashtable
223 */
224 void
225 erase(Node* node);
226
227private:
228 /** \brief attach node to bucket
229 */
230 void
231 attach(size_t bucket, Node* node);
232
233 /** \brief detach node from bucket
234 */
235 void
236 detach(size_t bucket, Node* node);
237
238 std::pair<const Node*, bool>
239 findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert);
240
241 void
242 computeThresholds();
243
244 void
245 resize(size_t newNBuckets);
246
247private:
248 std::vector<Node*> m_buckets;
249 Options m_options;
250 size_t m_size;
251 size_t m_expandThreshold;
252 size_t m_shrinkThreshold;
253};
254
255} // namespace name_tree
256} // namespace nfd
257
258#endif // NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP