blob: 822cb84709bc92fc93968c6ea20dc436e59e1deb [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06003 * Copyright (c) 2014, 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 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
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060036Cs::Cs(size_t nMaxPackets)
Junxiao Shia9388182014-12-13 23:16:09 -070037 : m_limit(nMaxPackets)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070038{
Junxiao Shia9388182014-12-13 23:16:09 -070039 BOOST_ASSERT(nMaxPackets > 0);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070040}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080041
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070042Cs::~Cs()
43{
Junxiao Shia9388182014-12-13 23:16:09 -070044 // It's necessary to put this empty destructor in cs.cpp,
45 // because cs::Entry has incomplete type in cs.hpp.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080046}
47
48void
49Cs::setLimit(size_t nMaxPackets)
50{
Junxiao Shia9388182014-12-13 23:16:09 -070051 BOOST_ASSERT(nMaxPackets > 0);
52 m_limit = nMaxPackets;
53 this->evict();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054}
55
56size_t
Junxiao Shia9388182014-12-13 23:16:09 -070057Cs::size() const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080058{
Junxiao Shia9388182014-12-13 23:16:09 -070059 return m_table.size();
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070060}
61
62bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080063Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070064{
Junxiao Shia9388182014-12-13 23:16:09 -070065 NFD_LOG_DEBUG("insert " << data.getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080066
Junxiao Shia9388182014-12-13 23:16:09 -070067 bool isNewEntry = false; TableIt it;
68 // use .insert because gcc46 does not support .emplace
69 std::tie(it, isNewEntry) = m_table.insert(Entry(data.shared_from_this(), isUnsolicited));
70 Entry& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080071
Junxiao Shia9388182014-12-13 23:16:09 -070072 if (!isNewEntry) { // existing entry
73 this->detachQueue(it);
74 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
75 if (entry.isUnsolicited() && !isUnsolicited) {
76 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080077 }
Junxiao Shiaf6569a2014-06-14 00:01:34 -070078 }
Junxiao Shia9388182014-12-13 23:16:09 -070079 entry.refresh();
80 this->attachQueue(it);
81
82 // check there are same amount of entries in the table and in queues
83 BOOST_ASSERT(m_table.size() == std::accumulate(m_queues, m_queues + QUEUE_UBOUND, 0U,
84 [] (size_t sum, const Queue queue) { return sum + queue.size(); }));
85
86 this->evict(); // XXX The new entry could be evicted, but it shouldn't matter.
87
88 return true;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089}
90
91bool
Junxiao Shia9388182014-12-13 23:16:09 -070092Cs::isNewRightmostCandidate(const Name& prefix, TableIt a, TableIt b)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080093{
Junxiao Shia9388182014-12-13 23:16:09 -070094 if (a->getFullName().size() == prefix.size()) {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040095 return true;
96 }
Junxiao Shia9388182014-12-13 23:16:09 -070097 return b->getFullName().at(prefix.size()) > a->getFullName().at(prefix.size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080098}
99
100const 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"));
106 TableIt rightmostCandidate = m_table.end();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800107
Junxiao Shia9388182014-12-13 23:16:09 -0700108 TableIt left = m_table.lower_bound(prefix);
109 TableIt right = m_table.end();
110 if (prefix.size() > 0) {
111 right = m_table.lower_bound(prefix.getSuccessor());
112 }
113 for (TableIt it = left; it != right; ++it) {
114 const Entry& entry = *it;
115 if (!entry.canSatisfy(interest)) {
116 NFD_LOG_TRACE("cannotSatisfy " << entry.getFullName());
117 continue;
118 }
119 NFD_LOG_TRACE("canSatisfy " << entry.getFullName());
120 if (!isRightmost) {
121 return entry.getData().get();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122 }
123
Junxiao Shia9388182014-12-13 23:16:09 -0700124 if (rightmostCandidate == m_table.end() ||
125 isNewRightmostCandidate(prefix, rightmostCandidate, it)) {
126 NFD_LOG_TRACE("rightmostCandidate=" << entry.getFullName());
127 rightmostCandidate = it;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800128 }
Junxiao Shia9388182014-12-13 23:16:09 -0700129 }
130 if (isRightmost) {
131 return rightmostCandidate == m_table.end() ? nullptr :
132 rightmostCandidate->getData().get();
133 }
134 return nullptr;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800135}
136
137void
Junxiao Shia9388182014-12-13 23:16:09 -0700138Cs::attachQueue(TableIt it)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139{
Junxiao Shia9388182014-12-13 23:16:09 -0700140 Entry& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800141
Junxiao Shia9388182014-12-13 23:16:09 -0700142 if (entry.queueType != QUEUE_NONE) {
143 this->detachQueue(it);
144 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800145
Junxiao Shia9388182014-12-13 23:16:09 -0700146 if (entry.isUnsolicited()) {
147 entry.queueType = QUEUE_UNSOLICITED;
148 }
149 else if (entry.isStale()) {
150 entry.queueType = QUEUE_STALE;
151 }
152 else {
153 entry.queueType = QUEUE_FIFO;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800154
Junxiao Shia9388182014-12-13 23:16:09 -0700155 if (entry.canStale()) {
156 entry.moveStaleEvent = scheduler::schedule(entry.getData()->getFreshnessPeriod(),
157 bind(&Cs::moveToStaleQueue, this, it));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800158 }
Junxiao Shia9388182014-12-13 23:16:09 -0700159 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800160
Junxiao Shia9388182014-12-13 23:16:09 -0700161 Queue& queue = m_queues[entry.queueType];
162 entry.queueIt = queue.insert(queue.end(), it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800163}
164
165void
Junxiao Shia9388182014-12-13 23:16:09 -0700166Cs::detachQueue(TableIt it)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800167{
Junxiao Shia9388182014-12-13 23:16:09 -0700168 Entry& entry = const_cast<Entry&>(*it);
169
170 BOOST_ASSERT(entry.queueType != QUEUE_NONE);
171
172 if (entry.queueType == QUEUE_FIFO) {
173 scheduler::cancel(entry.moveStaleEvent);
174 }
175
176 m_queues[entry.queueType].erase(entry.queueIt);
177 entry.queueType = QUEUE_NONE;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800178}
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700179
Junxiao Shia9388182014-12-13 23:16:09 -0700180void
181Cs::moveToStaleQueue(TableIt it)
182{
183 Entry& entry = const_cast<Entry&>(*it);
184
185 BOOST_ASSERT(entry.queueType == QUEUE_FIFO);
186 m_queues[QUEUE_FIFO].erase(entry.queueIt);
187
188 entry.queueType = QUEUE_STALE;
189 Queue& queue = m_queues[QUEUE_STALE];
190 entry.queueIt = queue.insert(queue.end(), it);
191}
192
193std::tuple<Cs::TableIt, std::string>
194Cs::evictPick()
195{
196 if (!m_queues[QUEUE_UNSOLICITED].empty()) {
197 return std::make_tuple(m_queues[QUEUE_UNSOLICITED].front(), "unsolicited");
198 }
199 if (!m_queues[QUEUE_STALE].empty()) {
200 return std::make_tuple(m_queues[QUEUE_STALE].front(), "stale");
201 }
202 if (!m_queues[QUEUE_FIFO].empty()) {
203 return std::make_tuple(m_queues[QUEUE_FIFO].front(), "fifo");
204 }
205
206 BOOST_ASSERT(false);
207 return std::make_tuple(m_table.end(), "error");
208}
209
210
211void
212Cs::evict()
213{
214 while (this->size() > m_limit) {
215 TableIt it; std::string reason;
216 std::tie(it, reason) = this->evictPick();
217
218 NFD_LOG_DEBUG("evict " << it->getFullName() << " " << reason);
219 this->detachQueue(it);
220 m_table.erase(it);
221 }
222}
223
224void
225Cs::dump()
226{
227 NFD_LOG_DEBUG("dump table");
228 for (const Entry& entry : m_table) {
229 NFD_LOG_TRACE(entry.getFullName());
230 }
231}
232
233} // namespace cs
234} // namespace nfd