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 | #ifndef NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP |
| 27 | #define NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP |
| 28 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 29 | #include "name-tree-entry.hpp" |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace name_tree { |
| 33 | |
| 34 | class Entry; |
| 35 | |
| 36 | /** \brief a single hash value |
| 37 | */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 38 | using HashValue = size_t; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 39 | |
| 40 | /** \brief a sequence of hash values |
| 41 | * \sa computeHashes |
| 42 | */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 43 | using HashSequence = std::vector<HashValue>; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 44 | |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 45 | /** \brief computes hash value of \p name.getPrefix(prefixLen) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 46 | */ |
| 47 | HashValue |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 48 | computeHash(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 49 | |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 50 | /** \brief computes hash values for each prefix of \p name.getPrefix(prefixLen) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 51 | * \return a hash sequence, where the i-th hash value equals computeHash(name, i) |
| 52 | */ |
| 53 | HashSequence |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 54 | computeHashes(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 55 | |
| 56 | /** \brief a hashtable node |
| 57 | * |
| 58 | * Zero or more nodes can be added to a hashtable bucket. They are organized as |
| 59 | * a doubly linked list through prev and next pointers. |
| 60 | */ |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 61 | class Node : noncopyable |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 62 | { |
| 63 | public: |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 64 | /** \post entry.getName() == name |
| 65 | * \post getNode(entry) == this |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 66 | */ |
| 67 | Node(HashValue h, const Name& name); |
| 68 | |
| 69 | /** \pre prev == nullptr |
| 70 | * \pre next == nullptr |
| 71 | */ |
| 72 | ~Node(); |
| 73 | |
| 74 | public: |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 75 | const HashValue hash; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 76 | Node* prev; |
| 77 | Node* next; |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 78 | mutable Entry entry; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** \return node associated with entry |
| 82 | * \note This function is for NameTree internal use. |
| 83 | */ |
| 84 | Node* |
| 85 | getNode(const Entry& entry); |
| 86 | |
| 87 | /** \brief invoke a function for each node in a doubly linked list |
| 88 | * \tparam N either Node or const Node |
| 89 | * \tparam F a functor with signature void F(N*) |
| 90 | * \note It's safe to delete the node in the function. |
| 91 | */ |
| 92 | template<typename N, typename F> |
| 93 | void |
| 94 | foreachNode(N* head, const F& func) |
| 95 | { |
| 96 | N* node = head; |
| 97 | while (node != nullptr) { |
| 98 | N* next = node->next; |
| 99 | func(node); |
| 100 | node = next; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** \brief provides options for Hashtable |
| 105 | */ |
| 106 | class HashtableOptions |
| 107 | { |
| 108 | public: |
| 109 | /** \brief constructor |
| 110 | * \post initialSize == size |
| 111 | * \post minSize == size |
| 112 | */ |
| 113 | explicit |
| 114 | HashtableOptions(size_t size = 16); |
| 115 | |
| 116 | public: |
| 117 | /** \brief initial number of buckets |
| 118 | */ |
| 119 | size_t initialSize; |
| 120 | |
| 121 | /** \brief minimal number of buckets |
| 122 | */ |
| 123 | size_t minSize; |
| 124 | |
| 125 | /** \brief if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded |
| 126 | */ |
| 127 | float expandLoadFactor = 0.5; |
| 128 | |
| 129 | /** \brief when hashtable is expanded, its new size is nBuckets*expandFactor |
| 130 | */ |
| 131 | float expandFactor = 2.0; |
| 132 | |
| 133 | /** \brief if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk |
| 134 | */ |
| 135 | float shrinkLoadFactor = 0.1; |
| 136 | |
| 137 | /** \brief when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize) |
| 138 | */ |
| 139 | float shrinkFactor = 0.5; |
| 140 | }; |
| 141 | |
| 142 | /** \brief a hashtable for fast exact name lookup |
| 143 | * |
| 144 | * The Hashtable contains a number of buckets. |
| 145 | * Each node is placed into a bucket determined by a hash value computed from its name. |
| 146 | * Hash collision is resolved through a doubly linked list in each bucket. |
| 147 | * The number of buckets is adjusted according to how many nodes are stored. |
| 148 | */ |
| 149 | class Hashtable |
| 150 | { |
| 151 | public: |
| 152 | typedef HashtableOptions Options; |
| 153 | |
| 154 | explicit |
| 155 | Hashtable(const Options& options); |
| 156 | |
| 157 | /** \brief deallocates all nodes |
| 158 | */ |
| 159 | ~Hashtable(); |
| 160 | |
| 161 | /** \return number of nodes |
| 162 | */ |
| 163 | size_t |
| 164 | size() const |
| 165 | { |
| 166 | return m_size; |
| 167 | } |
| 168 | |
| 169 | /** \return number of buckets |
| 170 | */ |
| 171 | size_t |
| 172 | getNBuckets() const |
| 173 | { |
| 174 | return m_buckets.size(); |
| 175 | } |
| 176 | |
| 177 | /** \return bucket index for hash value h |
| 178 | */ |
| 179 | size_t |
| 180 | computeBucketIndex(HashValue h) const |
| 181 | { |
| 182 | return h % this->getNBuckets(); |
| 183 | } |
| 184 | |
| 185 | /** \return i-th bucket |
| 186 | * \pre bucket < getNBuckets() |
| 187 | */ |
| 188 | const Node* |
| 189 | getBucket(size_t bucket) const |
| 190 | { |
| 191 | BOOST_ASSERT(bucket < this->getNBuckets()); |
| 192 | return m_buckets[bucket]; // don't use m_bucket.at() for better performance |
| 193 | } |
| 194 | |
| 195 | /** \brief find node for name.getPrefix(prefixLen) |
| 196 | * \pre name.size() > prefixLen |
| 197 | */ |
| 198 | const Node* |
| 199 | find(const Name& name, size_t prefixLen) const; |
| 200 | |
| 201 | /** \brief find node for name.getPrefix(prefixLen) |
| 202 | * \pre name.size() > prefixLen |
| 203 | * \pre hashes == computeHashes(name) |
| 204 | */ |
| 205 | const Node* |
| 206 | find(const Name& name, size_t prefixLen, const HashSequence& hashes) const; |
| 207 | |
| 208 | /** \brief find or insert node for name.getPrefix(prefixLen) |
| 209 | * \pre name.size() > prefixLen |
| 210 | * \pre hashes == computeHashes(name) |
| 211 | */ |
| 212 | std::pair<const Node*, bool> |
| 213 | insert(const Name& name, size_t prefixLen, const HashSequence& hashes); |
| 214 | |
| 215 | /** \brief delete node |
| 216 | * \pre node exists in this hashtable |
| 217 | */ |
| 218 | void |
| 219 | erase(Node* node); |
| 220 | |
| 221 | private: |
| 222 | /** \brief attach node to bucket |
| 223 | */ |
| 224 | void |
| 225 | attach(size_t bucket, Node* node); |
| 226 | |
| 227 | /** \brief detach node from bucket |
| 228 | */ |
| 229 | void |
| 230 | detach(size_t bucket, Node* node); |
| 231 | |
| 232 | std::pair<const Node*, bool> |
| 233 | findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert); |
| 234 | |
| 235 | void |
| 236 | computeThresholds(); |
| 237 | |
| 238 | void |
| 239 | resize(size_t newNBuckets); |
| 240 | |
| 241 | private: |
| 242 | std::vector<Node*> m_buckets; |
| 243 | Options m_options; |
| 244 | size_t m_size; |
| 245 | size_t m_expandThreshold; |
| 246 | size_t m_shrinkThreshold; |
| 247 | }; |
| 248 | |
| 249 | } // namespace name_tree |
| 250 | } // namespace nfd |
| 251 | |
| 252 | #endif // NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP |