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" |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 27 | #include "cs-entry.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 29 | #include "core/algorithm.hpp" |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 30 | #include <numeric> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 31 | |
| 32 | NFD_LOG_INIT("ContentStore"); |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 35 | namespace cs { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 36 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 37 | Cs::Cs(size_t nMaxPackets) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 38 | : m_limit(nMaxPackets) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 39 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 40 | BOOST_ASSERT(nMaxPackets > 0); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 41 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 43 | Cs::~Cs() |
| 44 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 45 | // It's necessary to put this empty destructor in cs.cpp, |
| 46 | // because cs::Entry has incomplete type in cs.hpp. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
| 50 | Cs::setLimit(size_t nMaxPackets) |
| 51 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 52 | BOOST_ASSERT(nMaxPackets > 0); |
| 53 | m_limit = nMaxPackets; |
| 54 | this->evict(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | size_t |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 58 | Cs::size() const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 59 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 60 | return m_table.size(); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 64 | Cs::insert(const Data& data, bool isUnsolicited) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 65 | { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 66 | NFD_LOG_DEBUG("insert " << data.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 67 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 68 | bool isNewEntry = false; TableIt it; |
| 69 | // use .insert because gcc46 does not support .emplace |
| 70 | std::tie(it, isNewEntry) = m_table.insert(Entry(data.shared_from_this(), isUnsolicited)); |
| 71 | Entry& entry = const_cast<Entry&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 72 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 73 | if (!isNewEntry) { // existing entry |
| 74 | this->detachQueue(it); |
| 75 | // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry. |
| 76 | if (entry.isUnsolicited() && !isUnsolicited) { |
| 77 | entry.unsetUnsolicited(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 78 | } |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 79 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 80 | entry.refresh(); |
| 81 | this->attachQueue(it); |
| 82 | |
| 83 | // check there are same amount of entries in the table and in queues |
| 84 | BOOST_ASSERT(m_table.size() == std::accumulate(m_queues, m_queues + QUEUE_UBOUND, 0U, |
| 85 | [] (size_t sum, const Queue queue) { return sum + queue.size(); })); |
| 86 | |
| 87 | this->evict(); // XXX The new entry could be evicted, but it shouldn't matter. |
| 88 | |
| 89 | return true; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 92 | const Data* |
| 93 | Cs::find(const Interest& interest) const |
| 94 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 95 | const Name& prefix = interest.getName(); |
| 96 | bool isRightmost = interest.getChildSelector() == 1; |
| 97 | NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L")); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 99 | TableIt first = m_table.lower_bound(prefix); |
| 100 | TableIt last = m_table.end(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 101 | if (prefix.size() > 0) { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 102 | last = m_table.lower_bound(prefix.getSuccessor()); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 103 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 104 | |
| 105 | TableIt match = last; |
| 106 | if (isRightmost) { |
| 107 | match = this->findRightmost(interest, first, last); |
| 108 | } |
| 109 | else { |
| 110 | match = this->findLeftmost(interest, first, last); |
| 111 | } |
| 112 | |
| 113 | if (match == last) { |
| 114 | NFD_LOG_DEBUG(" no-match"); |
| 115 | return nullptr; |
| 116 | } |
| 117 | NFD_LOG_DEBUG(" matching " << match->getName()); |
| 118 | return match->getData().get(); |
| 119 | } |
| 120 | |
| 121 | Cs::TableIt |
| 122 | Cs::findLeftmost(const Interest& interest, TableIt first, TableIt last) const |
| 123 | { |
| 124 | return std::find_if(first, last, bind(&cs::Entry::canSatisfy, _1, interest)); |
| 125 | } |
| 126 | |
| 127 | Cs::TableIt |
| 128 | Cs::findRightmost(const Interest& interest, TableIt first, TableIt last) const |
| 129 | { |
| 130 | // Each loop visits a sub-namespace under a prefix one component longer than Interest Name. |
| 131 | // If there is a match in that sub-namespace, the leftmost match is returned; |
| 132 | // otherwise, loop continues. |
| 133 | |
| 134 | size_t interestNameLength = interest.getName().size(); |
| 135 | for (TableIt right = last; right != first;) { |
| 136 | TableIt prev = std::prev(right); |
| 137 | |
| 138 | // special case: [first,prev] have exact Names |
| 139 | if (prev->getName().size() == interestNameLength) { |
| 140 | NFD_LOG_TRACE(" find-among-exact " << prev->getName()); |
| 141 | TableIt matchExact = this->findRightmostAmongExact(interest, first, right); |
| 142 | return matchExact == right ? last : matchExact; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 145 | Name prefix = prev->getName().getPrefix(interestNameLength + 1); |
| 146 | TableIt left = m_table.lower_bound(prefix); |
| 147 | |
| 148 | // normal case: [left,right) are under one-component-longer prefix |
| 149 | NFD_LOG_TRACE(" find-under-prefix " << prefix); |
| 150 | TableIt match = this->findLeftmost(interest, left, right); |
| 151 | if (match != right) { |
| 152 | return match; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 153 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 154 | right = left; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 155 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 156 | return last; |
| 157 | } |
| 158 | |
| 159 | Cs::TableIt |
| 160 | Cs::findRightmostAmongExact(const Interest& interest, TableIt first, TableIt last) const |
| 161 | { |
| 162 | return find_last_if(first, last, bind(&Entry::canSatisfy, _1, interest)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 166 | Cs::attachQueue(TableIt it) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 167 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 168 | Entry& entry = const_cast<Entry&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 169 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 170 | if (entry.queueType != QUEUE_NONE) { |
| 171 | this->detachQueue(it); |
| 172 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 173 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 174 | if (entry.isUnsolicited()) { |
| 175 | entry.queueType = QUEUE_UNSOLICITED; |
| 176 | } |
| 177 | else if (entry.isStale()) { |
| 178 | entry.queueType = QUEUE_STALE; |
| 179 | } |
| 180 | else { |
| 181 | entry.queueType = QUEUE_FIFO; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 182 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 183 | if (entry.canStale()) { |
| 184 | entry.moveStaleEvent = scheduler::schedule(entry.getData()->getFreshnessPeriod(), |
| 185 | bind(&Cs::moveToStaleQueue, this, it)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 186 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 187 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 188 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 189 | Queue& queue = m_queues[entry.queueType]; |
| 190 | entry.queueIt = queue.insert(queue.end(), it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 194 | Cs::detachQueue(TableIt it) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 195 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 196 | Entry& entry = const_cast<Entry&>(*it); |
| 197 | |
| 198 | BOOST_ASSERT(entry.queueType != QUEUE_NONE); |
| 199 | |
| 200 | if (entry.queueType == QUEUE_FIFO) { |
| 201 | scheduler::cancel(entry.moveStaleEvent); |
| 202 | } |
| 203 | |
| 204 | m_queues[entry.queueType].erase(entry.queueIt); |
| 205 | entry.queueType = QUEUE_NONE; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 206 | } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 207 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 208 | void |
| 209 | Cs::moveToStaleQueue(TableIt it) |
| 210 | { |
| 211 | Entry& entry = const_cast<Entry&>(*it); |
| 212 | |
| 213 | BOOST_ASSERT(entry.queueType == QUEUE_FIFO); |
| 214 | m_queues[QUEUE_FIFO].erase(entry.queueIt); |
| 215 | |
| 216 | entry.queueType = QUEUE_STALE; |
| 217 | Queue& queue = m_queues[QUEUE_STALE]; |
| 218 | entry.queueIt = queue.insert(queue.end(), it); |
| 219 | } |
| 220 | |
| 221 | std::tuple<Cs::TableIt, std::string> |
| 222 | Cs::evictPick() |
| 223 | { |
| 224 | if (!m_queues[QUEUE_UNSOLICITED].empty()) { |
| 225 | return std::make_tuple(m_queues[QUEUE_UNSOLICITED].front(), "unsolicited"); |
| 226 | } |
| 227 | if (!m_queues[QUEUE_STALE].empty()) { |
| 228 | return std::make_tuple(m_queues[QUEUE_STALE].front(), "stale"); |
| 229 | } |
| 230 | if (!m_queues[QUEUE_FIFO].empty()) { |
| 231 | return std::make_tuple(m_queues[QUEUE_FIFO].front(), "fifo"); |
| 232 | } |
| 233 | |
| 234 | BOOST_ASSERT(false); |
| 235 | return std::make_tuple(m_table.end(), "error"); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void |
| 240 | Cs::evict() |
| 241 | { |
| 242 | while (this->size() > m_limit) { |
| 243 | TableIt it; std::string reason; |
| 244 | std::tie(it, reason) = this->evictPick(); |
| 245 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 246 | NFD_LOG_DEBUG("evict " << it->getName() << " " << reason); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 247 | this->detachQueue(it); |
| 248 | m_table.erase(it); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | Cs::dump() |
| 254 | { |
| 255 | NFD_LOG_DEBUG("dump table"); |
| 256 | for (const Entry& entry : m_table) { |
| 257 | NFD_LOG_TRACE(entry.getFullName()); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | } // namespace cs |
| 262 | } // namespace nfd |