Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "in-memory-storage.hpp" |
| 23 | #include "in-memory-storage-entry.hpp" |
| 24 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 25 | namespace ndn { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 26 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 27 | const time::milliseconds InMemoryStorage::INFINITE_WINDOW(-1); |
| 28 | const time::milliseconds InMemoryStorage::ZERO_WINDOW(0); |
| 29 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 30 | InMemoryStorage::const_iterator::const_iterator(const Data* ptr, const Cache* cache, |
| 31 | Cache::index<byFullName>::type::iterator it) |
| 32 | : m_ptr(ptr) |
| 33 | , m_cache(cache) |
| 34 | , m_it(it) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | InMemoryStorage::const_iterator& |
| 39 | InMemoryStorage::const_iterator::operator++() |
| 40 | { |
| 41 | m_it++; |
| 42 | if (m_it != m_cache->get<byFullName>().end()) { |
| 43 | m_ptr = &((*m_it)->getData()); |
| 44 | } |
| 45 | else { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 46 | m_ptr = nullptr; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | InMemoryStorage::const_iterator |
| 53 | InMemoryStorage::const_iterator::operator++(int) |
| 54 | { |
| 55 | InMemoryStorage::const_iterator i(*this); |
| 56 | this->operator++(); |
| 57 | return i; |
| 58 | } |
| 59 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 60 | InMemoryStorage::const_iterator::reference |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 61 | InMemoryStorage::const_iterator::operator*() |
| 62 | { |
| 63 | return *m_ptr; |
| 64 | } |
| 65 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 66 | InMemoryStorage::const_iterator::pointer |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 67 | InMemoryStorage::const_iterator::operator->() |
| 68 | { |
| 69 | return m_ptr; |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | InMemoryStorage::const_iterator::operator==(const const_iterator& rhs) |
| 74 | { |
| 75 | return m_it == rhs.m_it; |
| 76 | } |
| 77 | |
| 78 | bool |
| 79 | InMemoryStorage::const_iterator::operator!=(const const_iterator& rhs) |
| 80 | { |
| 81 | return m_it != rhs.m_it; |
| 82 | } |
| 83 | |
| 84 | InMemoryStorage::InMemoryStorage(size_t limit) |
| 85 | : m_limit(limit) |
| 86 | , m_nPackets(0) |
| 87 | { |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 88 | init(); |
| 89 | } |
| 90 | |
| 91 | InMemoryStorage::InMemoryStorage(boost::asio::io_service& ioService, size_t limit) |
| 92 | : m_limit(limit) |
| 93 | , m_nPackets(0) |
| 94 | { |
| 95 | m_scheduler = make_unique<Scheduler>(ioService); |
| 96 | init(); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | InMemoryStorage::init() |
| 101 | { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 102 | // TODO consider a more suitable initial value |
| 103 | m_capacity = 10; |
| 104 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 105 | if (m_limit != std::numeric_limits<size_t>::max() && m_capacity > m_limit) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 106 | m_capacity = m_limit; |
| 107 | } |
| 108 | |
| 109 | for (size_t i = 0; i < m_capacity; i++) { |
| 110 | m_freeEntries.push(new InMemoryStorageEntry()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | InMemoryStorage::~InMemoryStorage() |
| 115 | { |
| 116 | // evict all items from cache |
| 117 | Cache::iterator it = m_cache.begin(); |
| 118 | while (it != m_cache.end()) { |
Alexander Afanasyev | 8e131fd | 2014-12-15 21:31:50 -0800 | [diff] [blame] | 119 | it = freeEntry(it); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | BOOST_ASSERT(m_freeEntries.size() == m_capacity); |
| 123 | |
| 124 | while (!m_freeEntries.empty()) { |
| 125 | delete m_freeEntries.top(); |
| 126 | m_freeEntries.pop(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | InMemoryStorage::setCapacity(size_t capacity) |
| 132 | { |
| 133 | size_t oldCapacity = m_capacity; |
| 134 | m_capacity = capacity; |
| 135 | |
| 136 | if (size() > m_capacity) { |
| 137 | ssize_t nAllowedFailures = size() - m_capacity; |
| 138 | while (size() > m_capacity) { |
| 139 | if (!evictItem() && --nAllowedFailures < 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 140 | BOOST_THROW_EXCEPTION(Error()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (m_capacity >= oldCapacity) { |
| 146 | for (size_t i = oldCapacity; i < m_capacity; i++) { |
| 147 | m_freeEntries.push(new InMemoryStorageEntry()); |
| 148 | } |
| 149 | } |
| 150 | else { |
| 151 | for (size_t i = oldCapacity; i > m_capacity; i--) { |
| 152 | delete m_freeEntries.top(); |
| 153 | m_freeEntries.pop(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | BOOST_ASSERT(size() + m_freeEntries.size() == m_capacity); |
| 158 | } |
| 159 | |
| 160 | void |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 161 | InMemoryStorage::insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 162 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 163 | // check if identical Data/Name already exists |
| 164 | auto it = m_cache.get<byFullName>().find(data.getFullName()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 165 | if (it != m_cache.get<byFullName>().end()) |
| 166 | return; |
| 167 | |
| 168 | //if full, double the capacity |
| 169 | bool doesReachLimit = (getLimit() == getCapacity()); |
| 170 | if (isFull() && !doesReachLimit) { |
| 171 | // note: This is incorrect if 2*capacity overflows, but memory should run out before that |
| 172 | size_t newCapacity = std::min(2 * getCapacity(), getLimit()); |
| 173 | setCapacity(newCapacity); |
| 174 | } |
| 175 | |
| 176 | //if full and reach limitation of the capacity, employ replacement policy |
| 177 | if (isFull() && doesReachLimit) { |
| 178 | evictItem(); |
| 179 | } |
| 180 | |
| 181 | //insert to cache |
| 182 | BOOST_ASSERT(m_freeEntries.size() > 0); |
| 183 | // take entry for the memory pool |
| 184 | InMemoryStorageEntry* entry = m_freeEntries.top(); |
| 185 | m_freeEntries.pop(); |
| 186 | m_nPackets++; |
| 187 | entry->setData(data); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 188 | if (m_scheduler != nullptr && mustBeFreshProcessingWindow > ZERO_WINDOW) { |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 189 | auto eventId = make_unique<util::scheduler::ScopedEventId>(*m_scheduler); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 190 | *eventId = m_scheduler->scheduleEvent(mustBeFreshProcessingWindow, [entry] { entry->markStale(); }); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 191 | entry->setMarkStaleEventId(std::move(eventId)); |
| 192 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 193 | m_cache.insert(entry); |
| 194 | |
| 195 | //let derived class do something with the entry |
| 196 | afterInsert(entry); |
| 197 | } |
| 198 | |
| 199 | shared_ptr<const Data> |
| 200 | InMemoryStorage::find(const Name& name) |
| 201 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 202 | auto it = m_cache.get<byFullName>().lower_bound(name); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 203 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 204 | // if not found, return null |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 205 | if (it == m_cache.get<byFullName>().end()) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 206 | return nullptr; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 209 | // if the given name is not the prefix of the lower_bound, return null |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 210 | if (!name.isPrefixOf((*it)->getFullName())) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 211 | return nullptr; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | afterAccess(*it); |
| 215 | return ((*it)->getData()).shared_from_this(); |
| 216 | } |
| 217 | |
| 218 | shared_ptr<const Data> |
| 219 | InMemoryStorage::find(const Interest& interest) |
| 220 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 221 | // if the interest contains implicit digest, it is possible to directly locate a packet. |
| 222 | auto it = m_cache.get<byFullName>().find(interest.getName()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 223 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 224 | // if a packet is located by its full name, it must be the packet to return. |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 225 | if (it != m_cache.get<byFullName>().end()) { |
| 226 | return ((*it)->getData()).shared_from_this(); |
| 227 | } |
| 228 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 229 | // if the packet is not discovered by last step, either the packet is not in the storage or |
| 230 | // the interest doesn't contains implicit digest. |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 231 | it = m_cache.get<byFullName>().lower_bound(interest.getName()); |
| 232 | |
| 233 | if (it == m_cache.get<byFullName>().end()) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 234 | return nullptr; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 237 | // to locate the element that has a just smaller name than the interest's |
| 238 | if (it != m_cache.get<byFullName>().begin()) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 239 | it--; |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 240 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 241 | |
| 242 | InMemoryStorageEntry* ret = selectChild(interest, it); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 243 | if (ret == nullptr) { |
| 244 | return nullptr; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 245 | } |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 246 | |
| 247 | // let derived class do something with the entry |
| 248 | afterAccess(ret); |
| 249 | return ret->getData().shared_from_this(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 252 | InMemoryStorage::Cache::index<InMemoryStorage::byFullName>::type::iterator |
| 253 | InMemoryStorage::findNextFresh(Cache::index<byFullName>::type::iterator it) const |
| 254 | { |
| 255 | for (; it != m_cache.get<byFullName>().end(); it++) { |
| 256 | if ((*it)->isFresh()) |
| 257 | return it; |
| 258 | } |
| 259 | |
| 260 | return it; |
| 261 | } |
| 262 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 263 | InMemoryStorageEntry* |
| 264 | InMemoryStorage::selectChild(const Interest& interest, |
| 265 | Cache::index<byFullName>::type::iterator startingPoint) const |
| 266 | { |
| 267 | BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end()); |
| 268 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 269 | if (startingPoint != m_cache.get<byFullName>().begin()) { |
| 270 | BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName()); |
| 271 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 272 | |
| 273 | bool hasLeftmostSelector = (interest.getChildSelector() <= 0); |
| 274 | bool hasRightmostSelector = !hasLeftmostSelector; |
| 275 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 276 | // filter out "stale" data |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 277 | if (interest.getMustBeFresh()) { |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 278 | startingPoint = findNextFresh(startingPoint); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 279 | } |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 280 | |
| 281 | if (startingPoint == m_cache.get<byFullName>().end()) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 285 | if (hasLeftmostSelector) { |
| 286 | if (interest.matchesData((*startingPoint)->getData())) { |
| 287 | return *startingPoint; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 288 | } |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 289 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 290 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 291 | // iterate to the right |
| 292 | auto rightmost = startingPoint; |
| 293 | if (startingPoint != m_cache.get<byFullName>().end()) { |
| 294 | auto rightmostCandidate = startingPoint; |
| 295 | Name currentChildPrefix(""); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 296 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 297 | while (true) { |
| 298 | ++rightmostCandidate; |
| 299 | // filter out "stale" data |
| 300 | if (interest.getMustBeFresh()) { |
| 301 | rightmostCandidate = findNextFresh(rightmostCandidate); |
| 302 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 303 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 304 | bool isInBoundaries = (rightmostCandidate != m_cache.get<byFullName>().end()); |
| 305 | bool isInPrefix = false; |
| 306 | if (isInBoundaries) { |
| 307 | isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName()); |
| 308 | } |
| 309 | |
| 310 | if (isInPrefix) { |
| 311 | if (interest.matchesData((*rightmostCandidate)->getData())) { |
| 312 | if (hasLeftmostSelector) { |
| 313 | return *rightmostCandidate; |
| 314 | } |
| 315 | |
| 316 | if (hasRightmostSelector) { |
| 317 | // get prefix which is one component longer than Interest name |
| 318 | const Name& childPrefix = (*rightmostCandidate)->getFullName().getPrefix(interest.getName().size() + 1); |
| 319 | if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix)) { |
| 320 | currentChildPrefix = childPrefix; |
| 321 | rightmost = rightmostCandidate; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 322 | } |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 323 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 324 | } |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 325 | } |
| 326 | else { |
| 327 | break; |
| 328 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 329 | } |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 330 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 331 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 332 | if (rightmost != startingPoint) { |
| 333 | return *rightmost; |
| 334 | } |
| 335 | |
| 336 | if (hasRightmostSelector) { // if rightmost was not found, try starting point |
| 337 | if (interest.matchesData((*startingPoint)->getData())) { |
| 338 | return *startingPoint; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 339 | } |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 340 | } |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 341 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 342 | return nullptr; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Alexander Afanasyev | 8e131fd | 2014-12-15 21:31:50 -0800 | [diff] [blame] | 345 | InMemoryStorage::Cache::iterator |
| 346 | InMemoryStorage::freeEntry(Cache::iterator it) |
| 347 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 348 | // push the *empty* entry into mem pool |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 349 | (*it)->release(); |
| 350 | m_freeEntries.push(*it); |
| 351 | m_nPackets--; |
Alexander Afanasyev | 8e131fd | 2014-12-15 21:31:50 -0800 | [diff] [blame] | 352 | return m_cache.erase(it); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void |
| 356 | InMemoryStorage::erase(const Name& prefix, const bool isPrefix) |
| 357 | { |
| 358 | if (isPrefix) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 359 | auto it = m_cache.get<byFullName>().lower_bound(prefix); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 360 | while (it != m_cache.get<byFullName>().end() && prefix.isPrefixOf((*it)->getName())) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 361 | // let derived class do something with the entry |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 362 | beforeErase(*it); |
Alexander Afanasyev | 8e131fd | 2014-12-15 21:31:50 -0800 | [diff] [blame] | 363 | it = freeEntry(it); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | else { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 367 | auto it = m_cache.get<byFullName>().find(prefix); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 368 | if (it == m_cache.get<byFullName>().end()) |
| 369 | return; |
| 370 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 371 | // let derived class do something with the entry |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 372 | beforeErase(*it); |
| 373 | freeEntry(it); |
| 374 | } |
| 375 | |
| 376 | if (m_freeEntries.size() > (2 * size())) |
| 377 | setCapacity(getCapacity() / 2); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | InMemoryStorage::eraseImpl(const Name& name) |
| 382 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 383 | auto it = m_cache.get<byFullName>().find(name); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 384 | if (it == m_cache.get<byFullName>().end()) |
| 385 | return; |
| 386 | |
| 387 | freeEntry(it); |
| 388 | } |
| 389 | |
| 390 | InMemoryStorage::const_iterator |
| 391 | InMemoryStorage::begin() const |
| 392 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 393 | auto it = m_cache.get<byFullName>().begin(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 394 | return const_iterator(&((*it)->getData()), &m_cache, it); |
| 395 | } |
| 396 | |
| 397 | InMemoryStorage::const_iterator |
| 398 | InMemoryStorage::end() const |
| 399 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 400 | auto it = m_cache.get<byFullName>().end(); |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 401 | return const_iterator(nullptr, &m_cache, it); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void |
| 405 | InMemoryStorage::afterInsert(InMemoryStorageEntry* entry) |
| 406 | { |
| 407 | } |
| 408 | |
| 409 | void |
| 410 | InMemoryStorage::beforeErase(InMemoryStorageEntry* entry) |
| 411 | { |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | InMemoryStorage::afterAccess(InMemoryStorageEntry* entry) |
| 416 | { |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | InMemoryStorage::printCache(std::ostream& os) const |
| 421 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 422 | // start from the upper layer towards bottom |
| 423 | for (const auto& elem : m_cache.get<byFullName>()) |
| 424 | os << elem->getFullName() << std::endl; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 427 | } // namespace ndn |