Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Teng Liang | 43bb231 | 2018-03-26 04:16:42 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_PIT_HPP |
| 27 | #define NFD_DAEMON_TABLE_PIT_HPP |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame^] | 29 | #include "name-tree.hpp" |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 30 | #include "pit-entry.hpp" |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 31 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 33 | namespace pit { |
| 34 | |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame^] | 35 | /** |
| 36 | * \brief An unordered iterable of all PIT entries matching a Data packet. |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 37 | */ |
Davide Pesavento | 1cb619e | 2018-04-10 17:13:53 -0400 | [diff] [blame] | 38 | using DataMatchResult = std::vector<shared_ptr<Entry>>; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 39 | |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame^] | 40 | /** |
| 41 | * \brief PIT iterator. |
| 42 | */ |
| 43 | class Iterator : public boost::forward_iterator_helper<Iterator, const Entry> |
| 44 | { |
| 45 | public: |
| 46 | /** |
| 47 | * \brief Constructor. |
| 48 | * \param ntIt a name tree iterator that visits name tree entries with one or more PIT entries |
| 49 | * \param iPitEntry make this iterator to dereference to the i-th PIT entry in name tree entry |
| 50 | */ |
| 51 | explicit |
| 52 | Iterator(const NameTree::const_iterator& ntIt = {}, size_t iPitEntry = 0) |
| 53 | : m_ntIt(ntIt) |
| 54 | , m_iPitEntry(iPitEntry) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | const Entry& |
| 59 | operator*() const noexcept |
| 60 | { |
| 61 | BOOST_ASSERT(m_ntIt != NameTree::const_iterator()); |
| 62 | BOOST_ASSERT(m_iPitEntry < m_ntIt->getPitEntries().size()); |
| 63 | return *m_ntIt->getPitEntries()[m_iPitEntry]; |
| 64 | } |
| 65 | |
| 66 | Iterator& |
| 67 | operator++(); |
| 68 | |
| 69 | friend bool |
| 70 | operator==(const Iterator& lhs, const Iterator& rhs) noexcept |
| 71 | { |
| 72 | return lhs.m_ntIt == rhs.m_ntIt && |
| 73 | lhs.m_iPitEntry == rhs.m_iPitEntry; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | NameTree::const_iterator m_ntIt; ///< current name tree entry |
| 78 | size_t m_iPitEntry; ///< current PIT entry within m_ntIt->getPitEntries() |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * \brief NFD's Interest Table. |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 83 | */ |
| 84 | class Pit : noncopyable |
| 85 | { |
| 86 | public: |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 87 | explicit |
| 88 | Pit(NameTree& nameTree); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 89 | |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame^] | 90 | /** |
| 91 | * \brief Returns the number of entries. |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 92 | */ |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 93 | size_t |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 94 | size() const |
| 95 | { |
| 96 | return m_nItems; |
| 97 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 98 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 99 | /** \brief Finds a PIT entry for \p interest |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 100 | * \param interest the Interest |
| 101 | * \return an existing entry with same Name and Selectors; otherwise nullptr |
| 102 | */ |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 103 | shared_ptr<Entry> |
| 104 | find(const Interest& interest) const |
| 105 | { |
| 106 | return const_cast<Pit*>(this)->findOrInsert(interest, false).first; |
| 107 | } |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 108 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 109 | /** \brief Inserts a PIT entry for \p interest |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 110 | * \param interest the Interest; must be created with make_shared |
| 111 | * \return a new or existing entry with same Name and Selectors, |
| 112 | * and true for new entry, false for existing entry |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 113 | */ |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 114 | std::pair<shared_ptr<Entry>, bool> |
| 115 | insert(const Interest& interest) |
| 116 | { |
| 117 | return this->findOrInsert(interest, true); |
| 118 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 119 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 120 | /** \brief Performs a Data match |
| 121 | * \return an iterable of all PIT entries matching \p data |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 122 | */ |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 123 | DataMatchResult |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 124 | findAllDataMatches(const Data& data) const; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 125 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 126 | /** \brief Deletes an entry |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 127 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 128 | void |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 129 | erase(Entry* entry) |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 130 | { |
| 131 | this->erase(entry, true); |
| 132 | } |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 133 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 134 | /** \brief Deletes in-records and out-records for \p face |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 135 | */ |
| 136 | void |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 137 | deleteInOutRecords(Entry* entry, const Face& face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 139 | public: // enumeration |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 140 | using const_iterator = Iterator; |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 141 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 142 | /** \return an iterator to the beginning |
Junxiao Shi | b30863e | 2016-08-15 21:32:01 +0000 | [diff] [blame] | 143 | * \note Iteration order is implementation-defined. |
| 144 | * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 145 | * is inserted or erased during iteration. |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 146 | */ |
| 147 | const_iterator |
| 148 | begin() const; |
| 149 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 150 | /** \return an iterator to the end |
| 151 | * \sa begin() |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 152 | */ |
| 153 | const_iterator |
Junxiao Shi | 3248205 | 2016-08-15 02:05:17 +0000 | [diff] [blame] | 154 | end() const |
| 155 | { |
| 156 | return Iterator(); |
| 157 | } |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 158 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 159 | private: |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 160 | void |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 161 | erase(Entry* pitEntry, bool canDeleteNte); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 162 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 163 | /** \brief Finds or inserts a PIT entry for \p interest |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 164 | * \param interest the Interest; must be created with make_shared if allowInsert |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 165 | * \param allowInsert whether inserting a new entry is allowed |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 166 | * \return if allowInsert, a new or existing entry with same Name+Selectors, |
| 167 | * and true for new entry, false for existing entry; |
| 168 | * if not allowInsert, an existing entry with same Name+Selectors and false, |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 169 | * or `{nullptr, true}` if there's no existing entry |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 170 | */ |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 171 | std::pair<shared_ptr<Entry>, bool> |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 172 | findOrInsert(const Interest& interest, bool allowInsert); |
| 173 | |
| 174 | private: |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 175 | NameTree& m_nameTree; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 176 | size_t m_nItems = 0; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 179 | } // namespace pit |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 180 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 181 | using pit::Pit; |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 183 | } // namespace nfd |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 184 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 185 | #endif // NFD_DAEMON_TABLE_PIT_HPP |