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