blob: 8578a0dbfa20286e8933b0ab1ec951c515b2b8f6 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi5640ec82015-01-07 21:51:19 -07004 * 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 Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * 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 Shi0fcb41e2014-01-24 10:29:43 -070024 */
25
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070026#include "cs.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050027#include "cs-policy-priority-fifo.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shi5640ec82015-01-07 21:51:19 -070029#include "core/algorithm.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000030#include <ndn-cxx/lp/tags.hpp>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080031
32NFD_LOG_INIT("ContentStore");
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080033
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070035namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070036
Junxiao Shifc206962015-01-16 11:12:22 -070037// http://en.cppreference.com/w/cpp/concept/ForwardIterator
38BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Cs::const_iterator>));
39// boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
40// which doesn't require DefaultConstructible
41#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
42static_assert(std::is_default_constructible<Cs::const_iterator>::value,
43 "Cs::const_iterator must be default-constructible");
44#else
45BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Cs::const_iterator>));
46#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
47
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050048unique_ptr<Policy>
49makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070050{
Davide Pesavento01f8dba2015-09-19 21:11:45 +020051 return make_unique<PriorityFifoPolicy>();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050052}
53
54Cs::Cs(size_t nMaxPackets, unique_ptr<Policy> policy)
55{
56 this->setPolicyImpl(policy);
57 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070058}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080059
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080060void
61Cs::setLimit(size_t nMaxPackets)
62{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050063 m_policy->setLimit(nMaxPackets);
64}
65
66size_t
67Cs::getLimit() const
68{
69 return m_policy->getLimit();
70}
71
72void
73Cs::setPolicy(unique_ptr<Policy> policy)
74{
75 BOOST_ASSERT(policy != nullptr);
76 BOOST_ASSERT(m_policy != nullptr);
77 size_t limit = m_policy->getLimit();
78 this->setPolicyImpl(policy);
79 m_policy->setLimit(limit);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080080}
81
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070082void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080083Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070084{
Junxiao Shi5640ec82015-01-07 21:51:19 -070085 NFD_LOG_DEBUG("insert " << data.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080086
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070087 if (m_policy->getLimit() == 0) {
88 // shortcut for disabled CS
89 return;
90 }
91
Junxiao Shi0de23a22015-12-03 20:07:02 +000092 // 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 Zhangffe8bbb2016-03-10 13:40:37 -070097 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070098 }
99 }
100
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500101 bool isNewEntry = false;
102 iterator it;
Junxiao Shia9388182014-12-13 23:16:09 -0700103 // use .insert because gcc46 does not support .emplace
Junxiao Shifc206962015-01-16 11:12:22 -0700104 std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited));
105 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800106
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500107 entry.updateStaleTime();
108
Junxiao Shia9388182014-12-13 23:16:09 -0700109 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -0700110 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
111 if (entry.isUnsolicited() && !isUnsolicited) {
112 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800113 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500114
115 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700116 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500117 else {
118 m_policy->afterInsert(it);
119 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800120}
121
mzhang4eab72492015-02-25 11:16:09 -0600122void
123Cs::find(const Interest& interest,
124 const HitCallback& hitCallback,
125 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800126{
mzhang4eab72492015-02-25 11:16:09 -0600127 BOOST_ASSERT(static_cast<bool>(hitCallback));
128 BOOST_ASSERT(static_cast<bool>(missCallback));
129
Junxiao Shia9388182014-12-13 23:16:09 -0700130 const Name& prefix = interest.getName();
131 bool isRightmost = interest.getChildSelector() == 1;
132 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800133
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500134 iterator first = m_table.lower_bound(prefix);
135 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700136 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700137 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700138 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700139
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500140 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700141 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");
mzhang4eab72492015-02-25 11:16:09 -0600150 missCallback(interest);
151 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700152 }
153 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500154 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600155 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700156}
157
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500158iterator
159Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700160{
Junxiao Shifc206962015-01-16 11:12:22 -0700161 return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
Junxiao Shi5640ec82015-01-07 21:51:19 -0700162}
163
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500164iterator
165Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700166{
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 Zhangcb6e05f2015-04-20 15:51:47 -0500172 for (iterator right = last; right != first;) {
173 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700174
175 // special case: [first,prev] have exact Names
176 if (prev->getName().size() == interestNameLength) {
177 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500178 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700179 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800180 }
181
Junxiao Shi5640ec82015-01-07 21:51:19 -0700182 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500183 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700184
185 // normal case: [left,right) are under one-component-longer prefix
186 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500187 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700188 if (match != right) {
189 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800190 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700191 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700192 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700193 return last;
194}
195
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500196iterator
197Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700198{
Junxiao Shifc206962015-01-16 11:12:22 -0700199 return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800200}
201
202void
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500203Cs::setPolicyImpl(unique_ptr<Policy>& policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800204{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500205 m_policy = std::move(policy);
206 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
207 m_table.erase(it);
208 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800209
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500210 m_policy->setCs(this);
211 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700212}
213
214void
215Cs::dump()
216{
217 NFD_LOG_DEBUG("dump table");
Junxiao Shifc206962015-01-16 11:12:22 -0700218 for (const EntryImpl& entry : m_table) {
Junxiao Shia9388182014-12-13 23:16:09 -0700219 NFD_LOG_TRACE(entry.getFullName());
220 }
221}
222
223} // namespace cs
224} // namespace nfd