HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_NAME_TREE_HPP |
| 27 | #define NFD_DAEMON_TABLE_NAME_TREE_HPP |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 28 | |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 29 | #include "name-tree-iterator.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace name_tree { |
| 33 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | class NameTree : noncopyable |
| 35 | { |
| 36 | public: |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 37 | typedef Iterator const_iterator; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 38 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 39 | explicit |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 40 | NameTree(size_t nBuckets = 1024); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 41 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 42 | public: // information |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 43 | /** \brief Get the number of occupied entries in the Name Tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 44 | */ |
| 45 | size_t |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 46 | size() const |
| 47 | { |
| 48 | return m_ht.size(); |
| 49 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 50 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 51 | /** \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. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 55 | */ |
| 56 | size_t |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 57 | getNBuckets() const |
| 58 | { |
| 59 | return m_ht.getNBuckets(); |
| 60 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 61 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 62 | /** \return Name Tree Entry on which a table entry is attached |
| 63 | */ |
| 64 | template<typename ENTRY> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 65 | shared_ptr<Entry> |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 66 | getEntry(const ENTRY& tableEntry) const |
| 67 | { |
| 68 | return tableEntry.m_nameTreeEntry.lock(); |
| 69 | } |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 70 | |
| 71 | public: // mutation |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 72 | /** \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 Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 78 | * \param name The querying name prefix. |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 79 | * \return The pointer to the Name Tree Entry that contains this full name prefix. |
| 80 | * \note Existing iterators are unaffected. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 81 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 82 | shared_ptr<Entry> |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 83 | lookup(const Name& name); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 84 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 85 | /** \brief get NameTree entry from FIB entry |
| 86 | * |
| 87 | * This is equivalent to .lookup(fibEntry.getPrefix()) |
| 88 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 89 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 90 | 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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 96 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 97 | 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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 103 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 104 | 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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 110 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 111 | lookup(const strategy_choice::Entry& strategyChoiceEntry) const; |
| 112 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 113 | /** \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. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 119 | */ |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 120 | size_t |
| 121 | eraseEntryIfEmpty(Entry* entry); |
| 122 | |
| 123 | /// \deprecated |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 124 | bool |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 125 | eraseEntryIfEmpty(shared_ptr<Entry> entry) |
| 126 | { |
| 127 | BOOST_ASSERT(entry != nullptr); |
| 128 | return this->eraseEntryIfEmpty(entry.get()) > 0; |
| 129 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 130 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 131 | public: // matching |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 132 | /** \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 Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 134 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 135 | shared_ptr<Entry> |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 136 | findExactMatch(const Name& name) const; |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 138 | /** \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. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 142 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 143 | shared_ptr<Entry> |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 144 | findLongestPrefixMatch(const Name& name, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 145 | const EntrySelector& entrySelector = AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 146 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 147 | shared_ptr<Entry> |
| 148 | findLongestPrefixMatch(shared_ptr<Entry> entry, |
| 149 | const EntrySelector& entrySelector = AnyEntry()) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 150 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 151 | /** \brief longest prefix matching for Interest name of the PIT entry |
| 152 | * |
| 153 | * This is equivalent to .findLongestPrefixMatch(pitEntry.getName(), AnyEntry()). |
| 154 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 155 | shared_ptr<Entry> |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 156 | findLongestPrefixMatch(const pit::Entry& pitEntry) const; |
| 157 | |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 158 | /** \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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 165 | * for (const Entry& nte : allMatches) { |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 166 | * ... |
| 167 | * } |
| 168 | * \endcode |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 169 | * \note Iteration order is implementation-specific and is undefined |
| 170 | * \note The returned iterator may get invalidated when NameTree is modified |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 171 | */ |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 172 | boost::iterator_range<const_iterator> |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 173 | findAllMatches(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 174 | const EntrySelector& entrySelector = AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 175 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 176 | public: // enumeration |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 177 | /** \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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 184 | * for (const Entry& nte : enumerable) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 185 | * ... |
| 186 | * } |
| 187 | * \endcode |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 188 | * \note Iteration order is implementation-specific and is undefined |
| 189 | * \note The returned iterator may get invalidated when NameTree is modified |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 190 | */ |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 191 | boost::iterator_range<const_iterator> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 192 | fullEnumerate(const EntrySelector& entrySelector = AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 193 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 194 | /** \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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 201 | * for (const Entry& nte : enumerable) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 202 | * ... |
| 203 | * } |
| 204 | * \endcode |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 205 | * \note Iteration order is implementation-specific and is undefined |
| 206 | * \note The returned iterator may get invalidated when NameTree is modified |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 207 | */ |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 208 | boost::iterator_range<const_iterator> |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 209 | partialEnumerate(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 210 | const EntrySubTreeSelector& entrySubTreeSelector = AnyEntrySubTree()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 211 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 212 | /** \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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 216 | const_iterator |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 217 | begin() const |
| 218 | { |
| 219 | return fullEnumerate().begin(); |
| 220 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 221 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 222 | /** \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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 226 | const_iterator |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 227 | end() const |
| 228 | { |
| 229 | return Iterator(); |
| 230 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 231 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 232 | private: |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 233 | Hashtable m_ht; |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 234 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 235 | friend class EnumerationImpl; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 236 | }; |
| 237 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 238 | } // namespace name_tree |
| 239 | |
| 240 | using name_tree::NameTree; |
| 241 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 242 | } // namespace nfd |
| 243 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 244 | #endif // NFD_DAEMON_TABLE_NAME_TREE_HPP |