blob: 2226335d08a3ebf3f679a405ebb4c496e159544d [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shi5640ec82015-01-07 21:51:19 -070028#include "core/algorithm.hpp"
Junxiao Shia9388182014-12-13 23:16:09 -070029#include <numeric>
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
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060047Cs::Cs(size_t nMaxPackets)
Junxiao Shia9388182014-12-13 23:16:09 -070048 : m_limit(nMaxPackets)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070049{
Junxiao Shia9388182014-12-13 23:16:09 -070050 BOOST_ASSERT(nMaxPackets > 0);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070051}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053void
54Cs::setLimit(size_t nMaxPackets)
55{
Junxiao Shia9388182014-12-13 23:16:09 -070056 BOOST_ASSERT(nMaxPackets > 0);
57 m_limit = nMaxPackets;
58 this->evict();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080059}
60
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070061bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080062Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070063{
Junxiao Shi5640ec82015-01-07 21:51:19 -070064 NFD_LOG_DEBUG("insert " << data.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080065
Junxiao Shi35b16b12015-02-16 22:14:57 -070066 // recognize CachingPolicy
67 using ndn::nfd::LocalControlHeader;
68 const LocalControlHeader& lch = data.getLocalControlHeader();
69 if (lch.hasCachingPolicy()) {
70 LocalControlHeader::CachingPolicy policy = lch.getCachingPolicy();
71 if (policy == LocalControlHeader::CachingPolicy::NO_CACHE) {
72 return false;
73 }
74 }
75
Junxiao Shia9388182014-12-13 23:16:09 -070076 bool isNewEntry = false; TableIt it;
77 // use .insert because gcc46 does not support .emplace
Junxiao Shifc206962015-01-16 11:12:22 -070078 std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited));
79 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080080
Junxiao Shia9388182014-12-13 23:16:09 -070081 if (!isNewEntry) { // existing entry
82 this->detachQueue(it);
83 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
84 if (entry.isUnsolicited() && !isUnsolicited) {
85 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080086 }
Junxiao Shiaf6569a2014-06-14 00:01:34 -070087 }
Junxiao Shifc206962015-01-16 11:12:22 -070088 entry.updateStaleTime();
Junxiao Shia9388182014-12-13 23:16:09 -070089 this->attachQueue(it);
90
91 // check there are same amount of entries in the table and in queues
Junxiao Shifc206962015-01-16 11:12:22 -070092 BOOST_ASSERT(m_table.size() == std::accumulate(m_queues, m_queues + QUEUE_MAX, 0U,
Junxiao Shia9388182014-12-13 23:16:09 -070093 [] (size_t sum, const Queue queue) { return sum + queue.size(); }));
94
95 this->evict(); // XXX The new entry could be evicted, but it shouldn't matter.
96
97 return true;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080098}
99
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800100const Data*
101Cs::find(const Interest& interest) const
102{
Junxiao Shia9388182014-12-13 23:16:09 -0700103 const Name& prefix = interest.getName();
104 bool isRightmost = interest.getChildSelector() == 1;
105 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800106
Junxiao Shi5640ec82015-01-07 21:51:19 -0700107 TableIt first = m_table.lower_bound(prefix);
108 TableIt last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700109 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700110 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700111 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700112
113 TableIt match = last;
114 if (isRightmost) {
115 match = this->findRightmost(interest, first, last);
116 }
117 else {
118 match = this->findLeftmost(interest, first, last);
119 }
120
121 if (match == last) {
122 NFD_LOG_DEBUG(" no-match");
123 return nullptr;
124 }
125 NFD_LOG_DEBUG(" matching " << match->getName());
Junxiao Shifc206962015-01-16 11:12:22 -0700126 return &match->getData();
Junxiao Shi5640ec82015-01-07 21:51:19 -0700127}
128
Junxiao Shifc206962015-01-16 11:12:22 -0700129TableIt
Junxiao Shi5640ec82015-01-07 21:51:19 -0700130Cs::findLeftmost(const Interest& interest, TableIt first, TableIt last) const
131{
Junxiao Shifc206962015-01-16 11:12:22 -0700132 return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
Junxiao Shi5640ec82015-01-07 21:51:19 -0700133}
134
Junxiao Shifc206962015-01-16 11:12:22 -0700135TableIt
Junxiao Shi5640ec82015-01-07 21:51:19 -0700136Cs::findRightmost(const Interest& interest, TableIt first, TableIt last) const
137{
138 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
139 // If there is a match in that sub-namespace, the leftmost match is returned;
140 // otherwise, loop continues.
141
142 size_t interestNameLength = interest.getName().size();
143 for (TableIt right = last; right != first;) {
144 TableIt prev = std::prev(right);
145
146 // special case: [first,prev] have exact Names
147 if (prev->getName().size() == interestNameLength) {
148 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
149 TableIt matchExact = this->findRightmostAmongExact(interest, first, right);
150 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800151 }
152
Junxiao Shi5640ec82015-01-07 21:51:19 -0700153 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
154 TableIt left = m_table.lower_bound(prefix);
155
156 // normal case: [left,right) are under one-component-longer prefix
157 NFD_LOG_TRACE(" find-under-prefix " << prefix);
158 TableIt match = this->findLeftmost(interest, left, right);
159 if (match != right) {
160 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800161 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700162 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700163 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700164 return last;
165}
166
Junxiao Shifc206962015-01-16 11:12:22 -0700167TableIt
Junxiao Shi5640ec82015-01-07 21:51:19 -0700168Cs::findRightmostAmongExact(const Interest& interest, TableIt first, TableIt last) const
169{
Junxiao Shifc206962015-01-16 11:12:22 -0700170 return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800171}
172
173void
Junxiao Shia9388182014-12-13 23:16:09 -0700174Cs::attachQueue(TableIt it)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800175{
Junxiao Shifc206962015-01-16 11:12:22 -0700176 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800177
Junxiao Shia9388182014-12-13 23:16:09 -0700178 if (entry.queueType != QUEUE_NONE) {
179 this->detachQueue(it);
180 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800181
Junxiao Shia9388182014-12-13 23:16:09 -0700182 if (entry.isUnsolicited()) {
183 entry.queueType = QUEUE_UNSOLICITED;
184 }
185 else if (entry.isStale()) {
186 entry.queueType = QUEUE_STALE;
187 }
188 else {
189 entry.queueType = QUEUE_FIFO;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800190
Junxiao Shia9388182014-12-13 23:16:09 -0700191 if (entry.canStale()) {
Junxiao Shifc206962015-01-16 11:12:22 -0700192 entry.moveStaleEvent = scheduler::schedule(entry.getData().getFreshnessPeriod(),
Junxiao Shia9388182014-12-13 23:16:09 -0700193 bind(&Cs::moveToStaleQueue, this, it));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800194 }
Junxiao Shia9388182014-12-13 23:16:09 -0700195 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800196
Junxiao Shia9388182014-12-13 23:16:09 -0700197 Queue& queue = m_queues[entry.queueType];
198 entry.queueIt = queue.insert(queue.end(), it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800199}
200
201void
Junxiao Shia9388182014-12-13 23:16:09 -0700202Cs::detachQueue(TableIt it)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800203{
Junxiao Shifc206962015-01-16 11:12:22 -0700204 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Junxiao Shia9388182014-12-13 23:16:09 -0700205
206 BOOST_ASSERT(entry.queueType != QUEUE_NONE);
207
208 if (entry.queueType == QUEUE_FIFO) {
209 scheduler::cancel(entry.moveStaleEvent);
210 }
211
212 m_queues[entry.queueType].erase(entry.queueIt);
213 entry.queueType = QUEUE_NONE;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800214}
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700215
Junxiao Shia9388182014-12-13 23:16:09 -0700216void
217Cs::moveToStaleQueue(TableIt it)
218{
Junxiao Shifc206962015-01-16 11:12:22 -0700219 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Junxiao Shia9388182014-12-13 23:16:09 -0700220
221 BOOST_ASSERT(entry.queueType == QUEUE_FIFO);
222 m_queues[QUEUE_FIFO].erase(entry.queueIt);
223
224 entry.queueType = QUEUE_STALE;
225 Queue& queue = m_queues[QUEUE_STALE];
226 entry.queueIt = queue.insert(queue.end(), it);
227}
228
Junxiao Shifc206962015-01-16 11:12:22 -0700229std::tuple<TableIt, std::string>
Junxiao Shia9388182014-12-13 23:16:09 -0700230Cs::evictPick()
231{
232 if (!m_queues[QUEUE_UNSOLICITED].empty()) {
233 return std::make_tuple(m_queues[QUEUE_UNSOLICITED].front(), "unsolicited");
234 }
235 if (!m_queues[QUEUE_STALE].empty()) {
236 return std::make_tuple(m_queues[QUEUE_STALE].front(), "stale");
237 }
238 if (!m_queues[QUEUE_FIFO].empty()) {
239 return std::make_tuple(m_queues[QUEUE_FIFO].front(), "fifo");
240 }
241
242 BOOST_ASSERT(false);
243 return std::make_tuple(m_table.end(), "error");
244}
245
246
247void
248Cs::evict()
249{
250 while (this->size() > m_limit) {
251 TableIt it; std::string reason;
252 std::tie(it, reason) = this->evictPick();
253
Junxiao Shi5640ec82015-01-07 21:51:19 -0700254 NFD_LOG_DEBUG("evict " << it->getName() << " " << reason);
Junxiao Shia9388182014-12-13 23:16:09 -0700255 this->detachQueue(it);
256 m_table.erase(it);
257 }
258}
259
260void
261Cs::dump()
262{
263 NFD_LOG_DEBUG("dump table");
Junxiao Shifc206962015-01-16 11:12:22 -0700264 for (const EntryImpl& entry : m_table) {
Junxiao Shia9388182014-12-13 23:16:09 -0700265 NFD_LOG_TRACE(entry.getFullName());
266 }
267}
268
269} // namespace cs
270} // namespace nfd