Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 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 | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/lp/tags.hpp> |
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 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 47 | unique_ptr<Policy> |
| 48 | makeDefaultPolicy() |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 49 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 50 | const std::string DEFAULT_POLICY = "priority_fifo"; |
| 51 | return Policy::create(DEFAULT_POLICY); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 54 | Cs::Cs(size_t nMaxPackets) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 55 | { |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 56 | this->setPolicyImpl(makeDefaultPolicy()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 57 | m_policy->setLimit(nMaxPackets); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 58 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 59 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 60 | void |
| 61 | Cs::setLimit(size_t nMaxPackets) |
| 62 | { |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 63 | m_policy->setLimit(nMaxPackets); |
| 64 | } |
| 65 | |
| 66 | size_t |
| 67 | Cs::getLimit() const |
| 68 | { |
| 69 | return m_policy->getLimit(); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | Cs::setPolicy(unique_ptr<Policy> policy) |
| 74 | { |
| 75 | BOOST_ASSERT(policy != nullptr); |
| 76 | BOOST_ASSERT(m_policy != nullptr); |
| 77 | size_t limit = m_policy->getLimit(); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 78 | this->setPolicyImpl(std::move(policy)); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 79 | m_policy->setLimit(limit); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 82 | void |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 83 | Cs::insert(const Data& data, bool isUnsolicited) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 84 | { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 85 | NFD_LOG_DEBUG("insert " << data.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 86 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 87 | if (m_policy->getLimit() == 0) { |
| 88 | // shortcut for disabled CS |
| 89 | return; |
| 90 | } |
| 91 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 92 | // recognize CachePolicy |
| 93 | shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>(); |
| 94 | if (tag != nullptr) { |
| 95 | lp::CachePolicyType policy = tag->get().getPolicy(); |
| 96 | if (policy == lp::CachePolicyType::NO_CACHE) { |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 97 | return; |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 101 | bool isNewEntry = false; |
| 102 | iterator it; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 103 | // use .insert because gcc46 does not support .emplace |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 104 | std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited)); |
| 105 | EntryImpl& entry = const_cast<EntryImpl&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 106 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 107 | entry.updateStaleTime(); |
| 108 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 109 | if (!isNewEntry) { // existing entry |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 110 | // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry. |
| 111 | if (entry.isUnsolicited() && !isUnsolicited) { |
| 112 | entry.unsetUnsolicited(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 113 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 114 | |
| 115 | m_policy->afterRefresh(it); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 116 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 117 | else { |
| 118 | m_policy->afterInsert(it); |
| 119 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 120 | } |
| 121 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 122 | void |
| 123 | Cs::find(const Interest& interest, |
| 124 | const HitCallback& hitCallback, |
| 125 | const MissCallback& missCallback) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 126 | { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 127 | BOOST_ASSERT(static_cast<bool>(hitCallback)); |
| 128 | BOOST_ASSERT(static_cast<bool>(missCallback)); |
| 129 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 130 | const Name& prefix = interest.getName(); |
| 131 | bool isRightmost = interest.getChildSelector() == 1; |
| 132 | NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L")); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 133 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 134 | iterator first = m_table.lower_bound(prefix); |
| 135 | iterator last = m_table.end(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 136 | if (prefix.size() > 0) { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 137 | last = m_table.lower_bound(prefix.getSuccessor()); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 138 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 139 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 140 | iterator match = last; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 141 | if (isRightmost) { |
| 142 | match = this->findRightmost(interest, first, last); |
| 143 | } |
| 144 | else { |
| 145 | match = this->findLeftmost(interest, first, last); |
| 146 | } |
| 147 | |
| 148 | if (match == last) { |
| 149 | NFD_LOG_DEBUG(" no-match"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 150 | missCallback(interest); |
| 151 | return; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 152 | } |
| 153 | NFD_LOG_DEBUG(" matching " << match->getName()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 154 | m_policy->beforeUse(match); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 155 | hitCallback(interest, match->getData()); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 158 | iterator |
| 159 | Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 160 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 161 | return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest)); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 164 | iterator |
| 165 | Cs::findRightmost(const Interest& interest, iterator first, iterator last) const |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 166 | { |
| 167 | // Each loop visits a sub-namespace under a prefix one component longer than Interest Name. |
| 168 | // If there is a match in that sub-namespace, the leftmost match is returned; |
| 169 | // otherwise, loop continues. |
| 170 | |
| 171 | size_t interestNameLength = interest.getName().size(); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 172 | for (iterator right = last; right != first;) { |
| 173 | iterator prev = std::prev(right); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 174 | |
| 175 | // special case: [first,prev] have exact Names |
| 176 | if (prev->getName().size() == interestNameLength) { |
| 177 | NFD_LOG_TRACE(" find-among-exact " << prev->getName()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 178 | iterator matchExact = this->findRightmostAmongExact(interest, first, right); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 179 | return matchExact == right ? last : matchExact; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 182 | Name prefix = prev->getName().getPrefix(interestNameLength + 1); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 183 | iterator left = m_table.lower_bound(prefix); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 184 | |
| 185 | // normal case: [left,right) are under one-component-longer prefix |
| 186 | NFD_LOG_TRACE(" find-under-prefix " << prefix); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 187 | iterator match = this->findLeftmost(interest, left, right); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 188 | if (match != right) { |
| 189 | return match; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 190 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 191 | right = left; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 192 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 193 | return last; |
| 194 | } |
| 195 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 196 | iterator |
| 197 | Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 198 | { |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 199 | return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 203 | Cs::setPolicyImpl(unique_ptr<Policy> policy) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 204 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 205 | NFD_LOG_DEBUG("set-policy " << policy->getName()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 206 | m_policy = std::move(policy); |
| 207 | m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) { |
| 208 | m_table.erase(it); |
| 209 | }); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 210 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 211 | m_policy->setCs(this); |
| 212 | BOOST_ASSERT(m_policy->getCs() == this); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void |
| 216 | Cs::dump() |
| 217 | { |
| 218 | NFD_LOG_DEBUG("dump table"); |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 219 | for (const EntryImpl& entry : m_table) { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 220 | NFD_LOG_TRACE(entry.getFullName()); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | } // namespace cs |
| 225 | } // namespace nfd |