blob: a0bcbf6ac4d2bd6ee6a3c3181429cb4701ab7094 [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 Pesaventoaa9e3b22022-10-21 17:00:07 -04003 * Copyright (c) 2014-2022, 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"
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040030
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070032namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070033
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040034/** \brief Implements the Content Store.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000035 *
36 * This Content Store implementation consists of a Table and a replacement policy.
37 *
38 * The Table is a container ( \c std::set ) sorted by full Names of stored Data packets.
39 * Data packets are wrapped in Entry objects. Each Entry contains the Data packet itself,
40 * and a few additional attributes such as when the Data becomes non-fresh.
41 *
42 * The replacement policy is implemented in a subclass of \c Policy.
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070043 */
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080044class Cs : noncopyable
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070045{
46public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047 explicit
Junxiao Shib4a5acd2016-12-07 19:59:18 +000048 Cs(size_t nMaxPackets = 10);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080049
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040050 /** \brief Inserts a Data packet.
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070051 */
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070052 void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053 insert(const Data& data, bool isUnsolicited = false);
54
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040055 /** \brief Asynchronously erases entries under \p prefix.
Junxiao Shi14b39182019-04-29 19:19:18 +000056 * \tparam AfterEraseCallback `void f(size_t nErased)`
Junxiao Shi30c37ab2018-04-09 14:26:47 +000057 * \param prefix name prefix of entries
58 * \param limit max number of entries to erase
Junxiao Shi14b39182019-04-29 19:19:18 +000059 * \param cb callback to receive the actual number of erased entries; must not be empty;
Junxiao Shi30c37ab2018-04-09 14:26:47 +000060 * it may be invoked either before or after erase() returns
61 */
Junxiao Shi14b39182019-04-29 19:19:18 +000062 template<typename AfterEraseCallback>
Junxiao Shi30c37ab2018-04-09 14:26:47 +000063 void
Junxiao Shi14b39182019-04-29 19:19:18 +000064 erase(const Name& prefix, size_t limit, AfterEraseCallback&& cb)
65 {
66 size_t nErased = eraseImpl(prefix, limit);
67 cb(nErased);
68 }
mzhang4eab72492015-02-25 11:16:09 -060069
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040070 /** \brief Finds the best matching Data packet.
Junxiao Shi14b39182019-04-29 19:19:18 +000071 * \tparam HitCallback `void f(const Interest&, const Data&)`
72 * \tparam MissCallback `void f(const Interest&)`
mzhang4eab72492015-02-25 11:16:09 -060073 * \param interest the Interest for lookup
Junxiao Shi14b39182019-04-29 19:19:18 +000074 * \param hit a callback if a match is found; must not be empty
75 * \param miss a callback if there's no match; must not be empty
mzhang4eab72492015-02-25 11:16:09 -060076 * \note A lookup invokes either callback exactly once.
77 * The callback may be invoked either before or after find() returns
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070078 */
Junxiao Shi14b39182019-04-29 19:19:18 +000079 template<typename HitCallback, typename MissCallback>
mzhang4eab72492015-02-25 11:16:09 -060080 void
Junxiao Shi14b39182019-04-29 19:19:18 +000081 find(const Interest& interest, HitCallback&& hit, MissCallback&& miss) const
82 {
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060083 auto match = findImpl(interest);
Junxiao Shi14b39182019-04-29 19:19:18 +000084 if (match == m_table.end()) {
85 miss(interest);
86 return;
87 }
88 hit(interest, match->getData());
89 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080090
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /** \brief Get number of stored packets.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080092 */
93 size_t
Junxiao Shifc206962015-01-16 11:12:22 -070094 size() const
95 {
96 return m_table.size();
97 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080098
Junxiao Shi3d2049f2018-01-26 23:46:06 +000099public: // configuration
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400100 /** \brief Get capacity (in number of packets).
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000101 */
102 size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400103 getLimit() const noexcept
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000104 {
105 return m_policy->getLimit();
106 }
107
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400108 /** \brief Change capacity (in number of packets).
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000109 */
Junxiao Shia9388182014-12-13 23:16:09 -0700110 void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000111 setLimit(size_t nMaxPackets)
112 {
113 return m_policy->setLimit(nMaxPackets);
114 }
115
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400116 /** \brief Get replacement policy.
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000117 */
118 Policy*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400119 getPolicy() const noexcept
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000120 {
121 return m_policy.get();
122 }
123
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400124 /** \brief Change replacement policy.
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000125 * \pre size() == 0
126 */
127 void
128 setPolicy(unique_ptr<Policy> policy);
129
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400130 /** \brief Get CS_ENABLE_ADMIT flag.
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000131 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
132 */
133 bool
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400134 shouldAdmit() const noexcept
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000135 {
136 return m_shouldAdmit;
137 }
138
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400139 /** \brief Set CS_ENABLE_ADMIT flag.
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000140 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
141 */
142 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400143 enableAdmit(bool shouldAdmit) noexcept;
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000144
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400145 /** \brief Get CS_ENABLE_SERVE flag.
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000146 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
147 */
148 bool
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400149 shouldServe() const noexcept
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000150 {
151 return m_shouldServe;
152 }
153
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400154 /** \brief Set CS_ENABLE_SERVE flag.
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000155 * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config
156 */
157 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400158 enableServe(bool shouldServe) noexcept;
Junxiao Shia9388182014-12-13 23:16:09 -0700159
Junxiao Shifc206962015-01-16 11:12:22 -0700160public: // enumeration
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600161 using const_iterator = Table::const_iterator;
Junxiao Shifc206962015-01-16 11:12:22 -0700162
163 const_iterator
164 begin() const
165 {
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000166 return m_table.begin();
Junxiao Shifc206962015-01-16 11:12:22 -0700167 }
168
169 const_iterator
170 end() const
171 {
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000172 return m_table.end();
Junxiao Shifc206962015-01-16 11:12:22 -0700173 }
174
Junxiao Shi14b39182019-04-29 19:19:18 +0000175private:
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600176 std::pair<const_iterator, const_iterator>
Junxiao Shi14b39182019-04-29 19:19:18 +0000177 findPrefixRange(const Name& prefix) const;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700178
Junxiao Shi14b39182019-04-29 19:19:18 +0000179 size_t
180 eraseImpl(const Name& prefix, size_t limit);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700181
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600182 const_iterator
Junxiao Shi14b39182019-04-29 19:19:18 +0000183 findImpl(const Interest& interest) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800184
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800185 void
Junxiao Shi52555b02016-11-25 21:39:06 +0000186 setPolicyImpl(unique_ptr<Policy> policy);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800187
Davide Pesavento264af772021-02-09 21:48:24 -0500188NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000189 void
190 dump();
191
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800192private:
Junxiao Shia9388182014-12-13 23:16:09 -0700193 Table m_table;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500194 unique_ptr<Policy> m_policy;
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000195 signal::ScopedConnection m_beforeEvictConnection;
196
Davide Pesavento3dade002019-03-19 11:29:56 -0600197 bool m_shouldAdmit = true; ///< if false, no Data will be admitted
198 bool m_shouldServe = true; ///< if false, all lookups will miss
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700199};
200
Junxiao Shia9388182014-12-13 23:16:09 -0700201} // namespace cs
202
203using cs::Cs;
204
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800205} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800206
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700207#endif // NFD_DAEMON_TABLE_CS_HPP