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