blob: b5f02006ed05b5fc0dac49a203bcaa3c82a249e8 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi2af9ca32016-08-25 21:51:02 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05004 * 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-internal.hpp"
30#include "cs-entry-impl.hpp"
31
32namespace nfd {
33namespace cs {
34
35class Cs;
36
37/** \brief represents a CS replacement policy
38 */
39class Policy : noncopyable
40{
41public:
42 explicit
43 Policy(const std::string& policyName);
44
45 virtual
Junxiao Shi2af9ca32016-08-25 21:51:02 +000046 ~Policy() = default;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050047
48 const std::string&
49 getName() const;
50
51public:
52 /** \brief gets cs
53 */
54 Cs*
55 getCs() const;
56
57 /** \brief sets cs
58 */
59 void
60 setCs(Cs *cs);
61
62 /** \brief gets hard limit (in number of entries)
63 */
64 size_t
65 getLimit() const;
66
67 /** \brief sets hard limit (in number of entries)
68 * \post getLimit() == nMaxEntries
69 * \post cs.size() <= getLimit()
70 *
71 * The policy may evict entries if necessary.
72 */
73 void
74 setLimit(size_t nMaxEntries);
75
76 /** \brief emits when an entry is being evicted
77 *
78 * A policy implementation should emit this signal to cause CS to erase the entry from its index.
79 * CS should connect to this signal and erase the entry upon signal emission.
80 */
81 signal::Signal<Policy, iterator> beforeEvict;
82
83 /** \brief invoked by CS after a new entry is inserted
84 * \post cs.size() <= getLimit()
85 *
86 * The policy may evict entries if necessary.
87 * During this process, \p i might be evicted.
88 */
89 void
90 afterInsert(iterator i);
91
92 /** \brief invoked by CS after an existing entry is refreshed by same Data
93 *
94 * The policy may witness this refresh to make better eviction decisions in the future.
95 */
96 void
97 afterRefresh(iterator i);
98
99 /** \brief invoked by CS before an entry is erased due to management command
100 * \warning CS must not invoke this method if an entry is erased due to eviction.
101 */
102 void
103 beforeErase(iterator i);
104
105 /** \brief invoked by CS before an entry is used to match a lookup
106 *
107 * The policy may witness this usage to make better eviction decisions in the future.
108 */
109 void
110 beforeUse(iterator i);
111
112protected:
113 /** \brief invoked after a new entry is created in CS
114 *
115 * When overridden in a subclass, a policy implementation should decide whether to accept \p i.
116 * If \p i is accepted, it should be inserted into a cleanup index.
117 * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry.
118 * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal,
119 * in order to keep CS size under limit.
120 */
121 virtual void
122 doAfterInsert(iterator i) = 0;
123
124 /** \brief invoked after an existing entry is refreshed by same Data
125 *
126 * When overridden in a subclass, a policy implementation may witness this operation
127 * and adjust its cleanup index.
128 */
129 virtual void
130 doAfterRefresh(iterator i) = 0;
131
132 /** \brief invoked before an entry is erased due to management command
133 * \note This will not be invoked for an entry being evicted by policy.
134 *
135 * When overridden in a subclass, a policy implementation should erase \p i
136 * from its cleanup index without emitted \p afterErase signal.
137 */
138 virtual void
139 doBeforeErase(iterator i) = 0;
140
141 /** \brief invoked before an entry is used to match a lookup
142 *
143 * When overridden in a subclass, a policy implementation may witness this operation
144 * and adjust its cleanup index.
145 */
146 virtual void
147 doBeforeUse(iterator i) = 0;
148
149 /** \brief evicts zero or more entries
150 * \post CS size does not exceed hard limit
151 */
152 virtual void
153 evictEntries() = 0;
154
155protected:
156 DECLARE_SIGNAL_EMIT(beforeEvict)
157
158private:
159 std::string m_policyName;
160 size_t m_limit;
161 Cs* m_cs;
162};
163
164inline const std::string&
165Policy::getName() const
166{
167 return m_policyName;
168}
169
170inline Cs*
171Policy::getCs() const
172{
173 return m_cs;
174}
175
176inline void
177Policy::setCs(Cs *cs)
178{
179 m_cs = cs;
180}
181
182inline size_t
183Policy::getLimit() const
184{
185 return m_limit;
186}
187
188} // namespace cs
189} // namespace nfd
190
Junxiao Shi2af9ca32016-08-25 21:51:02 +0000191#endif // NFD_DAEMON_TABLE_CS_POLICY_HPP