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-2024, 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 #include <functional>
32 #include <map>
33 #include <set>
34 
35 namespace nfd::cs {
36 
37 class Cs;
38 
42 class Policy : noncopyable
43 {
44 public: // registry
45  template<typename P>
46  static void
47  registerPolicy(std::string_view policyName = P::POLICY_NAME)
48  {
49  BOOST_ASSERT(!policyName.empty());
50  auto r = getRegistry().insert_or_assign(std::string(policyName), [] { return make_unique<P>(); });
51  BOOST_VERIFY(r.second);
52  }
53 
58  static unique_ptr<Policy>
59  create(const std::string& policyName);
60 
64  static std::set<std::string>
66 
67 public:
68  virtual
69  ~Policy() = default;
70 
71  const std::string&
72  getName() const noexcept
73  {
74  return m_policyName;
75  }
76 
80  Cs*
81  getCs() const noexcept
82  {
83  return m_cs;
84  }
85 
89  void
90  setCs(Cs* cs) noexcept
91  {
92  m_cs = cs;
93  }
94 
98  size_t
99  getLimit() const noexcept
100  {
101  return m_limit;
102  }
103 
110  void
111  setLimit(size_t nMaxEntries);
112 
113 public:
117  using EntryRef = Table::const_iterator;
118 
124  signal::Signal<Policy, EntryRef> beforeEvict;
125 
132  void
134 
139  void
141 
145  void
147 
152  void
153  beforeUse(EntryRef i);
154 
155 protected:
164  virtual void
166 
172  virtual void
174 
181  virtual void
183 
189  virtual void
191 
195  virtual void
197 
198 protected:
199  explicit
200  Policy(std::string_view policyName);
201 
202  DECLARE_SIGNAL_EMIT(beforeEvict)
203 
204 private: // registry
205  using CreateFunc = std::function<unique_ptr<Policy>()>;
206  using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
207 
208  static Registry&
209  getRegistry();
210 
211 private:
212  const std::string m_policyName;
213  size_t m_limit;
214  Cs* m_cs;
215 };
216 
217 } // namespace nfd::cs
218 
222 #define NFD_REGISTER_CS_POLICY(P) \
223 static class NfdAuto ## P ## CsPolicyRegistrationClass \
224 { \
225 public: \
226  NfdAuto ## P ## CsPolicyRegistrationClass() \
227  { \
228  ::nfd::cs::Policy::registerPolicy<P>(); \
229  } \
230 } g_nfdAuto ## P ## CsPolicyRegistrationVariable
231 
232 #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP
Implements the Content Store.
Definition: cs.hpp:45
Represents a CS replacement policy.
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 void registerPolicy(std::string_view policyName=P::POLICY_NAME)
Definition: cs-policy.hpp:47
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:90
Cs * getCs() const noexcept
Returns a pointer to the associated CS instance.
Definition: cs-policy.hpp:81
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:72
Policy(std::string_view policyName)
Definition: cs-policy.cpp:61
Table::const_iterator EntryRef
A reference to a CS entry.
Definition: cs-policy.hpp:117
size_t getLimit() const noexcept
Gets hard limit (in number of entries).
Definition: cs-policy.hpp:99
signal::Signal< Policy, EntryRef > beforeEvict
Signal emitted when an entry is being evicted.
Definition: cs-policy.hpp:124
void afterRefresh(EntryRef i)
Invoked by CS after an existing entry is refreshed by same Data.
Definition: cs-policy.cpp:82