blob: 06dab3e9af5ecb350359d053ed148970d435b73e [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"
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070027#include <type_traits>
Junxiao Shicbba04c2014-01-26 14:21:22 -070028
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -080029#include <boost/concept/assert.hpp>
30#include <boost/concept_check.hpp>
31#include <type_traits>
32
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070034namespace pit {
35
36#if HAVE_IS_MOVE_CONSTRUCTIBLE
37static_assert(std::is_move_constructible<DataMatchResult>::value,
38 "DataMatchResult must be MoveConstructible");
39#endif // HAVE_IS_MOVE_CONSTRUCTIBLE
40
41} // namespace pit
Junxiao Shicbba04c2014-01-26 14:21:22 -070042
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -080043// http://en.cppreference.com/w/cpp/concept/ForwardIterator
44BOOST_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
48static_assert(std::is_default_constructible<Pit::const_iterator>::value,
49 "Pit::const_iterator must be default-constructible");
50#else
51BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Pit::const_iterator>));
52#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
53
Junxiao Shi30d35992014-04-03 14:51:58 -070054Pit::Pit(NameTree& nameTree)
55 : m_nameTree(nameTree)
56 , m_nItems(0)
Junxiao Shicbba04c2014-01-26 14:21:22 -070057{
58}
59
Junxiao Shicbba04c2014-01-26 14:21:22 -070060std::pair<shared_ptr<pit::Entry>, bool>
Junxiao Shi5e5e4452015-09-24 16:56:52 -070061Pit::findOrInsert(const Interest& interest, bool allowInsert)
Junxiao Shicbba04c2014-01-26 14:21:22 -070062{
Junxiao Shi28797792016-05-26 18:10:18 +000063 // determine which NameTree entry should the PIT entry be attached onto
Junxiao Shi4370fde2016-02-24 12:20:46 -070064 const Name& name = interest.getName();
65 bool isEndWithDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
Junxiao Shi28797792016-05-26 18:10:18 +000066 const Name& nteName = isEndWithDigest ? name.getPrefix(-1) : name;
67
68 // ensure NameTree entry exists
Junxiao Shi811c0102016-08-10 04:12:45 +000069 name_tree::Entry* nte = nullptr;
Junxiao Shi28797792016-05-26 18:10:18 +000070 if (allowInsert) {
Junxiao Shi811c0102016-08-10 04:12:45 +000071 nte = m_nameTree.lookup(nteName).get();
Junxiao Shi28797792016-05-26 18:10:18 +000072 BOOST_ASSERT(nte != nullptr);
73 }
74 else {
75 nte = m_nameTree.findExactMatch(nteName);
76 if (nte == nullptr) {
77 return {nullptr, true};
78 }
79 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060080
Junxiao Shi4370fde2016-02-24 12:20:46 -070081 // check if PIT entry already exists
Junxiao Shi28797792016-05-26 18:10:18 +000082 size_t nteNameLen = nteName.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -070083 const std::vector<shared_ptr<pit::Entry>>& pitEntries = nte->getPitEntries();
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070084 auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
Junxiao Shi4370fde2016-02-24 12:20:46 -070085 [&interest, nteNameLen] (const shared_ptr<pit::Entry>& entry) -> bool {
86 // initial part of the name is guaranteed to be the same
87 BOOST_ASSERT(entry->getInterest().getName().compare(0, nteNameLen,
88 interest.getName(), 0, nteNameLen) == 0);
89 // compare implicit digest (or its absence) only
90 return entry->getInterest().getName().compare(nteNameLen, Name::npos,
91 interest.getName(), nteNameLen) == 0 &&
92 entry->getInterest().getSelectors() == interest.getSelectors();
93 });
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070094 if (it != pitEntries.end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070095 return {*it, false};
96 }
97
98 if (!allowInsert) {
Junxiao Shi28797792016-05-26 18:10:18 +000099 BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700100 return {nullptr, true};
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700101 }
Haowei Yuan78c84d12014-02-27 15:35:13 -0600102
Junxiao Shi4370fde2016-02-24 12:20:46 -0700103 auto entry = make_shared<pit::Entry>(interest);
104 nte->insertPitEntry(entry);
Junxiao Shi28797792016-05-26 18:10:18 +0000105 ++m_nItems;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700106 return {entry, true};
Junxiao Shicbba04c2014-01-26 14:21:22 -0700107}
108
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700109pit::DataMatchResult
Junxiao Shicbba04c2014-01-26 14:21:22 -0700110Pit::findAllDataMatches(const Data& data) const
111{
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700112 auto&& ntMatches = m_nameTree.findAllMatches(data.getName(),
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700113 [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); });
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700114
115 pit::DataMatchResult matches;
116 for (const name_tree::Entry& nte : ntMatches) {
117 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700118 if (pitEntry->getInterest().matchesData(data))
119 matches.emplace_back(pitEntry);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700120 }
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700121 }
Haowei Yuan78c84d12014-02-27 15:35:13 -0600122
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700123 return matches;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700124}
125
126void
Junxiao Shi02b73f52016-07-28 01:48:27 +0000127Pit::erase(shared_ptr<pit::Entry> entry)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700128{
Junxiao Shi02b73f52016-07-28 01:48:27 +0000129 this->erase(entry, true);
130}
Haowei Yuan78c84d12014-02-27 15:35:13 -0600131
Junxiao Shi02b73f52016-07-28 01:48:27 +0000132void
133Pit::erase(shared_ptr<pit::Entry> entry, bool canDeleteNte)
134{
135 shared_ptr<name_tree::Entry> nte = m_nameTree.getEntry(*entry);
136 BOOST_ASSERT(nte != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600137
Junxiao Shi02b73f52016-07-28 01:48:27 +0000138 nte->erasePitEntry(entry);
139 if (canDeleteNte) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000140 m_nameTree.eraseIfEmpty(nte.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000141 }
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700142 --m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700143}
144
Junxiao Shi02b73f52016-07-28 01:48:27 +0000145void
146Pit::deleteInOutRecords(shared_ptr<pit::Entry> entry, const Face& face)
147{
148 BOOST_ASSERT(entry != nullptr);
149
150 entry->deleteInRecord(face);
151 entry->deleteOutRecord(face);
152
153 /// \todo decide whether to delete PIT entry if there's no more in/out-record left
154}
155
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800156Pit::const_iterator
157Pit::begin() const
158{
159 return const_iterator(m_nameTree.fullEnumerate(
160 [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }).begin());
161}
162
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800163} // namespace nfd