cs-policy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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.
10  *
11  * 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/>.
24  */
25 
26 #ifndef NFD_DAEMON_TABLE_CS_POLICY_HPP
27 #define NFD_DAEMON_TABLE_CS_POLICY_HPP
28 
29 #include "cs-entry.hpp"
30 
31 namespace nfd::cs {
32 
33 class Cs;
34 
38 class Policy : noncopyable
39 {
40 public: // registry
41  template<typename P>
42  static void
43  registerPolicy(const std::string& policyName = P::POLICY_NAME)
44  {
45  BOOST_ASSERT(!policyName.empty());
46  auto r = getRegistry().insert_or_assign(policyName, [] { return make_unique<P>(); });
47  BOOST_VERIFY(r.second);
48  }
49 
54  static unique_ptr<Policy>
55  create(const std::string& policyName);
56 
60  static std::set<std::string>
62 
63 public:
64  virtual
65  ~Policy() = default;
66 
67  const std::string&
68  getName() const noexcept
69  {
70  return m_policyName;
71  }
72 
76  Cs*
77  getCs() const noexcept
78  {
79  return m_cs;
80  }
81 
85  void
86  setCs(Cs* cs) noexcept
87  {
88  m_cs = cs;
89  }
90 
94  size_t
95  getLimit() const noexcept
96  {
97  return m_limit;
98  }
99 
106  void
107  setLimit(size_t nMaxEntries);
108 
109 public:
113  using EntryRef = Table::const_iterator;
114 
120  signal::Signal<Policy, EntryRef> beforeEvict;
121 
128  void
130 
135  void
137 
141  void
143 
148  void
149  beforeUse(EntryRef i);
150 
151 protected:
160  virtual void
162 
168  virtual void
170 
177  virtual void
179 
185  virtual void
187 
191  virtual void
193 
194 protected:
195  explicit
196  Policy(std::string_view policyName);
197 
198  DECLARE_SIGNAL_EMIT(beforeEvict)
199 
200 private: // registry
201  using CreateFunc = std::function<unique_ptr<Policy>()>;
202  using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
203 
204  static Registry&
205  getRegistry();
206 
207 private:
208  const std::string m_policyName;
209  size_t m_limit;
210  Cs* m_cs;
211 };
212 
213 } // namespace nfd::cs
214 
218 #define NFD_REGISTER_CS_POLICY(P) \
219 static class NfdAuto ## P ## CsPolicyRegistrationClass \
220 { \
221 public: \
222  NfdAuto ## P ## CsPolicyRegistrationClass() \
223  { \
224  ::nfd::cs::Policy::registerPolicy<P>(); \
225  } \
226 } g_nfdAuto ## P ## CsPolicyRegistrationVariable
227 
228 #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP
Implements the Content Store.
Definition: cs.hpp:45
Represents a CS replacement policy.
Definition: cs-policy.hpp:39
static void registerPolicy(const std::string &policyName=P::POLICY_NAME)
Definition: cs-policy.hpp:43
virtual void doAfterInsert(EntryRef i)=0
Invoked after a new entry is created in CS.
virtual void doAfterRefresh(EntryRef i)=0
Invoked after an existing entry is refreshed by same Data.
void setLimit(size_t nMaxEntries)
Sets hard limit (in number of entries).
Definition: cs-policy.cpp:67
void beforeUse(EntryRef i)
Invoked by CS before an entry is used to match a lookup.
Definition: cs-policy.cpp:96
void afterInsert(EntryRef i)
Invoked by CS after a new entry is inserted.
Definition: cs-policy.cpp:75
static std::set< std::string > getPolicyNames()
Returns a list of available policy names.
Definition: cs-policy.cpp:53
static unique_ptr< Policy > create(const std::string &policyName)
Returns a cs::Policy identified by policyName, or nullptr if policyName is unknown.
Definition: cs-policy.cpp:45
virtual void evictEntries()=0
Evicts zero or more entries.
virtual void doBeforeErase(EntryRef i)=0
Invoked before an entry is erased due to management command.
void setCs(Cs *cs) noexcept
Sets the associated CS instance.
Definition: cs-policy.hpp:86
Cs * getCs() const noexcept
Returns a pointer to the associated CS instance.
Definition: cs-policy.hpp:77
virtual ~Policy()=default
virtual void doBeforeUse(EntryRef i)=0
Invoked before an entry is used to match a lookup.
void beforeErase(EntryRef i)
Invoked by CS before an entry is erased due to management command.
Definition: cs-policy.cpp:89
const std::string & getName() const noexcept
Definition: cs-policy.hpp:68
Policy(std::string_view policyName)
Definition: cs-policy.cpp:61
Table::const_iterator EntryRef
A reference to a CS entry.
Definition: cs-policy.hpp:113
size_t getLimit() const noexcept
Gets hard limit (in number of entries).
Definition: cs-policy.hpp:95
signal::Signal< Policy, EntryRef > beforeEvict
Signal emitted when an entry is being evicted.
Definition: cs-policy.hpp:120
void afterRefresh(EntryRef i)
Invoked by CS after an existing entry is refreshed by same Data.
Definition: cs-policy.cpp:82