Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 25 | |
| 26 | #include "pit.hpp" |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 28 | namespace nfd::pit { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | f5409d4 | 2016-08-17 02:47:12 +0000 | [diff] [blame] | 30 | static inline bool |
| 31 | nteHasPitEntries(const name_tree::Entry& nte) |
| 32 | { |
| 33 | return nte.hasPitEntries(); |
| 34 | } |
| 35 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 36 | Pit::Pit(NameTree& nameTree) |
| 37 | : m_nameTree(nameTree) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 41 | std::pair<shared_ptr<Entry>, bool> |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 42 | Pit::findOrInsert(const Interest& interest, bool allowInsert) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 43 | { |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 44 | // determine which NameTree entry should the PIT entry be attached onto |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 45 | const Name& name = interest.getName(); |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 46 | bool hasDigest = name.size() > 0 && name[-1].isImplicitSha256Digest(); |
| 47 | size_t nteDepth = name.size() - static_cast<int>(hasDigest); |
| 48 | nteDepth = std::min(nteDepth, NameTree::getMaxDepth()); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 49 | |
| 50 | // ensure NameTree entry exists |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 51 | name_tree::Entry* nte = nullptr; |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 52 | if (allowInsert) { |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 53 | nte = &m_nameTree.lookup(name, nteDepth); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 54 | } |
| 55 | else { |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 56 | nte = m_nameTree.findExactMatch(name, nteDepth); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 57 | if (nte == nullptr) { |
| 58 | return {nullptr, true}; |
| 59 | } |
| 60 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 61 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 62 | // check if PIT entry already exists |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 63 | const auto& pitEntries = nte->getPitEntries(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 64 | auto it = std::find_if(pitEntries.begin(), pitEntries.end(), |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 65 | [&interest, nteDepth] (const shared_ptr<Entry>& entry) { |
| 66 | // NameTree guarantees first nteDepth components are equal |
| 67 | return entry->canMatch(interest, nteDepth); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 68 | }); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 69 | if (it != pitEntries.end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 70 | return {*it, false}; |
| 71 | } |
| 72 | |
| 73 | if (!allowInsert) { |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 74 | BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 75 | return {nullptr, true}; |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 76 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 77 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 78 | auto entry = make_shared<Entry>(interest); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 79 | nte->insertPitEntry(entry); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 80 | ++m_nItems; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 81 | return {entry, true}; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 84 | DataMatchResult |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 85 | Pit::findAllDataMatches(const Data& data) const |
| 86 | { |
Junxiao Shi | f5409d4 | 2016-08-17 02:47:12 +0000 | [diff] [blame] | 87 | auto&& ntMatches = m_nameTree.findAllMatches(data.getName(), &nteHasPitEntries); |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | 09cf13c | 2016-08-15 02:05:00 +0000 | [diff] [blame] | 89 | DataMatchResult matches; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 90 | for (const auto& nte : ntMatches) { |
| 91 | for (const auto& pitEntry : nte.getPitEntries()) { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 92 | if (pitEntry->getInterest().matchesData(data)) |
| 93 | matches.emplace_back(pitEntry); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 94 | } |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 95 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 96 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 97 | return matches; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 101 | Pit::erase(Entry* entry, bool canDeleteNte) |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 102 | { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 103 | name_tree::Entry* nte = m_nameTree.getEntry(*entry); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 104 | BOOST_ASSERT(nte != nullptr); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 105 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 106 | nte->erasePitEntry(entry); |
| 107 | if (canDeleteNte) { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 108 | m_nameTree.eraseIfEmpty(nte); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 109 | } |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 110 | --m_nItems; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 113 | void |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 114 | Pit::deleteInOutRecords(Entry* entry, const Face& face) |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 115 | { |
| 116 | BOOST_ASSERT(entry != nullptr); |
| 117 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 118 | entry->deleteInRecord(face); |
| 119 | entry->deleteOutRecord(face); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 120 | |
| 121 | /// \todo decide whether to delete PIT entry if there's no more in/out-record left |
| 122 | } |
| 123 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 124 | Pit::const_iterator |
| 125 | Pit::begin() const |
| 126 | { |
Junxiao Shi | f5409d4 | 2016-08-17 02:47:12 +0000 | [diff] [blame] | 127 | return const_iterator(m_nameTree.fullEnumerate(&nteHasPitEntries).begin()); |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 130 | } // namespace nfd::pit |