blob: dafec47956a69797fe7f52c02a608b24eadc08be [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "pit.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -07008
Alexander Afanasyev18bbf812014-01-29 01:40:23 -08009namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070010
Haowei Yuan78c84d12014-02-27 15:35:13 -060011Pit::Pit(NameTree& nameTree) : m_nameTree(nameTree), m_nItems(0)
Junxiao Shicbba04c2014-01-26 14:21:22 -070012{
13}
14
15Pit::~Pit()
16{
17}
18
19static inline bool
Haowei Yuane1079fc2014-03-08 14:41:25 -060020predicate_NameTreeEntry_hasPitEntry(const name_tree::Entry& entry)
21{
22 return entry.hasPitEntries();
23}
24
25static inline bool
Junxiao Shicbba04c2014-01-26 14:21:22 -070026operator==(const Exclude& a, const Exclude& b)
27{
28 const Block& aBlock = a.wireEncode();
29 const Block& bBlock = b.wireEncode();
30 return aBlock.size() == bBlock.size() &&
31 0 == memcmp(aBlock.wire(), bBlock.wire(), aBlock.size());
32}
33
34static inline bool
35predicate_PitEntry_similar_Interest(shared_ptr<pit::Entry> entry,
36 const Interest& interest)
37{
38 const Interest& pi = entry->getInterest();
39 return pi.getName().equals(interest.getName()) &&
40 pi.getMinSuffixComponents() == interest.getMinSuffixComponents() &&
41 pi.getMaxSuffixComponents() == interest.getMaxSuffixComponents() &&
42 // TODO PublisherPublicKeyLocator (ndn-cpp-dev #1157)
43 pi.getExclude() == interest.getExclude() &&
44 pi.getChildSelector() == interest.getChildSelector() &&
45 pi.getMustBeFresh() == interest.getMustBeFresh();
46}
47
48std::pair<shared_ptr<pit::Entry>, bool>
49Pit::insert(const Interest& interest)
50{
Haowei Yuan78c84d12014-02-27 15:35:13 -060051 // - first lookup() the Interest Name in the NameTree, which will creates all
52 // the intermedia nodes, starting from the shortest prefix.
Haowei Yuane1079fc2014-03-08 14:41:25 -060053 // - if it is guaranteed that this Interest already has a NameTree Entry, we
Haowei Yuan78c84d12014-02-27 15:35:13 -060054 // could use findExactMatch() instead.
Haowei Yuane1079fc2014-03-08 14:41:25 -060055 // - Alternatively, we could try to do findExactMatch() first, if not found,
56 // then do lookup().
Haowei Yuan78c84d12014-02-27 15:35:13 -060057 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName());
58
59 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
60
61 std::vector<shared_ptr<pit::Entry> >& pitEntries = nameTreeEntry->getPitEntries();
62
63 // then check if this Interest is already in the PIT entries
64 std::vector<shared_ptr<pit::Entry> >::iterator it = std::find_if(pitEntries.begin(), pitEntries.end(), bind(&predicate_PitEntry_similar_Interest, _1, interest));
65
66 if (it != pitEntries.end())
67 {
68 return std::make_pair(*it, false);
69 }
70 else
71 {
72 shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest);
73 nameTreeEntry->insertPitEntry(entry);
74
75 // Increase m_nItmes only if we create a new PIT Entry
76 m_nItems++;
77
78 return std::make_pair(entry, true);
79 }
Junxiao Shicbba04c2014-01-26 14:21:22 -070080}
81
82shared_ptr<pit::DataMatchResult>
83Pit::findAllDataMatches(const Data& data) const
84{
85 shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>();
Haowei Yuan78c84d12014-02-27 15:35:13 -060086
Haowei Yuane1079fc2014-03-08 14:41:25 -060087 for (NameTree::const_iterator it =
88 m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry);
89 it != m_nameTree.end(); it++)
Haowei Yuan78c84d12014-02-27 15:35:13 -060090 {
Haowei Yuane1079fc2014-03-08 14:41:25 -060091 std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries();
Haowei Yuan78c84d12014-02-27 15:35:13 -060092 for (size_t i = 0; i < pitEntries.size(); i++)
93 {
94 if (pitEntries[i]->getInterest().matchesName(data.getName()))
95 result->push_back(pitEntries[i]);
96 }
Junxiao Shicbba04c2014-01-26 14:21:22 -070097 }
Haowei Yuan78c84d12014-02-27 15:35:13 -060098
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 return result;
100}
101
102void
Haowei Yuan78c84d12014-02-27 15:35:13 -0600103Pit::erase(shared_ptr<pit::Entry> pitEntry)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700104{
Haowei Yuan78c84d12014-02-27 15:35:13 -0600105 // first get the NPE
106 // If pit-entry.hpp stores a NameTree Entry for each PIT, we could also use the get() method
107 // directly, saving one hash computation.
108 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(pitEntry->getName());
109
110 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
111
112 // erase this PIT entry
Haowei Yuane1079fc2014-03-08 14:41:25 -0600113 if (static_cast<bool>(nameTreeEntry))
Haowei Yuan78c84d12014-02-27 15:35:13 -0600114 {
115 nameTreeEntry->erasePitEntry(pitEntry);
116 m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
117
118 m_nItems--;
119 }
Junxiao Shicbba04c2014-01-26 14:21:22 -0700120}
121
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800122} // namespace nfd