blob: eb2438b7c9b535fba63a9556805cca15b03643dd [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Teng Liang43bb2312018-03-26 04:16:42 -07002/*
ashiqopud3ae85d2019-02-17 02:29:55 +00003 * Copyright (c) 2014-2019, 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
29#include "pit-entry.hpp"
Junxiao Shi09cf13c2016-08-15 02:05:00 +000030#include "pit-iterator.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
Davide Pesavento1cb619e2018-04-10 17:13:53 -040035/** \class nfd::pit::DataMatchResult
36 * \brief An unordered iterable of all PIT entries matching Data.
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070037 *
Davide Pesavento1cb619e2018-04-10 17:13:53 -040038 * This type has the following member functions:
Teng Liang43bb2312018-03-26 04:16:42 -070039 * - `iterator<shared_ptr<Entry>> begin()`
40 * - `iterator<shared_ptr<Entry>> end()`
41 * - `size_t size() const`
Junxiao Shicbba04c2014-01-26 14:21:22 -070042 */
Davide Pesavento1cb619e2018-04-10 17:13:53 -040043using DataMatchResult = std::vector<shared_ptr<Entry>>;
Junxiao Shicbba04c2014-01-26 14:21:22 -070044
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070045/** \brief represents the Interest Table
Junxiao Shicbba04c2014-01-26 14:21:22 -070046 */
47class Pit : noncopyable
48{
49public:
Haowei Yuan78c84d12014-02-27 15:35:13 -060050 explicit
51 Pit(NameTree& nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -060052
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070053 /** \return number of entries
Haowei Yuan78c84d12014-02-27 15:35:13 -060054 */
Haowei Yuane1079fc2014-03-08 14:41:25 -060055 size_t
Junxiao Shi09cf13c2016-08-15 02:05:00 +000056 size() const
57 {
58 return m_nItems;
59 }
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 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +000065 shared_ptr<Entry>
66 find(const Interest& interest) const
67 {
68 return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
69 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -070070
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070071 /** \brief inserts a PIT entry for Interest
Junxiao Shi5e5e4452015-09-24 16:56:52 -070072 * \param interest the Interest; must be created with make_shared
73 * \return a new or existing entry with same Name and Selectors,
74 * and true for new entry, false for existing entry
Junxiao Shicbba04c2014-01-26 14:21:22 -070075 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +000076 std::pair<shared_ptr<Entry>, bool>
77 insert(const Interest& interest)
78 {
79 return this->findOrInsert(interest, true);
80 }
Haowei Yuane1079fc2014-03-08 14:41:25 -060081
Junxiao Shicbba04c2014-01-26 14:21:22 -070082 /** \brief performs a Data match
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070083 * \return an iterable of all PIT entries matching data
Junxiao Shicbba04c2014-01-26 14:21:22 -070084 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +000085 DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -070086 findAllDataMatches(const Data& data) const;
Haowei Yuane1079fc2014-03-08 14:41:25 -060087
Junxiao Shidbef6dc2016-08-15 02:58:36 +000088 /** \brief deletes an entry
Haowei Yuane1079fc2014-03-08 14:41:25 -060089 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070090 void
Junxiao Shidbef6dc2016-08-15 02:58:36 +000091 erase(Entry* entry)
Junxiao Shi09cf13c2016-08-15 02:05:00 +000092 {
93 this->erase(entry, true);
94 }
Junxiao Shi02b73f52016-07-28 01:48:27 +000095
ashiqopud3ae85d2019-02-17 02:29:55 +000096 /** \brief deletes all in-records and out-records for \p face
Junxiao Shi02b73f52016-07-28 01:48:27 +000097 */
98 void
ashiqopud3ae85d2019-02-17 02:29:55 +000099 deleteInOutRecordsByFace(Entry* entry, const Face& face);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700100
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800101public: // enumeration
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000102 typedef Iterator const_iterator;
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800103
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000104 /** \return an iterator to the beginning
Junxiao Shib30863e2016-08-15 21:32:01 +0000105 * \note Iteration order is implementation-defined.
106 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
107 * is inserted or erased during enumeration.
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800108 */
109 const_iterator
110 begin() const;
111
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000112 /** \return an iterator to the end
113 * \sa begin()
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800114 */
115 const_iterator
Junxiao Shi32482052016-08-15 02:05:17 +0000116 end() const
117 {
118 return Iterator();
119 }
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800120
Junxiao Shicbba04c2014-01-26 14:21:22 -0700121private:
Junxiao Shi02b73f52016-07-28 01:48:27 +0000122 void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000123 erase(Entry* pitEntry, bool canDeleteNte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000124
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700125 /** \brief finds or inserts a PIT entry for Interest
126 * \param interest the Interest; must be created with make_shared if allowInsert
127 * \param allowInsert whether inserting new entry is allowed.
128 * \return if allowInsert, a new or existing entry with same Name+Selectors,
129 * and true for new entry, false for existing entry;
130 * if not allowInsert, an existing entry with same Name+Selectors and false,
131 * or {nullptr, true} if there's no existing entry
132 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000133 std::pair<shared_ptr<Entry>, bool>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700134 findOrInsert(const Interest& interest, bool allowInsert);
135
136private:
Haowei Yuan78c84d12014-02-27 15:35:13 -0600137 NameTree& m_nameTree;
138 size_t m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700139};
140
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000141} // namespace pit
Haowei Yuan78c84d12014-02-27 15:35:13 -0600142
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000143using pit::Pit;
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800144
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800145} // namespace nfd
Junxiao Shicbba04c2014-01-26 14:21:22 -0700146
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700147#endif // NFD_DAEMON_TABLE_PIT_HPP