blob: ad9f3143881e9dd800cd706696a50a4257257269 [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
Junxiao Shi029401f2016-08-05 12:55:14 +000029#include "name-tree-iterator.hpp"
HYuana9b85752014-02-26 02:32:30 -060030
31namespace nfd {
32namespace name_tree {
33
HYuana9b85752014-02-26 02:32:30 -060034class NameTree : noncopyable
35{
36public:
Junxiao Shi029401f2016-08-05 12:55:14 +000037 typedef Iterator const_iterator;
Haowei Yuane1079fc2014-03-08 14:41:25 -060038
HYuana9b85752014-02-26 02:32:30 -060039 explicit
Haowei Yuanf52dac72014-03-24 23:35:03 -050040 NameTree(size_t nBuckets = 1024);
HYuana9b85752014-02-26 02:32:30 -060041
Alexander Afanasyevb3033242014-08-04 11:09:05 -070042public: // information
Junxiao Shi2570f3e2016-07-27 02:48:29 +000043 /** \brief Get the number of occupied entries in the Name Tree
HYuana9b85752014-02-26 02:32:30 -060044 */
45 size_t
Junxiao Shib660b4c2016-08-06 20:47:44 +000046 size() const
47 {
48 return m_ht.size();
49 }
HYuana9b85752014-02-26 02:32:30 -060050
Junxiao Shi2570f3e2016-07-27 02:48:29 +000051 /** \brief Get the number of buckets in the Name Tree (NPHT)
52 *
53 * The number of buckets is the one that used to create the hash
54 * table, i.e., m_nBuckets.
HYuana9b85752014-02-26 02:32:30 -060055 */
56 size_t
Junxiao Shib660b4c2016-08-06 20:47:44 +000057 getNBuckets() const
58 {
59 return m_ht.getNBuckets();
60 }
HYuana9b85752014-02-26 02:32:30 -060061
Junxiao Shib184e532016-05-26 18:09:57 +000062 /** \return Name Tree Entry on which a table entry is attached
63 */
64 template<typename ENTRY>
Junxiao Shi2570f3e2016-07-27 02:48:29 +000065 shared_ptr<Entry>
Junxiao Shib660b4c2016-08-06 20:47:44 +000066 getEntry(const ENTRY& tableEntry) const
67 {
68 return tableEntry.m_nameTreeEntry.lock();
69 }
Alexander Afanasyevb3033242014-08-04 11:09:05 -070070
71public: // mutation
Junxiao Shi2570f3e2016-07-27 02:48:29 +000072 /** \brief Look for the Name Tree Entry that contains this name prefix.
73 *
74 * Starts from the shortest name prefix, and then increase the
75 * number of name components by one each time. All non-existing Name Tree
76 * Entries will be created.
77 *
Junxiao Shib660b4c2016-08-06 20:47:44 +000078 * \param name The querying name prefix.
Junxiao Shi2570f3e2016-07-27 02:48:29 +000079 * \return The pointer to the Name Tree Entry that contains this full name prefix.
80 * \note Existing iterators are unaffected.
HYuana9b85752014-02-26 02:32:30 -060081 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +000082 shared_ptr<Entry>
Junxiao Shib660b4c2016-08-06 20:47:44 +000083 lookup(const Name& name);
HYuana9b85752014-02-26 02:32:30 -060084
Junxiao Shib184e532016-05-26 18:09:57 +000085 /** \brief get NameTree entry from FIB entry
86 *
87 * This is equivalent to .lookup(fibEntry.getPrefix())
88 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +000089 shared_ptr<Entry>
Junxiao Shib184e532016-05-26 18:09:57 +000090 lookup(const fib::Entry& fibEntry) const;
91
92 /** \brief get NameTree entry from PIT entry
93 *
94 * This is equivalent to .lookup(pitEntry.getName()).
95 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +000096 shared_ptr<Entry>
Junxiao Shib184e532016-05-26 18:09:57 +000097 lookup(const pit::Entry& pitEntry);
98
99 /** \brief get NameTree entry from Measurements entry
100 *
101 * This is equivalent to .lookup(measurementsEntry.getName())
102 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000103 shared_ptr<Entry>
Junxiao Shib184e532016-05-26 18:09:57 +0000104 lookup(const measurements::Entry& measurementsEntry) const;
105
106 /** \brief get NameTree entry from StrategyChoice entry
107 *
108 * This is equivalent to .lookup(strategyChoiceEntry.getName())
109 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000110 shared_ptr<Entry>
Junxiao Shib184e532016-05-26 18:09:57 +0000111 lookup(const strategy_choice::Entry& strategyChoiceEntry) const;
112
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000113 /** \brief Delete a Name Tree Entry if this entry is empty.
114 * \param entry The entry to be deleted if empty.
115 * \note This function must be called after a table entry is detached from Name Tree
116 * entry. The function deletes a Name Tree entry if nothing is attached to it and
117 * it has no children, then repeats the same process on its ancestors.
118 * \note Existing iterators, except those pointing to deleted entries, are unaffected.
HYuana9b85752014-02-26 02:32:30 -0600119 */
Junxiao Shib660b4c2016-08-06 20:47:44 +0000120 size_t
121 eraseEntryIfEmpty(Entry* entry);
122
123 /// \deprecated
HYuana9b85752014-02-26 02:32:30 -0600124 bool
Junxiao Shib660b4c2016-08-06 20:47:44 +0000125 eraseEntryIfEmpty(shared_ptr<Entry> entry)
126 {
127 BOOST_ASSERT(entry != nullptr);
128 return this->eraseEntryIfEmpty(entry.get()) > 0;
129 }
HYuana9b85752014-02-26 02:32:30 -0600130
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700131public: // matching
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000132 /** \brief Exact match lookup for the given name prefix.
133 * \return nullptr if this prefix is not found; otherwise return the Name Tree Entry address
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700134 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000135 shared_ptr<Entry>
Junxiao Shib660b4c2016-08-06 20:47:44 +0000136 findExactMatch(const Name& name) const;
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700137
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000138 /** \brief Longest prefix matching for the given name
139 *
140 * Starts from the full name string, reduce the number of name component
141 * by one each time, until an Entry is found.
HYuana9b85752014-02-26 02:32:30 -0600142 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000143 shared_ptr<Entry>
Junxiao Shib660b4c2016-08-06 20:47:44 +0000144 findLongestPrefixMatch(const Name& name,
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000145 const EntrySelector& entrySelector = AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600146
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000147 shared_ptr<Entry>
148 findLongestPrefixMatch(shared_ptr<Entry> entry,
149 const EntrySelector& entrySelector = AnyEntry()) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800150
Junxiao Shi4370fde2016-02-24 12:20:46 -0700151 /** \brief longest prefix matching for Interest name of the PIT entry
152 *
153 * This is equivalent to .findLongestPrefixMatch(pitEntry.getName(), AnyEntry()).
154 */
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000155 shared_ptr<Entry>
Junxiao Shi4370fde2016-02-24 12:20:46 -0700156 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
157
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700158 /** \brief Enumerate all the name prefixes that satisfy the prefix and entrySelector
159 * \return an unspecified type that have .begin() and .end() methods
160 * and is usable with range-based for
161 *
162 * Example:
163 * \code{.cpp}
164 * auto&& allMatches = nt.findAllMatches(Name("/A/B/C"));
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000165 * for (const Entry& nte : allMatches) {
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700166 * ...
167 * }
168 * \endcode
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800169 * \note Iteration order is implementation-specific and is undefined
170 * \note The returned iterator may get invalidated when NameTree is modified
HYuana9b85752014-02-26 02:32:30 -0600171 */
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700172 boost::iterator_range<const_iterator>
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700173 findAllMatches(const Name& prefix,
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000174 const EntrySelector& entrySelector = AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600175
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700176public: // enumeration
Junxiao Shi60607c72014-11-26 22:40:36 -0700177 /** \brief Enumerate all entries, optionally filtered by an EntrySelector.
178 * \return an unspecified type that have .begin() and .end() methods
179 * and is usable with range-based for
180 *
181 * Example:
182 * \code{.cpp}
183 * auto&& enumerable = nt.fullEnumerate();
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000184 * for (const Entry& nte : enumerable) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700185 * ...
186 * }
187 * \endcode
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800188 * \note Iteration order is implementation-specific and is undefined
189 * \note The returned iterator may get invalidated when NameTree is modified
HYuana9b85752014-02-26 02:32:30 -0600190 */
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700191 boost::iterator_range<const_iterator>
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000192 fullEnumerate(const EntrySelector& entrySelector = AnyEntry()) const;
HYuana9b85752014-02-26 02:32:30 -0600193
Junxiao Shi60607c72014-11-26 22:40:36 -0700194 /** \brief Enumerate all entries under a prefix, optionally filtered by an EntrySubTreeSelector.
195 * \return an unspecified type that have .begin() and .end() methods
196 * and is usable with range-based for
197 *
198 * Example:
199 * \code{.cpp}
200 * auto&& enumerable = nt.partialEnumerate(Name("/A/B/C"));
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000201 * for (const Entry& nte : enumerable) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700202 * ...
203 * }
204 * \endcode
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800205 * \note Iteration order is implementation-specific and is undefined
206 * \note The returned iterator may get invalidated when NameTree is modified
Junxiao Shi60607c72014-11-26 22:40:36 -0700207 */
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700208 boost::iterator_range<const_iterator>
Junxiao Shi40631842014-03-01 13:52:37 -0700209 partialEnumerate(const Name& prefix,
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000210 const EntrySubTreeSelector& entrySubTreeSelector = AnyEntrySubTree()) const;
HYuana9b85752014-02-26 02:32:30 -0600211
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800212 /** \brief Get an iterator pointing to the first NameTree entry
213 * \note Iteration order is implementation-specific and is undefined
214 * \note The returned iterator may get invalidated when NameTree is modified
215 */
Haowei Yuane1079fc2014-03-08 14:41:25 -0600216 const_iterator
Junxiao Shib660b4c2016-08-06 20:47:44 +0000217 begin() const
218 {
219 return fullEnumerate().begin();
220 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600221
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800222 /** \brief Get an iterator referring to the past-the-end FIB entry
223 * \note Iteration order is implementation-specific and is undefined
224 * \note The returned iterator may get invalidated when NameTree is modified
225 */
Haowei Yuane1079fc2014-03-08 14:41:25 -0600226 const_iterator
Junxiao Shib660b4c2016-08-06 20:47:44 +0000227 end() const
228 {
229 return Iterator();
230 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600231
HYuana9b85752014-02-26 02:32:30 -0600232private:
Junxiao Shib660b4c2016-08-06 20:47:44 +0000233 Hashtable m_ht;
Junxiao Shib184e532016-05-26 18:09:57 +0000234
Junxiao Shib660b4c2016-08-06 20:47:44 +0000235 friend class EnumerationImpl;
HYuana9b85752014-02-26 02:32:30 -0600236};
237
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000238} // namespace name_tree
239
240using name_tree::NameTree;
241
HYuana9b85752014-02-26 02:32:30 -0600242} // namespace nfd
243
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700244#endif // NFD_DAEMON_TABLE_NAME_TREE_HPP