Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 27 | #include <type_traits> |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 29 | #include <boost/concept/assert.hpp> |
| 30 | #include <boost/concept_check.hpp> |
| 31 | #include <type_traits> |
| 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 34 | namespace pit { |
| 35 | |
| 36 | #if HAVE_IS_MOVE_CONSTRUCTIBLE |
| 37 | static_assert(std::is_move_constructible<DataMatchResult>::value, |
| 38 | "DataMatchResult must be MoveConstructible"); |
| 39 | #endif // HAVE_IS_MOVE_CONSTRUCTIBLE |
| 40 | |
| 41 | } // namespace pit |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 43 | // http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 44 | BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Pit::const_iterator>)); |
| 45 | // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html, |
| 46 | // which doesn't require DefaultConstructible |
| 47 | #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 48 | static_assert(std::is_default_constructible<Pit::const_iterator>::value, |
| 49 | "Pit::const_iterator must be default-constructible"); |
| 50 | #else |
| 51 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Pit::const_iterator>)); |
| 52 | #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 53 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 54 | Pit::Pit(NameTree& nameTree) |
| 55 | : m_nameTree(nameTree) |
| 56 | , m_nItems(0) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 60 | std::pair<shared_ptr<pit::Entry>, bool> |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 61 | Pit::findOrInsert(const Interest& interest, bool allowInsert) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 62 | { |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 63 | // determine which NameTree entry should the PIT entry be attached onto |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 64 | const Name& name = interest.getName(); |
| 65 | bool isEndWithDigest = name.size() > 0 && name[-1].isImplicitSha256Digest(); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 66 | const Name& nteName = isEndWithDigest ? name.getPrefix(-1) : name; |
| 67 | |
| 68 | // ensure NameTree entry exists |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 69 | name_tree::Entry* nte = nullptr; |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 70 | if (allowInsert) { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 71 | nte = &m_nameTree.lookup(nteName); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 72 | } |
| 73 | else { |
| 74 | nte = m_nameTree.findExactMatch(nteName); |
| 75 | if (nte == nullptr) { |
| 76 | return {nullptr, true}; |
| 77 | } |
| 78 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 79 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 80 | // check if PIT entry already exists |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 81 | size_t nteNameLen = nteName.size(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 82 | const std::vector<shared_ptr<pit::Entry>>& pitEntries = nte->getPitEntries(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 83 | auto it = std::find_if(pitEntries.begin(), pitEntries.end(), |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 84 | [&interest, nteNameLen] (const shared_ptr<pit::Entry>& entry) -> bool { |
| 85 | // initial part of the name is guaranteed to be the same |
| 86 | BOOST_ASSERT(entry->getInterest().getName().compare(0, nteNameLen, |
| 87 | interest.getName(), 0, nteNameLen) == 0); |
| 88 | // compare implicit digest (or its absence) only |
| 89 | return entry->getInterest().getName().compare(nteNameLen, Name::npos, |
| 90 | interest.getName(), nteNameLen) == 0 && |
| 91 | entry->getInterest().getSelectors() == interest.getSelectors(); |
| 92 | }); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 93 | if (it != pitEntries.end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 94 | return {*it, false}; |
| 95 | } |
| 96 | |
| 97 | if (!allowInsert) { |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 98 | BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 99 | return {nullptr, true}; |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 100 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 101 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 102 | auto entry = make_shared<pit::Entry>(interest); |
| 103 | nte->insertPitEntry(entry); |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 104 | ++m_nItems; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 105 | return {entry, true}; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 108 | pit::DataMatchResult |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 109 | Pit::findAllDataMatches(const Data& data) const |
| 110 | { |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 111 | auto&& ntMatches = m_nameTree.findAllMatches(data.getName(), |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 112 | [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }); |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 113 | |
| 114 | pit::DataMatchResult matches; |
| 115 | for (const name_tree::Entry& nte : ntMatches) { |
| 116 | for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 117 | if (pitEntry->getInterest().matchesData(data)) |
| 118 | matches.emplace_back(pitEntry); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 119 | } |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 120 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 121 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 122 | return matches; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 126 | Pit::erase(shared_ptr<pit::Entry> entry) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 127 | { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 128 | this->erase(entry, true); |
| 129 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 130 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 131 | void |
| 132 | Pit::erase(shared_ptr<pit::Entry> entry, bool canDeleteNte) |
| 133 | { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 134 | name_tree::Entry* nte = m_nameTree.getEntry(*entry); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 135 | BOOST_ASSERT(nte != nullptr); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 136 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 137 | nte->erasePitEntry(entry); |
| 138 | if (canDeleteNte) { |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 139 | m_nameTree.eraseIfEmpty(nte); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 140 | } |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 141 | --m_nItems; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 144 | void |
| 145 | Pit::deleteInOutRecords(shared_ptr<pit::Entry> entry, const Face& face) |
| 146 | { |
| 147 | BOOST_ASSERT(entry != nullptr); |
| 148 | |
| 149 | entry->deleteInRecord(face); |
| 150 | entry->deleteOutRecord(face); |
| 151 | |
| 152 | /// \todo decide whether to delete PIT entry if there's no more in/out-record left |
| 153 | } |
| 154 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 155 | Pit::const_iterator |
| 156 | Pit::begin() const |
| 157 | { |
| 158 | return const_iterator(m_nameTree.fullEnumerate( |
| 159 | [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }).begin()); |
| 160 | } |
| 161 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 162 | } // namespace nfd |