blob: cbfac865d437a143fc4748c782e1602d606e9400 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shib2bcbcd2014-11-08 09:30:28 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_PIT_HPP
27#define NFD_DAEMON_TABLE_PIT_HPP
Junxiao Shicbba04c2014-01-26 14:21:22 -070028
Haowei Yuan78c84d12014-02-27 15:35:13 -060029#include "name-tree.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070030#include "pit-entry.hpp"
Haowei Yuan78c84d12014-02-27 15:35:13 -060031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070033namespace pit {
34
35/** \class DataMatchResult
36 * \brief an unordered iterable of all PIT entries matching Data
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070037 *
Junxiao Shicbba04c2014-01-26 14:21:22 -070038 * This type shall support:
39 * iterator<shared_ptr<pit::Entry>> begin()
40 * iterator<shared_ptr<pit::Entry>> end()
41 */
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070042typedef std::vector<shared_ptr<pit::Entry>> DataMatchResult;
Junxiao Shicbba04c2014-01-26 14:21:22 -070043
44} // namespace pit
45
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070046/** \brief represents the Interest Table
Junxiao Shicbba04c2014-01-26 14:21:22 -070047 */
48class Pit : noncopyable
49{
50public:
Haowei Yuan78c84d12014-02-27 15:35:13 -060051 explicit
52 Pit(NameTree& nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -060053
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070054 /** \return number of entries
Haowei Yuan78c84d12014-02-27 15:35:13 -060055 */
Haowei Yuane1079fc2014-03-08 14:41:25 -060056 size_t
Haowei Yuan78c84d12014-02-27 15:35:13 -060057 size() const;
Haowei Yuane1079fc2014-03-08 14:41:25 -060058
Junxiao Shi5e5e4452015-09-24 16:56:52 -070059 /** \brief finds a PIT entry for Interest
60 * \param interest the Interest
61 * \return an existing entry with same Name and Selectors; otherwise nullptr
62 */
63 shared_ptr<pit::Entry>
64 find(const Interest& interest) const;
65
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070066 /** \brief inserts a PIT entry for Interest
Junxiao Shi5e5e4452015-09-24 16:56:52 -070067 * \param interest the Interest; must be created with make_shared
68 * \return a new or existing entry with same Name and Selectors,
69 * and true for new entry, false for existing entry
Junxiao Shicbba04c2014-01-26 14:21:22 -070070 */
71 std::pair<shared_ptr<pit::Entry>, bool>
72 insert(const Interest& interest);
Haowei Yuane1079fc2014-03-08 14:41:25 -060073
Junxiao Shicbba04c2014-01-26 14:21:22 -070074 /** \brief performs a Data match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070075 * \return an iterable of all PIT entries matching data
Junxiao Shicbba04c2014-01-26 14:21:22 -070076 */
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070077 pit::DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -070078 findAllDataMatches(const Data& data) const;
Haowei Yuane1079fc2014-03-08 14:41:25 -060079
Haowei Yuan78c84d12014-02-27 15:35:13 -060080 /**
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070081 * \brief erases a PIT Entry
Haowei Yuane1079fc2014-03-08 14:41:25 -060082 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070083 void
Junxiao Shi02b73f52016-07-28 01:48:27 +000084 erase(shared_ptr<pit::Entry> entry);
85
86 /** \brief deletes in-record and out-record for face
87 */
88 void
89 deleteInOutRecords(shared_ptr<pit::Entry> entry, const Face& face);
Junxiao Shicbba04c2014-01-26 14:21:22 -070090
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -080091public: // enumeration
92 class const_iterator;
93
94 /** \brief returns an iterator pointing to the first PIT entry
95 * \note Iteration order is implementation-specific and is undefined
96 * \note The returned iterator may get invalidated if PIT or another NameTree-based
97 * table is modified
98 */
99 const_iterator
100 begin() const;
101
102 /** \brief returns an iterator referring to the past-the-end PIT entry
103 * \note The returned iterator may get invalidated if PIT or another NameTree-based
104 * table is modified
105 */
106 const_iterator
107 end() const;
108
109 class const_iterator : public std::iterator<std::forward_iterator_tag, const pit::Entry>
110 {
111 public:
112 const_iterator();
113
114 explicit
115 const_iterator(const NameTree::const_iterator& it);
116
117 ~const_iterator();
118
119 const pit::Entry&
120 operator*() const;
121
122 shared_ptr<pit::Entry>
123 operator->() const;
124
125 const_iterator&
126 operator++();
127
128 const_iterator
129 operator++(int);
130
131 bool
132 operator==(const const_iterator& other) const;
133
134 bool
135 operator!=(const const_iterator& other) const;
136
137 private:
138 NameTree::const_iterator m_nameTreeIterator;
139 /** \brief Index of the current visiting PIT entry in NameTree node
140 *
141 * Index is used to ensure that dereferencing of m_nameTreeIterator happens only when
142 * const_iterator is dereferenced or advanced.
143 */
144 size_t m_iPitEntry;
145 };
146
Junxiao Shicbba04c2014-01-26 14:21:22 -0700147private:
Junxiao Shi02b73f52016-07-28 01:48:27 +0000148 /**
149 * \brief erases a PIT Entry
150 */
151 void
152 erase(shared_ptr<pit::Entry> pitEntry, bool canDeleteNte);
153
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700154 /** \brief finds or inserts a PIT entry for Interest
155 * \param interest the Interest; must be created with make_shared if allowInsert
156 * \param allowInsert whether inserting new entry is allowed.
157 * \return if allowInsert, a new or existing entry with same Name+Selectors,
158 * and true for new entry, false for existing entry;
159 * if not allowInsert, an existing entry with same Name+Selectors and false,
160 * or {nullptr, true} if there's no existing entry
161 */
162 std::pair<shared_ptr<pit::Entry>, bool>
163 findOrInsert(const Interest& interest, bool allowInsert);
164
165private:
Haowei Yuan78c84d12014-02-27 15:35:13 -0600166 NameTree& m_nameTree;
167 size_t m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700168};
169
Haowei Yuan78c84d12014-02-27 15:35:13 -0600170inline size_t
171Pit::size() const
172{
173 return m_nItems;
174}
175
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700176inline shared_ptr<pit::Entry>
177Pit::find(const Interest& interest) const
178{
179 return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
180}
181
182inline std::pair<shared_ptr<pit::Entry>, bool>
183Pit::insert(const Interest& interest)
184{
185 return this->findOrInsert(interest, true);
186}
187
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800188inline Pit::const_iterator
189Pit::end() const
190{
191 return const_iterator(m_nameTree.end());
192}
193
194inline
195Pit::const_iterator::const_iterator()
196 : m_iPitEntry(0)
197{
198}
199
200inline
201Pit::const_iterator::const_iterator(const NameTree::const_iterator& it)
202 : m_nameTreeIterator(it)
203 , m_iPitEntry(0)
204{
205}
206
207inline
208Pit::const_iterator::~const_iterator()
209{
210}
211
212inline Pit::const_iterator
213Pit::const_iterator::operator++(int)
214{
215 Pit::const_iterator temp(*this);
216 ++(*this);
217 return temp;
218}
219
220inline Pit::const_iterator&
221Pit::const_iterator::operator++()
222{
223 ++m_iPitEntry;
224 if (m_iPitEntry < m_nameTreeIterator->getPitEntries().size()) {
225 return *this;
226 }
227
228 ++m_nameTreeIterator;
229 m_iPitEntry = 0;
230 return *this;
231}
232
233inline const pit::Entry&
234Pit::const_iterator::operator*() const
235{
236 return *(this->operator->());
237}
238
239inline shared_ptr<pit::Entry>
240Pit::const_iterator::operator->() const
241{
242 return m_nameTreeIterator->getPitEntries().at(m_iPitEntry);
243}
244
245inline bool
246Pit::const_iterator::operator==(const Pit::const_iterator& other) const
247{
248 return m_nameTreeIterator == other.m_nameTreeIterator &&
249 m_iPitEntry == other.m_iPitEntry;
250}
251
252inline bool
253Pit::const_iterator::operator!=(const Pit::const_iterator& other) const
254{
255 return !(*this == other);
256}
257
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800258} // namespace nfd
Junxiao Shicbba04c2014-01-26 14:21:22 -0700259
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700260#endif // NFD_DAEMON_TABLE_PIT_HPP