Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 84c65c0 | 2017-07-05 18:40:34 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 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. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_CS_HPP |
| 27 | #define NFD_DAEMON_TABLE_CS_HPP |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 28 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 29 | #include "cs-policy.hpp" |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 30 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 31 | namespace nfd { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 32 | namespace cs { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 34 | /** \brief implements the Content Store |
| 35 | * |
| 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 Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 43 | */ |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 44 | class Cs : noncopyable |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 45 | { |
| 46 | public: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 47 | explicit |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 48 | Cs(size_t nMaxPackets = 10); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 49 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 50 | /** \brief inserts a Data packet |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 51 | */ |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 52 | void |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 53 | insert(const Data& data, bool isUnsolicited = false); |
| 54 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 55 | /** \brief asynchronously erases entries under \p prefix |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 56 | * \tparam AfterEraseCallback `void f(size_t nErased)` |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 57 | * \param prefix name prefix of entries |
| 58 | * \param limit max number of entries to erase |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 59 | * \param cb callback to receive the actual number of erased entries; must not be empty; |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 60 | * it may be invoked either before or after erase() returns |
| 61 | */ |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 62 | template<typename AfterEraseCallback> |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 63 | void |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 64 | erase(const Name& prefix, size_t limit, AfterEraseCallback&& cb) |
| 65 | { |
| 66 | size_t nErased = eraseImpl(prefix, limit); |
| 67 | cb(nErased); |
| 68 | } |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 69 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 70 | /** \brief finds the best matching Data packet |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 71 | * \tparam HitCallback `void f(const Interest&, const Data&)` |
| 72 | * \tparam MissCallback `void f(const Interest&)` |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 73 | * \param interest the Interest for lookup |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 74 | * \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 |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 76 | * \note A lookup invokes either callback exactly once. |
| 77 | * The callback may be invoked either before or after find() returns |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 78 | */ |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 79 | template<typename HitCallback, typename MissCallback> |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 80 | void |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 81 | find(const Interest& interest, HitCallback&& hit, MissCallback&& miss) const |
| 82 | { |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 83 | auto match = findImpl(interest); |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 84 | if (match == m_table.end()) { |
| 85 | miss(interest); |
| 86 | return; |
| 87 | } |
| 88 | hit(interest, match->getData()); |
| 89 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 90 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 91 | /** \brief get number of stored packets |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 92 | */ |
| 93 | size_t |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 94 | size() const |
| 95 | { |
| 96 | return m_table.size(); |
| 97 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 99 | public: // configuration |
| 100 | /** \brief get capacity (in number of packets) |
| 101 | */ |
| 102 | size_t |
| 103 | getLimit() const |
| 104 | { |
| 105 | return m_policy->getLimit(); |
| 106 | } |
| 107 | |
| 108 | /** \brief change capacity (in number of packets) |
| 109 | */ |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 110 | void |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 111 | setLimit(size_t nMaxPackets) |
| 112 | { |
| 113 | return m_policy->setLimit(nMaxPackets); |
| 114 | } |
| 115 | |
| 116 | /** \brief get replacement policy |
| 117 | */ |
| 118 | Policy* |
| 119 | getPolicy() const |
| 120 | { |
| 121 | return m_policy.get(); |
| 122 | } |
| 123 | |
| 124 | /** \brief change replacement policy |
| 125 | * \pre size() == 0 |
| 126 | */ |
| 127 | void |
| 128 | setPolicy(unique_ptr<Policy> policy); |
| 129 | |
| 130 | /** \brief get CS_ENABLE_ADMIT flag |
| 131 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 132 | */ |
| 133 | bool |
| 134 | shouldAdmit() const |
| 135 | { |
| 136 | return m_shouldAdmit; |
| 137 | } |
| 138 | |
| 139 | /** \brief set CS_ENABLE_ADMIT flag |
| 140 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 141 | */ |
| 142 | void |
| 143 | enableAdmit(bool shouldAdmit); |
| 144 | |
| 145 | /** \brief get CS_ENABLE_SERVE flag |
| 146 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 147 | */ |
| 148 | bool |
| 149 | shouldServe() const |
| 150 | { |
| 151 | return m_shouldServe; |
| 152 | } |
| 153 | |
| 154 | /** \brief set CS_ENABLE_SERVE flag |
| 155 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 156 | */ |
| 157 | void |
| 158 | enableServe(bool shouldServe); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 160 | public: // enumeration |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 161 | using const_iterator = Table::const_iterator; |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 162 | |
| 163 | const_iterator |
| 164 | begin() const |
| 165 | { |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 166 | return m_table.begin(); |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | const_iterator |
| 170 | end() const |
| 171 | { |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 172 | return m_table.end(); |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 175 | private: |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 176 | std::pair<const_iterator, const_iterator> |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 177 | findPrefixRange(const Name& prefix) const; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 178 | |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 179 | size_t |
| 180 | eraseImpl(const Name& prefix, size_t limit); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 181 | |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 182 | const_iterator |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 183 | findImpl(const Interest& interest) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 184 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 185 | void |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 186 | setPolicyImpl(unique_ptr<Policy> policy); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 187 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 188 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 189 | void |
| 190 | dump(); |
| 191 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 192 | private: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 193 | Table m_table; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 194 | unique_ptr<Policy> m_policy; |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 195 | signal::ScopedConnection m_beforeEvictConnection; |
| 196 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 197 | bool m_shouldAdmit = true; ///< if false, no Data will be admitted |
| 198 | bool m_shouldServe = true; ///< if false, all lookups will miss |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 201 | } // namespace cs |
| 202 | |
| 203 | using cs::Cs; |
| 204 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 205 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 207 | #endif // NFD_DAEMON_TABLE_CS_HPP |