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