Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [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 | |
| 35 | /** \class DataMatchResult |
| 36 | * \brief an unordered iterable of all PIT entries matching Data |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 37 | * |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 38 | * This type shall support: |
| 39 | * iterator<shared_ptr<pit::Entry>> begin() |
| 40 | * iterator<shared_ptr<pit::Entry>> end() |
| 41 | */ |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 42 | typedef std::vector<shared_ptr<pit::Entry>> DataMatchResult; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 43 | |
| 44 | } // namespace pit |
| 45 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 46 | /** \brief represents the Interest Table |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 47 | */ |
| 48 | class Pit : noncopyable |
| 49 | { |
| 50 | public: |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 51 | explicit |
| 52 | Pit(NameTree& nameTree); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 53 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 54 | ~Pit(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 55 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 56 | /** \return number of entries |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 57 | */ |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 58 | size_t |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 59 | size() const; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 60 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 61 | /** \brief inserts a PIT entry for Interest |
| 62 | * |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 63 | * If an entry for exact same name and selectors exists, that entry is returned. |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 64 | * \return the entry, and true for new entry, false for existing entry |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 65 | */ |
| 66 | std::pair<shared_ptr<pit::Entry>, bool> |
| 67 | insert(const Interest& interest); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 68 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 69 | /** \brief performs a Data match |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 70 | * \return an iterable of all PIT entries matching data |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 71 | */ |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 72 | pit::DataMatchResult |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 73 | findAllDataMatches(const Data& data) const; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 74 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 75 | /** |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 76 | * \brief erases a PIT Entry |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 77 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 78 | void |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 79 | erase(shared_ptr<pit::Entry> pitEntry); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 81 | public: // enumeration |
| 82 | class const_iterator; |
| 83 | |
| 84 | /** \brief returns an iterator pointing to the first PIT entry |
| 85 | * \note Iteration order is implementation-specific and is undefined |
| 86 | * \note The returned iterator may get invalidated if PIT or another NameTree-based |
| 87 | * table is modified |
| 88 | */ |
| 89 | const_iterator |
| 90 | begin() const; |
| 91 | |
| 92 | /** \brief returns an iterator referring to the past-the-end PIT entry |
| 93 | * \note The returned iterator may get invalidated if PIT or another NameTree-based |
| 94 | * table is modified |
| 95 | */ |
| 96 | const_iterator |
| 97 | end() const; |
| 98 | |
| 99 | class const_iterator : public std::iterator<std::forward_iterator_tag, const pit::Entry> |
| 100 | { |
| 101 | public: |
| 102 | const_iterator(); |
| 103 | |
| 104 | explicit |
| 105 | const_iterator(const NameTree::const_iterator& it); |
| 106 | |
| 107 | ~const_iterator(); |
| 108 | |
| 109 | const pit::Entry& |
| 110 | operator*() const; |
| 111 | |
| 112 | shared_ptr<pit::Entry> |
| 113 | operator->() const; |
| 114 | |
| 115 | const_iterator& |
| 116 | operator++(); |
| 117 | |
| 118 | const_iterator |
| 119 | operator++(int); |
| 120 | |
| 121 | bool |
| 122 | operator==(const const_iterator& other) const; |
| 123 | |
| 124 | bool |
| 125 | operator!=(const const_iterator& other) const; |
| 126 | |
| 127 | private: |
| 128 | NameTree::const_iterator m_nameTreeIterator; |
| 129 | /** \brief Index of the current visiting PIT entry in NameTree node |
| 130 | * |
| 131 | * Index is used to ensure that dereferencing of m_nameTreeIterator happens only when |
| 132 | * const_iterator is dereferenced or advanced. |
| 133 | */ |
| 134 | size_t m_iPitEntry; |
| 135 | }; |
| 136 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 137 | private: |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 138 | NameTree& m_nameTree; |
| 139 | size_t m_nItems; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 142 | inline size_t |
| 143 | Pit::size() const |
| 144 | { |
| 145 | return m_nItems; |
| 146 | } |
| 147 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 148 | inline Pit::const_iterator |
| 149 | Pit::end() const |
| 150 | { |
| 151 | return const_iterator(m_nameTree.end()); |
| 152 | } |
| 153 | |
| 154 | inline |
| 155 | Pit::const_iterator::const_iterator() |
| 156 | : m_iPitEntry(0) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | inline |
| 161 | Pit::const_iterator::const_iterator(const NameTree::const_iterator& it) |
| 162 | : m_nameTreeIterator(it) |
| 163 | , m_iPitEntry(0) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | inline |
| 168 | Pit::const_iterator::~const_iterator() |
| 169 | { |
| 170 | } |
| 171 | |
| 172 | inline Pit::const_iterator |
| 173 | Pit::const_iterator::operator++(int) |
| 174 | { |
| 175 | Pit::const_iterator temp(*this); |
| 176 | ++(*this); |
| 177 | return temp; |
| 178 | } |
| 179 | |
| 180 | inline Pit::const_iterator& |
| 181 | Pit::const_iterator::operator++() |
| 182 | { |
| 183 | ++m_iPitEntry; |
| 184 | if (m_iPitEntry < m_nameTreeIterator->getPitEntries().size()) { |
| 185 | return *this; |
| 186 | } |
| 187 | |
| 188 | ++m_nameTreeIterator; |
| 189 | m_iPitEntry = 0; |
| 190 | return *this; |
| 191 | } |
| 192 | |
| 193 | inline const pit::Entry& |
| 194 | Pit::const_iterator::operator*() const |
| 195 | { |
| 196 | return *(this->operator->()); |
| 197 | } |
| 198 | |
| 199 | inline shared_ptr<pit::Entry> |
| 200 | Pit::const_iterator::operator->() const |
| 201 | { |
| 202 | return m_nameTreeIterator->getPitEntries().at(m_iPitEntry); |
| 203 | } |
| 204 | |
| 205 | inline bool |
| 206 | Pit::const_iterator::operator==(const Pit::const_iterator& other) const |
| 207 | { |
| 208 | return m_nameTreeIterator == other.m_nameTreeIterator && |
| 209 | m_iPitEntry == other.m_iPitEntry; |
| 210 | } |
| 211 | |
| 212 | inline bool |
| 213 | Pit::const_iterator::operator!=(const Pit::const_iterator& other) const |
| 214 | { |
| 215 | return !(*this == other); |
| 216 | } |
| 217 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 218 | } // namespace nfd |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 219 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 220 | #endif // NFD_DAEMON_TABLE_PIT_HPP |