Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 13d85e0 | 2017-07-07 07:34:02 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 22 | #ifndef NDN_IMS_IN_MEMORY_STORAGE_HPP |
| 23 | #define NDN_IMS_IN_MEMORY_STORAGE_HPP |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 24 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 25 | #include "in-memory-storage-entry.hpp" |
| 26 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 27 | #include <iterator> |
| 28 | #include <stack> |
| 29 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 30 | #include <boost/multi_index_container.hpp> |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 31 | #include <boost/multi_index/identity.hpp> |
| 32 | #include <boost/multi_index/mem_fun.hpp> |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 33 | #include <boost/multi_index/member.hpp> |
| 34 | #include <boost/multi_index/ordered_index.hpp> |
| 35 | #include <boost/multi_index/sequenced_index.hpp> |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 38 | |
| 39 | /** @brief Represents in-memory storage |
| 40 | */ |
| 41 | class InMemoryStorage : noncopyable |
| 42 | { |
| 43 | public: |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 44 | // multi_index_container to implement storage |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 45 | class byFullName; |
| 46 | |
| 47 | typedef boost::multi_index_container< |
| 48 | InMemoryStorageEntry*, |
| 49 | boost::multi_index::indexed_by< |
| 50 | |
| 51 | // by Full Name |
| 52 | boost::multi_index::ordered_unique< |
| 53 | boost::multi_index::tag<byFullName>, |
| 54 | boost::multi_index::const_mem_fun<InMemoryStorageEntry, const Name&, |
| 55 | &InMemoryStorageEntry::getFullName>, |
| 56 | std::less<Name> |
| 57 | > |
| 58 | |
| 59 | > |
| 60 | > Cache; |
| 61 | |
| 62 | /** @brief Represents a self-defined const_iterator for the in-memory storage |
| 63 | * |
| 64 | * @note Don't try to instantiate this class directly, use InMemoryStorage::begin() instead. |
| 65 | */ |
| 66 | class const_iterator : public std::iterator<std::input_iterator_tag, const Data> |
| 67 | { |
| 68 | public: |
| 69 | const_iterator(const Data* ptr, const Cache* cache, |
| 70 | Cache::index<byFullName>::type::iterator it); |
| 71 | |
| 72 | const_iterator& |
| 73 | operator++(); |
| 74 | |
| 75 | const_iterator |
| 76 | operator++(int); |
| 77 | |
| 78 | const Data& |
| 79 | operator*(); |
| 80 | |
| 81 | const Data* |
| 82 | operator->(); |
| 83 | |
| 84 | bool |
| 85 | operator==(const const_iterator& rhs); |
| 86 | |
| 87 | bool |
| 88 | operator!=(const const_iterator& rhs); |
| 89 | |
| 90 | private: |
| 91 | const Data* m_ptr; |
| 92 | const Cache* m_cache; |
| 93 | Cache::index<byFullName>::type::iterator m_it; |
| 94 | }; |
| 95 | |
| 96 | /** @brief Represents an error might be thrown during reduce the current capacity of the |
| 97 | * in-memory storage through function setCapacity(size_t nMaxPackets). |
| 98 | */ |
| 99 | class Error : public std::runtime_error |
| 100 | { |
| 101 | public: |
Junxiao Shi | 13d85e0 | 2017-07-07 07:34:02 +0000 | [diff] [blame] | 102 | Error() |
| 103 | : std::runtime_error("Cannot reduce the capacity of the in-memory storage!") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | }; |
| 107 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 108 | /** @brief Create a InMemoryStorage with up to @p limit entries |
| 109 | * The InMemoryStorage created through this method will ignore MustBeFresh in interest processing |
| 110 | */ |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 111 | explicit |
| 112 | InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max()); |
| 113 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 114 | /** @brief Create a InMemoryStorage with up to @p limit entries |
| 115 | * The InMemoryStorage created through this method will handle MustBeFresh in interest processing |
| 116 | */ |
| 117 | explicit |
| 118 | InMemoryStorage(boost::asio::io_service& ioService, |
| 119 | size_t limit = std::numeric_limits<size_t>::max()); |
| 120 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 121 | /** @note Please make sure to implement it to free m_freeEntries and evict |
| 122 | * all items in the derived class for anybody who wishes to inherit this class |
| 123 | */ |
| 124 | virtual |
| 125 | ~InMemoryStorage(); |
| 126 | |
| 127 | /** @brief Inserts a Data packet |
| 128 | * |
Junxiao Shi | 13d85e0 | 2017-07-07 07:34:02 +0000 | [diff] [blame] | 129 | * @param data the packet to insert, must be signed and have wire encoding |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 130 | * @param mustBeFreshProcessingWindow Beyond this time period after the data is inserted, the |
| 131 | * data can only be used to answer interest without MustBeFresh selector. |
| 132 | * |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 133 | * @note Packets are considered duplicate if the name with implicit digest matches. |
| 134 | * The new Data packet with the identical name, but a different payload |
| 135 | * will be placed in the in-memory storage. |
| 136 | * |
| 137 | * @note It will invoke afterInsert(shared_ptr<InMemoryStorageEntry>). |
| 138 | */ |
| 139 | void |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 140 | insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow = INFINITE_WINDOW); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 141 | |
| 142 | /** @brief Finds the best match Data for an Interest |
| 143 | * |
| 144 | * @note It will invoke afterAccess(shared_ptr<InMemoryStorageEntry>). |
| 145 | * As currently it is impossible to determine whether a Name contains implicit digest or not, |
| 146 | * therefore this find function is not able to locate a packet according to an interest( |
| 147 | * including implicit digest) whose name is not the full name of the data matching the |
| 148 | * implicit digest. |
| 149 | * |
| 150 | * @return{ the best match, if any; otherwise a null shared_ptr } |
| 151 | */ |
| 152 | shared_ptr<const Data> |
| 153 | find(const Interest& interest); |
| 154 | |
| 155 | /** @brief Finds the best match Data for a Name with or without |
| 156 | * the implicit digest. |
| 157 | * |
| 158 | * If packets with the same name but different digests exist |
| 159 | * and the Name supplied is the one without implicit digest, a packet |
| 160 | * will be arbitrarily chosen to return. |
| 161 | * |
| 162 | * @note It will invoke afterAccess(shared_ptr<InMemoryStorageEntry>). |
| 163 | * |
| 164 | * @return{ the one matched the Name; otherwise a null shared_ptr } |
| 165 | */ |
| 166 | shared_ptr<const Data> |
| 167 | find(const Name& name); |
| 168 | |
| 169 | /** @brief Deletes in-memory storage entry by prefix by default. |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 170 | * @param prefix Exact name of a prefix of the data to remove |
| 171 | * @param isPrefix If false, the function will only delete the |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 172 | * entry completely matched with the prefix according to canonical ordering. |
| 173 | * For this case, user should substitute the prefix with full name. |
| 174 | * |
| 175 | * @note Please do not use this function directly in any derived class to erase |
| 176 | * entry in the cache, use eraseHelper instead. |
| 177 | * @note It will invoke beforeErase(shared_ptr<InMemoryStorageEntry>). |
| 178 | */ |
| 179 | void |
| 180 | erase(const Name& prefix, const bool isPrefix = true); |
| 181 | |
| 182 | /** @return{ maximum number of packets that can be allowed to store in in-memory storage } |
| 183 | */ |
| 184 | size_t |
| 185 | getLimit() const |
| 186 | { |
| 187 | return m_limit; |
| 188 | } |
| 189 | |
| 190 | /** @return{ number of packets stored in in-memory storage } |
| 191 | */ |
| 192 | size_t |
| 193 | size() const |
| 194 | { |
| 195 | return m_nPackets; |
| 196 | } |
| 197 | |
| 198 | /** @brief Returns begin iterator of the in-memory storage ordering by |
| 199 | * name with digest |
| 200 | * |
| 201 | * @return{ const_iterator pointing to the beginning of the m_cache } |
| 202 | */ |
| 203 | InMemoryStorage::const_iterator |
| 204 | begin() const; |
| 205 | |
| 206 | /** @brief Returns end iterator of the in-memory storage ordering by |
| 207 | * name with digest |
| 208 | * |
| 209 | * @return{ const_iterator pointing to the end of the m_cache } |
| 210 | */ |
| 211 | InMemoryStorage::const_iterator |
| 212 | end() const; |
| 213 | |
Junxiao Shi | 9984850 | 2014-10-13 19:22:22 -0700 | [diff] [blame] | 214 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 215 | /** @brief Update the entry when the entry is returned by the find() function |
| 216 | * according to derived class implemented replacement policy |
| 217 | */ |
| 218 | virtual void |
| 219 | afterAccess(InMemoryStorageEntry* entry); |
| 220 | |
| 221 | /** @brief Update the entry or other data structures |
| 222 | * after a entry is successfully inserted |
| 223 | * according to derived class implemented replacement policy |
| 224 | */ |
| 225 | virtual void |
| 226 | afterInsert(InMemoryStorageEntry* entry); |
| 227 | |
| 228 | /** @brief Update the entry or other data structures |
| 229 | * before a entry is successfully erased |
| 230 | * according to derived class implemented replacement policy |
| 231 | */ |
| 232 | virtual void |
| 233 | beforeErase(InMemoryStorageEntry* entry); |
| 234 | |
| 235 | /** @brief Removes one Data packet from in-memory storage based on |
| 236 | * derived class implemented replacement policy |
| 237 | * |
| 238 | * Please do not use this function directly in any derived class to erase |
| 239 | * entry in the cache, use eraseHelper instead. |
| 240 | * @return{ whether the Data was removed } |
| 241 | */ |
| 242 | virtual bool |
| 243 | evictItem() = 0; |
| 244 | |
Junxiao Shi | 9984850 | 2014-10-13 19:22:22 -0700 | [diff] [blame] | 245 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 246 | /** @brief sets current capacity of in-memory storage (in packets) |
| 247 | */ |
| 248 | void |
| 249 | setCapacity(size_t nMaxPackets); |
| 250 | |
| 251 | /** @brief returns current capacity of in-memory storage (in packets) |
| 252 | * @return{ number of packets that can be stored in application cache } |
| 253 | */ |
| 254 | size_t |
| 255 | getCapacity() const |
| 256 | { |
| 257 | return m_capacity; |
| 258 | } |
| 259 | |
| 260 | /** @brief returns true if the in-memory storage uses up the current capacity, false otherwise |
| 261 | */ |
| 262 | bool |
| 263 | isFull() const |
| 264 | { |
| 265 | return size() >= m_capacity; |
| 266 | } |
| 267 | |
| 268 | /** @brief deletes in-memory storage entries by the Name with implicit digest. |
| 269 | * |
| 270 | * This is the function one should use to erase entry in the cache |
| 271 | * in derived class. |
| 272 | * It won't invoke beforeErase(shared_ptr<Entry>). |
| 273 | */ |
| 274 | void |
| 275 | eraseImpl(const Name& name); |
| 276 | |
| 277 | /** @brief Prints contents of the in-memory storage |
| 278 | */ |
| 279 | void |
| 280 | printCache(std::ostream& os) const; |
| 281 | |
Junxiao Shi | 9984850 | 2014-10-13 19:22:22 -0700 | [diff] [blame] | 282 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 283 | /** @brief free in-memory storage entries by an iterator pointing to that entry. |
Alexander Afanasyev | 8e131fd | 2014-12-15 21:31:50 -0800 | [diff] [blame] | 284 | @return An iterator pointing to the element that followed the last element erased. |
| 285 | */ |
| 286 | Cache::iterator |
| 287 | freeEntry(Cache::iterator it); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 288 | |
| 289 | /** @brief Implements child selector (leftmost, rightmost, undeclared). |
| 290 | * Operates on the first layer of a skip list. |
| 291 | * |
| 292 | * startingPoint must be less than Interest Name. |
| 293 | * startingPoint can be equal to Interest Name only |
| 294 | * when the item is in the begin() position. |
| 295 | * |
| 296 | * Iterates toward greater Names, terminates when application cache entry falls out of Interest |
| 297 | * prefix. When childSelector = leftmost, returns first application cache entry that satisfies |
| 298 | * other selectors. When childSelector = rightmost, it goes till the end, and returns application |
| 299 | * cache entry that satisfies other selectors. Returned application cache entry is the leftmost |
| 300 | * child of the rightmost child. |
| 301 | * @return{ the best match, if any; otherwise 0 } |
| 302 | */ |
| 303 | InMemoryStorageEntry* |
| 304 | selectChild(const Interest& interest, |
| 305 | Cache::index<byFullName>::type::iterator startingPoint) const; |
| 306 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 307 | /** @brief Get the next iterator (include startingPoint) that satisfies MustBeFresh requirement |
| 308 | * |
| 309 | * @param startingPoint The iterator to start with. |
| 310 | * @return The next qualified iterator |
| 311 | */ |
| 312 | Cache::index<byFullName>::type::iterator |
| 313 | findNextFresh(Cache::index<byFullName>::type::iterator startingPoint) const; |
| 314 | |
| 315 | private: |
| 316 | void |
| 317 | init(); |
| 318 | |
| 319 | public: |
| 320 | static const time::milliseconds INFINITE_WINDOW; |
| 321 | |
| 322 | private: |
| 323 | static const time::milliseconds ZERO_WINDOW; |
| 324 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 325 | private: |
| 326 | Cache m_cache; |
| 327 | /// user defined maximum capacity of the in-memory storage in packets |
| 328 | size_t m_limit; |
| 329 | /// current capacity of the in-memory storage in packets |
| 330 | size_t m_capacity; |
| 331 | /// current number of packets in in-memory storage |
| 332 | size_t m_nPackets; |
| 333 | /// memory pool |
| 334 | std::stack<InMemoryStorageEntry*> m_freeEntries; |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 335 | /// scheduler |
| 336 | unique_ptr<Scheduler> m_scheduler; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 339 | } // namespace ndn |
| 340 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 341 | #endif // NDN_IMS_IN_MEMORY_STORAGE_HPP |