blob: 9ea3d8b2858a1aa2471c117c0ecd15ae2d99319a [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5640ec82015-01-07 21:51:19 -07003 * 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 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{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050050 return unique_ptr<Policy>(new PriorityFifoPolicy());
51}
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
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070081bool
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
Junxiao Shi35b16b12015-02-16 22:14:57 -070086 // recognize CachingPolicy
87 using ndn::nfd::LocalControlHeader;
88 const LocalControlHeader& lch = data.getLocalControlHeader();
89 if (lch.hasCachingPolicy()) {
90 LocalControlHeader::CachingPolicy policy = lch.getCachingPolicy();
91 if (policy == LocalControlHeader::CachingPolicy::NO_CACHE) {
92 return false;
93 }
94 }
95
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050096 bool isNewEntry = false;
97 iterator it;
Junxiao Shia9388182014-12-13 23:16:09 -070098 // use .insert because gcc46 does not support .emplace
Junxiao Shifc206962015-01-16 11:12:22 -070099 std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited));
100 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800101
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500102 entry.updateStaleTime();
103
Junxiao Shia9388182014-12-13 23:16:09 -0700104 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -0700105 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
106 if (entry.isUnsolicited() && !isUnsolicited) {
107 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800108 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500109
110 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700111 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500112 else {
113 m_policy->afterInsert(it);
114 }
Junxiao Shia9388182014-12-13 23:16:09 -0700115
116 return true;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800117}
118
mzhang4eab72492015-02-25 11:16:09 -0600119void
120Cs::find(const Interest& interest,
121 const HitCallback& hitCallback,
122 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800123{
mzhang4eab72492015-02-25 11:16:09 -0600124 BOOST_ASSERT(static_cast<bool>(hitCallback));
125 BOOST_ASSERT(static_cast<bool>(missCallback));
126
Junxiao Shia9388182014-12-13 23:16:09 -0700127 const Name& prefix = interest.getName();
128 bool isRightmost = interest.getChildSelector() == 1;
129 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800130
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500131 iterator first = m_table.lower_bound(prefix);
132 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700133 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700134 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700135 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700136
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500137 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700138 if (isRightmost) {
139 match = this->findRightmost(interest, first, last);
140 }
141 else {
142 match = this->findLeftmost(interest, first, last);
143 }
144
145 if (match == last) {
146 NFD_LOG_DEBUG(" no-match");
mzhang4eab72492015-02-25 11:16:09 -0600147 missCallback(interest);
148 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700149 }
150 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500151 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600152 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700153}
154
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500155iterator
156Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700157{
Junxiao Shifc206962015-01-16 11:12:22 -0700158 return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
Junxiao Shi5640ec82015-01-07 21:51:19 -0700159}
160
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500161iterator
162Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700163{
164 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
165 // If there is a match in that sub-namespace, the leftmost match is returned;
166 // otherwise, loop continues.
167
168 size_t interestNameLength = interest.getName().size();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500169 for (iterator right = last; right != first;) {
170 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700171
172 // special case: [first,prev] have exact Names
173 if (prev->getName().size() == interestNameLength) {
174 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500175 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700176 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800177 }
178
Junxiao Shi5640ec82015-01-07 21:51:19 -0700179 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500180 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700181
182 // normal case: [left,right) are under one-component-longer prefix
183 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500184 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700185 if (match != right) {
186 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800187 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700188 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700189 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700190 return last;
191}
192
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500193iterator
194Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700195{
Junxiao Shifc206962015-01-16 11:12:22 -0700196 return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800197}
198
199void
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500200Cs::setPolicyImpl(unique_ptr<Policy>& policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800201{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500202 m_policy = std::move(policy);
203 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
204 m_table.erase(it);
205 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800206
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500207 m_policy->setCs(this);
208 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700209}
210
211void
212Cs::dump()
213{
214 NFD_LOG_DEBUG("dump table");
Junxiao Shifc206962015-01-16 11:12:22 -0700215 for (const EntryImpl& entry : m_table) {
Junxiao Shia9388182014-12-13 23:16:09 -0700216 NFD_LOG_TRACE(entry.getFullName());
217 }
218}
219
220} // namespace cs
221} // namespace nfd