30 #include <ndn-cxx/lp/tags.hpp>
38 BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Cs::const_iterator>));
41 #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
42 static_assert(std::is_default_constructible<Cs::const_iterator>::value,
43 "Cs::const_iterator must be default-constructible");
45 BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Cs::const_iterator>));
46 #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
51 return make_unique<PriorityFifoPolicy>();
54 Cs::Cs(
size_t nMaxPackets, unique_ptr<Policy> policy)
56 this->setPolicyImpl(policy);
57 m_policy->setLimit(nMaxPackets);
63 m_policy->setLimit(nMaxPackets);
69 return m_policy->getLimit();
75 BOOST_ASSERT(policy !=
nullptr);
76 BOOST_ASSERT(m_policy !=
nullptr);
77 size_t limit = m_policy->getLimit();
78 this->setPolicyImpl(policy);
79 m_policy->setLimit(limit);
87 if (m_policy->getLimit() == 0) {
93 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
95 lp::CachePolicyType policy = tag->get().getPolicy();
96 if (policy == lp::CachePolicyType::NO_CACHE) {
101 bool isNewEntry =
false;
104 std::tie(it, isNewEntry) = m_table.insert(
EntryImpl(data.shared_from_this(), isUnsolicited));
115 m_policy->afterRefresh(it);
118 m_policy->afterInsert(it);
127 BOOST_ASSERT(static_cast<bool>(hitCallback));
128 BOOST_ASSERT(static_cast<bool>(missCallback));
130 const Name& prefix = interest.getName();
131 bool isRightmost = interest.getChildSelector() == 1;
132 NFD_LOG_DEBUG(
"find " << prefix << (isRightmost ?
" R" :
" L"));
134 iterator first = m_table.lower_bound(prefix);
136 if (prefix.size() > 0) {
137 last = m_table.lower_bound(prefix.getSuccessor());
142 match = this->findRightmost(interest, first, last);
145 match = this->findLeftmost(interest, first, last);
150 missCallback(interest);
154 m_policy->beforeUse(match);
155 hitCallback(interest, match->getData());
159 Cs::findLeftmost(
const Interest& interest,
iterator first,
iterator last)
const
165 Cs::findRightmost(
const Interest& interest,
iterator first,
iterator last)
const
171 size_t interestNameLength = interest.getName().size();
172 for (
iterator right = last; right != first;) {
176 if (prev->getName().size() == interestNameLength) {
178 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
179 return matchExact == right ? last : matchExact;
182 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
183 iterator left = m_table.lower_bound(prefix);
187 iterator match = this->findLeftmost(interest, left, right);
188 if (match != right) {
197 Cs::findRightmostAmongExact(
const Interest& interest,
iterator first,
iterator last)
const
203 Cs::setPolicyImpl(unique_ptr<Policy>& policy)
205 m_policy = std::move(policy);
206 m_beforeEvictConnection = m_policy->beforeEvict.connect([
this] (
iterator it) {
210 m_policy->setCs(
this);
211 BOOST_ASSERT(m_policy->getCs() ==
this);
218 for (
const EntryImpl& entry : m_table) {
void updateStaleTime()
refreshes stale time relative to current time
#define NFD_LOG_DEBUG(expression)
std::function< void(const Interest &, const Data &data)> HitCallback
It find_last_if(It first, It last, Pred p)
finds the last element satisfying a predicate
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
Table::const_iterator iterator
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void insert(const Data &data, bool isUnsolicited=false)
inserts a Data packet
unique_ptr< Policy > makeDefaultPolicy()
void setLimit(size_t nMaxPackets)
changes capacity (in number of packets)
void setPolicy(unique_ptr< Policy > policy)
changes cs replacement policy
std::function< void(const Interest &)> MissCallback
an Entry in ContentStore implementation
#define NFD_LOG_INIT(name)
bool isUnsolicited() const
#define NFD_LOG_TRACE(expression)
bool canSatisfy(const Interest &interest) const
determines whether Interest can be satisified by the stored Data
void find(const Interest &interest, const HitCallback &hitCallback, const MissCallback &missCallback) const
finds the best matching Data packet
Cs(size_t nMaxPackets=10, unique_ptr< Policy > policy=makeDefaultPolicy())