Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- 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 Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 8 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 9 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 10 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 11 | Pit::Pit(NameTree& nameTree) : m_nameTree(nameTree), m_nItems(0) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 12 | { |
| 13 | } |
| 14 | |
| 15 | Pit::~Pit() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | static inline bool |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 20 | predicate_NameTreeEntry_hasPitEntry(const name_tree::Entry& entry) |
| 21 | { |
| 22 | return entry.hasPitEntries(); |
| 23 | } |
| 24 | |
| 25 | static inline bool |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 26 | operator==(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 | |
| 34 | static inline bool |
| 35 | predicate_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 | |
| 48 | std::pair<shared_ptr<pit::Entry>, bool> |
| 49 | Pit::insert(const Interest& interest) |
| 50 | { |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 51 | // - first lookup() the Interest Name in the NameTree, which will creates all |
| 52 | // the intermedia nodes, starting from the shortest prefix. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 53 | // - if it is guaranteed that this Interest already has a NameTree Entry, we |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 54 | // could use findExactMatch() instead. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 55 | // - Alternatively, we could try to do findExactMatch() first, if not found, |
| 56 | // then do lookup(). |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 57 | 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 Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | shared_ptr<pit::DataMatchResult> |
| 83 | Pit::findAllDataMatches(const Data& data) const |
| 84 | { |
| 85 | shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 86 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 87 | for (NameTree::const_iterator it = |
| 88 | m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry); |
| 89 | it != m_nameTree.end(); it++) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 90 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 91 | std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 92 | 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 Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 97 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 98 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 99 | return result; |
| 100 | } |
| 101 | |
| 102 | void |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 103 | Pit::erase(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 104 | { |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 105 | // 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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 113 | if (static_cast<bool>(nameTreeEntry)) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 114 | { |
| 115 | nameTreeEntry->erasePitEntry(pitEntry); |
| 116 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
| 117 | |
| 118 | m_nItems--; |
| 119 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 122 | } // namespace nfd |