blob: 0832a317b986cbad35010a505a43e717d6d95b90 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiee5a4442014-07-27 17:13:43 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shiee5a4442014-07-27 17:13:43 -070024 */
HYuana9b85752014-02-26 02:32:30 -060025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_NAME_TREE_HPP
27#define NFD_DAEMON_TABLE_NAME_TREE_HPP
HYuana9b85752014-02-26 02:32:30 -060028
29#include "common.hpp"
30#include "name-tree-entry.hpp"
31
32namespace nfd {
33namespace name_tree {
34
35/**
Haowei Yuanf52dac72014-03-24 23:35:03 -050036 * \brief Compute the hash value of the given name prefix's WIRE FORMAT
HYuana9b85752014-02-26 02:32:30 -060037 */
Haowei Yuanf52dac72014-03-24 23:35:03 -050038size_t
39computeHash(const Name& prefix);
40
41/**
42 * \brief Incrementally compute hash values
43 * \return Return a vector of hash values, starting from the root prefix
44 */
45std::vector<size_t>
46computeHashSet(const Name& prefix);
HYuana9b85752014-02-26 02:32:30 -060047
Junxiao Shi40631842014-03-01 13:52:37 -070048/// a predicate to accept or reject an Entry in find operations
49typedef function<bool (const Entry& entry)> EntrySelector;
50
Haowei Yuane1079fc2014-03-08 14:41:25 -060051/**
52 * \brief a predicate to accept or reject an Entry and its children
53 * \return .first indicates whether entry should be accepted;
54 * .second indicates whether entry's children should be visited
55 */
56typedef function<std::pair<bool,bool> (const Entry& entry)> EntrySubTreeSelector;
57
Junxiao Shi40631842014-03-01 13:52:37 -070058struct AnyEntry {
59 bool
60 operator()(const Entry& entry)
61 {
62 return true;
63 }
64};
65
Haowei Yuane1079fc2014-03-08 14:41:25 -060066struct AnyEntrySubTree {
67 std::pair<bool, bool>
68 operator()(const Entry& entry)
69 {
70 return std::make_pair(true, true);
71 }
72};
73
HYuana9b85752014-02-26 02:32:30 -060074} // namespace name_tree
75
76/**
Haowei Yuane1079fc2014-03-08 14:41:25 -060077 * \brief Class Name Tree
HYuana9b85752014-02-26 02:32:30 -060078 */
79class NameTree : noncopyable
80{
81public:
Haowei Yuane1079fc2014-03-08 14:41:25 -060082 class const_iterator;
83
HYuana9b85752014-02-26 02:32:30 -060084 explicit
Haowei Yuanf52dac72014-03-24 23:35:03 -050085 NameTree(size_t nBuckets = 1024);
HYuana9b85752014-02-26 02:32:30 -060086
87 ~NameTree();
88
Alexander Afanasyevb3033242014-08-04 11:09:05 -070089public: // information
HYuana9b85752014-02-26 02:32:30 -060090 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -060091 * \brief Get the number of occupied entries in the Name Tree
HYuana9b85752014-02-26 02:32:30 -060092 */
93 size_t
94 size() const;
95
96 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -060097 * \brief Get the number of buckets in the Name Tree (NPHT)
98 * \details The number of buckets is the one that used to create the hash
HYuana9b85752014-02-26 02:32:30 -060099 * table, i.e., m_nBuckets.
100 */
101 size_t
102 getNBuckets() const;
103
104 /**
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700105 * \brief Dump all the information stored in the Name Tree for debugging.
106 */
107 void
108 dump(std::ostream& output) const;
109
110public: // mutation
111 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600112 * \brief Look for the Name Tree Entry that contains this name prefix.
113 * \details Starts from the shortest name prefix, and then increase the
HYuana9b85752014-02-26 02:32:30 -0600114 * number of name components by one each time. All non-existing Name Tree
115 * Entries will be created.
Haowei Yuane1079fc2014-03-08 14:41:25 -0600116 * \param prefix The querying name prefix.
117 * \return The pointer to the Name Tree Entry that contains this full name
HYuana9b85752014-02-26 02:32:30 -0600118 * prefix.
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700119 * \note Existing iterators are unaffected.
HYuana9b85752014-02-26 02:32:30 -0600120 */
121 shared_ptr<name_tree::Entry>
122 lookup(const Name& prefix);
123
124 /**
Junxiao Shiee5a4442014-07-27 17:13:43 -0700125 * \brief Delete a Name Tree Entry if this entry is empty.
126 * \param entry The entry to be deleted if empty.
127 * \note This function must be called after a table entry is detached from Name Tree
128 * entry. The function deletes a Name Tree entry if nothing is attached to it and
129 * it has no children, then repeats the same process on its ancestors.
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700130 * \note Existing iterators, except those pointing to deleted entries, are unaffected.
HYuana9b85752014-02-26 02:32:30 -0600131 */
132 bool
133 eraseEntryIfEmpty(shared_ptr<name_tree::Entry> entry);
134
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700135public: // shortcut access
136 /// get NameTree entry from attached FIB entry
137 shared_ptr<name_tree::Entry>
138 get(const fib::Entry& fibEntry) const;
139
140 /// get NameTree entry from attached PIT entry
141 shared_ptr<name_tree::Entry>
142 get(const pit::Entry& pitEntry) const;
143
144 /// get NameTree entry from attached Measurements entry
145 shared_ptr<name_tree::Entry>
146 get(const measurements::Entry& measurementsEntry) const;
147
148 /// get NameTree entry from attached StrategyChoice entry
149 shared_ptr<name_tree::Entry>
150 get(const strategy_choice::Entry& strategyChoiceEntry) const;
151
152public: // matching
153 /**
154 * \brief Exact match lookup for the given name prefix.
155 * \return a null shared_ptr if this prefix is not found;
156 * otherwise return the Name Tree Entry address
157 */
158 shared_ptr<name_tree::Entry>
159 findExactMatch(const Name& prefix) const;
160
HYuana9b85752014-02-26 02:32:30 -0600161 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600162 * \brief Longest prefix matching for the given name
163 * \details Starts from the full name string, reduce the number of name component
HYuana9b85752014-02-26 02:32:30 -0600164 * by one each time, until an Entry is found.
165 */
166 shared_ptr<name_tree::Entry>
Junxiao Shi40631842014-03-01 13:52:37 -0700167 findLongestPrefixMatch(const Name& prefix,
Haowei Yuane1079fc2014-03-08 14:41:25 -0600168 const name_tree::EntrySelector& entrySelector =
169 name_tree::AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600170
HangZhangcb4fc832014-03-11 16:57:11 +0800171 shared_ptr<name_tree::Entry>
172 findLongestPrefixMatch(shared_ptr<name_tree::Entry> entry,
173 const name_tree::EntrySelector& entrySelector =
174 name_tree::AnyEntry()) const;
175
HYuana9b85752014-02-26 02:32:30 -0600176 /**
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700177 * \brief Enumerate all the name prefixes that satisfy the prefix and entrySelector
HYuana9b85752014-02-26 02:32:30 -0600178 */
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700179 const_iterator
180 findAllMatches(const Name& prefix,
181 const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600182
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700183public: // enumeration
HYuana9b85752014-02-26 02:32:30 -0600184 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600185 * \brief Enumerate all the name prefixes stored in the Name Tree.
HYuana9b85752014-02-26 02:32:30 -0600186 */
Haowei Yuane1079fc2014-03-08 14:41:25 -0600187 const_iterator
188 fullEnumerate(const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600189
190 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600191 * \brief Enumerate all the name prefixes that satisfies the EntrySubTreeSelector.
192 */
193 const_iterator
Junxiao Shi40631842014-03-01 13:52:37 -0700194 partialEnumerate(const Name& prefix,
Junxiao Shiefceadc2014-03-09 18:52:57 -0700195 const name_tree::EntrySubTreeSelector& entrySubTreeSelector =
196 name_tree::AnyEntrySubTree()) const;
HYuana9b85752014-02-26 02:32:30 -0600197
Haowei Yuane1079fc2014-03-08 14:41:25 -0600198 const_iterator
199 begin() const;
200
201 const_iterator
202 end() const;
203
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700204 enum IteratorType {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600205 FULL_ENUMERATE_TYPE,
206 PARTIAL_ENUMERATE_TYPE,
207 FIND_ALL_MATCHES_TYPE
208 };
209
210 class const_iterator : public std::iterator<std::forward_iterator_tag, name_tree::Entry>
211 {
212 public:
213 friend class NameTree;
214
215 const_iterator(NameTree::IteratorType type,
216 const NameTree& nameTree,
217 shared_ptr<name_tree::Entry> entry,
218 const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry(),
219 const name_tree::EntrySubTreeSelector& entrySubTreeSelector = name_tree::AnyEntrySubTree());
220
221 ~const_iterator();
222
223 const name_tree::Entry&
224 operator*() const;
225
226 shared_ptr<name_tree::Entry>
227 operator->() const;
228
229 const_iterator
230 operator++();
231
232 const_iterator
233 operator++(int);
234
235 bool
236 operator==(const const_iterator& other) const;
237
238 bool
239 operator!=(const const_iterator& other) const;
240
241 private:
Haowei Yuane1079fc2014-03-08 14:41:25 -0600242 const NameTree& m_nameTree;
243 shared_ptr<name_tree::Entry> m_entry;
244 shared_ptr<name_tree::Entry> m_subTreeRoot;
245 shared_ptr<name_tree::EntrySelector> m_entrySelector;
246 shared_ptr<name_tree::EntrySubTreeSelector> m_entrySubTreeSelector;
247 NameTree::IteratorType m_type;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700248 bool m_shouldVisitChildren;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600249 };
HYuana9b85752014-02-26 02:32:30 -0600250
251private:
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700252 /**
253 * \brief Resize the hash table size when its load factor reaches a threshold.
254 * \details As we are currently using a hand-written hash table implementation
255 * for the Name Tree, the hash table resize() function should be kept in the
256 * name-tree.hpp file.
257 * \param newNBuckets The number of buckets for the new hash table.
258 */
259 void
260 resize(size_t newNBuckets);
261
262private:
Haowei Yuane1079fc2014-03-08 14:41:25 -0600263 size_t m_nItems; // Number of items being stored
264 size_t m_nBuckets; // Number of hash buckets
Haowei Yuanf52dac72014-03-24 23:35:03 -0500265 size_t m_minNBuckets; // Minimum number of hash buckets
266 double m_enlargeLoadFactor;
267 size_t m_enlargeThreshold;
268 int m_enlargeFactor;
269 double m_shrinkLoadFactor;
270 size_t m_shrinkThreshold;
271 double m_shrinkFactor;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600272 name_tree::Node** m_buckets; // Name Tree Buckets in the NPHT
273 shared_ptr<name_tree::Entry> m_end;
274 const_iterator m_endIterator;
HYuana9b85752014-02-26 02:32:30 -0600275
276 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600277 * \brief Create a Name Tree Entry if it does not exist, or return the existing
HYuana9b85752014-02-26 02:32:30 -0600278 * Name Tree Entry address.
Haowei Yuane1079fc2014-03-08 14:41:25 -0600279 * \details Called by lookup() only.
280 * \return The first item is the Name Tree Entry address, the second item is
HYuana9b85752014-02-26 02:32:30 -0600281 * a bool value indicates whether this is an old entry (false) or a new
282 * entry (true).
283 */
284 std::pair<shared_ptr<name_tree::Entry>, bool>
285 insert(const Name& prefix);
HYuana9b85752014-02-26 02:32:30 -0600286};
287
Haowei Yuane1079fc2014-03-08 14:41:25 -0600288inline NameTree::const_iterator::~const_iterator()
289{
290}
291
HYuana9b85752014-02-26 02:32:30 -0600292inline size_t
293NameTree::size() const
294{
295 return m_nItems;
296}
297
298inline size_t
299NameTree::getNBuckets() const
300{
301 return m_nBuckets;
302}
303
Junxiao Shiefceadc2014-03-09 18:52:57 -0700304inline shared_ptr<name_tree::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +0800305NameTree::get(const fib::Entry& fibEntry) const
306{
307 return fibEntry.m_nameTreeEntry;
308}
309
310inline shared_ptr<name_tree::Entry>
311NameTree::get(const pit::Entry& pitEntry) const
312{
313 return pitEntry.m_nameTreeEntry;
314}
315
316inline shared_ptr<name_tree::Entry>
317NameTree::get(const measurements::Entry& measurementsEntry) const
318{
319 return measurementsEntry.m_nameTreeEntry;
320}
321
322inline shared_ptr<name_tree::Entry>
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700323NameTree::get(const strategy_choice::Entry& strategyChoiceEntry) const
HangZhangcb4fc832014-03-11 16:57:11 +0800324{
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700325 return strategyChoiceEntry.m_nameTreeEntry;
HangZhangcb4fc832014-03-11 16:57:11 +0800326}
327
Haowei Yuane1079fc2014-03-08 14:41:25 -0600328inline NameTree::const_iterator
329NameTree::begin() const
330{
331 return fullEnumerate();
332}
333
334inline NameTree::const_iterator
335NameTree::end() const
336{
337 return m_endIterator;
338}
339
340inline const name_tree::Entry&
341NameTree::const_iterator::operator*() const
342{
343 return *m_entry;
344}
345
346inline shared_ptr<name_tree::Entry>
347NameTree::const_iterator::operator->() const
348{
349 return m_entry;
350}
351
352inline NameTree::const_iterator
353NameTree::const_iterator::operator++(int)
354{
355 NameTree::const_iterator temp(*this);
356 ++(*this);
357 return temp;
358}
359
360inline bool
361NameTree::const_iterator::operator==(const NameTree::const_iterator& other) const
362{
363 return m_entry == other.m_entry;
364}
365
366inline bool
367NameTree::const_iterator::operator!=(const NameTree::const_iterator& other) const
368{
369 return m_entry != other.m_entry;
370}
371
HYuana9b85752014-02-26 02:32:30 -0600372} // namespace nfd
373
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700374#endif // NFD_DAEMON_TABLE_NAME_TREE_HPP