Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 | 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 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 36 | Cs::Cs(size_t nMaxPackets) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 37 | : m_limit(nMaxPackets) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 38 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 39 | BOOST_ASSERT(nMaxPackets > 0); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 40 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 41 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 42 | Cs::~Cs() |
| 43 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 44 | // It's necessary to put this empty destructor in cs.cpp, |
| 45 | // because cs::Entry has incomplete type in cs.hpp. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
| 49 | Cs::setLimit(size_t nMaxPackets) |
| 50 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 51 | BOOST_ASSERT(nMaxPackets > 0); |
| 52 | m_limit = nMaxPackets; |
| 53 | this->evict(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | size_t |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 57 | Cs::size() const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 58 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 59 | return m_table.size(); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 63 | Cs::insert(const Data& data, bool isUnsolicited) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 64 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 65 | NFD_LOG_DEBUG("insert " << data.getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 66 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 67 | bool isNewEntry = false; TableIt it; |
| 68 | // use .insert because gcc46 does not support .emplace |
| 69 | std::tie(it, isNewEntry) = m_table.insert(Entry(data.shared_from_this(), isUnsolicited)); |
| 70 | Entry& entry = const_cast<Entry&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 71 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 72 | if (!isNewEntry) { // existing entry |
| 73 | this->detachQueue(it); |
| 74 | // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry. |
| 75 | if (entry.isUnsolicited() && !isUnsolicited) { |
| 76 | entry.unsetUnsolicited(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 77 | } |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 78 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 79 | entry.refresh(); |
| 80 | this->attachQueue(it); |
| 81 | |
| 82 | // check there are same amount of entries in the table and in queues |
| 83 | BOOST_ASSERT(m_table.size() == std::accumulate(m_queues, m_queues + QUEUE_UBOUND, 0U, |
| 84 | [] (size_t sum, const Queue queue) { return sum + queue.size(); })); |
| 85 | |
| 86 | this->evict(); // XXX The new entry could be evicted, but it shouldn't matter. |
| 87 | |
| 88 | return true; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 92 | Cs::isNewRightmostCandidate(const Name& prefix, TableIt a, TableIt b) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 93 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 94 | if (a->getFullName().size() == prefix.size()) { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 95 | return true; |
| 96 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 97 | return b->getFullName().at(prefix.size()) > a->getFullName().at(prefix.size()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | const Data* |
| 101 | Cs::find(const Interest& interest) const |
| 102 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 103 | const Name& prefix = interest.getName(); |
| 104 | bool isRightmost = interest.getChildSelector() == 1; |
| 105 | NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L")); |
| 106 | TableIt rightmostCandidate = m_table.end(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 107 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 108 | TableIt left = m_table.lower_bound(prefix); |
| 109 | TableIt right = m_table.end(); |
| 110 | if (prefix.size() > 0) { |
| 111 | right = m_table.lower_bound(prefix.getSuccessor()); |
| 112 | } |
| 113 | for (TableIt it = left; it != right; ++it) { |
| 114 | const Entry& entry = *it; |
| 115 | if (!entry.canSatisfy(interest)) { |
| 116 | NFD_LOG_TRACE("cannotSatisfy " << entry.getFullName()); |
| 117 | continue; |
| 118 | } |
| 119 | NFD_LOG_TRACE("canSatisfy " << entry.getFullName()); |
| 120 | if (!isRightmost) { |
| 121 | return entry.getData().get(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 124 | if (rightmostCandidate == m_table.end() || |
| 125 | isNewRightmostCandidate(prefix, rightmostCandidate, it)) { |
| 126 | NFD_LOG_TRACE("rightmostCandidate=" << entry.getFullName()); |
| 127 | rightmostCandidate = it; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 128 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 129 | } |
| 130 | if (isRightmost) { |
| 131 | return rightmostCandidate == m_table.end() ? nullptr : |
| 132 | rightmostCandidate->getData().get(); |
| 133 | } |
| 134 | return nullptr; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 138 | Cs::attachQueue(TableIt it) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 139 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 140 | Entry& entry = const_cast<Entry&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 141 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 142 | if (entry.queueType != QUEUE_NONE) { |
| 143 | this->detachQueue(it); |
| 144 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 145 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 146 | if (entry.isUnsolicited()) { |
| 147 | entry.queueType = QUEUE_UNSOLICITED; |
| 148 | } |
| 149 | else if (entry.isStale()) { |
| 150 | entry.queueType = QUEUE_STALE; |
| 151 | } |
| 152 | else { |
| 153 | entry.queueType = QUEUE_FIFO; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 154 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 155 | if (entry.canStale()) { |
| 156 | entry.moveStaleEvent = scheduler::schedule(entry.getData()->getFreshnessPeriod(), |
| 157 | bind(&Cs::moveToStaleQueue, this, it)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 158 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 159 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 160 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 161 | Queue& queue = m_queues[entry.queueType]; |
| 162 | entry.queueIt = queue.insert(queue.end(), it); |
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::detachQueue(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); |
| 169 | |
| 170 | BOOST_ASSERT(entry.queueType != QUEUE_NONE); |
| 171 | |
| 172 | if (entry.queueType == QUEUE_FIFO) { |
| 173 | scheduler::cancel(entry.moveStaleEvent); |
| 174 | } |
| 175 | |
| 176 | m_queues[entry.queueType].erase(entry.queueIt); |
| 177 | entry.queueType = QUEUE_NONE; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 178 | } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 179 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 180 | void |
| 181 | Cs::moveToStaleQueue(TableIt it) |
| 182 | { |
| 183 | Entry& entry = const_cast<Entry&>(*it); |
| 184 | |
| 185 | BOOST_ASSERT(entry.queueType == QUEUE_FIFO); |
| 186 | m_queues[QUEUE_FIFO].erase(entry.queueIt); |
| 187 | |
| 188 | entry.queueType = QUEUE_STALE; |
| 189 | Queue& queue = m_queues[QUEUE_STALE]; |
| 190 | entry.queueIt = queue.insert(queue.end(), it); |
| 191 | } |
| 192 | |
| 193 | std::tuple<Cs::TableIt, std::string> |
| 194 | Cs::evictPick() |
| 195 | { |
| 196 | if (!m_queues[QUEUE_UNSOLICITED].empty()) { |
| 197 | return std::make_tuple(m_queues[QUEUE_UNSOLICITED].front(), "unsolicited"); |
| 198 | } |
| 199 | if (!m_queues[QUEUE_STALE].empty()) { |
| 200 | return std::make_tuple(m_queues[QUEUE_STALE].front(), "stale"); |
| 201 | } |
| 202 | if (!m_queues[QUEUE_FIFO].empty()) { |
| 203 | return std::make_tuple(m_queues[QUEUE_FIFO].front(), "fifo"); |
| 204 | } |
| 205 | |
| 206 | BOOST_ASSERT(false); |
| 207 | return std::make_tuple(m_table.end(), "error"); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | void |
| 212 | Cs::evict() |
| 213 | { |
| 214 | while (this->size() > m_limit) { |
| 215 | TableIt it; std::string reason; |
| 216 | std::tie(it, reason) = this->evictPick(); |
| 217 | |
| 218 | NFD_LOG_DEBUG("evict " << it->getFullName() << " " << reason); |
| 219 | this->detachQueue(it); |
| 220 | m_table.erase(it); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | Cs::dump() |
| 226 | { |
| 227 | NFD_LOG_DEBUG("dump table"); |
| 228 | for (const Entry& entry : m_table) { |
| 229 | NFD_LOG_TRACE(entry.getFullName()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | } // namespace cs |
| 234 | } // namespace nfd |