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" |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 28 | #include "core/city-hash.hpp" |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 29 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 30 | #include <boost/concept/assert.hpp> |
| 31 | #include <boost/concept_check.hpp> |
| 32 | #include <type_traits> |
| 33 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | namespace nfd { |
| 35 | |
| 36 | NFD_LOG_INIT("NameTree"); |
| 37 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 38 | // http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 39 | BOOST_CONCEPT_ASSERT((boost::ForwardIterator<NameTree::const_iterator>)); |
| 40 | // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html, |
| 41 | // which doesn't require DefaultConstructible |
| 42 | #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 43 | static_assert(std::is_default_constructible<NameTree::const_iterator>::value, |
| 44 | "NameTree::const_iterator must be default-constructible"); |
| 45 | #else |
| 46 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<NameTree::const_iterator>)); |
| 47 | #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 48 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 49 | namespace name_tree { |
| 50 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 51 | class Hash32 |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 52 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 53 | public: |
| 54 | static size_t |
| 55 | compute(const char* buffer, size_t length) |
| 56 | { |
| 57 | return static_cast<size_t>(CityHash32(buffer, length)); |
| 58 | } |
| 59 | }; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 60 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 61 | class Hash64 |
| 62 | { |
| 63 | public: |
| 64 | static size_t |
| 65 | compute(const char* buffer, size_t length) |
| 66 | { |
| 67 | return static_cast<size_t>(CityHash64(buffer, length)); |
| 68 | } |
| 69 | }; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 70 | |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 71 | /// @cond NoDocumentation |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 72 | typedef boost::mpl::if_c<sizeof(size_t) >= 8, Hash64, Hash32>::type CityHash; |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 73 | /// @endcond |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 74 | |
| 75 | // Interface of different hash functions |
| 76 | size_t |
| 77 | computeHash(const Name& prefix) |
| 78 | { |
| 79 | prefix.wireEncode(); // guarantees prefix's wire buffer is not empty |
| 80 | |
| 81 | size_t hashValue = 0; |
| 82 | size_t hashUpdate = 0; |
| 83 | |
| 84 | for (Name::const_iterator it = prefix.begin(); it != prefix.end(); it++) |
| 85 | { |
| 86 | const char* wireFormat = reinterpret_cast<const char*>( it->wire() ); |
| 87 | hashUpdate = CityHash::compute(wireFormat, it->size()); |
| 88 | hashValue ^= hashUpdate; |
| 89 | } |
| 90 | |
| 91 | return hashValue; |
| 92 | } |
| 93 | |
| 94 | std::vector<size_t> |
| 95 | computeHashSet(const Name& prefix) |
| 96 | { |
| 97 | prefix.wireEncode(); // guarantees prefix's wire buffer is not empty |
| 98 | |
| 99 | size_t hashValue = 0; |
| 100 | size_t hashUpdate = 0; |
| 101 | |
| 102 | std::vector<size_t> hashValueSet; |
| 103 | hashValueSet.push_back(hashValue); |
| 104 | |
| 105 | for (Name::const_iterator it = prefix.begin(); it != prefix.end(); it++) |
| 106 | { |
| 107 | const char* wireFormat = reinterpret_cast<const char*>( it->wire() ); |
| 108 | hashUpdate = CityHash::compute(wireFormat, it->size()); |
| 109 | hashValue ^= hashUpdate; |
| 110 | hashValueSet.push_back(hashValue); |
| 111 | } |
| 112 | |
| 113 | return hashValueSet; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } // namespace name_tree |
| 117 | |
| 118 | NameTree::NameTree(size_t nBuckets) |
| 119 | : m_nItems(0) |
| 120 | , m_nBuckets(nBuckets) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 121 | , m_minNBuckets(nBuckets) |
| 122 | , m_enlargeLoadFactor(0.5) // more than 50% buckets loaded |
| 123 | , m_enlargeFactor(2) // double the hash table size |
| 124 | , m_shrinkLoadFactor(0.1) // less than 10% buckets loaded |
| 125 | , m_shrinkFactor(0.5) // reduce the number of buckets by half |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 126 | , m_endIterator(FULL_ENUMERATE_TYPE, *this, m_end) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 127 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 128 | m_enlargeThreshold = static_cast<size_t>(m_enlargeLoadFactor * |
| 129 | static_cast<double>(m_nBuckets)); |
| 130 | |
| 131 | m_shrinkThreshold = static_cast<size_t>(m_shrinkLoadFactor * |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 132 | static_cast<double>(m_nBuckets)); |
| 133 | |
| 134 | // array of node pointers |
| 135 | m_buckets = new name_tree::Node*[m_nBuckets]; |
| 136 | // Initialize the pointer array |
| 137 | for (size_t i = 0; i < m_nBuckets; i++) |
| 138 | m_buckets[i] = 0; |
| 139 | } |
| 140 | |
| 141 | NameTree::~NameTree() |
| 142 | { |
| 143 | for (size_t i = 0; i < m_nBuckets; i++) |
| 144 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 145 | if (m_buckets[i] != 0) { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 146 | delete m_buckets[i]; |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 147 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | delete [] m_buckets; |
| 151 | } |
| 152 | |
| 153 | // insert() is a private function, and called by only lookup() |
| 154 | std::pair<shared_ptr<name_tree::Entry>, bool> |
| 155 | NameTree::insert(const Name& prefix) |
| 156 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 157 | NFD_LOG_TRACE("insert " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 158 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 159 | size_t hashValue = name_tree::computeHash(prefix); |
| 160 | size_t loc = hashValue % m_nBuckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 161 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 162 | NFD_LOG_TRACE("Name " << prefix << " hash value = " << hashValue << " location = " << loc); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 163 | |
| 164 | // Check if this Name has been stored |
| 165 | name_tree::Node* node = m_buckets[loc]; |
| 166 | name_tree::Node* nodePrev = node; // initialize nodePrev to node |
| 167 | |
| 168 | for (node = m_buckets[loc]; node != 0; node = node->m_next) |
| 169 | { |
| 170 | if (static_cast<bool>(node->m_entry)) |
| 171 | { |
| 172 | if (prefix == node->m_entry->m_prefix) |
| 173 | { |
| 174 | return std::make_pair(node->m_entry, false); // false: old entry |
| 175 | } |
| 176 | } |
| 177 | nodePrev = node; |
| 178 | } |
| 179 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 180 | NFD_LOG_TRACE("Did not find " << prefix << ", need to insert it to the table"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 181 | |
| 182 | // If no bucket is empty occupied, we need to create a new node, and it is |
| 183 | // linked from nodePrev |
| 184 | node = new name_tree::Node(); |
| 185 | node->m_prev = nodePrev; |
| 186 | |
| 187 | if (nodePrev == 0) |
| 188 | { |
| 189 | m_buckets[loc] = node; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | nodePrev->m_next = node; |
| 194 | } |
| 195 | |
| 196 | // Create a new Entry |
| 197 | shared_ptr<name_tree::Entry> entry(make_shared<name_tree::Entry>(prefix)); |
| 198 | entry->setHash(hashValue); |
| 199 | node->m_entry = entry; // link the Entry to its Node |
| 200 | entry->m_node = node; // link the node to Entry. Used in eraseEntryIfEmpty. |
| 201 | |
| 202 | return std::make_pair(entry, true); // true: new entry |
| 203 | } |
| 204 | |
| 205 | // Name Prefix Lookup. Create Name Tree Entry if not found |
| 206 | shared_ptr<name_tree::Entry> |
| 207 | NameTree::lookup(const Name& prefix) |
| 208 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 209 | NFD_LOG_TRACE("lookup " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 210 | |
| 211 | shared_ptr<name_tree::Entry> entry; |
| 212 | shared_ptr<name_tree::Entry> parent; |
| 213 | |
| 214 | for (size_t i = 0; i <= prefix.size(); i++) |
| 215 | { |
| 216 | Name temp = prefix.getPrefix(i); |
| 217 | |
| 218 | // insert() will create the entry if it does not exist. |
| 219 | std::pair<shared_ptr<name_tree::Entry>, bool> ret = insert(temp); |
| 220 | entry = ret.first; |
| 221 | |
| 222 | if (ret.second == true) |
| 223 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 224 | m_nItems++; // Increase the counter |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 225 | entry->m_parent = parent; |
| 226 | |
| 227 | if (static_cast<bool>(parent)) |
| 228 | { |
| 229 | parent->m_children.push_back(entry); |
| 230 | } |
| 231 | } |
| 232 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 233 | if (m_nItems > m_enlargeThreshold) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 234 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 235 | resize(m_enlargeFactor * m_nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | parent = entry; |
| 239 | } |
| 240 | return entry; |
| 241 | } |
| 242 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 243 | // return {false: this entry is not empty, true: this entry is empty and erased} |
| 244 | bool |
| 245 | NameTree::eraseEntryIfEmpty(shared_ptr<name_tree::Entry> entry) |
| 246 | { |
| 247 | BOOST_ASSERT(static_cast<bool>(entry)); |
| 248 | |
| 249 | NFD_LOG_TRACE("eraseEntryIfEmpty " << entry->getPrefix()); |
| 250 | |
| 251 | // first check if this Entry can be erased |
| 252 | if (entry->isEmpty()) |
| 253 | { |
| 254 | // update child-related info in the parent |
| 255 | shared_ptr<name_tree::Entry> parent = entry->getParent(); |
| 256 | |
| 257 | if (static_cast<bool>(parent)) |
| 258 | { |
| 259 | std::vector<shared_ptr<name_tree::Entry> >& parentChildrenList = |
| 260 | parent->getChildren(); |
| 261 | |
| 262 | bool isFound = false; |
| 263 | size_t size = parentChildrenList.size(); |
| 264 | for (size_t i = 0; i < size; i++) |
| 265 | { |
| 266 | if (parentChildrenList[i] == entry) |
| 267 | { |
| 268 | parentChildrenList[i] = parentChildrenList[size - 1]; |
| 269 | parentChildrenList.pop_back(); |
| 270 | isFound = true; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | BOOST_VERIFY(isFound == true); |
| 276 | } |
| 277 | |
| 278 | // remove this Entry and its Name Tree Node |
| 279 | name_tree::Node* node = entry->m_node; |
| 280 | name_tree::Node* nodePrev = node->m_prev; |
| 281 | |
| 282 | // configure the previous node |
| 283 | if (nodePrev != 0) |
| 284 | { |
| 285 | // link the previous node to the next node |
| 286 | nodePrev->m_next = node->m_next; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | m_buckets[entry->getHash() % m_nBuckets] = node->m_next; |
| 291 | } |
| 292 | |
| 293 | // link the previous node with the next node (skip the erased one) |
| 294 | if (node->m_next != 0) |
| 295 | { |
| 296 | node->m_next->m_prev = nodePrev; |
| 297 | node->m_next = 0; |
| 298 | } |
| 299 | |
| 300 | BOOST_ASSERT(node->m_next == 0); |
| 301 | |
| 302 | m_nItems--; |
| 303 | delete node; |
| 304 | |
| 305 | if (static_cast<bool>(parent)) |
| 306 | eraseEntryIfEmpty(parent); |
| 307 | |
| 308 | size_t newNBuckets = static_cast<size_t>(m_shrinkFactor * |
| 309 | static_cast<double>(m_nBuckets)); |
| 310 | |
| 311 | if (newNBuckets >= m_minNBuckets && m_nItems < m_shrinkThreshold) |
| 312 | { |
| 313 | resize(newNBuckets); |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | |
| 318 | } // if this entry is empty |
| 319 | |
| 320 | return false; // if this entry is not empty |
| 321 | } |
| 322 | |
| 323 | shared_ptr<name_tree::Entry> |
| 324 | NameTree::get(const pit::Entry& pitEntry) |
| 325 | { |
| 326 | shared_ptr<name_tree::Entry> nte = pitEntry.m_nameTreeEntry; |
| 327 | if (nte->getPrefix().size() == pitEntry.getName().size()) { |
| 328 | return nte; |
| 329 | } |
| 330 | |
| 331 | BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest()); |
| 332 | BOOST_ASSERT(nte->getPrefix() == pitEntry.getName().getPrefix(-1)); |
| 333 | return this->lookup(pitEntry.getName()); |
| 334 | } |
| 335 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 336 | // Exact Match |
| 337 | shared_ptr<name_tree::Entry> |
| 338 | NameTree::findExactMatch(const Name& prefix) const |
| 339 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 340 | NFD_LOG_TRACE("findExactMatch " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 341 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 342 | size_t hashValue = name_tree::computeHash(prefix); |
| 343 | size_t loc = hashValue % m_nBuckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 344 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 345 | NFD_LOG_TRACE("Name " << prefix << " hash value = " << hashValue << |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 346 | " location = " << loc); |
| 347 | |
| 348 | shared_ptr<name_tree::Entry> entry; |
| 349 | name_tree::Node* node = 0; |
| 350 | |
| 351 | for (node = m_buckets[loc]; node != 0; node = node->m_next) |
| 352 | { |
| 353 | entry = node->m_entry; |
| 354 | if (static_cast<bool>(entry)) |
| 355 | { |
| 356 | if (hashValue == entry->getHash() && prefix == entry->getPrefix()) |
| 357 | { |
| 358 | return entry; |
| 359 | } |
| 360 | } // if entry |
| 361 | } // for node |
| 362 | |
| 363 | // if not found, the default value of entry (null pointer) will be returned |
| 364 | entry.reset(); |
| 365 | return entry; |
| 366 | } |
| 367 | |
| 368 | // Longest Prefix Match |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 369 | shared_ptr<name_tree::Entry> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 370 | NameTree::findLongestPrefixMatch(const Name& prefix, const name_tree::EntrySelector& entrySelector) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 371 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 372 | NFD_LOG_TRACE("findLongestPrefixMatch " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 373 | |
| 374 | shared_ptr<name_tree::Entry> entry; |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 375 | std::vector<size_t> hashValueSet = name_tree::computeHashSet(prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 376 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 377 | size_t hashValue = 0; |
| 378 | size_t loc = 0; |
| 379 | |
| 380 | for (int i = static_cast<int>(prefix.size()); i >= 0; i--) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 381 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 382 | hashValue = hashValueSet[i]; |
| 383 | loc = hashValue % m_nBuckets; |
| 384 | |
| 385 | name_tree::Node* node = 0; |
| 386 | for (node = m_buckets[loc]; node != 0; node = node->m_next) |
| 387 | { |
| 388 | entry = node->m_entry; |
| 389 | if (static_cast<bool>(entry)) |
| 390 | { |
| 391 | // isPrefixOf() is used to avoid making a copy of the name |
| 392 | if (hashValue == entry->getHash() && |
| 393 | entry->getPrefix().isPrefixOf(prefix) && |
| 394 | entrySelector(*entry)) |
| 395 | { |
| 396 | return entry; |
| 397 | } |
| 398 | } // if entry |
| 399 | } // for node |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 400 | } |
| 401 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 402 | // if not found, the default value of entry (null pointer) will be returned |
| 403 | entry.reset(); |
| 404 | return entry; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 405 | } |
| 406 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 407 | shared_ptr<name_tree::Entry> |
| 408 | NameTree::findLongestPrefixMatch(shared_ptr<name_tree::Entry> entry, |
| 409 | const name_tree::EntrySelector& entrySelector) const |
| 410 | { |
| 411 | while (static_cast<bool>(entry)) |
| 412 | { |
| 413 | if (entrySelector(*entry)) |
| 414 | return entry; |
| 415 | entry = entry->getParent(); |
| 416 | } |
| 417 | return shared_ptr<name_tree::Entry>(); |
| 418 | } |
| 419 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 420 | shared_ptr<name_tree::Entry> |
| 421 | NameTree::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 422 | { |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 423 | shared_ptr<name_tree::Entry> nte = pitEntry.m_nameTreeEntry; |
| 424 | if (nte->getPrefix().size() == pitEntry.getName().size()) { |
| 425 | return nte; |
| 426 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 427 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 428 | BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest()); |
| 429 | BOOST_ASSERT(nte->getPrefix() == pitEntry.getName().getPrefix(-1)); |
| 430 | shared_ptr<name_tree::Entry> exact = this->findExactMatch(pitEntry.getName()); |
| 431 | return exact == nullptr ? nte : exact; |
| 432 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 433 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 434 | boost::iterator_range<NameTree::const_iterator> |
| 435 | NameTree::findAllMatches(const Name& prefix, |
| 436 | const name_tree::EntrySelector& entrySelector) const |
| 437 | { |
| 438 | NFD_LOG_TRACE("NameTree::findAllMatches" << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 439 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 440 | // As we are using Name Prefix Hash Table, and the current LPM() is |
| 441 | // implemented as starting from full name, and reduce the number of |
| 442 | // components by 1 each time, we could use it here. |
| 443 | // For trie-like design, it could be more efficient by walking down the |
| 444 | // trie from the root node. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 445 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 446 | shared_ptr<name_tree::Entry> entry = findLongestPrefixMatch(prefix, entrySelector); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 447 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame^] | 448 | if (static_cast<bool>(entry)) { |
| 449 | const_iterator begin(FIND_ALL_MATCHES_TYPE, *this, entry, entrySelector); |
| 450 | return {begin, end()}; |
| 451 | } |
| 452 | // If none of the entry satisfies the requirements, then return the end() iterator. |
| 453 | return {end(), end()}; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 454 | } |
| 455 | |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 456 | boost::iterator_range<NameTree::const_iterator> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 457 | NameTree::fullEnumerate(const name_tree::EntrySelector& entrySelector) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 458 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 459 | NFD_LOG_TRACE("fullEnumerate"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 460 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 461 | // find the first eligible entry |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 462 | for (size_t i = 0; i < m_nBuckets; i++) { |
| 463 | for (name_tree::Node* node = m_buckets[i]; node != 0; node = node->m_next) { |
| 464 | if (static_cast<bool>(node->m_entry) && entrySelector(*node->m_entry)) { |
| 465 | const_iterator it(FULL_ENUMERATE_TYPE, *this, node->m_entry, entrySelector); |
| 466 | return {it, end()}; |
| 467 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 468 | } |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 469 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 470 | |
| 471 | // If none of the entry satisfies the requirements, then return the end() iterator. |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 472 | return {end(), end()}; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 473 | } |
| 474 | |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 475 | boost::iterator_range<NameTree::const_iterator> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 476 | NameTree::partialEnumerate(const Name& prefix, |
| 477 | const name_tree::EntrySubTreeSelector& entrySubTreeSelector) const |
| 478 | { |
| 479 | // the first step is to process the root node |
| 480 | shared_ptr<name_tree::Entry> entry = findExactMatch(prefix); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 481 | if (!static_cast<bool>(entry)) { |
| 482 | return {end(), end()}; |
| 483 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 484 | |
| 485 | std::pair<bool, bool>result = entrySubTreeSelector(*entry); |
| 486 | const_iterator it(PARTIAL_ENUMERATE_TYPE, |
| 487 | *this, |
| 488 | entry, |
| 489 | name_tree::AnyEntry(), |
| 490 | entrySubTreeSelector); |
| 491 | |
| 492 | it.m_shouldVisitChildren = (result.second && entry->hasChildren()); |
| 493 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 494 | if (result.first) { |
| 495 | // root node is acceptable |
| 496 | } |
| 497 | else { |
| 498 | // let the ++ operator handle it |
| 499 | ++it; |
| 500 | } |
| 501 | return {it, end()}; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 502 | } |
| 503 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 504 | // Hash Table Resize |
| 505 | void |
| 506 | NameTree::resize(size_t newNBuckets) |
| 507 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 508 | NFD_LOG_TRACE("resize"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 509 | |
| 510 | name_tree::Node** newBuckets = new name_tree::Node*[newNBuckets]; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 511 | size_t count = 0; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 512 | |
| 513 | // referenced ccnx hashtb.c hashtb_rehash() |
| 514 | name_tree::Node** pp = 0; |
| 515 | name_tree::Node* p = 0; |
| 516 | name_tree::Node* pre = 0; |
| 517 | name_tree::Node* q = 0; // record p->m_next |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 518 | size_t i; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 519 | uint32_t h; |
| 520 | uint32_t b; |
| 521 | |
| 522 | for (i = 0; i < newNBuckets; i++) |
| 523 | { |
| 524 | newBuckets[i] = 0; |
| 525 | } |
| 526 | |
| 527 | for (i = 0; i < m_nBuckets; i++) |
| 528 | { |
| 529 | for (p = m_buckets[i]; p != 0; p = q) |
| 530 | { |
| 531 | count++; |
| 532 | q = p->m_next; |
| 533 | BOOST_ASSERT(static_cast<bool>(p->m_entry)); |
| 534 | h = p->m_entry->m_hash; |
| 535 | b = h % newNBuckets; |
Haowei Yuan | 694bfb6 | 2014-04-01 14:44:11 -0500 | [diff] [blame] | 536 | pre = 0; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 537 | for (pp = &newBuckets[b]; *pp != 0; pp = &((*pp)->m_next)) |
| 538 | { |
| 539 | pre = *pp; |
| 540 | continue; |
| 541 | } |
| 542 | p->m_prev = pre; |
| 543 | p->m_next = *pp; // Actually *pp always == 0 in this case |
| 544 | *pp = p; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | BOOST_ASSERT(count == m_nItems); |
| 549 | |
| 550 | name_tree::Node** oldBuckets = m_buckets; |
| 551 | m_buckets = newBuckets; |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 552 | delete [] oldBuckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 553 | |
| 554 | m_nBuckets = newNBuckets; |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 555 | |
| 556 | m_enlargeThreshold = static_cast<size_t>(m_enlargeLoadFactor * |
| 557 | static_cast<double>(m_nBuckets)); |
| 558 | m_shrinkThreshold = static_cast<size_t>(m_shrinkLoadFactor * |
| 559 | static_cast<double>(m_nBuckets)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // For debugging |
| 563 | void |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 564 | NameTree::dump(std::ostream& output) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 565 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 566 | NFD_LOG_TRACE("dump()"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 567 | |
| 568 | name_tree::Node* node = 0; |
| 569 | shared_ptr<name_tree::Entry> entry; |
| 570 | |
| 571 | using std::endl; |
| 572 | |
| 573 | for (size_t i = 0; i < m_nBuckets; i++) |
| 574 | { |
| 575 | for (node = m_buckets[i]; node != 0; node = node->m_next) |
| 576 | { |
| 577 | entry = node->m_entry; |
| 578 | |
| 579 | // if the Entry exist, dump its information |
| 580 | if (static_cast<bool>(entry)) |
| 581 | { |
| 582 | output << "Bucket" << i << "\t" << entry->m_prefix.toUri() << endl; |
| 583 | output << "\t\tHash " << entry->m_hash << endl; |
| 584 | |
| 585 | if (static_cast<bool>(entry->m_parent)) |
| 586 | { |
| 587 | output << "\t\tparent->" << entry->m_parent->m_prefix.toUri(); |
| 588 | } |
| 589 | else |
| 590 | { |
| 591 | output << "\t\tROOT"; |
| 592 | } |
| 593 | output << endl; |
| 594 | |
| 595 | if (entry->m_children.size() != 0) |
| 596 | { |
| 597 | output << "\t\tchildren = " << entry->m_children.size() << endl; |
| 598 | |
| 599 | for (size_t j = 0; j < entry->m_children.size(); j++) |
| 600 | { |
| 601 | output << "\t\t\tChild " << j << " " << |
| 602 | entry->m_children[j]->getPrefix() << endl; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | } // if (static_cast<bool>(entry)) |
| 607 | |
| 608 | } // for node |
| 609 | } // for int i |
| 610 | |
| 611 | output << "Bucket count = " << m_nBuckets << endl; |
| 612 | output << "Stored item = " << m_nItems << endl; |
| 613 | output << "--------------------------\n"; |
| 614 | } |
| 615 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 616 | NameTree::const_iterator::const_iterator() |
| 617 | : m_nameTree(nullptr) |
| 618 | { |
| 619 | } |
| 620 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 621 | NameTree::const_iterator::const_iterator(NameTree::IteratorType type, |
| 622 | const NameTree& nameTree, |
| 623 | shared_ptr<name_tree::Entry> entry, |
| 624 | const name_tree::EntrySelector& entrySelector, |
| 625 | const name_tree::EntrySubTreeSelector& entrySubTreeSelector) |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 626 | : m_nameTree(&nameTree) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 627 | , m_entry(entry) |
| 628 | , m_subTreeRoot(entry) |
| 629 | , m_entrySelector(make_shared<name_tree::EntrySelector>(entrySelector)) |
| 630 | , m_entrySubTreeSelector(make_shared<name_tree::EntrySubTreeSelector>(entrySubTreeSelector)) |
| 631 | , m_type(type) |
| 632 | , m_shouldVisitChildren(true) |
| 633 | { |
| 634 | } |
| 635 | |
| 636 | // operator++() |
| 637 | NameTree::const_iterator |
| 638 | NameTree::const_iterator::operator++() |
| 639 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 640 | NFD_LOG_TRACE("const_iterator::operator++()"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 641 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 642 | BOOST_ASSERT(m_entry != m_nameTree->m_end); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 643 | |
| 644 | if (m_type == FULL_ENUMERATE_TYPE) // fullEnumerate |
| 645 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 646 | // process the entries in the same bucket first |
| 647 | while (m_entry->m_node->m_next != 0) |
| 648 | { |
| 649 | m_entry = m_entry->m_node->m_next->m_entry; |
| 650 | if ((*m_entrySelector)(*m_entry)) |
| 651 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 652 | return *this; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | // process other buckets |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 657 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 658 | for (int newLocation = m_entry->m_hash % m_nameTree->m_nBuckets + 1; |
| 659 | newLocation < static_cast<int>(m_nameTree->m_nBuckets); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 660 | ++newLocation) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 661 | { |
| 662 | // process each bucket |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 663 | name_tree::Node* node = m_nameTree->m_buckets[newLocation]; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 664 | while (node != 0) |
| 665 | { |
| 666 | m_entry = node->m_entry; |
| 667 | if ((*m_entrySelector)(*m_entry)) |
| 668 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 669 | return *this; |
| 670 | } |
| 671 | node = node->m_next; |
| 672 | } |
| 673 | } |
hwyuan | 8082993 | 2015-07-11 22:59:13 -0500 | [diff] [blame] | 674 | |
| 675 | // Reach the end() |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 676 | m_entry = m_nameTree->m_end; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 677 | return *this; |
| 678 | } |
| 679 | |
| 680 | if (m_type == PARTIAL_ENUMERATE_TYPE) // partialEnumerate |
| 681 | { |
| 682 | // We use pre-order traversal. |
| 683 | // if at the root, it could have already been accepted, or this |
| 684 | // iterator was just declared, and root doesn't satisfy the |
| 685 | // requirement |
| 686 | // The if() section handles this special case |
| 687 | // Essentially, we need to check root's fist child, and the rest will |
| 688 | // be the same as normal process |
| 689 | if (m_entry == m_subTreeRoot) |
| 690 | { |
| 691 | if (m_shouldVisitChildren) |
| 692 | { |
| 693 | m_entry = m_entry->getChildren()[0]; |
| 694 | std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry)); |
| 695 | m_shouldVisitChildren = (result.second && m_entry->hasChildren()); |
| 696 | if(result.first) |
| 697 | { |
| 698 | return *this; |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | // the first child did not meet the requirement |
| 703 | // the rest of the process can just fall through the while loop |
| 704 | // as normal |
| 705 | } |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | // no children, should return end(); |
| 710 | // just fall through |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // The first thing to do is to visit its child, or go to find its possible |
| 715 | // siblings |
| 716 | while (m_entry != m_subTreeRoot) |
| 717 | { |
| 718 | if (m_shouldVisitChildren) |
| 719 | { |
| 720 | // If this subtree should be visited |
| 721 | m_entry = m_entry->getChildren()[0]; |
| 722 | std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry)); |
| 723 | m_shouldVisitChildren = (result.second && m_entry->hasChildren()); |
| 724 | if (result.first) // if this node is acceptable |
| 725 | { |
| 726 | return *this; |
| 727 | } |
| 728 | else |
| 729 | { |
| 730 | // do nothing, as this node is essentially ignored |
| 731 | // send this node to the while loop. |
| 732 | } |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | // Should try to find its sibling |
| 737 | shared_ptr<name_tree::Entry> parent = m_entry->getParent(); |
| 738 | |
| 739 | std::vector<shared_ptr<name_tree::Entry> >& parentChildrenList = parent->getChildren(); |
| 740 | bool isFound = false; |
| 741 | size_t i = 0; |
| 742 | for (i = 0; i < parentChildrenList.size(); i++) |
| 743 | { |
| 744 | if (parentChildrenList[i] == m_entry) |
| 745 | { |
| 746 | isFound = true; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
Junxiao Shi | ac7b437 | 2014-12-13 21:47:04 -0700 | [diff] [blame] | 751 | BOOST_VERIFY(isFound == true); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 752 | if (i < parentChildrenList.size() - 1) // m_entry not the last child |
| 753 | { |
| 754 | m_entry = parentChildrenList[i + 1]; |
| 755 | std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry)); |
| 756 | m_shouldVisitChildren = (result.second && m_entry->hasChildren()); |
| 757 | if (result.first) // if this node is acceptable |
| 758 | { |
| 759 | return *this; |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | // do nothing, as this node is essentially ignored |
| 764 | // send this node to the while loop. |
| 765 | } |
| 766 | } |
| 767 | else |
| 768 | { |
| 769 | // m_entry is the last child, no more sibling, should try to find parent's sibling |
| 770 | m_shouldVisitChildren = false; |
| 771 | m_entry = parent; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 776 | m_entry = m_nameTree->m_end; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 777 | return *this; |
| 778 | } |
| 779 | |
| 780 | if (m_type == FIND_ALL_MATCHES_TYPE) // findAllMatches |
| 781 | { |
| 782 | // Assumption: at the beginning, m_entry was initialized with the first |
| 783 | // eligible Name Tree entry (i.e., has a PIT entry that can be satisfied |
| 784 | // by the Data packet) |
| 785 | |
| 786 | while (static_cast<bool>(m_entry->getParent())) |
| 787 | { |
| 788 | m_entry = m_entry->getParent(); |
| 789 | if ((*m_entrySelector)(*m_entry)) |
| 790 | return *this; |
| 791 | } |
| 792 | |
| 793 | // Reach to the end (Root) |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 794 | m_entry = m_nameTree->m_end; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 795 | return *this; |
| 796 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 797 | |
| 798 | BOOST_ASSERT(false); // unknown type |
| 799 | return *this; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 800 | } |
| 801 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 802 | } // namespace nfd |