blob: df6c88b4c5afe1d4930070fb66502936813bd73e [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento84c65c02017-07-05 18:40:34 +00002/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi5640ec82015-01-07 21:51:19 -07004 * 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.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * 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/>.
Junxiao Shia9388182014-12-13 23:16:09 -070024 */
25
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_CS_HPP
27#define NFD_DAEMON_TABLE_CS_HPP
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080028
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050029#include "cs-policy.hpp"
30#include "cs-internal.hpp"
Junxiao Shifc206962015-01-16 11:12:22 -070031#include "cs-entry-impl.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060032
Junxiao Shifc206962015-01-16 11:12:22 -070033#include <boost/iterator/transform_iterator.hpp>
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040034
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080035namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070036namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070037
Junxiao Shi30c37ab2018-04-09 14:26:47 +000038/** \brief implements the Content Store
39 *
40 * This Content Store implementation consists of a Table and a replacement policy.
41 *
42 * The Table is a container ( \c std::set ) sorted by full Names of stored Data packets.
43 * Data packets are wrapped in Entry objects. Each Entry contains the Data packet itself,
44 * and a few additional attributes such as when the Data becomes non-fresh.
45 *
46 * The replacement policy is implemented in a subclass of \c Policy.
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070047 */
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080048class Cs : noncopyable
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070049{
50public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080051 explicit
Junxiao Shib4a5acd2016-12-07 19:59:18 +000052 Cs(size_t nMaxPackets = 10);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070054 /** \brief inserts a Data packet
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070055 */
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070056 void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080057 insert(const Data& data, bool isUnsolicited = false);
58
Junxiao Shi30c37ab2018-04-09 14:26:47 +000059 /** \brief asynchronously erases entries under \p prefix
Junxiao Shi14b39182019-04-29 19:19:18 +000060 * \tparam AfterEraseCallback `void f(size_t nErased)`
Junxiao Shi30c37ab2018-04-09 14:26:47 +000061 * \param prefix name prefix of entries
62 * \param limit max number of entries to erase
Junxiao Shi14b39182019-04-29 19:19:18 +000063 * \param cb callback to receive the actual number of erased entries; must not be empty;
Junxiao Shi30c37ab2018-04-09 14:26:47 +000064 * it may be invoked either before or after erase() returns
65 */
Junxiao Shi14b39182019-04-29 19:19:18 +000066 template<typename AfterEraseCallback>
Junxiao Shi30c37ab2018-04-09 14:26:47 +000067 void
Junxiao Shi14b39182019-04-29 19:19:18 +000068 erase(const Name& prefix, size_t limit, AfterEraseCallback&& cb)
69 {
70 size_t nErased = eraseImpl(prefix, limit);
71 cb(nErased);
72 }
mzhang4eab72492015-02-25 11:16:09 -060073
Junxiao Shia9388182014-12-13 23:16:09 -070074 /** \brief finds the best matching Data packet
Junxiao Shi14b39182019-04-29 19:19:18 +000075 * \tparam HitCallback `void f(const Interest&, const Data&)`
76 * \tparam MissCallback `void f(const Interest&)`
mzhang4eab72492015-02-25 11:16:09 -060077 * \param interest the Interest for lookup
Junxiao Shi14b39182019-04-29 19:19:18 +000078 * \param hit a callback if a match is found; must not be empty
79 * \param miss a callback if there's no match; must not be empty
mzhang4eab72492015-02-25 11:16:09 -060080 * \note A lookup invokes either callback exactly once.
81 * The callback may be invoked either before or after find() returns
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070082 */
Junxiao Shi14b39182019-04-29 19:19:18 +000083 template<typename HitCallback, typename MissCallback>
mzhang4eab72492015-02-25 11:16:09 -060084 void
Junxiao Shi14b39182019-04-29 19:19:18 +000085 find(const Interest& interest, HitCallback&& hit, MissCallback&& miss) const
86 {
87 iterator match = findImpl(interest);
88 if (match == m_table.end()) {
89 miss(interest);
90 return;
91 }
92 hit(interest, match->getData());
93 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080094
Junxiao Shi3d2049f2018-01-26 23:46:06 +000095 /** \brief get number of stored packets
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080096 */
97 size_t
Junxiao Shifc206962015-01-16 11:12:22 -070098 size() const
99 {
100 return m_table.size();
101 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800102
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000103public: // configuration
104 /** \brief get capacity (in number of packets)
105 */
106 size_t
107 getLimit() const
108 {
109 return m_policy->getLimit();
110 }
111
112 /** \brief change capacity (in number of packets)
113 */
Junxiao Shia9388182014-12-13 23:16:09 -0700114 void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000115 setLimit(size_t nMaxPackets)
116 {
117 return m_policy->setLimit(nMaxPackets);
118 }
119
120 /** \brief get replacement policy
121 */
122 Policy*
123 getPolicy() const
124 {
125 return m_policy.get();
126 }
127
128 /** \brief change replacement policy
129 * \pre size() == 0
130 */
131 void
132 setPolicy(unique_ptr<Policy> policy);
133
134 /** \brief get CS_ENABLE_ADMIT flag
135 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
136 */
137 bool
138 shouldAdmit() const
139 {
140 return m_shouldAdmit;
141 }
142
143 /** \brief set CS_ENABLE_ADMIT flag
144 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
145 */
146 void
147 enableAdmit(bool shouldAdmit);
148
149 /** \brief get CS_ENABLE_SERVE flag
150 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
151 */
152 bool
153 shouldServe() const
154 {
155 return m_shouldServe;
156 }
157
158 /** \brief set CS_ENABLE_SERVE flag
159 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
160 */
161 void
162 enableServe(bool shouldServe);
Junxiao Shia9388182014-12-13 23:16:09 -0700163
Junxiao Shifc206962015-01-16 11:12:22 -0700164public: // enumeration
Junxiao Shi14b39182019-04-29 19:19:18 +0000165 class EntryFromEntryImpl
Junxiao Shifc206962015-01-16 11:12:22 -0700166 {
Junxiao Shi14b39182019-04-29 19:19:18 +0000167 public:
Junxiao Shifc206962015-01-16 11:12:22 -0700168 typedef const Entry& result_type;
169
170 const Entry&
171 operator()(const EntryImpl& entry) const
172 {
173 return entry;
174 }
Junxiao Shia9388182014-12-13 23:16:09 -0700175 };
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176
Junxiao Shifc206962015-01-16 11:12:22 -0700177 /** \brief ContentStore iterator (public API)
178 */
Junxiao Shi14b39182019-04-29 19:19:18 +0000179 using const_iterator = boost::transform_iterator<EntryFromEntryImpl, iterator, const Entry&>;
Junxiao Shifc206962015-01-16 11:12:22 -0700180
181 const_iterator
182 begin() const
183 {
184 return boost::make_transform_iterator(m_table.begin(), EntryFromEntryImpl());
185 }
186
187 const_iterator
188 end() const
189 {
190 return boost::make_transform_iterator(m_table.end(), EntryFromEntryImpl());
191 }
192
Junxiao Shi14b39182019-04-29 19:19:18 +0000193private:
194 std::pair<iterator, iterator>
195 findPrefixRange(const Name& prefix) const;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700196
Junxiao Shi14b39182019-04-29 19:19:18 +0000197 size_t
198 eraseImpl(const Name& prefix, size_t limit);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700199
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500200 iterator
Junxiao Shi14b39182019-04-29 19:19:18 +0000201 findImpl(const Interest& interest) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800203 void
Junxiao Shi52555b02016-11-25 21:39:06 +0000204 setPolicyImpl(unique_ptr<Policy> policy);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800205
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000206PUBLIC_WITH_TESTS_ELSE_PRIVATE:
207 void
208 dump();
209
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800210private:
Junxiao Shia9388182014-12-13 23:16:09 -0700211 Table m_table;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500212 unique_ptr<Policy> m_policy;
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000213 signal::ScopedConnection m_beforeEvictConnection;
214
Davide Pesavento3dade002019-03-19 11:29:56 -0600215 bool m_shouldAdmit = true; ///< if false, no Data will be admitted
216 bool m_shouldServe = true; ///< if false, all lookups will miss
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700217};
218
Junxiao Shia9388182014-12-13 23:16:09 -0700219} // namespace cs
220
221using cs::Cs;
222
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800223} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800224
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700225#endif // NFD_DAEMON_TABLE_CS_HPP