Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef REPO_STORAGE_INDEX_HPP |
| 21 | #define REPO_STORAGE_INDEX_HPP |
| 22 | |
| 23 | #include "common.hpp" |
| 24 | #include "skiplist.hpp" |
| 25 | #include <queue> |
| 26 | |
| 27 | namespace repo { |
| 28 | |
| 29 | class Index : noncopyable |
| 30 | { |
| 31 | public: |
| 32 | class Error : public std::runtime_error |
| 33 | { |
| 34 | public: |
| 35 | explicit |
| 36 | Error(const std::string& what) |
| 37 | : std::runtime_error(what) |
| 38 | { |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | class Entry |
| 43 | { |
| 44 | public: |
| 45 | class Error : public std::runtime_error |
| 46 | { |
| 47 | public: |
| 48 | explicit |
| 49 | Error(const std::string& what) |
| 50 | : std::runtime_error(what) |
| 51 | { |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | public: |
| 56 | |
| 57 | /** |
| 58 | * @brief used by skiplist to construct node |
| 59 | */ |
| 60 | Entry() |
| 61 | { |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * @brief construct Entry by data and id number |
| 66 | */ |
| 67 | Entry(const Data& data, const int64_t id); |
| 68 | |
| 69 | /** |
| 70 | * @brief construct Entry by fullName, keyLocator and id number |
| 71 | */ |
| 72 | Entry(const Name& fullName, const KeyLocator& keyLocator, const int64_t id); |
| 73 | |
| 74 | /** |
| 75 | * @brief construct Entry by fullName, keyLocatorHash and id number |
| 76 | * @param fullName full name with digest computed from data |
| 77 | * @param keyLocatorHash keyLocator hashed by sha256 |
| 78 | * @param id record ID from database |
| 79 | */ |
| 80 | Entry(const Name& fullName, const ndn::ConstBufferPtr& keyLocatorHash, const int64_t id); |
| 81 | |
| 82 | /** |
| 83 | * @brief implicit construct Entry by full name |
| 84 | * |
| 85 | * Allow implicit conversion from Name for SkipList lookups by Name |
| 86 | */ |
| 87 | Entry(const Name& name); |
| 88 | |
| 89 | /** |
| 90 | * @brief get the name of entry |
| 91 | */ |
| 92 | const Name& |
| 93 | getName() const |
| 94 | { |
| 95 | return m_name; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @brief get the keyLocator hash value of the entry |
| 100 | */ |
| 101 | const ndn::ConstBufferPtr& |
| 102 | getKeyLocatorHash() const |
| 103 | { |
| 104 | return m_keyLocatorHash; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @brief get record ID from database |
| 109 | */ |
| 110 | const int64_t |
| 111 | getId() const |
| 112 | { |
| 113 | return m_id; |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | operator>(const Entry& entry) const |
| 118 | { |
| 119 | return m_name > entry.getName(); |
| 120 | } |
| 121 | |
| 122 | bool |
| 123 | operator<(const Entry& entry) const |
| 124 | { |
| 125 | return m_name < entry.getName(); |
| 126 | } |
| 127 | |
| 128 | bool |
| 129 | operator==(const Entry& entry) const |
| 130 | { |
| 131 | return m_name == entry.getName(); |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | operator!=(const Entry& entry) const |
| 136 | { |
| 137 | return m_name != entry.getName(); |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | Name m_name; |
| 142 | ndn::ConstBufferPtr m_keyLocatorHash; |
| 143 | int64_t m_id; |
| 144 | }; |
| 145 | |
| 146 | private: |
| 147 | |
| 148 | typedef SkipList<Entry> IndexSkipList; |
| 149 | |
| 150 | public: |
| 151 | explicit |
| 152 | Index(const size_t nMaxPackets); |
| 153 | |
| 154 | /** |
| 155 | * @brief insert entries into index |
| 156 | * @param data used to construct entries |
| 157 | * @param id obtained from database |
| 158 | */ |
| 159 | bool |
| 160 | insert(const Data& data, const int64_t id); |
| 161 | |
| 162 | /** |
| 163 | * @brief insert entries into index |
| 164 | * @param data used to construct entries |
| 165 | * @param id obtained from database |
| 166 | */ |
| 167 | bool |
| 168 | insert(const Name& fullName, const int64_t id, |
| 169 | const ndn::ConstBufferPtr& keyLocatorHash); |
| 170 | |
| 171 | /** |
| 172 | * @brief erase the entry in index by its fullname |
| 173 | */ |
| 174 | bool |
| 175 | erase(const Name& fullName); |
| 176 | |
| 177 | /** @brief find the Entry for best match of an Interest |
| 178 | * @return ID and fullName of the Entry, or (0,ignored) if not found |
| 179 | */ |
| 180 | std::pair<int64_t, Name> |
| 181 | find(const Interest& interest) const; |
| 182 | |
| 183 | /** @brief find the first Entry under a Name prefix |
| 184 | * @return ID and fullName of the Entry, or (0,ignored) if not found |
| 185 | */ |
| 186 | std::pair<int64_t, Name> |
| 187 | find(const Name& name) const; |
| 188 | |
| 189 | /** |
| 190 | * @brief determine whether same Data is already in the index |
| 191 | * @return true if identical Data exists, false otherwise |
| 192 | */ |
| 193 | bool |
| 194 | hasData(const Data& data) const; |
| 195 | |
| 196 | /** |
| 197 | * @brief compute the hash value of keyLocator |
| 198 | */ |
| 199 | static const ndn::ConstBufferPtr |
| 200 | computeKeyLocatorHash(const KeyLocator& keyLocator); |
| 201 | |
| 202 | const size_t |
| 203 | size() const |
| 204 | { |
| 205 | return m_size; |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | /** |
| 210 | * @brief select entries which satisfy the selectors in interest and return their name |
| 211 | * @param interest used to select entries by comparing the name and checking selectors |
| 212 | * @param idName save the id and name of found entries |
| 213 | * @param startingPoint the entry whose name is equal or larger than the interest name |
| 214 | */ |
| 215 | std::pair<int64_t, Name> |
| 216 | selectChild(const Interest& interest, |
| 217 | IndexSkipList::const_iterator startingPoint) const; |
| 218 | |
| 219 | /** |
| 220 | * @brief check whether the index is full |
| 221 | */ |
| 222 | bool |
| 223 | isFull() const |
| 224 | { |
| 225 | return m_size >= m_maxPackets; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @brief find the first entry with the prefix |
| 230 | * @param prefix used to request the entries |
| 231 | * @param startingPoint the entry whose name is equal or larger than the interest name |
| 232 | * @return int64_t the id number of found entry |
| 233 | * @return Name the name of found entry |
| 234 | */ |
| 235 | std::pair<int64_t, Name> |
| 236 | findFirstEntry(const Name& prefix, |
| 237 | IndexSkipList::const_iterator startingPoint) const; |
| 238 | |
| 239 | private: |
| 240 | IndexSkipList m_skipList; |
| 241 | size_t m_maxPackets; |
| 242 | size_t m_size; |
| 243 | }; |
| 244 | |
| 245 | } // namespace repo |
| 246 | |
| 247 | #endif // REPO_STORAGE_INDEX_HPP |