blob: dab57eae266be17da17a178ea6bd8986e64ed67d [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/*
Davide Pesaventob124b8e2024-02-16 16:54:26 -05003 * Copyright (c) 2014-2024, 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
Davide Pesaventob124b8e2024-02-16 16:54:26 -050029#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
Davide Pesaventob124b8e2024-02-16 16:54:26 -050035/**
36 * \brief An unordered iterable of all PIT entries matching a Data packet.
Junxiao Shicbba04c2014-01-26 14:21:22 -070037 */
Davide Pesavento1cb619e2018-04-10 17:13:53 -040038using DataMatchResult = std::vector<shared_ptr<Entry>>;
Junxiao Shicbba04c2014-01-26 14:21:22 -070039
Davide Pesaventob124b8e2024-02-16 16:54:26 -050040/**
41 * \brief PIT iterator.
42 */
43class Iterator : public boost::forward_iterator_helper<Iterator, const Entry>
44{
45public:
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
76private:
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 Shicbba04c2014-01-26 14:21:22 -070083 */
84class Pit : noncopyable
85{
86public:
Haowei Yuan78c84d12014-02-27 15:35:13 -060087 explicit
88 Pit(NameTree& nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -060089
Davide Pesaventob124b8e2024-02-16 16:54:26 -050090 /**
91 * \brief Returns the number of entries.
Haowei Yuan78c84d12014-02-27 15:35:13 -060092 */
Haowei Yuane1079fc2014-03-08 14:41:25 -060093 size_t
Junxiao Shi09cf13c2016-08-15 02:05:00 +000094 size() const
95 {
96 return m_nItems;
97 }
Haowei Yuane1079fc2014-03-08 14:41:25 -060098
Davide Pesavento50a6af32019-02-21 00:04:40 -050099 /** \brief Finds a PIT entry for \p interest
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700100 * \param interest the Interest
101 * \return an existing entry with same Name and Selectors; otherwise nullptr
102 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000103 shared_ptr<Entry>
104 find(const Interest& interest) const
105 {
106 return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
107 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700108
Davide Pesavento50a6af32019-02-21 00:04:40 -0500109 /** \brief Inserts a PIT entry for \p interest
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700110 * \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 Shicbba04c2014-01-26 14:21:22 -0700113 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000114 std::pair<shared_ptr<Entry>, bool>
115 insert(const Interest& interest)
116 {
117 return this->findOrInsert(interest, true);
118 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600119
Davide Pesavento50a6af32019-02-21 00:04:40 -0500120 /** \brief Performs a Data match
121 * \return an iterable of all PIT entries matching \p data
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000123 DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -0700124 findAllDataMatches(const Data& data) const;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600125
Davide Pesavento50a6af32019-02-21 00:04:40 -0500126 /** \brief Deletes an entry
Haowei Yuane1079fc2014-03-08 14:41:25 -0600127 */
Junxiao Shicbba04c2014-01-26 14:21:22 -0700128 void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000129 erase(Entry* entry)
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000130 {
131 this->erase(entry, true);
132 }
Junxiao Shi02b73f52016-07-28 01:48:27 +0000133
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000134 /** \brief Deletes in-records and out-records for \p face
Junxiao Shi02b73f52016-07-28 01:48:27 +0000135 */
136 void
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000137 deleteInOutRecords(Entry* entry, const Face& face);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700138
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800139public: // enumeration
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400140 using const_iterator = Iterator;
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800141
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000142 /** \return an iterator to the beginning
Junxiao Shib30863e2016-08-15 21:32:01 +0000143 * \note Iteration order is implementation-defined.
144 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
Davide Pesavento50a6af32019-02-21 00:04:40 -0500145 * is inserted or erased during iteration.
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800146 */
147 const_iterator
148 begin() const;
149
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000150 /** \return an iterator to the end
151 * \sa begin()
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800152 */
153 const_iterator
Junxiao Shi32482052016-08-15 02:05:17 +0000154 end() const
155 {
156 return Iterator();
157 }
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800158
Junxiao Shicbba04c2014-01-26 14:21:22 -0700159private:
Junxiao Shi02b73f52016-07-28 01:48:27 +0000160 void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000161 erase(Entry* pitEntry, bool canDeleteNte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000162
Davide Pesavento50a6af32019-02-21 00:04:40 -0500163 /** \brief Finds or inserts a PIT entry for \p interest
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700164 * \param interest the Interest; must be created with make_shared if allowInsert
Davide Pesavento50a6af32019-02-21 00:04:40 -0500165 * \param allowInsert whether inserting a new entry is allowed
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700166 * \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 Pesavento50a6af32019-02-21 00:04:40 -0500169 * or `{nullptr, true}` if there's no existing entry
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700170 */
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000171 std::pair<shared_ptr<Entry>, bool>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172 findOrInsert(const Interest& interest, bool allowInsert);
173
174private:
Haowei Yuan78c84d12014-02-27 15:35:13 -0600175 NameTree& m_nameTree;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500176 size_t m_nItems = 0;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700177};
178
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000179} // namespace pit
Haowei Yuan78c84d12014-02-27 15:35:13 -0600180
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000181using pit::Pit;
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800182
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800183} // namespace nfd
Junxiao Shicbba04c2014-01-26 14:21:22 -0700184
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700185#endif // NFD_DAEMON_TABLE_PIT_HPP