pit.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "pit.hpp"
27 
28 namespace nfd {
29 namespace pit {
30 
31 static inline bool
33 {
34  return nte.hasPitEntries();
35 }
36 
37 Pit::Pit(NameTree& nameTree)
38  : m_nameTree(nameTree)
39  , m_nItems(0)
40 {
41 }
42 
43 std::pair<shared_ptr<Entry>, bool>
44 Pit::findOrInsert(const Interest& interest, bool allowInsert)
45 {
46  // determine which NameTree entry should the PIT entry be attached onto
47  const Name& name = interest.getName();
48  bool isEndWithDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
49  const Name& nteName = isEndWithDigest ? name.getPrefix(-1) : name;
50 
51  // ensure NameTree entry exists
52  name_tree::Entry* nte = nullptr;
53  if (allowInsert) {
54  nte = &m_nameTree.lookup(nteName);
55  }
56  else {
57  nte = m_nameTree.findExactMatch(nteName);
58  if (nte == nullptr) {
59  return {nullptr, true};
60  }
61  }
62 
63  // check if PIT entry already exists
64  size_t nteNameLen = nteName.size();
65  const std::vector<shared_ptr<Entry>>& pitEntries = nte->getPitEntries();
66  auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
67  [&interest, nteNameLen] (const shared_ptr<Entry>& entry) -> bool {
68  // initial part of the name is guaranteed to be the same
69  BOOST_ASSERT(entry->getInterest().getName().compare(0, nteNameLen,
70  interest.getName(), 0, nteNameLen) == 0);
71  // compare implicit digest (or its absence) only
72  return entry->getInterest().getName().compare(nteNameLen, Name::npos,
73  interest.getName(), nteNameLen) == 0 &&
74  entry->getInterest().getSelectors() == interest.getSelectors();
75  });
76  if (it != pitEntries.end()) {
77  return {*it, false};
78  }
79 
80  if (!allowInsert) {
81  BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call
82  return {nullptr, true};
83  }
84 
85  auto entry = make_shared<Entry>(interest);
86  nte->insertPitEntry(entry);
87  ++m_nItems;
88  return {entry, true};
89 }
90 
92 Pit::findAllDataMatches(const Data& data) const
93 {
94  auto&& ntMatches = m_nameTree.findAllMatches(data.getName(), &nteHasPitEntries);
95 
96  DataMatchResult matches;
97  for (const name_tree::Entry& nte : ntMatches) {
98  for (const shared_ptr<Entry>& pitEntry : nte.getPitEntries()) {
99  if (pitEntry->getInterest().matchesData(data))
100  matches.emplace_back(pitEntry);
101  }
102  }
103 
104  return matches;
105 }
106 
107 void
108 Pit::erase(Entry* entry, bool canDeleteNte)
109 {
110  name_tree::Entry* nte = m_nameTree.getEntry(*entry);
111  BOOST_ASSERT(nte != nullptr);
112 
113  nte->erasePitEntry(entry);
114  if (canDeleteNte) {
115  m_nameTree.eraseIfEmpty(nte);
116  }
117  --m_nItems;
118 }
119 
120 void
121 Pit::deleteInOutRecords(Entry* entry, const Face& face)
122 {
123  BOOST_ASSERT(entry != nullptr);
124 
125  entry->deleteInRecord(face);
126  entry->deleteOutRecord(face);
127 
129 }
130 
132 Pit::begin() const
133 {
134  return const_iterator(m_nameTree.fullEnumerate(&nteHasPitEntries).begin());
135 }
136 
137 } // namespace pit
138 } // namespace nfd
const_iterator begin() const
Definition: pit.cpp:132
void deleteInRecord(const Face &face)
delete the in-record for face if it exists
Definition: pit-entry.cpp:60
bool isEmpty() const
an unordered iterable of all PIT entries matching Data
void erasePitEntry(pit::Entry *pitEntry)
PIT iterator.
void deleteOutRecord(const Face &face)
delete the out-record for face if it exists
Definition: pit-entry.cpp:97
Pit(NameTree &nameTree)
Definition: pit.cpp:37
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
an Interest table entry
Definition: pit-entry.hpp:57
Iterator const_iterator
Definition: pit.hpp:101
bool hasPitEntries() const
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void deleteInOutRecords(Entry *entry, const Face &face)
deletes in-record and out-record for face
Definition: pit.cpp:121
an entry in the name tree
static bool nteHasPitEntries(const name_tree::Entry &nte)
Definition: pit.cpp:32
DataMatchResult findAllDataMatches(const Data &data) const
performs a Data match
Definition: pit.cpp:92
void erase(Entry *entry)
deletes an entry
Definition: pit.hpp:90
std::vector< shared_ptr< Entry > > DataMatchResult
Definition: pit.hpp:42
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)