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