blob: f2c76841a291b7e4263bcc7cc499db095de7e8be [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi042a3312017-09-15 02:51:20 +00002/*
Davide Pesavento4d2e7882024-02-15 23:00:21 -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/>.
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
26#include "pit.hpp"
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -080027
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040028namespace nfd::pit {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070029
Davide Pesaventob124b8e2024-02-16 16:54:26 -050030Iterator&
31Iterator::operator++()
Junxiao Shif5409d42016-08-17 02:47:12 +000032{
Davide Pesaventob124b8e2024-02-16 16:54:26 -050033 BOOST_ASSERT(m_ntIt != NameTree::const_iterator());
34 BOOST_ASSERT(m_iPitEntry < m_ntIt->getPitEntries().size());
35
36 if (++m_iPitEntry >= m_ntIt->getPitEntries().size()) {
37 ++m_ntIt;
38 m_iPitEntry = 0;
39 BOOST_ASSERT(m_ntIt == NameTree::const_iterator() || m_ntIt->hasPitEntries());
40 }
41 return *this;
Junxiao Shif5409d42016-08-17 02:47:12 +000042}
43
Junxiao Shi30d35992014-04-03 14:51:58 -070044Pit::Pit(NameTree& nameTree)
45 : m_nameTree(nameTree)
Junxiao Shicbba04c2014-01-26 14:21:22 -070046{
47}
48
Junxiao Shi09cf13c2016-08-15 02:05:00 +000049std::pair<shared_ptr<Entry>, bool>
Junxiao Shi5e5e4452015-09-24 16:56:52 -070050Pit::findOrInsert(const Interest& interest, bool allowInsert)
Junxiao Shicbba04c2014-01-26 14:21:22 -070051{
Junxiao Shi28797792016-05-26 18:10:18 +000052 // determine which NameTree entry should the PIT entry be attached onto
Junxiao Shi4370fde2016-02-24 12:20:46 -070053 const Name& name = interest.getName();
Junxiao Shi057d1492018-03-20 17:14:18 +000054 bool hasDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
Davide Pesaventob124b8e2024-02-16 16:54:26 -050055 size_t nteDepth = name.size() - static_cast<size_t>(hasDigest);
Junxiao Shi057d1492018-03-20 17:14:18 +000056 nteDepth = std::min(nteDepth, NameTree::getMaxDepth());
Junxiao Shi28797792016-05-26 18:10:18 +000057
58 // ensure NameTree entry exists
Junxiao Shi811c0102016-08-10 04:12:45 +000059 name_tree::Entry* nte = nullptr;
Junxiao Shi28797792016-05-26 18:10:18 +000060 if (allowInsert) {
Junxiao Shi057d1492018-03-20 17:14:18 +000061 nte = &m_nameTree.lookup(name, nteDepth);
Junxiao Shi28797792016-05-26 18:10:18 +000062 }
63 else {
Junxiao Shi057d1492018-03-20 17:14:18 +000064 nte = m_nameTree.findExactMatch(name, nteDepth);
Junxiao Shi28797792016-05-26 18:10:18 +000065 if (nte == nullptr) {
66 return {nullptr, true};
67 }
68 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060069
Junxiao Shi4370fde2016-02-24 12:20:46 -070070 // check if PIT entry already exists
Junxiao Shi057d1492018-03-20 17:14:18 +000071 const auto& pitEntries = nte->getPitEntries();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070072 auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
Junxiao Shi057d1492018-03-20 17:14:18 +000073 [&interest, nteDepth] (const shared_ptr<Entry>& entry) {
74 // NameTree guarantees first nteDepth components are equal
75 return entry->canMatch(interest, nteDepth);
Junxiao Shi4370fde2016-02-24 12:20:46 -070076 });
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070077 if (it != pitEntries.end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070078 return {*it, false};
79 }
80
81 if (!allowInsert) {
Junxiao Shi28797792016-05-26 18:10:18 +000082 BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call
Junxiao Shi5e5e4452015-09-24 16:56:52 -070083 return {nullptr, true};
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070084 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060085
Junxiao Shi09cf13c2016-08-15 02:05:00 +000086 auto entry = make_shared<Entry>(interest);
Junxiao Shi4370fde2016-02-24 12:20:46 -070087 nte->insertPitEntry(entry);
Junxiao Shi28797792016-05-26 18:10:18 +000088 ++m_nItems;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070089 return {entry, true};
Junxiao Shicbba04c2014-01-26 14:21:22 -070090}
91
Davide Pesaventob124b8e2024-02-16 16:54:26 -050092static bool
93nteHasPitEntries(const name_tree::Entry& nte)
94{
95 return nte.hasPitEntries();
96}
97
Junxiao Shi09cf13c2016-08-15 02:05:00 +000098DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -070099Pit::findAllDataMatches(const Data& data) const
100{
Junxiao Shif5409d42016-08-17 02:47:12 +0000101 auto&& ntMatches = m_nameTree.findAllMatches(data.getName(), &nteHasPitEntries);
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700102
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000103 DataMatchResult matches;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500104 for (const auto& nte : ntMatches) {
105 for (const auto& pitEntry : nte.getPitEntries()) {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700106 if (pitEntry->getInterest().matchesData(data))
107 matches.emplace_back(pitEntry);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700108 }
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700109 }
Haowei Yuan78c84d12014-02-27 15:35:13 -0600110
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700111 return matches;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112}
113
114void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000115Pit::erase(Entry* entry, bool canDeleteNte)
Junxiao Shi02b73f52016-07-28 01:48:27 +0000116{
Junxiao Shi7f358432016-08-11 17:06:33 +0000117 name_tree::Entry* nte = m_nameTree.getEntry(*entry);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000118 BOOST_ASSERT(nte != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600119
Junxiao Shi02b73f52016-07-28 01:48:27 +0000120 nte->erasePitEntry(entry);
121 if (canDeleteNte) {
Junxiao Shi7f358432016-08-11 17:06:33 +0000122 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000123 }
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700124 --m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125}
126
Junxiao Shi02b73f52016-07-28 01:48:27 +0000127void
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000128Pit::deleteInOutRecords(Entry* entry, const Face& face)
Junxiao Shi02b73f52016-07-28 01:48:27 +0000129{
130 BOOST_ASSERT(entry != nullptr);
131
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500132 auto in = entry->findInRecord(face);
Davide Pesavento4d2e7882024-02-15 23:00:21 -0500133 if (in != entry->in_end()) {
134 entry->deleteInRecord(in);
135 }
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000136 entry->deleteOutRecord(face);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000137
138 /// \todo decide whether to delete PIT entry if there's no more in/out-record left
139}
140
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800141Pit::const_iterator
142Pit::begin() const
143{
Junxiao Shif5409d42016-08-17 02:47:12 +0000144 return const_iterator(m_nameTree.fullEnumerate(&nteHasPitEntries).begin());
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800145}
146
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400147} // namespace nfd::pit