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" |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 24 | #include <set> |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 25 | |
| 26 | namespace repo { |
| 27 | |
| 28 | class Index : noncopyable |
| 29 | { |
| 30 | public: |
| 31 | class Error : public std::runtime_error |
| 32 | { |
| 33 | public: |
| 34 | explicit |
| 35 | Error(const std::string& what) |
| 36 | : std::runtime_error(what) |
| 37 | { |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | class Entry |
| 42 | { |
| 43 | public: |
| 44 | class Error : public std::runtime_error |
| 45 | { |
| 46 | public: |
| 47 | explicit |
| 48 | Error(const std::string& what) |
| 49 | : std::runtime_error(what) |
| 50 | { |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | public: |
| 55 | |
| 56 | /** |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 57 | * @brief used by set to construct node |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 58 | */ |
| 59 | Entry() |
| 60 | { |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * @brief construct Entry by data and id number |
| 65 | */ |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 66 | Entry(const Data& data, int64_t id); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * @brief construct Entry by fullName, keyLocator and id number |
| 70 | */ |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 71 | Entry(const Name& fullName, const KeyLocator& keyLocator, int64_t id); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * @brief construct Entry by fullName, keyLocatorHash and id number |
| 75 | * @param fullName full name with digest computed from data |
| 76 | * @param keyLocatorHash keyLocator hashed by sha256 |
| 77 | * @param id record ID from database |
| 78 | */ |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 79 | Entry(const Name& fullName, const ndn::ConstBufferPtr& keyLocatorHash, int64_t id); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * @brief implicit construct Entry by full name |
| 83 | * |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 84 | * Allow implicit conversion from Name for set lookups by Name |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 85 | */ |
| 86 | Entry(const Name& name); |
| 87 | |
| 88 | /** |
| 89 | * @brief get the name of entry |
| 90 | */ |
| 91 | const Name& |
| 92 | getName() const |
| 93 | { |
| 94 | return m_name; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @brief get the keyLocator hash value of the entry |
| 99 | */ |
| 100 | const ndn::ConstBufferPtr& |
| 101 | getKeyLocatorHash() const |
| 102 | { |
| 103 | return m_keyLocatorHash; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @brief get record ID from database |
| 108 | */ |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 109 | int64_t |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 110 | getId() const |
| 111 | { |
| 112 | return m_id; |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | operator>(const Entry& entry) const |
| 117 | { |
| 118 | return m_name > entry.getName(); |
| 119 | } |
| 120 | |
| 121 | bool |
| 122 | operator<(const Entry& entry) const |
| 123 | { |
| 124 | return m_name < entry.getName(); |
| 125 | } |
| 126 | |
| 127 | bool |
| 128 | operator==(const Entry& entry) const |
| 129 | { |
| 130 | return m_name == entry.getName(); |
| 131 | } |
| 132 | |
| 133 | bool |
| 134 | operator!=(const Entry& entry) const |
| 135 | { |
| 136 | return m_name != entry.getName(); |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | Name m_name; |
| 141 | ndn::ConstBufferPtr m_keyLocatorHash; |
| 142 | int64_t m_id; |
| 143 | }; |
| 144 | |
| 145 | private: |
| 146 | |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 147 | typedef std::set<Entry> IndexContainer; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 148 | |
| 149 | public: |
| 150 | explicit |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 151 | Index(size_t nMaxPackets); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * @brief insert entries into index |
| 155 | * @param data used to construct entries |
| 156 | * @param id obtained from database |
| 157 | */ |
| 158 | bool |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 159 | insert(const Data& data, int64_t id); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * @brief insert entries into index |
| 163 | * @param data used to construct entries |
| 164 | * @param id obtained from database |
| 165 | */ |
| 166 | bool |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 167 | insert(const Name& fullName, int64_t id, |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 168 | const ndn::ConstBufferPtr& keyLocatorHash); |
| 169 | |
| 170 | /** |
| 171 | * @brief erase the entry in index by its fullname |
| 172 | */ |
| 173 | bool |
| 174 | erase(const Name& fullName); |
| 175 | |
| 176 | /** @brief find the Entry for best match of an Interest |
| 177 | * @return ID and fullName of the Entry, or (0,ignored) if not found |
| 178 | */ |
| 179 | std::pair<int64_t, Name> |
| 180 | find(const Interest& interest) const; |
| 181 | |
| 182 | /** @brief find the first Entry under a Name prefix |
| 183 | * @return ID and fullName of the Entry, or (0,ignored) if not found |
| 184 | */ |
| 185 | std::pair<int64_t, Name> |
| 186 | find(const Name& name) const; |
| 187 | |
| 188 | /** |
| 189 | * @brief determine whether same Data is already in the index |
| 190 | * @return true if identical Data exists, false otherwise |
| 191 | */ |
| 192 | bool |
| 193 | hasData(const Data& data) const; |
| 194 | |
| 195 | /** |
| 196 | * @brief compute the hash value of keyLocator |
| 197 | */ |
| 198 | static const ndn::ConstBufferPtr |
| 199 | computeKeyLocatorHash(const KeyLocator& keyLocator); |
| 200 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame^] | 201 | size_t |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 202 | size() const |
| 203 | { |
| 204 | return m_size; |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | /** |
| 209 | * @brief select entries which satisfy the selectors in interest and return their name |
| 210 | * @param interest used to select entries by comparing the name and checking selectors |
| 211 | * @param idName save the id and name of found entries |
| 212 | * @param startingPoint the entry whose name is equal or larger than the interest name |
| 213 | */ |
| 214 | std::pair<int64_t, Name> |
| 215 | selectChild(const Interest& interest, |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 216 | IndexContainer::const_iterator startingPoint) const; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * @brief check whether the index is full |
| 220 | */ |
| 221 | bool |
| 222 | isFull() const |
| 223 | { |
| 224 | return m_size >= m_maxPackets; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @brief find the first entry with the prefix |
| 229 | * @param prefix used to request the entries |
| 230 | * @param startingPoint the entry whose name is equal or larger than the interest name |
| 231 | * @return int64_t the id number of found entry |
| 232 | * @return Name the name of found entry |
| 233 | */ |
| 234 | std::pair<int64_t, Name> |
| 235 | findFirstEntry(const Name& prefix, |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 236 | IndexContainer::const_iterator startingPoint) const; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 237 | |
| 238 | private: |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 239 | IndexContainer m_indexContainer; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 240 | size_t m_maxPackets; |
| 241 | size_t m_size; |
| 242 | }; |
| 243 | |
| 244 | } // namespace repo |
| 245 | |
| 246 | #endif // REPO_STORAGE_INDEX_HPP |