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