blob: adab0855da5d7c860f69ae3fabc62289bef5b386 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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 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 Shicbba04c2014-01-26 14:21:22 -070054 ~Pit();
Haowei Yuan78c84d12014-02-27 15:35:13 -060055
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070056 /** \return number of entries
Haowei Yuan78c84d12014-02-27 15:35:13 -060057 */
Haowei Yuane1079fc2014-03-08 14:41:25 -060058 size_t
Haowei Yuan78c84d12014-02-27 15:35:13 -060059 size() const;
Haowei Yuane1079fc2014-03-08 14:41:25 -060060
Junxiao Shi5e5e4452015-09-24 16:56:52 -070061 /** \brief finds a PIT entry for Interest
62 * \param interest the Interest
63 * \return an existing entry with same Name and Selectors; otherwise nullptr
64 */
65 shared_ptr<pit::Entry>
66 find(const Interest& interest) const;
67
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070068 /** \brief inserts a PIT entry for Interest
Junxiao Shi5e5e4452015-09-24 16:56:52 -070069 * \param interest the Interest; must be created with make_shared
70 * \return a new or existing entry with same Name and Selectors,
71 * and true for new entry, false for existing entry
Junxiao Shicbba04c2014-01-26 14:21:22 -070072 */
73 std::pair<shared_ptr<pit::Entry>, bool>
74 insert(const Interest& interest);
Haowei Yuane1079fc2014-03-08 14:41:25 -060075
Junxiao Shicbba04c2014-01-26 14:21:22 -070076 /** \brief performs a Data match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070077 * \return an iterable of all PIT entries matching data
Junxiao Shicbba04c2014-01-26 14:21:22 -070078 */
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070079 pit::DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -070080 findAllDataMatches(const Data& data) const;
Haowei Yuane1079fc2014-03-08 14:41:25 -060081
Haowei Yuan78c84d12014-02-27 15:35:13 -060082 /**
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070083 * \brief erases a PIT Entry
Haowei Yuane1079fc2014-03-08 14:41:25 -060084 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070085 void
Haowei Yuan78c84d12014-02-27 15:35:13 -060086 erase(shared_ptr<pit::Entry> pitEntry);
Junxiao Shicbba04c2014-01-26 14:21:22 -070087
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -080088public: // enumeration
89 class const_iterator;
90
91 /** \brief returns an iterator pointing to the first PIT entry
92 * \note Iteration order is implementation-specific and is undefined
93 * \note The returned iterator may get invalidated if PIT or another NameTree-based
94 * table is modified
95 */
96 const_iterator
97 begin() const;
98
99 /** \brief returns an iterator referring to the past-the-end PIT entry
100 * \note The returned iterator may get invalidated if PIT or another NameTree-based
101 * table is modified
102 */
103 const_iterator
104 end() const;
105
106 class const_iterator : public std::iterator<std::forward_iterator_tag, const pit::Entry>
107 {
108 public:
109 const_iterator();
110
111 explicit
112 const_iterator(const NameTree::const_iterator& it);
113
114 ~const_iterator();
115
116 const pit::Entry&
117 operator*() const;
118
119 shared_ptr<pit::Entry>
120 operator->() const;
121
122 const_iterator&
123 operator++();
124
125 const_iterator
126 operator++(int);
127
128 bool
129 operator==(const const_iterator& other) const;
130
131 bool
132 operator!=(const const_iterator& other) const;
133
134 private:
135 NameTree::const_iterator m_nameTreeIterator;
136 /** \brief Index of the current visiting PIT entry in NameTree node
137 *
138 * Index is used to ensure that dereferencing of m_nameTreeIterator happens only when
139 * const_iterator is dereferenced or advanced.
140 */
141 size_t m_iPitEntry;
142 };
143
Junxiao Shicbba04c2014-01-26 14:21:22 -0700144private:
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700145 /** \brief finds or inserts a PIT entry for Interest
146 * \param interest the Interest; must be created with make_shared if allowInsert
147 * \param allowInsert whether inserting new entry is allowed.
148 * \return if allowInsert, a new or existing entry with same Name+Selectors,
149 * and true for new entry, false for existing entry;
150 * if not allowInsert, an existing entry with same Name+Selectors and false,
151 * or {nullptr, true} if there's no existing entry
152 */
153 std::pair<shared_ptr<pit::Entry>, bool>
154 findOrInsert(const Interest& interest, bool allowInsert);
155
156private:
Haowei Yuan78c84d12014-02-27 15:35:13 -0600157 NameTree& m_nameTree;
158 size_t m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700159};
160
Haowei Yuan78c84d12014-02-27 15:35:13 -0600161inline size_t
162Pit::size() const
163{
164 return m_nItems;
165}
166
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700167inline shared_ptr<pit::Entry>
168Pit::find(const Interest& interest) const
169{
170 return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
171}
172
173inline std::pair<shared_ptr<pit::Entry>, bool>
174Pit::insert(const Interest& interest)
175{
176 return this->findOrInsert(interest, true);
177}
178
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800179inline Pit::const_iterator
180Pit::end() const
181{
182 return const_iterator(m_nameTree.end());
183}
184
185inline
186Pit::const_iterator::const_iterator()
187 : m_iPitEntry(0)
188{
189}
190
191inline
192Pit::const_iterator::const_iterator(const NameTree::const_iterator& it)
193 : m_nameTreeIterator(it)
194 , m_iPitEntry(0)
195{
196}
197
198inline
199Pit::const_iterator::~const_iterator()
200{
201}
202
203inline Pit::const_iterator
204Pit::const_iterator::operator++(int)
205{
206 Pit::const_iterator temp(*this);
207 ++(*this);
208 return temp;
209}
210
211inline Pit::const_iterator&
212Pit::const_iterator::operator++()
213{
214 ++m_iPitEntry;
215 if (m_iPitEntry < m_nameTreeIterator->getPitEntries().size()) {
216 return *this;
217 }
218
219 ++m_nameTreeIterator;
220 m_iPitEntry = 0;
221 return *this;
222}
223
224inline const pit::Entry&
225Pit::const_iterator::operator*() const
226{
227 return *(this->operator->());
228}
229
230inline shared_ptr<pit::Entry>
231Pit::const_iterator::operator->() const
232{
233 return m_nameTreeIterator->getPitEntries().at(m_iPitEntry);
234}
235
236inline bool
237Pit::const_iterator::operator==(const Pit::const_iterator& other) const
238{
239 return m_nameTreeIterator == other.m_nameTreeIterator &&
240 m_iPitEntry == other.m_iPitEntry;
241}
242
243inline bool
244Pit::const_iterator::operator!=(const Pit::const_iterator& other) const
245{
246 return !(*this == other);
247}
248
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800249} // namespace nfd
Junxiao Shicbba04c2014-01-26 14:21:22 -0700250
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700251#endif // NFD_DAEMON_TABLE_PIT_HPP