blob: 19df21a82f58e3bc31a113628e2abe74bf77abe1 [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"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080030
31NFD_LOG_INIT("ContentStore");
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070034namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070035
Junxiao Shifc206962015-01-16 11:12:22 -070036// http://en.cppreference.com/w/cpp/concept/ForwardIterator
37BOOST_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
41static_assert(std::is_default_constructible<Cs::const_iterator>::value,
42 "Cs::const_iterator must be default-constructible");
43#else
44BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Cs::const_iterator>));
45#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
46
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050047unique_ptr<Policy>
48makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070049{
Davide Pesavento01f8dba2015-09-19 21:11:45 +020050 return make_unique<PriorityFifoPolicy>();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050051}
52
53Cs::Cs(size_t nMaxPackets, unique_ptr<Policy> policy)
54{
55 this->setPolicyImpl(policy);
56 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070057}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080058
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080059void
60Cs::setLimit(size_t nMaxPackets)
61{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050062 m_policy->setLimit(nMaxPackets);
63}
64
65size_t
66Cs::getLimit() const
67{
68 return m_policy->getLimit();
69}
70
71void
72Cs::setPolicy(unique_ptr<Policy> policy)
73{
74 BOOST_ASSERT(policy != nullptr);
75 BOOST_ASSERT(m_policy != nullptr);
76 size_t limit = m_policy->getLimit();
77 this->setPolicyImpl(policy);
78 m_policy->setLimit(limit);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080079}
80
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070081void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080082Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070083{
Junxiao Shi5640ec82015-01-07 21:51:19 -070084 NFD_LOG_DEBUG("insert " << data.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080085
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070086 if (m_policy->getLimit() == 0) {
87 // shortcut for disabled CS
88 return;
89 }
90
Junxiao Shi0de23a22015-12-03 20:07:02 +000091 // recognize CachePolicy
92 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
93 if (tag != nullptr) {
94 lp::CachePolicyType policy = tag->get().getPolicy();
95 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070096 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070097 }
98 }
99
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500100 bool isNewEntry = false;
101 iterator it;
Junxiao Shia9388182014-12-13 23:16:09 -0700102 // use .insert because gcc46 does not support .emplace
Junxiao Shifc206962015-01-16 11:12:22 -0700103 std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited));
104 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500106 entry.updateStaleTime();
107
Junxiao Shia9388182014-12-13 23:16:09 -0700108 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -0700109 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
110 if (entry.isUnsolicited() && !isUnsolicited) {
111 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800112 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500113
114 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700115 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500116 else {
117 m_policy->afterInsert(it);
118 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119}
120
mzhang4eab72492015-02-25 11:16:09 -0600121void
122Cs::find(const Interest& interest,
123 const HitCallback& hitCallback,
124 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800125{
mzhang4eab72492015-02-25 11:16:09 -0600126 BOOST_ASSERT(static_cast<bool>(hitCallback));
127 BOOST_ASSERT(static_cast<bool>(missCallback));
128
Junxiao Shia9388182014-12-13 23:16:09 -0700129 const Name& prefix = interest.getName();
130 bool isRightmost = interest.getChildSelector() == 1;
131 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800132
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500133 iterator first = m_table.lower_bound(prefix);
134 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700135 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700136 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700137 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700138
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500139 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700140 if (isRightmost) {
141 match = this->findRightmost(interest, first, last);
142 }
143 else {
144 match = this->findLeftmost(interest, first, last);
145 }
146
147 if (match == last) {
148 NFD_LOG_DEBUG(" no-match");
mzhang4eab72492015-02-25 11:16:09 -0600149 missCallback(interest);
150 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700151 }
152 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500153 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600154 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700155}
156
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500157iterator
158Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700159{
Junxiao Shifc206962015-01-16 11:12:22 -0700160 return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
Junxiao Shi5640ec82015-01-07 21:51:19 -0700161}
162
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500163iterator
164Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700165{
166 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
167 // If there is a match in that sub-namespace, the leftmost match is returned;
168 // otherwise, loop continues.
169
170 size_t interestNameLength = interest.getName().size();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500171 for (iterator right = last; right != first;) {
172 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700173
174 // special case: [first,prev] have exact Names
175 if (prev->getName().size() == interestNameLength) {
176 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500177 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700178 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800179 }
180
Junxiao Shi5640ec82015-01-07 21:51:19 -0700181 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500182 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700183
184 // normal case: [left,right) are under one-component-longer prefix
185 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500186 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700187 if (match != right) {
188 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800189 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700190 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700191 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700192 return last;
193}
194
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500195iterator
196Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700197{
Junxiao Shifc206962015-01-16 11:12:22 -0700198 return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800199}
200
201void
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500202Cs::setPolicyImpl(unique_ptr<Policy>& policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800203{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500204 m_policy = std::move(policy);
205 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
206 m_table.erase(it);
207 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500209 m_policy->setCs(this);
210 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700211}
212
213void
214Cs::dump()
215{
216 NFD_LOG_DEBUG("dump table");
Junxiao Shifc206962015-01-16 11:12:22 -0700217 for (const EntryImpl& entry : m_table) {
Junxiao Shia9388182014-12-13 23:16:09 -0700218 NFD_LOG_TRACE(entry.getFullName());
219 }
220}
221
222} // namespace cs
223} // namespace nfd