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 | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
| 26 | #include "name-tree.hpp" |
| 27 | #include "core/logger.hpp" |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 28 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 29 | #include <boost/concept/assert.hpp> |
| 30 | #include <boost/concept_check.hpp> |
| 31 | #include <type_traits> |
| 32 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 34 | namespace name_tree { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 35 | |
| 36 | NFD_LOG_INIT("NameTree"); |
| 37 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 38 | NameTree::NameTree(size_t nBuckets) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 39 | : m_ht(HashtableOptions(nBuckets)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 40 | { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 41 | } |
| 42 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 43 | shared_ptr<Entry> |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 44 | NameTree::lookup(const Name& name) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 45 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 46 | NFD_LOG_TRACE("lookup " << name); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 47 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 48 | HashSequence hashes = computeHashes(name); |
| 49 | const Node* node = nullptr; |
| 50 | Entry* parent = nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 51 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 52 | for (size_t prefixLen = 0; prefixLen <= name.size(); ++prefixLen) { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 53 | bool isNew = false; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 54 | std::tie(node, isNew) = m_ht.insert(name, prefixLen, hashes); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 55 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 56 | if (isNew && parent != nullptr) { |
| 57 | node->entry->setParent(*parent); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 58 | } |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 59 | parent = node->entry.get(); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 60 | } |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 61 | return node->entry; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 64 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 65 | NameTree::lookup(const fib::Entry& fibEntry) const |
| 66 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 67 | shared_ptr<Entry> nte = this->getEntry(fibEntry); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 68 | BOOST_ASSERT(nte == nullptr || nte->getFibEntry() == &fibEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 69 | return nte; |
| 70 | } |
| 71 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 72 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 73 | NameTree::lookup(const pit::Entry& pitEntry) |
| 74 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 75 | shared_ptr<Entry> nte = this->getEntry(pitEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 76 | if (nte == nullptr) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 80 | if (nte->getName().size() == pitEntry.getName().size()) { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 81 | return nte; |
| 82 | } |
| 83 | |
| 84 | BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest()); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 85 | BOOST_ASSERT(nte->getName() == pitEntry.getName().getPrefix(-1)); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 86 | return this->lookup(pitEntry.getName()); |
| 87 | } |
| 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 | NameTree::lookup(const measurements::Entry& measurementsEntry) const |
| 91 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 92 | shared_ptr<Entry> nte = this->getEntry(measurementsEntry); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 93 | BOOST_ASSERT(nte == nullptr || nte->getMeasurementsEntry() == &measurementsEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 94 | return nte; |
| 95 | } |
| 96 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 97 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 98 | NameTree::lookup(const strategy_choice::Entry& strategyChoiceEntry) const |
| 99 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 100 | shared_ptr<Entry> nte = this->getEntry(strategyChoiceEntry); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 101 | BOOST_ASSERT(nte == nullptr || nte->getStrategyChoiceEntry() == &strategyChoiceEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 102 | return nte; |
| 103 | } |
| 104 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 105 | size_t |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 106 | NameTree::eraseIfEmpty(Entry* entry) |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 107 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 108 | BOOST_ASSERT(entry != nullptr); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 110 | size_t nErased = 0; |
| 111 | for (Entry* parent = nullptr; entry != nullptr && entry->isEmpty(); entry = parent) { |
| 112 | parent = entry->getParent(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 114 | if (parent != nullptr) { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 115 | entry->unsetParent(); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 116 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 118 | m_ht.erase(getNode(*entry)); |
| 119 | ++nErased; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 122 | if (nErased == 0) { |
| 123 | NFD_LOG_TRACE("not-erase " << entry->getName()); |
| 124 | } |
| 125 | return nErased; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 128 | Entry* |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 129 | NameTree::findExactMatch(const Name& name) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 130 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 131 | const Node* node = m_ht.find(name, name.size()); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 132 | return node == nullptr ? nullptr : node->entry.get(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 133 | } |
| 134 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 135 | Entry* |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 136 | NameTree::findLongestPrefixMatch(const Name& name, const EntrySelector& entrySelector) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 137 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 138 | HashSequence hashes = computeHashes(name); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 139 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 140 | for (ssize_t prefixLen = name.size(); prefixLen >= 0; --prefixLen) { |
| 141 | const Node* node = m_ht.find(name, prefixLen, hashes); |
| 142 | if (node != nullptr && entrySelector(*node->entry)) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 143 | return node->entry.get(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 144 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 145 | } |
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 | return nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 150 | Entry* |
| 151 | NameTree::findLongestPrefixMatch(const Entry& entry1, const EntrySelector& entrySelector) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 152 | { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 153 | Entry* entry = const_cast<Entry*>(&entry1); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 154 | while (entry != nullptr) { |
| 155 | if (entrySelector(*entry)) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 156 | return entry; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 157 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 158 | entry = entry->getParent(); |
| 159 | } |
| 160 | return nullptr; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 163 | Entry* |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 164 | NameTree::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 165 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 166 | shared_ptr<Entry> nte = this->getEntry(pitEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 167 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 168 | if (nte->getName().size() == pitEntry.getName().size()) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 169 | return nte.get(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 170 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 171 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 172 | // special case: PIT entry whose Interest name ends with an implicit digest |
| 173 | // are attached to the name tree entry with one-shorter-prefix. |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 174 | BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest()); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 175 | BOOST_ASSERT(nte->getName() == pitEntry.getName().getPrefix(-1)); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 176 | Entry* exact = this->findExactMatch(pitEntry.getName()); |
| 177 | return exact == nullptr ? nte.get() : exact; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 178 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 179 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 180 | boost::iterator_range<NameTree::const_iterator> |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 181 | NameTree::findAllMatches(const Name& name, const EntrySelector& entrySelector) const |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 182 | { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 183 | NFD_LOG_TRACE("NameTree::findAllMatches" << name); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 184 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 185 | // As we are using Name Prefix Hash Table, and the current LPM() is |
| 186 | // implemented as starting from full name, and reduce the number of |
| 187 | // components by 1 each time, we could use it here. |
| 188 | // For trie-like design, it could be more efficient by walking down the |
| 189 | // trie from the root node. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 190 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 191 | Entry* entry = findLongestPrefixMatch(name, entrySelector); |
| 192 | return {Iterator(make_shared<PrefixMatchImpl>(*this, entrySelector), entry), end()}; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 193 | } |
| 194 | |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 195 | boost::iterator_range<NameTree::const_iterator> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 196 | NameTree::fullEnumerate(const EntrySelector& entrySelector) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 197 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 198 | NFD_LOG_TRACE("fullEnumerate"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 199 | |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 200 | return {Iterator(make_shared<FullEnumerationImpl>(*this, entrySelector), nullptr), end()}; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 203 | boost::iterator_range<NameTree::const_iterator> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 204 | NameTree::partialEnumerate(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 205 | const EntrySubTreeSelector& entrySubTreeSelector) const |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 206 | { |
| 207 | // the first step is to process the root node |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame^] | 208 | Entry* entry = this->findExactMatch(prefix); |
| 209 | return {Iterator(make_shared<PartialEnumerationImpl>(*this, entrySubTreeSelector), entry), end()}; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 210 | } |
| 211 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 212 | } // namespace name_tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 213 | } // namespace nfd |