blob: 3bc816ab31460be6893721b1b532b21a50c55b07 [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"
Junxiao Shia9388182014-12-13 23:16:09 -070027#include "cs-entry.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 Shia9388182014-12-13 23:16:09 -070030#include <numeric>
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
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060037Cs::Cs(size_t nMaxPackets)
Junxiao Shia9388182014-12-13 23:16:09 -070038 : m_limit(nMaxPackets)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070039{
Junxiao Shia9388182014-12-13 23:16:09 -070040 BOOST_ASSERT(nMaxPackets > 0);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070041}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080042
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070043Cs::~Cs()
44{
Junxiao Shia9388182014-12-13 23:16:09 -070045 // It's necessary to put this empty destructor in cs.cpp,
46 // because cs::Entry has incomplete type in cs.hpp.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047}
48
49void
50Cs::setLimit(size_t nMaxPackets)
51{
Junxiao Shia9388182014-12-13 23:16:09 -070052 BOOST_ASSERT(nMaxPackets > 0);
53 m_limit = nMaxPackets;
54 this->evict();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080055}
56
57size_t
Junxiao Shia9388182014-12-13 23:16:09 -070058Cs::size() const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080059{
Junxiao Shia9388182014-12-13 23:16:09 -070060 return m_table.size();
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070061}
62
63bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080064Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070065{
Junxiao Shi5640ec82015-01-07 21:51:19 -070066 NFD_LOG_DEBUG("insert " << data.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080067
Junxiao Shia9388182014-12-13 23:16:09 -070068 bool isNewEntry = false; TableIt it;
69 // use .insert because gcc46 does not support .emplace
70 std::tie(it, isNewEntry) = m_table.insert(Entry(data.shared_from_this(), isUnsolicited));
71 Entry& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072
Junxiao Shia9388182014-12-13 23:16:09 -070073 if (!isNewEntry) { // existing entry
74 this->detachQueue(it);
75 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
76 if (entry.isUnsolicited() && !isUnsolicited) {
77 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080078 }
Junxiao Shiaf6569a2014-06-14 00:01:34 -070079 }
Junxiao Shia9388182014-12-13 23:16:09 -070080 entry.refresh();
81 this->attachQueue(it);
82
83 // check there are same amount of entries in the table and in queues
84 BOOST_ASSERT(m_table.size() == std::accumulate(m_queues, m_queues + QUEUE_UBOUND, 0U,
85 [] (size_t sum, const Queue queue) { return sum + queue.size(); }));
86
87 this->evict(); // XXX The new entry could be evicted, but it shouldn't matter.
88
89 return true;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080090}
91
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080092const Data*
93Cs::find(const Interest& interest) const
94{
Junxiao Shia9388182014-12-13 23:16:09 -070095 const Name& prefix = interest.getName();
96 bool isRightmost = interest.getChildSelector() == 1;
97 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080098
Junxiao Shi5640ec82015-01-07 21:51:19 -070099 TableIt first = m_table.lower_bound(prefix);
100 TableIt last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700101 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700102 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700103 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700104
105 TableIt match = last;
106 if (isRightmost) {
107 match = this->findRightmost(interest, first, last);
108 }
109 else {
110 match = this->findLeftmost(interest, first, last);
111 }
112
113 if (match == last) {
114 NFD_LOG_DEBUG(" no-match");
115 return nullptr;
116 }
117 NFD_LOG_DEBUG(" matching " << match->getName());
118 return match->getData().get();
119}
120
121Cs::TableIt
122Cs::findLeftmost(const Interest& interest, TableIt first, TableIt last) const
123{
124 return std::find_if(first, last, bind(&cs::Entry::canSatisfy, _1, interest));
125}
126
127Cs::TableIt
128Cs::findRightmost(const Interest& interest, TableIt first, TableIt last) const
129{
130 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
131 // If there is a match in that sub-namespace, the leftmost match is returned;
132 // otherwise, loop continues.
133
134 size_t interestNameLength = interest.getName().size();
135 for (TableIt right = last; right != first;) {
136 TableIt prev = std::prev(right);
137
138 // special case: [first,prev] have exact Names
139 if (prev->getName().size() == interestNameLength) {
140 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
141 TableIt matchExact = this->findRightmostAmongExact(interest, first, right);
142 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800143 }
144
Junxiao Shi5640ec82015-01-07 21:51:19 -0700145 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
146 TableIt left = m_table.lower_bound(prefix);
147
148 // normal case: [left,right) are under one-component-longer prefix
149 NFD_LOG_TRACE(" find-under-prefix " << prefix);
150 TableIt match = this->findLeftmost(interest, left, right);
151 if (match != right) {
152 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800153 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700154 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700155 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700156 return last;
157}
158
159Cs::TableIt
160Cs::findRightmostAmongExact(const Interest& interest, TableIt first, TableIt last) const
161{
162 return find_last_if(first, last, bind(&Entry::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800163}
164
165void
Junxiao Shia9388182014-12-13 23:16:09 -0700166Cs::attachQueue(TableIt it)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800167{
Junxiao Shia9388182014-12-13 23:16:09 -0700168 Entry& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800169
Junxiao Shia9388182014-12-13 23:16:09 -0700170 if (entry.queueType != QUEUE_NONE) {
171 this->detachQueue(it);
172 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800173
Junxiao Shia9388182014-12-13 23:16:09 -0700174 if (entry.isUnsolicited()) {
175 entry.queueType = QUEUE_UNSOLICITED;
176 }
177 else if (entry.isStale()) {
178 entry.queueType = QUEUE_STALE;
179 }
180 else {
181 entry.queueType = QUEUE_FIFO;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800182
Junxiao Shia9388182014-12-13 23:16:09 -0700183 if (entry.canStale()) {
184 entry.moveStaleEvent = scheduler::schedule(entry.getData()->getFreshnessPeriod(),
185 bind(&Cs::moveToStaleQueue, this, it));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800186 }
Junxiao Shia9388182014-12-13 23:16:09 -0700187 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800188
Junxiao Shia9388182014-12-13 23:16:09 -0700189 Queue& queue = m_queues[entry.queueType];
190 entry.queueIt = queue.insert(queue.end(), it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800191}
192
193void
Junxiao Shia9388182014-12-13 23:16:09 -0700194Cs::detachQueue(TableIt it)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800195{
Junxiao Shia9388182014-12-13 23:16:09 -0700196 Entry& entry = const_cast<Entry&>(*it);
197
198 BOOST_ASSERT(entry.queueType != QUEUE_NONE);
199
200 if (entry.queueType == QUEUE_FIFO) {
201 scheduler::cancel(entry.moveStaleEvent);
202 }
203
204 m_queues[entry.queueType].erase(entry.queueIt);
205 entry.queueType = QUEUE_NONE;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800206}
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700207
Junxiao Shia9388182014-12-13 23:16:09 -0700208void
209Cs::moveToStaleQueue(TableIt it)
210{
211 Entry& entry = const_cast<Entry&>(*it);
212
213 BOOST_ASSERT(entry.queueType == QUEUE_FIFO);
214 m_queues[QUEUE_FIFO].erase(entry.queueIt);
215
216 entry.queueType = QUEUE_STALE;
217 Queue& queue = m_queues[QUEUE_STALE];
218 entry.queueIt = queue.insert(queue.end(), it);
219}
220
221std::tuple<Cs::TableIt, std::string>
222Cs::evictPick()
223{
224 if (!m_queues[QUEUE_UNSOLICITED].empty()) {
225 return std::make_tuple(m_queues[QUEUE_UNSOLICITED].front(), "unsolicited");
226 }
227 if (!m_queues[QUEUE_STALE].empty()) {
228 return std::make_tuple(m_queues[QUEUE_STALE].front(), "stale");
229 }
230 if (!m_queues[QUEUE_FIFO].empty()) {
231 return std::make_tuple(m_queues[QUEUE_FIFO].front(), "fifo");
232 }
233
234 BOOST_ASSERT(false);
235 return std::make_tuple(m_table.end(), "error");
236}
237
238
239void
240Cs::evict()
241{
242 while (this->size() > m_limit) {
243 TableIt it; std::string reason;
244 std::tie(it, reason) = this->evictPick();
245
Junxiao Shi5640ec82015-01-07 21:51:19 -0700246 NFD_LOG_DEBUG("evict " << it->getName() << " " << reason);
Junxiao Shia9388182014-12-13 23:16:09 -0700247 this->detachQueue(it);
248 m_table.erase(it);
249 }
250}
251
252void
253Cs::dump()
254{
255 NFD_LOG_DEBUG("dump table");
256 for (const Entry& entry : m_table) {
257 NFD_LOG_TRACE(entry.getFullName());
258 }
259}
260
261} // namespace cs
262} // namespace nfd