blob: 8698da88fdcf38008a104688a4873b9a8c0377c6 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shicbba04c2014-01-26 14:21:22 -070024
25#include "pit.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070026
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080027namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070028
Haowei Yuan78c84d12014-02-27 15:35:13 -060029Pit::Pit(NameTree& nameTree) : m_nameTree(nameTree), m_nItems(0)
Junxiao Shicbba04c2014-01-26 14:21:22 -070030{
31}
32
33Pit::~Pit()
34{
35}
36
37static inline bool
Haowei Yuane1079fc2014-03-08 14:41:25 -060038predicate_NameTreeEntry_hasPitEntry(const name_tree::Entry& entry)
39{
40 return entry.hasPitEntries();
41}
42
43static inline bool
Junxiao Shicbba04c2014-01-26 14:21:22 -070044operator==(const Exclude& a, const Exclude& b)
45{
46 const Block& aBlock = a.wireEncode();
47 const Block& bBlock = b.wireEncode();
48 return aBlock.size() == bBlock.size() &&
49 0 == memcmp(aBlock.wire(), bBlock.wire(), aBlock.size());
50}
51
52static inline bool
53predicate_PitEntry_similar_Interest(shared_ptr<pit::Entry> entry,
54 const Interest& interest)
55{
56 const Interest& pi = entry->getInterest();
57 return pi.getName().equals(interest.getName()) &&
58 pi.getMinSuffixComponents() == interest.getMinSuffixComponents() &&
59 pi.getMaxSuffixComponents() == interest.getMaxSuffixComponents() &&
60 // TODO PublisherPublicKeyLocator (ndn-cpp-dev #1157)
61 pi.getExclude() == interest.getExclude() &&
62 pi.getChildSelector() == interest.getChildSelector() &&
63 pi.getMustBeFresh() == interest.getMustBeFresh();
64}
65
66std::pair<shared_ptr<pit::Entry>, bool>
67Pit::insert(const Interest& interest)
68{
Haowei Yuan78c84d12014-02-27 15:35:13 -060069 // - first lookup() the Interest Name in the NameTree, which will creates all
70 // the intermedia nodes, starting from the shortest prefix.
Haowei Yuane1079fc2014-03-08 14:41:25 -060071 // - if it is guaranteed that this Interest already has a NameTree Entry, we
Haowei Yuan78c84d12014-02-27 15:35:13 -060072 // could use findExactMatch() instead.
Haowei Yuane1079fc2014-03-08 14:41:25 -060073 // - Alternatively, we could try to do findExactMatch() first, if not found,
74 // then do lookup().
Haowei Yuan78c84d12014-02-27 15:35:13 -060075 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName());
76
77 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
78
79 std::vector<shared_ptr<pit::Entry> >& pitEntries = nameTreeEntry->getPitEntries();
80
81 // then check if this Interest is already in the PIT entries
82 std::vector<shared_ptr<pit::Entry> >::iterator it = std::find_if(pitEntries.begin(), pitEntries.end(), bind(&predicate_PitEntry_similar_Interest, _1, interest));
83
84 if (it != pitEntries.end())
85 {
86 return std::make_pair(*it, false);
87 }
88 else
89 {
90 shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest);
91 nameTreeEntry->insertPitEntry(entry);
92
93 // Increase m_nItmes only if we create a new PIT Entry
94 m_nItems++;
95
96 return std::make_pair(entry, true);
97 }
Junxiao Shicbba04c2014-01-26 14:21:22 -070098}
99
100shared_ptr<pit::DataMatchResult>
101Pit::findAllDataMatches(const Data& data) const
102{
103 shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>();
Haowei Yuan78c84d12014-02-27 15:35:13 -0600104
Haowei Yuane1079fc2014-03-08 14:41:25 -0600105 for (NameTree::const_iterator it =
106 m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry);
107 it != m_nameTree.end(); it++)
Haowei Yuan78c84d12014-02-27 15:35:13 -0600108 {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600109 std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries();
Haowei Yuan78c84d12014-02-27 15:35:13 -0600110 for (size_t i = 0; i < pitEntries.size(); i++)
111 {
112 if (pitEntries[i]->getInterest().matchesName(data.getName()))
113 result->push_back(pitEntries[i]);
114 }
Junxiao Shicbba04c2014-01-26 14:21:22 -0700115 }
Haowei Yuan78c84d12014-02-27 15:35:13 -0600116
Junxiao Shicbba04c2014-01-26 14:21:22 -0700117 return result;
118}
119
120void
Haowei Yuan78c84d12014-02-27 15:35:13 -0600121Pit::erase(shared_ptr<pit::Entry> pitEntry)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122{
Haowei Yuan78c84d12014-02-27 15:35:13 -0600123 // first get the NPE
124 // If pit-entry.hpp stores a NameTree Entry for each PIT, we could also use the get() method
125 // directly, saving one hash computation.
126 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(pitEntry->getName());
127
128 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
129
130 // erase this PIT entry
Haowei Yuane1079fc2014-03-08 14:41:25 -0600131 if (static_cast<bool>(nameTreeEntry))
Haowei Yuan78c84d12014-02-27 15:35:13 -0600132 {
133 nameTreeEntry->erasePitEntry(pitEntry);
134 m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
135
136 m_nItems--;
137 }
Junxiao Shicbba04c2014-01-26 14:21:22 -0700138}
139
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800140} // namespace nfd