blob: 15a717b581cdf10c7066ab76cecf28c4f733d278 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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
Junxiao Shi4370fde2016-02-24 12:20:46 -0700136 /** \brief get NameTree entry from FIB entry
137 *
138 * This is equivalent to .lookup(fibEntry.getPrefix())
139 */
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700140 shared_ptr<name_tree::Entry>
141 get(const fib::Entry& fibEntry) const;
142
Junxiao Shi4370fde2016-02-24 12:20:46 -0700143 /** \brief get NameTree entry from PIT entry
144 *
145 * This is equivalent to .lookup(pitEntry.getName()).
146 */
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700147 shared_ptr<name_tree::Entry>
Junxiao Shi4370fde2016-02-24 12:20:46 -0700148 get(const pit::Entry& pitEntry);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700149
Junxiao Shi4370fde2016-02-24 12:20:46 -0700150 /** \brief get NameTree entry from Measurements entry
151 *
152 * This is equivalent to .lookup(measurementsEntry.getName())
153 */
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700154 shared_ptr<name_tree::Entry>
155 get(const measurements::Entry& measurementsEntry) const;
156
Junxiao Shi4370fde2016-02-24 12:20:46 -0700157 /** \brief get NameTree entry from StrategyChoice entry
158 *
159 * This is equivalent to .lookup(strategyChoiceEntry.getName())
160 */
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700161 shared_ptr<name_tree::Entry>
162 get(const strategy_choice::Entry& strategyChoiceEntry) const;
163
164public: // matching
165 /**
166 * \brief Exact match lookup for the given name prefix.
167 * \return a null shared_ptr if this prefix is not found;
168 * otherwise return the Name Tree Entry address
169 */
170 shared_ptr<name_tree::Entry>
171 findExactMatch(const Name& prefix) const;
172
HYuana9b85752014-02-26 02:32:30 -0600173 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600174 * \brief Longest prefix matching for the given name
175 * \details Starts from the full name string, reduce the number of name component
HYuana9b85752014-02-26 02:32:30 -0600176 * by one each time, until an Entry is found.
177 */
178 shared_ptr<name_tree::Entry>
Junxiao Shi40631842014-03-01 13:52:37 -0700179 findLongestPrefixMatch(const Name& prefix,
Haowei Yuane1079fc2014-03-08 14:41:25 -0600180 const name_tree::EntrySelector& entrySelector =
181 name_tree::AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600182
HangZhangcb4fc832014-03-11 16:57:11 +0800183 shared_ptr<name_tree::Entry>
184 findLongestPrefixMatch(shared_ptr<name_tree::Entry> entry,
185 const name_tree::EntrySelector& entrySelector =
186 name_tree::AnyEntry()) const;
187
Junxiao Shi4370fde2016-02-24 12:20:46 -0700188 /** \brief longest prefix matching for Interest name of the PIT entry
189 *
190 * This is equivalent to .findLongestPrefixMatch(pitEntry.getName(), AnyEntry()).
191 */
192 shared_ptr<name_tree::Entry>
193 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
194
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700195 /** \brief Enumerate all the name prefixes that satisfy the prefix and entrySelector
196 * \return an unspecified type that have .begin() and .end() methods
197 * and is usable with range-based for
198 *
199 * Example:
200 * \code{.cpp}
201 * auto&& allMatches = nt.findAllMatches(Name("/A/B/C"));
202 * for (const name_tree::Entry& nte : allMatches) {
203 * ...
204 * }
205 * \endcode
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800206 * \note Iteration order is implementation-specific and is undefined
207 * \note The returned iterator may get invalidated when NameTree is modified
HYuana9b85752014-02-26 02:32:30 -0600208 */
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700209 boost::iterator_range<const_iterator>
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700210 findAllMatches(const Name& prefix,
211 const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600212
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700213public: // enumeration
Junxiao Shi60607c72014-11-26 22:40:36 -0700214 /** \brief Enumerate all entries, optionally filtered by an EntrySelector.
215 * \return an unspecified type that have .begin() and .end() methods
216 * and is usable with range-based for
217 *
218 * Example:
219 * \code{.cpp}
220 * auto&& enumerable = nt.fullEnumerate();
221 * for (const name_tree::Entry& nte : enumerable) {
222 * ...
223 * }
224 * \endcode
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800225 * \note Iteration order is implementation-specific and is undefined
226 * \note The returned iterator may get invalidated when NameTree is modified
HYuana9b85752014-02-26 02:32:30 -0600227 */
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700228 boost::iterator_range<const_iterator>
Haowei Yuane1079fc2014-03-08 14:41:25 -0600229 fullEnumerate(const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600230
Junxiao Shi60607c72014-11-26 22:40:36 -0700231 /** \brief Enumerate all entries under a prefix, optionally filtered by an EntrySubTreeSelector.
232 * \return an unspecified type that have .begin() and .end() methods
233 * and is usable with range-based for
234 *
235 * Example:
236 * \code{.cpp}
237 * auto&& enumerable = nt.partialEnumerate(Name("/A/B/C"));
238 * for (const name_tree::Entry& nte : enumerable) {
239 * ...
240 * }
241 * \endcode
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800242 * \note Iteration order is implementation-specific and is undefined
243 * \note The returned iterator may get invalidated when NameTree is modified
Junxiao Shi60607c72014-11-26 22:40:36 -0700244 */
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700245 boost::iterator_range<const_iterator>
Junxiao Shi40631842014-03-01 13:52:37 -0700246 partialEnumerate(const Name& prefix,
Junxiao Shiefceadc2014-03-09 18:52:57 -0700247 const name_tree::EntrySubTreeSelector& entrySubTreeSelector =
248 name_tree::AnyEntrySubTree()) const;
HYuana9b85752014-02-26 02:32:30 -0600249
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800250 /** \brief Get an iterator pointing to the first NameTree entry
251 * \note Iteration order is implementation-specific and is undefined
252 * \note The returned iterator may get invalidated when NameTree is modified
253 */
Haowei Yuane1079fc2014-03-08 14:41:25 -0600254 const_iterator
255 begin() const;
256
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800257 /** \brief Get an iterator referring to the past-the-end FIB entry
258 * \note Iteration order is implementation-specific and is undefined
259 * \note The returned iterator may get invalidated when NameTree is modified
260 */
Haowei Yuane1079fc2014-03-08 14:41:25 -0600261 const_iterator
262 end() const;
263
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700264 enum IteratorType {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600265 FULL_ENUMERATE_TYPE,
266 PARTIAL_ENUMERATE_TYPE,
267 FIND_ALL_MATCHES_TYPE
268 };
269
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800270 class const_iterator : public std::iterator<std::forward_iterator_tag, const name_tree::Entry>
Haowei Yuane1079fc2014-03-08 14:41:25 -0600271 {
272 public:
273 friend class NameTree;
274
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800275 const_iterator();
276
Haowei Yuane1079fc2014-03-08 14:41:25 -0600277 const_iterator(NameTree::IteratorType type,
278 const NameTree& nameTree,
279 shared_ptr<name_tree::Entry> entry,
280 const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry(),
281 const name_tree::EntrySubTreeSelector& entrySubTreeSelector = name_tree::AnyEntrySubTree());
282
283 ~const_iterator();
284
285 const name_tree::Entry&
286 operator*() const;
287
288 shared_ptr<name_tree::Entry>
289 operator->() const;
290
291 const_iterator
292 operator++();
293
294 const_iterator
295 operator++(int);
296
297 bool
298 operator==(const const_iterator& other) const;
299
300 bool
301 operator!=(const const_iterator& other) const;
302
303 private:
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800304 const NameTree* m_nameTree;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600305 shared_ptr<name_tree::Entry> m_entry;
306 shared_ptr<name_tree::Entry> m_subTreeRoot;
307 shared_ptr<name_tree::EntrySelector> m_entrySelector;
308 shared_ptr<name_tree::EntrySubTreeSelector> m_entrySubTreeSelector;
309 NameTree::IteratorType m_type;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700310 bool m_shouldVisitChildren;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600311 };
HYuana9b85752014-02-26 02:32:30 -0600312
313private:
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700314 /**
315 * \brief Resize the hash table size when its load factor reaches a threshold.
316 * \details As we are currently using a hand-written hash table implementation
317 * for the Name Tree, the hash table resize() function should be kept in the
318 * name-tree.hpp file.
319 * \param newNBuckets The number of buckets for the new hash table.
320 */
321 void
322 resize(size_t newNBuckets);
323
324private:
Haowei Yuane1079fc2014-03-08 14:41:25 -0600325 size_t m_nItems; // Number of items being stored
326 size_t m_nBuckets; // Number of hash buckets
Haowei Yuanf52dac72014-03-24 23:35:03 -0500327 size_t m_minNBuckets; // Minimum number of hash buckets
328 double m_enlargeLoadFactor;
329 size_t m_enlargeThreshold;
330 int m_enlargeFactor;
331 double m_shrinkLoadFactor;
332 size_t m_shrinkThreshold;
333 double m_shrinkFactor;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600334 name_tree::Node** m_buckets; // Name Tree Buckets in the NPHT
335 shared_ptr<name_tree::Entry> m_end;
336 const_iterator m_endIterator;
HYuana9b85752014-02-26 02:32:30 -0600337
338 /**
Haowei Yuane1079fc2014-03-08 14:41:25 -0600339 * \brief Create a Name Tree Entry if it does not exist, or return the existing
HYuana9b85752014-02-26 02:32:30 -0600340 * Name Tree Entry address.
Haowei Yuane1079fc2014-03-08 14:41:25 -0600341 * \details Called by lookup() only.
342 * \return The first item is the Name Tree Entry address, the second item is
HYuana9b85752014-02-26 02:32:30 -0600343 * a bool value indicates whether this is an old entry (false) or a new
344 * entry (true).
345 */
346 std::pair<shared_ptr<name_tree::Entry>, bool>
347 insert(const Name& prefix);
HYuana9b85752014-02-26 02:32:30 -0600348};
349
Haowei Yuane1079fc2014-03-08 14:41:25 -0600350inline NameTree::const_iterator::~const_iterator()
351{
352}
353
HYuana9b85752014-02-26 02:32:30 -0600354inline size_t
355NameTree::size() const
356{
357 return m_nItems;
358}
359
360inline size_t
361NameTree::getNBuckets() const
362{
363 return m_nBuckets;
364}
365
Junxiao Shiefceadc2014-03-09 18:52:57 -0700366inline shared_ptr<name_tree::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +0800367NameTree::get(const fib::Entry& fibEntry) const
368{
369 return fibEntry.m_nameTreeEntry;
370}
371
372inline shared_ptr<name_tree::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +0800373NameTree::get(const measurements::Entry& measurementsEntry) const
374{
375 return measurementsEntry.m_nameTreeEntry;
376}
377
378inline shared_ptr<name_tree::Entry>
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700379NameTree::get(const strategy_choice::Entry& strategyChoiceEntry) const
HangZhangcb4fc832014-03-11 16:57:11 +0800380{
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700381 return strategyChoiceEntry.m_nameTreeEntry;
HangZhangcb4fc832014-03-11 16:57:11 +0800382}
383
Haowei Yuane1079fc2014-03-08 14:41:25 -0600384inline NameTree::const_iterator
385NameTree::begin() const
386{
Junxiao Shi60607c72014-11-26 22:40:36 -0700387 return fullEnumerate().begin();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600388}
389
390inline NameTree::const_iterator
391NameTree::end() const
392{
393 return m_endIterator;
394}
395
396inline const name_tree::Entry&
397NameTree::const_iterator::operator*() const
398{
399 return *m_entry;
400}
401
402inline shared_ptr<name_tree::Entry>
403NameTree::const_iterator::operator->() const
404{
405 return m_entry;
406}
407
408inline NameTree::const_iterator
409NameTree::const_iterator::operator++(int)
410{
411 NameTree::const_iterator temp(*this);
412 ++(*this);
413 return temp;
414}
415
416inline bool
417NameTree::const_iterator::operator==(const NameTree::const_iterator& other) const
418{
419 return m_entry == other.m_entry;
420}
421
422inline bool
423NameTree::const_iterator::operator!=(const NameTree::const_iterator& other) const
424{
425 return m_entry != other.m_entry;
426}
427
HYuana9b85752014-02-26 02:32:30 -0600428} // namespace nfd
429
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700430#endif // NFD_DAEMON_TABLE_NAME_TREE_HPP