30 #include <ndn-cxx/lp/tags.hpp> 42 const std::string DEFAULT_POLICY =
"priority_fifo";
51 m_policy->setLimit(nMaxPackets);
57 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
63 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
65 lp::CachePolicyType policy = tag->get().getPolicy();
66 if (policy == lp::CachePolicyType::NO_CACHE) {
72 bool isNewEntry =
false;
73 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
84 m_policy->afterRefresh(it);
87 m_policy->afterInsert(it);
96 BOOST_ASSERT(static_cast<bool>(hitCallback));
97 BOOST_ASSERT(static_cast<bool>(missCallback));
99 if (!m_shouldServe || m_policy->getLimit() == 0) {
100 missCallback(interest);
103 const Name& prefix = interest.getName();
104 bool isRightmost = interest.getChildSelector() == 1;
105 NFD_LOG_DEBUG(
"find " << prefix << (isRightmost ?
" R" :
" L"));
107 iterator first = m_table.lower_bound(prefix);
109 if (prefix.size() > 0) {
110 last = m_table.lower_bound(prefix.getSuccessor());
115 match = this->findRightmost(interest, first, last);
118 match = this->findLeftmost(interest, first, last);
123 missCallback(interest);
127 m_policy->beforeUse(match);
128 hitCallback(interest, match->getData());
132 Cs::findLeftmost(
const Interest& interest,
iterator first,
iterator last)
const 138 Cs::findRightmost(
const Interest& interest,
iterator first,
iterator last)
const 144 size_t interestNameLength = interest.getName().size();
145 for (
iterator right = last; right != first;) {
149 if (prev->getName().size() == interestNameLength) {
151 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
152 return matchExact == right ? last : matchExact;
155 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
156 iterator left = m_table.lower_bound(prefix);
160 iterator match = this->findLeftmost(interest, left, right);
161 if (match != right) {
170 Cs::findRightmostAmongExact(
const Interest& interest,
iterator first,
iterator last)
const 187 BOOST_ASSERT(policy !=
nullptr);
188 BOOST_ASSERT(m_policy !=
nullptr);
189 size_t limit = m_policy->getLimit();
190 this->setPolicyImpl(std::move(policy));
191 m_policy->setLimit(limit);
195 Cs::setPolicyImpl(unique_ptr<Policy> policy)
198 m_policy = std::move(policy);
199 m_beforeEvictConnection = m_policy->beforeEvict.connect([
this] (
iterator it) {
203 m_policy->setCs(
this);
204 BOOST_ASSERT(m_policy->getCs() ==
this);
210 if (m_shouldAdmit == shouldAdmit) {
214 NFD_LOG_INFO((shouldAdmit ?
"Enabling" :
"Disabling") <<
" Data admittance");
220 if (m_shouldServe == shouldServe) {
224 NFD_LOG_INFO((shouldServe ?
"Enabling" :
"Disabling") <<
" Data serving");
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
bool shouldServe() const
get CS_ENABLE_SERVE flag
implements the ContentStore
void enableAdmit(bool shouldAdmit)
set CS_ENABLE_ADMIT flag
static unique_ptr< Policy > create(const std::string &policyName)
Table::const_iterator iterator
#define NFD_LOG_INFO(expression)
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
boost::transform_iterator< EntryFromEntryImpl, iterator, const Entry & > const_iterator
ContentStore iterator (public API)
void enableServe(bool shouldServe)
set CS_ENABLE_SERVE flag
unique_ptr< Policy > makeDefaultPolicy()
void setPolicy(unique_ptr< Policy > policy)
change replacement policy
std::function< void(const Interest &)> MissCallback
NFD_ASSERT_FORWARD_ITERATOR(Cs::const_iterator)
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)
bool shouldAdmit() const
get CS_ENABLE_ADMIT flag