Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 26 | #include "cs.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 28 | #include "core/algorithm.hpp" |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 29 | #include <numeric> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 30 | |
| 31 | NFD_LOG_INIT("ContentStore"); |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 34 | namespace cs { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 35 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 36 | // http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 37 | BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Cs::const_iterator>)); |
| 38 | // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html, |
| 39 | // which doesn't require DefaultConstructible |
| 40 | #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 41 | static_assert(std::is_default_constructible<Cs::const_iterator>::value, |
| 42 | "Cs::const_iterator must be default-constructible"); |
| 43 | #else |
| 44 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Cs::const_iterator>)); |
| 45 | #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 46 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 47 | Cs::Cs(size_t nMaxPackets) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 48 | : m_limit(nMaxPackets) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 49 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 50 | BOOST_ASSERT(nMaxPackets > 0); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 51 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 52 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 53 | void |
| 54 | Cs::setLimit(size_t nMaxPackets) |
| 55 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 56 | BOOST_ASSERT(nMaxPackets > 0); |
| 57 | m_limit = nMaxPackets; |
| 58 | this->evict(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 61 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 62 | Cs::insert(const Data& data, bool isUnsolicited) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 63 | { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 64 | NFD_LOG_DEBUG("insert " << data.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 65 | |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 66 | // recognize CachingPolicy |
| 67 | using ndn::nfd::LocalControlHeader; |
| 68 | const LocalControlHeader& lch = data.getLocalControlHeader(); |
| 69 | if (lch.hasCachingPolicy()) { |
| 70 | LocalControlHeader::CachingPolicy policy = lch.getCachingPolicy(); |
| 71 | if (policy == LocalControlHeader::CachingPolicy::NO_CACHE) { |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 76 | bool isNewEntry = false; TableIt it; |
| 77 | // use .insert because gcc46 does not support .emplace |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 78 | std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited)); |
| 79 | EntryImpl& entry = const_cast<EntryImpl&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 80 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 81 | if (!isNewEntry) { // existing entry |
| 82 | this->detachQueue(it); |
| 83 | // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry. |
| 84 | if (entry.isUnsolicited() && !isUnsolicited) { |
| 85 | entry.unsetUnsolicited(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 86 | } |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 87 | } |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 88 | entry.updateStaleTime(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 89 | this->attachQueue(it); |
| 90 | |
| 91 | // check there are same amount of entries in the table and in queues |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 92 | BOOST_ASSERT(m_table.size() == std::accumulate(m_queues, m_queues + QUEUE_MAX, 0U, |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 93 | [] (size_t sum, const Queue queue) { return sum + queue.size(); })); |
| 94 | |
| 95 | this->evict(); // XXX The new entry could be evicted, but it shouldn't matter. |
| 96 | |
| 97 | return true; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | } |
| 99 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 100 | void |
| 101 | Cs::find(const Interest& interest, |
| 102 | const HitCallback& hitCallback, |
| 103 | const MissCallback& missCallback) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 104 | { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 105 | BOOST_ASSERT(static_cast<bool>(hitCallback)); |
| 106 | BOOST_ASSERT(static_cast<bool>(missCallback)); |
| 107 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 108 | const Name& prefix = interest.getName(); |
| 109 | bool isRightmost = interest.getChildSelector() == 1; |
| 110 | NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L")); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 111 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 112 | TableIt first = m_table.lower_bound(prefix); |
| 113 | TableIt last = m_table.end(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 114 | if (prefix.size() > 0) { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 115 | last = m_table.lower_bound(prefix.getSuccessor()); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 116 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 117 | |
| 118 | TableIt match = last; |
| 119 | if (isRightmost) { |
| 120 | match = this->findRightmost(interest, first, last); |
| 121 | } |
| 122 | else { |
| 123 | match = this->findLeftmost(interest, first, last); |
| 124 | } |
| 125 | |
| 126 | if (match == last) { |
| 127 | NFD_LOG_DEBUG(" no-match"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 128 | missCallback(interest); |
| 129 | return; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 130 | } |
| 131 | NFD_LOG_DEBUG(" matching " << match->getName()); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 132 | hitCallback(interest, match->getData()); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 135 | TableIt |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 136 | Cs::findLeftmost(const Interest& interest, TableIt first, TableIt last) const |
| 137 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 138 | return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest)); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 141 | TableIt |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 142 | Cs::findRightmost(const Interest& interest, TableIt first, TableIt last) const |
| 143 | { |
| 144 | // Each loop visits a sub-namespace under a prefix one component longer than Interest Name. |
| 145 | // If there is a match in that sub-namespace, the leftmost match is returned; |
| 146 | // otherwise, loop continues. |
| 147 | |
| 148 | size_t interestNameLength = interest.getName().size(); |
| 149 | for (TableIt right = last; right != first;) { |
| 150 | TableIt prev = std::prev(right); |
| 151 | |
| 152 | // special case: [first,prev] have exact Names |
| 153 | if (prev->getName().size() == interestNameLength) { |
| 154 | NFD_LOG_TRACE(" find-among-exact " << prev->getName()); |
| 155 | TableIt matchExact = this->findRightmostAmongExact(interest, first, right); |
| 156 | return matchExact == right ? last : matchExact; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 159 | Name prefix = prev->getName().getPrefix(interestNameLength + 1); |
| 160 | TableIt left = m_table.lower_bound(prefix); |
| 161 | |
| 162 | // normal case: [left,right) are under one-component-longer prefix |
| 163 | NFD_LOG_TRACE(" find-under-prefix " << prefix); |
| 164 | TableIt match = this->findLeftmost(interest, left, right); |
| 165 | if (match != right) { |
| 166 | return match; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 167 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 168 | right = left; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 169 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 170 | return last; |
| 171 | } |
| 172 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 173 | TableIt |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 174 | Cs::findRightmostAmongExact(const Interest& interest, TableIt first, TableIt last) const |
| 175 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 176 | return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 180 | Cs::attachQueue(TableIt it) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 181 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 182 | EntryImpl& entry = const_cast<EntryImpl&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 183 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 184 | if (entry.queueType != QUEUE_NONE) { |
| 185 | this->detachQueue(it); |
| 186 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 187 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 188 | if (entry.isUnsolicited()) { |
| 189 | entry.queueType = QUEUE_UNSOLICITED; |
| 190 | } |
| 191 | else if (entry.isStale()) { |
| 192 | entry.queueType = QUEUE_STALE; |
| 193 | } |
| 194 | else { |
| 195 | entry.queueType = QUEUE_FIFO; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 196 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 197 | if (entry.canStale()) { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 198 | entry.moveStaleEvent = scheduler::schedule(entry.getData().getFreshnessPeriod(), |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 199 | bind(&Cs::moveToStaleQueue, this, it)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 200 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 201 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 202 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 203 | Queue& queue = m_queues[entry.queueType]; |
| 204 | entry.queueIt = queue.insert(queue.end(), it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 208 | Cs::detachQueue(TableIt it) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 209 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 210 | EntryImpl& entry = const_cast<EntryImpl&>(*it); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 211 | |
| 212 | BOOST_ASSERT(entry.queueType != QUEUE_NONE); |
| 213 | |
| 214 | if (entry.queueType == QUEUE_FIFO) { |
| 215 | scheduler::cancel(entry.moveStaleEvent); |
| 216 | } |
| 217 | |
| 218 | m_queues[entry.queueType].erase(entry.queueIt); |
| 219 | entry.queueType = QUEUE_NONE; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 220 | } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 221 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 222 | void |
| 223 | Cs::moveToStaleQueue(TableIt it) |
| 224 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 225 | EntryImpl& entry = const_cast<EntryImpl&>(*it); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 226 | |
| 227 | BOOST_ASSERT(entry.queueType == QUEUE_FIFO); |
| 228 | m_queues[QUEUE_FIFO].erase(entry.queueIt); |
| 229 | |
| 230 | entry.queueType = QUEUE_STALE; |
| 231 | Queue& queue = m_queues[QUEUE_STALE]; |
| 232 | entry.queueIt = queue.insert(queue.end(), it); |
| 233 | } |
| 234 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 235 | std::tuple<TableIt, std::string> |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 236 | Cs::evictPick() |
| 237 | { |
| 238 | if (!m_queues[QUEUE_UNSOLICITED].empty()) { |
| 239 | return std::make_tuple(m_queues[QUEUE_UNSOLICITED].front(), "unsolicited"); |
| 240 | } |
| 241 | if (!m_queues[QUEUE_STALE].empty()) { |
| 242 | return std::make_tuple(m_queues[QUEUE_STALE].front(), "stale"); |
| 243 | } |
| 244 | if (!m_queues[QUEUE_FIFO].empty()) { |
| 245 | return std::make_tuple(m_queues[QUEUE_FIFO].front(), "fifo"); |
| 246 | } |
| 247 | |
| 248 | BOOST_ASSERT(false); |
| 249 | return std::make_tuple(m_table.end(), "error"); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void |
| 254 | Cs::evict() |
| 255 | { |
| 256 | while (this->size() > m_limit) { |
| 257 | TableIt it; std::string reason; |
| 258 | std::tie(it, reason) = this->evictPick(); |
| 259 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 260 | NFD_LOG_DEBUG("evict " << it->getName() << " " << reason); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 261 | this->detachQueue(it); |
| 262 | m_table.erase(it); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | Cs::dump() |
| 268 | { |
| 269 | NFD_LOG_DEBUG("dump table"); |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 270 | for (const EntryImpl& entry : m_table) { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 271 | NFD_LOG_TRACE(entry.getFullName()); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | } // namespace cs |
| 276 | } // namespace nfd |