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 | /* |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | |
| 26 | /** \file |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 27 | * |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 28 | * \brief implements the ContentStore |
| 29 | * |
| 30 | * This ContentStore implementation consists of two data structures, |
| 31 | * a Table, and a set of cleanup queues. |
| 32 | * |
| 33 | * The Table is a container (std::set) sorted by full Names of stored Data packets. |
| 34 | * Data packets are wrapped in Entry objects. |
| 35 | * Each Entry contain the Data packet itself, |
| 36 | * and a few addition attributes such as the staleness of the Data packet. |
| 37 | * |
| 38 | * The cleanup queues are three doubly linked lists which stores Table iterators. |
| 39 | * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively. |
| 40 | * Table iterator is placed into, removed from, and moved between suitable queues |
| 41 | * whenever an Entry is added, removed, or has other attribute changes. |
| 42 | * The Table iterator of an Entry should be in exactly one queue at any moment. |
| 43 | * Within each queue, the iterators are kept in first-in-first-out order. |
| 44 | * Eviction procedure exhausts the first queue before moving onto the next queue, |
| 45 | * in the order of unsolicited, stale, and fresh queue. |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 46 | */ |
| 47 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 48 | #ifndef NFD_DAEMON_TABLE_CS_HPP |
| 49 | #define NFD_DAEMON_TABLE_CS_HPP |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 50 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 51 | #include "cs-policy.hpp" |
| 52 | #include "cs-internal.hpp" |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 53 | #include "cs-entry-impl.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 54 | #include <ndn-cxx/util/signal.hpp> |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 55 | #include <boost/iterator/transform_iterator.hpp> |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 56 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 57 | namespace nfd { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 58 | namespace cs { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 60 | /** \brief represents the ContentStore |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 61 | */ |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 62 | class Cs : noncopyable |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 63 | { |
| 64 | public: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 65 | explicit |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 66 | Cs(size_t nMaxPackets = 10); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 67 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 68 | /** \brief inserts a Data packet |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 69 | */ |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 70 | void |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 71 | insert(const Data& data, bool isUnsolicited = false); |
| 72 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 73 | typedef std::function<void(const Interest&, const Data& data)> HitCallback; |
| 74 | typedef std::function<void(const Interest&)> MissCallback; |
| 75 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 76 | /** \brief finds the best matching Data packet |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 77 | * \param interest the Interest for lookup |
| 78 | * \param hitCallback a callback if a match is found; must not be empty |
| 79 | * \param missCallback a callback if there's no match; must not be empty |
| 80 | * \note A lookup invokes either callback exactly once. |
| 81 | * The callback may be invoked either before or after find() returns |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 82 | */ |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 83 | void |
| 84 | find(const Interest& interest, |
| 85 | const HitCallback& hitCallback, |
| 86 | const MissCallback& missCallback) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 87 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 88 | /** \brief get number of stored packets |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 89 | */ |
| 90 | size_t |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 91 | size() const |
| 92 | { |
| 93 | return m_table.size(); |
| 94 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 95 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 96 | public: // configuration |
| 97 | /** \brief get capacity (in number of packets) |
| 98 | */ |
| 99 | size_t |
| 100 | getLimit() const |
| 101 | { |
| 102 | return m_policy->getLimit(); |
| 103 | } |
| 104 | |
| 105 | /** \brief change capacity (in number of packets) |
| 106 | */ |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 107 | void |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 108 | setLimit(size_t nMaxPackets) |
| 109 | { |
| 110 | return m_policy->setLimit(nMaxPackets); |
| 111 | } |
| 112 | |
| 113 | /** \brief get replacement policy |
| 114 | */ |
| 115 | Policy* |
| 116 | getPolicy() const |
| 117 | { |
| 118 | return m_policy.get(); |
| 119 | } |
| 120 | |
| 121 | /** \brief change replacement policy |
| 122 | * \pre size() == 0 |
| 123 | */ |
| 124 | void |
| 125 | setPolicy(unique_ptr<Policy> policy); |
| 126 | |
| 127 | /** \brief get CS_ENABLE_ADMIT flag |
| 128 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 129 | */ |
| 130 | bool |
| 131 | shouldAdmit() const |
| 132 | { |
| 133 | return m_shouldAdmit; |
| 134 | } |
| 135 | |
| 136 | /** \brief set CS_ENABLE_ADMIT flag |
| 137 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 138 | */ |
| 139 | void |
| 140 | enableAdmit(bool shouldAdmit); |
| 141 | |
| 142 | /** \brief get CS_ENABLE_SERVE flag |
| 143 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 144 | */ |
| 145 | bool |
| 146 | shouldServe() const |
| 147 | { |
| 148 | return m_shouldServe; |
| 149 | } |
| 150 | |
| 151 | /** \brief set CS_ENABLE_SERVE flag |
| 152 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-config |
| 153 | */ |
| 154 | void |
| 155 | enableServe(bool shouldServe); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 156 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 157 | public: // enumeration |
| 158 | struct EntryFromEntryImpl |
| 159 | { |
| 160 | typedef const Entry& result_type; |
| 161 | |
| 162 | const Entry& |
| 163 | operator()(const EntryImpl& entry) const |
| 164 | { |
| 165 | return entry; |
| 166 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 167 | }; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 168 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 169 | /** \brief ContentStore iterator (public API) |
| 170 | */ |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 171 | typedef boost::transform_iterator<EntryFromEntryImpl, iterator, const Entry&> const_iterator; |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 172 | |
| 173 | const_iterator |
| 174 | begin() const |
| 175 | { |
| 176 | return boost::make_transform_iterator(m_table.begin(), EntryFromEntryImpl()); |
| 177 | } |
| 178 | |
| 179 | const_iterator |
| 180 | end() const |
| 181 | { |
| 182 | return boost::make_transform_iterator(m_table.end(), EntryFromEntryImpl()); |
| 183 | } |
| 184 | |
| 185 | private: // find |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 186 | /** \brief find leftmost match in [first,last) |
| 187 | * \return the leftmost match, or last if not found |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 188 | */ |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 189 | iterator |
| 190 | findLeftmost(const Interest& interest, iterator left, iterator right) const; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 191 | |
| 192 | /** \brief find rightmost match in [first,last) |
| 193 | * \return the rightmost match, or last if not found |
| 194 | */ |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 195 | iterator |
| 196 | findRightmost(const Interest& interest, iterator first, iterator last) const; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 197 | |
| 198 | /** \brief find rightmost match among entries with exact Names in [first,last) |
| 199 | * \return the rightmost match, or last if not found |
| 200 | */ |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 201 | iterator |
| 202 | findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 203 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 204 | void |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 205 | setPolicyImpl(unique_ptr<Policy> policy); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 206 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 207 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 208 | void |
| 209 | dump(); |
| 210 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 211 | private: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 212 | Table m_table; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 213 | unique_ptr<Policy> m_policy; |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 214 | signal::ScopedConnection m_beforeEvictConnection; |
| 215 | |
| 216 | bool m_shouldAdmit; ///< if false, no Data will be admitted |
| 217 | bool m_shouldServe; ///< if false, all lookups will miss |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 220 | } // namespace cs |
| 221 | |
| 222 | using cs::Cs; |
| 223 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 224 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 225 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 226 | #endif // NFD_DAEMON_TABLE_CS_HPP |