29 #include <ndn-cxx/lp/tags.hpp>
30 #include <ndn-cxx/util/concepts.hpp>
36 static unique_ptr<Policy>
45 m_policy->setLimit(nMaxPackets);
51 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
57 auto tag = data.getTag<lp::CachePolicyTag>();
59 lp::CachePolicyType policy = tag->get().getPolicy();
60 if (policy == lp::CachePolicyType::NO_CACHE) {
65 auto [it, isNewEntry] = m_table.emplace(data.shared_from_this(), isUnsolicited);
66 auto& entry =
const_cast<Entry&
>(*it);
72 if (entry.isUnsolicited() && !isUnsolicited) {
73 entry.clearUnsolicited();
75 m_policy->afterRefresh(it);
78 m_policy->afterInsert(it);
82 std::pair<Cs::const_iterator, Cs::const_iterator>
83 Cs::findPrefixRange(
const Name& prefix)
const
85 auto first = m_table.lower_bound(prefix);
86 auto last = m_table.end();
87 if (!prefix.empty()) {
88 last = m_table.lower_bound(prefix.getSuccessor());
94 Cs::eraseImpl(
const Name& prefix,
size_t limit)
97 std::tie(i, last) = findPrefixRange(prefix);
100 while (i != last && nErased < limit) {
101 m_policy->beforeErase(i);
102 i = m_table.erase(i);
109 Cs::findImpl(
const Interest& interest)
const
111 if (!m_shouldServe || m_policy->getLimit() == 0) {
112 return m_table.end();
115 const Name& prefix = interest.getName();
116 auto range = findPrefixRange(prefix);
117 auto match = std::find_if(range.first, range.second,
118 [&interest] (
const auto& entry) { return entry.canSatisfy(interest); });
120 if (match == range.second) {
122 return m_table.end();
124 NFD_LOG_DEBUG(
"find " << prefix <<
" matching " << match->getName());
125 m_policy->beforeUse(match);
133 for (
const Entry& entry : m_table) {
141 BOOST_ASSERT(policy !=
nullptr);
142 BOOST_ASSERT(m_policy !=
nullptr);
143 size_t limit = m_policy->getLimit();
144 this->setPolicyImpl(std::move(policy));
145 m_policy->setLimit(limit);
149 Cs::setPolicyImpl(unique_ptr<Policy> policy)
152 m_policy = std::move(policy);
153 m_beforeEvictConnection = m_policy->beforeEvict.connect([
this] (
auto it) { m_table.erase(it); });
155 m_policy->setCs(
this);
156 BOOST_ASSERT(m_policy->getCs() ==
this);
162 if (m_shouldAdmit == shouldAdmit) {
165 m_shouldAdmit = shouldAdmit;
166 NFD_LOG_INFO((shouldAdmit ?
"Enabling" :
"Disabling") <<
" Data admittance");
172 if (m_shouldServe == shouldServe) {
175 m_shouldServe = shouldServe;
176 NFD_LOG_INFO((shouldServe ?
"Enabling" :
"Disabling") <<
" Data serving");
Table::const_iterator const_iterator
void enableAdmit(bool shouldAdmit) noexcept
Set CS_ENABLE_ADMIT flag.
void setPolicy(unique_ptr< Policy > policy)
Change replacement policy.
Cs(size_t nMaxPackets=10)
void insert(const Data &data, bool isUnsolicited=false)
Inserts a Data packet.
void enableServe(bool shouldServe) noexcept
Set CS_ENABLE_SERVE flag.
void updateFreshUntil()
Recalculate when the entry would become non-fresh, relative to current time.
static unique_ptr< Policy > create(const std::string &policyName)
Returns a cs::Policy identified by policyName, or nullptr if policyName is unknown.
#define NFD_LOG_INIT(name)
static unique_ptr< Policy > makeDefaultPolicy()