blob: 91bc5a85a25fb046c4479d47cee91b8760156f2c [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, 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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028namespace nfd {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070029namespace pit {
30
Junxiao Shi30d35992014-04-03 14:51:58 -070031Pit::Pit(NameTree& nameTree)
32 : m_nameTree(nameTree)
33 , m_nItems(0)
Junxiao Shicbba04c2014-01-26 14:21:22 -070034{
35}
36
Junxiao Shi09cf13c2016-08-15 02:05:00 +000037std::pair<shared_ptr<Entry>, bool>
Junxiao Shi5e5e4452015-09-24 16:56:52 -070038Pit::findOrInsert(const Interest& interest, bool allowInsert)
Junxiao Shicbba04c2014-01-26 14:21:22 -070039{
Junxiao Shi28797792016-05-26 18:10:18 +000040 // determine which NameTree entry should the PIT entry be attached onto
Junxiao Shi4370fde2016-02-24 12:20:46 -070041 const Name& name = interest.getName();
42 bool isEndWithDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
Junxiao Shi28797792016-05-26 18:10:18 +000043 const Name& nteName = isEndWithDigest ? name.getPrefix(-1) : name;
44
45 // ensure NameTree entry exists
Junxiao Shi811c0102016-08-10 04:12:45 +000046 name_tree::Entry* nte = nullptr;
Junxiao Shi28797792016-05-26 18:10:18 +000047 if (allowInsert) {
Junxiao Shi7f358432016-08-11 17:06:33 +000048 nte = &m_nameTree.lookup(nteName);
Junxiao Shi28797792016-05-26 18:10:18 +000049 }
50 else {
51 nte = m_nameTree.findExactMatch(nteName);
52 if (nte == nullptr) {
53 return {nullptr, true};
54 }
55 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060056
Junxiao Shi4370fde2016-02-24 12:20:46 -070057 // check if PIT entry already exists
Junxiao Shi28797792016-05-26 18:10:18 +000058 size_t nteNameLen = nteName.size();
Junxiao Shi09cf13c2016-08-15 02:05:00 +000059 const std::vector<shared_ptr<Entry>>& pitEntries = nte->getPitEntries();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070060 auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
Junxiao Shi09cf13c2016-08-15 02:05:00 +000061 [&interest, nteNameLen] (const shared_ptr<Entry>& entry) -> bool {
Junxiao Shi4370fde2016-02-24 12:20:46 -070062 // initial part of the name is guaranteed to be the same
63 BOOST_ASSERT(entry->getInterest().getName().compare(0, nteNameLen,
64 interest.getName(), 0, nteNameLen) == 0);
65 // compare implicit digest (or its absence) only
66 return entry->getInterest().getName().compare(nteNameLen, Name::npos,
67 interest.getName(), nteNameLen) == 0 &&
68 entry->getInterest().getSelectors() == interest.getSelectors();
69 });
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070070 if (it != pitEntries.end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070071 return {*it, false};
72 }
73
74 if (!allowInsert) {
Junxiao Shi28797792016-05-26 18:10:18 +000075 BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call
Junxiao Shi5e5e4452015-09-24 16:56:52 -070076 return {nullptr, true};
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070077 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060078
Junxiao Shi09cf13c2016-08-15 02:05:00 +000079 auto entry = make_shared<Entry>(interest);
Junxiao Shi4370fde2016-02-24 12:20:46 -070080 nte->insertPitEntry(entry);
Junxiao Shi28797792016-05-26 18:10:18 +000081 ++m_nItems;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082 return {entry, true};
Junxiao Shicbba04c2014-01-26 14:21:22 -070083}
84
Junxiao Shi09cf13c2016-08-15 02:05:00 +000085DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -070086Pit::findAllDataMatches(const Data& data) const
87{
Junxiao Shi2b73ca32014-11-17 19:16:08 -070088 auto&& ntMatches = m_nameTree.findAllMatches(data.getName(),
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070089 [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); });
Junxiao Shi2b73ca32014-11-17 19:16:08 -070090
Junxiao Shi09cf13c2016-08-15 02:05:00 +000091 DataMatchResult matches;
Junxiao Shi2b73ca32014-11-17 19:16:08 -070092 for (const name_tree::Entry& nte : ntMatches) {
Junxiao Shi09cf13c2016-08-15 02:05:00 +000093 for (const shared_ptr<Entry>& pitEntry : nte.getPitEntries()) {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070094 if (pitEntry->getInterest().matchesData(data))
95 matches.emplace_back(pitEntry);
Junxiao Shicbba04c2014-01-26 14:21:22 -070096 }
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070097 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060098
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070099 return matches;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700100}
101
102void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000103Pit::erase(Entry* entry, bool canDeleteNte)
Junxiao Shi02b73f52016-07-28 01:48:27 +0000104{
Junxiao Shi7f358432016-08-11 17:06:33 +0000105 name_tree::Entry* nte = m_nameTree.getEntry(*entry);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000106 BOOST_ASSERT(nte != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600107
Junxiao Shi02b73f52016-07-28 01:48:27 +0000108 nte->erasePitEntry(entry);
109 if (canDeleteNte) {
Junxiao Shi7f358432016-08-11 17:06:33 +0000110 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000111 }
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700112 --m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700113}
114
Junxiao Shi02b73f52016-07-28 01:48:27 +0000115void
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000116Pit::deleteInOutRecords(shared_ptr<Entry> entry, const Face& face)
Junxiao Shi02b73f52016-07-28 01:48:27 +0000117{
118 BOOST_ASSERT(entry != nullptr);
119
120 entry->deleteInRecord(face);
121 entry->deleteOutRecord(face);
122
123 /// \todo decide whether to delete PIT entry if there's no more in/out-record left
124}
125
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800126Pit::const_iterator
127Pit::begin() const
128{
129 return const_iterator(m_nameTree.fullEnumerate(
130 [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }).begin());
131}
132
Junxiao Shi09cf13c2016-08-15 02:05:00 +0000133} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800134} // namespace nfd