Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "name-tree-hashtable.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/city-hash.hpp" |
| 28 | #include "common/logger.hpp" |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace name_tree { |
| 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(NameTreeHashtable); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 34 | |
| 35 | class Hash32 |
| 36 | { |
| 37 | public: |
| 38 | static HashValue |
| 39 | compute(const void* buffer, size_t length) |
| 40 | { |
| 41 | return static_cast<HashValue>(CityHash32(reinterpret_cast<const char*>(buffer), length)); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | class Hash64 |
| 46 | { |
| 47 | public: |
| 48 | static HashValue |
| 49 | compute(const void* buffer, size_t length) |
| 50 | { |
| 51 | return static_cast<HashValue>(CityHash64(reinterpret_cast<const char*>(buffer), length)); |
| 52 | } |
| 53 | }; |
| 54 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame^] | 55 | /** |
| 56 | * \brief A type with a `compute()` static method to compute the hash value from a raw buffer |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 57 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame^] | 58 | using HashFunc = std::conditional_t<(sizeof(HashValue) > 4), Hash64, Hash32>; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 59 | |
| 60 | HashValue |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 61 | computeHash(const Name& name, size_t prefixLen) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 62 | { |
| 63 | name.wireEncode(); // ensure wire buffer exists |
| 64 | |
| 65 | HashValue h = 0; |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 66 | for (size_t i = 0, last = std::min(prefixLen, name.size()); i < last; ++i) { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 67 | const name::Component& comp = name[i]; |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 68 | h ^= HashFunc::compute(comp.data(), comp.size()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 69 | } |
| 70 | return h; |
| 71 | } |
| 72 | |
| 73 | HashSequence |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 74 | computeHashes(const Name& name, size_t prefixLen) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 75 | { |
| 76 | name.wireEncode(); // ensure wire buffer exists |
| 77 | |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 78 | size_t last = std::min(prefixLen, name.size()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 79 | HashSequence seq; |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 80 | seq.reserve(last + 1); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 81 | |
| 82 | HashValue h = 0; |
| 83 | seq.push_back(h); |
| 84 | |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 85 | for (size_t i = 0; i < last; ++i) { |
| 86 | const name::Component& comp = name[i]; |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 87 | h ^= HashFunc::compute(comp.data(), comp.size()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 88 | seq.push_back(h); |
| 89 | } |
| 90 | return seq; |
| 91 | } |
| 92 | |
| 93 | Node::Node(HashValue h, const Name& name) |
| 94 | : hash(h) |
| 95 | , prev(nullptr) |
| 96 | , next(nullptr) |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 97 | , entry(name, this) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 98 | { |
| 99 | } |
| 100 | |
| 101 | Node::~Node() |
| 102 | { |
| 103 | BOOST_ASSERT(prev == nullptr); |
| 104 | BOOST_ASSERT(next == nullptr); |
| 105 | } |
| 106 | |
| 107 | Node* |
| 108 | getNode(const Entry& entry) |
| 109 | { |
| 110 | return entry.m_node; |
| 111 | } |
| 112 | |
| 113 | HashtableOptions::HashtableOptions(size_t size) |
| 114 | : initialSize(size) |
| 115 | , minSize(size) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | Hashtable::Hashtable(const Options& options) |
| 120 | : m_options(options) |
| 121 | , m_size(0) |
| 122 | { |
| 123 | BOOST_ASSERT(m_options.minSize > 0); |
| 124 | BOOST_ASSERT(m_options.initialSize >= m_options.minSize); |
| 125 | BOOST_ASSERT(m_options.expandLoadFactor > 0.0); |
| 126 | BOOST_ASSERT(m_options.expandLoadFactor <= 1.0); |
| 127 | BOOST_ASSERT(m_options.expandFactor > 1.0); |
| 128 | BOOST_ASSERT(m_options.shrinkLoadFactor >= 0.0); |
| 129 | BOOST_ASSERT(m_options.shrinkLoadFactor < 1.0); |
| 130 | BOOST_ASSERT(m_options.shrinkFactor > 0.0); |
| 131 | BOOST_ASSERT(m_options.shrinkFactor < 1.0); |
| 132 | |
| 133 | m_buckets.resize(options.initialSize); |
| 134 | this->computeThresholds(); |
| 135 | } |
| 136 | |
| 137 | Hashtable::~Hashtable() |
| 138 | { |
| 139 | for (size_t i = 0; i < m_buckets.size(); ++i) { |
| 140 | foreachNode(m_buckets[i], [] (Node* node) { |
| 141 | node->prev = node->next = nullptr; |
| 142 | delete node; |
| 143 | }); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | Hashtable::attach(size_t bucket, Node* node) |
| 149 | { |
| 150 | node->prev = nullptr; |
| 151 | node->next = m_buckets[bucket]; |
| 152 | |
| 153 | if (node->next != nullptr) { |
| 154 | BOOST_ASSERT(node->next->prev == nullptr); |
| 155 | node->next->prev = node; |
| 156 | } |
| 157 | |
| 158 | m_buckets[bucket] = node; |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | Hashtable::detach(size_t bucket, Node* node) |
| 163 | { |
| 164 | if (node->prev != nullptr) { |
| 165 | BOOST_ASSERT(node->prev->next == node); |
| 166 | node->prev->next = node->next; |
| 167 | } |
| 168 | else { |
| 169 | BOOST_ASSERT(m_buckets[bucket] == node); |
| 170 | m_buckets[bucket] = node->next; |
| 171 | } |
| 172 | |
| 173 | if (node->next != nullptr) { |
| 174 | BOOST_ASSERT(node->next->prev == node); |
| 175 | node->next->prev = node->prev; |
| 176 | } |
| 177 | |
| 178 | node->prev = node->next = nullptr; |
| 179 | } |
| 180 | |
| 181 | std::pair<const Node*, bool> |
| 182 | Hashtable::findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert) |
| 183 | { |
| 184 | size_t bucket = this->computeBucketIndex(h); |
| 185 | |
| 186 | for (const Node* node = m_buckets[bucket]; node != nullptr; node = node->next) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 187 | if (node->hash == h && name.compare(0, prefixLen, node->entry.getName()) == 0) { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 188 | NFD_LOG_TRACE("found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket); |
| 189 | return {node, false}; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (!allowInsert) { |
| 194 | NFD_LOG_TRACE("not-found " << name.getPrefix(prefixLen) << " hash=" << h << " bucket=" << bucket); |
| 195 | return {nullptr, false}; |
| 196 | } |
| 197 | |
| 198 | Node* node = new Node(h, name.getPrefix(prefixLen)); |
| 199 | this->attach(bucket, node); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 200 | NFD_LOG_TRACE("insert " << node->entry.getName() << " hash=" << h << " bucket=" << bucket); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 201 | ++m_size; |
| 202 | |
| 203 | if (m_size > m_expandThreshold) { |
| 204 | this->resize(static_cast<size_t>(m_options.expandFactor * this->getNBuckets())); |
| 205 | } |
| 206 | |
| 207 | return {node, true}; |
| 208 | } |
| 209 | |
| 210 | const Node* |
| 211 | Hashtable::find(const Name& name, size_t prefixLen) const |
| 212 | { |
| 213 | HashValue h = computeHash(name, prefixLen); |
| 214 | return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, h, false).first; |
| 215 | } |
| 216 | |
| 217 | const Node* |
| 218 | Hashtable::find(const Name& name, size_t prefixLen, const HashSequence& hashes) const |
| 219 | { |
| 220 | BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen)); |
| 221 | return const_cast<Hashtable*>(this)->findOrInsert(name, prefixLen, hashes[prefixLen], false).first; |
| 222 | } |
| 223 | |
| 224 | std::pair<const Node*, bool> |
| 225 | Hashtable::insert(const Name& name, size_t prefixLen, const HashSequence& hashes) |
| 226 | { |
| 227 | BOOST_ASSERT(hashes.at(prefixLen) == computeHash(name, prefixLen)); |
| 228 | return this->findOrInsert(name, prefixLen, hashes[prefixLen], true); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | Hashtable::erase(Node* node) |
| 233 | { |
| 234 | BOOST_ASSERT(node != nullptr); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 235 | BOOST_ASSERT(node->entry.getParent() == nullptr); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 236 | |
| 237 | size_t bucket = this->computeBucketIndex(node->hash); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 238 | NFD_LOG_TRACE("erase " << node->entry.getName() << " hash=" << node->hash << " bucket=" << bucket); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 239 | |
| 240 | this->detach(bucket, node); |
| 241 | delete node; |
| 242 | --m_size; |
| 243 | |
| 244 | if (m_size < m_shrinkThreshold) { |
| 245 | size_t newNBuckets = std::max(m_options.minSize, |
| 246 | static_cast<size_t>(m_options.shrinkFactor * this->getNBuckets())); |
| 247 | this->resize(newNBuckets); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | Hashtable::computeThresholds() |
| 253 | { |
| 254 | m_expandThreshold = static_cast<size_t>(m_options.expandLoadFactor * this->getNBuckets()); |
| 255 | m_shrinkThreshold = static_cast<size_t>(m_options.shrinkLoadFactor * this->getNBuckets()); |
| 256 | NFD_LOG_TRACE("thresholds expand=" << m_expandThreshold << " shrink=" << m_shrinkThreshold); |
| 257 | } |
| 258 | |
| 259 | void |
| 260 | Hashtable::resize(size_t newNBuckets) |
| 261 | { |
| 262 | if (this->getNBuckets() == newNBuckets) { |
| 263 | return; |
| 264 | } |
| 265 | NFD_LOG_DEBUG("resize from=" << this->getNBuckets() << " to=" << newNBuckets); |
| 266 | |
| 267 | std::vector<Node*> oldBuckets; |
| 268 | oldBuckets.swap(m_buckets); |
| 269 | m_buckets.resize(newNBuckets); |
| 270 | |
| 271 | for (Node* head : oldBuckets) { |
| 272 | foreachNode(head, [this] (Node* node) { |
| 273 | size_t bucket = this->computeBucketIndex(node->hash); |
| 274 | this->attach(bucket, node); |
| 275 | }); |
| 276 | } |
| 277 | |
| 278 | this->computeThresholds(); |
| 279 | } |
| 280 | |
| 281 | } // namespace name_tree |
| 282 | } // namespace nfd |