pit.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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  * The University of Memphis.
10  *
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/>.
24  */
25 
26 #include "pit.hpp"
27 
28 namespace nfd::pit {
29 
30 static inline bool
32 {
33  return nte.hasPitEntries();
34 }
35 
36 Pit::Pit(NameTree& nameTree)
37  : m_nameTree(nameTree)
38 {
39 }
40 
41 std::pair<shared_ptr<Entry>, bool>
42 Pit::findOrInsert(const Interest& interest, bool allowInsert)
43 {
44  // determine which NameTree entry should the PIT entry be attached onto
45  const Name& name = interest.getName();
46  bool hasDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
47  size_t nteDepth = name.size() - static_cast<int>(hasDigest);
48  nteDepth = std::min(nteDepth, NameTree::getMaxDepth());
49 
50  // ensure NameTree entry exists
51  name_tree::Entry* nte = nullptr;
52  if (allowInsert) {
53  nte = &m_nameTree.lookup(name, nteDepth);
54  }
55  else {
56  nte = m_nameTree.findExactMatch(name, nteDepth);
57  if (nte == nullptr) {
58  return {nullptr, true};
59  }
60  }
61 
62  // check if PIT entry already exists
63  const auto& pitEntries = nte->getPitEntries();
64  auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
65  [&interest, nteDepth] (const shared_ptr<Entry>& entry) {
66  // NameTree guarantees first nteDepth components are equal
67  return entry->canMatch(interest, nteDepth);
68  });
69  if (it != pitEntries.end()) {
70  return {*it, false};
71  }
72 
73  if (!allowInsert) {
74  BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call
75  return {nullptr, true};
76  }
77 
78  auto entry = make_shared<Entry>(interest);
79  nte->insertPitEntry(entry);
80  ++m_nItems;
81  return {entry, true};
82 }
83 
85 Pit::findAllDataMatches(const Data& data) const
86 {
87  auto&& ntMatches = m_nameTree.findAllMatches(data.getName(), &nteHasPitEntries);
88 
89  DataMatchResult matches;
90  for (const auto& nte : ntMatches) {
91  for (const auto& pitEntry : nte.getPitEntries()) {
92  if (pitEntry->getInterest().matchesData(data))
93  matches.emplace_back(pitEntry);
94  }
95  }
96 
97  return matches;
98 }
99 
100 void
101 Pit::erase(Entry* entry, bool canDeleteNte)
102 {
103  name_tree::Entry* nte = m_nameTree.getEntry(*entry);
104  BOOST_ASSERT(nte != nullptr);
105 
106  nte->erasePitEntry(entry);
107  if (canDeleteNte) {
108  m_nameTree.eraseIfEmpty(nte);
109  }
110  --m_nItems;
111 }
112 
113 void
114 Pit::deleteInOutRecords(Entry* entry, const Face& face)
115 {
116  BOOST_ASSERT(entry != nullptr);
117 
118  entry->deleteInRecord(face);
119  entry->deleteOutRecord(face);
120 
122 }
123 
125 Pit::begin() const
126 {
127  return const_iterator(m_nameTree.fullEnumerate(&nteHasPitEntries).begin());
128 }
129 
130 } // namespace nfd::pit
Generalization of a network interface.
Definition: face.hpp:56
An entry in the name tree.
bool hasPitEntries() const
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)
void erasePitEntry(pit::Entry *pitEntry)
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
bool isEmpty() const
A common index structure for FIB, PIT, StrategyChoice, and Measurements.
Definition: name-tree.hpp:37
Range findAllMatches(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
All-prefixes match lookup.
Definition: name-tree.cpp:212
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
Delete the entry if it is empty.
Definition: name-tree.cpp:122
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
Enumerate all entries.
Definition: name-tree.cpp:225
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51
Entry & lookup(const Name &name, size_t prefixLen)
Find or insert an entry by name.
Definition: name-tree.cpp:43
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:77
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
Exact match lookup.
Definition: name-tree.cpp:149
An unordered iterable of all PIT entries matching Data.
Represents an entry in the Interest table (PIT).
Definition: pit-entry.hpp:62
void deleteInRecord(const Face &face)
Delete the in-record for face if it exists.
Definition: pit-entry.cpp:74
void deleteOutRecord(const Face &face)
Delete the out-record for face if it exists.
Definition: pit-entry.cpp:113
PIT iterator.
const_iterator begin() const
Definition: pit.cpp:125
Pit(NameTree &nameTree)
Definition: pit.cpp:36
DataMatchResult findAllDataMatches(const Data &data) const
Performs a Data match.
Definition: pit.cpp:85
Iterator const_iterator
Definition: pit.hpp:102
void deleteInOutRecords(Entry *entry, const Face &face)
Deletes in-records and out-records for face.
Definition: pit.cpp:114
void erase(Entry *entry)
Deletes an entry.
Definition: pit.hpp:91
Definition: fib.hpp:40
std::vector< shared_ptr< Entry > > DataMatchResult
Definition: pit.hpp:43
static bool nteHasPitEntries(const name_tree::Entry &nte)
Definition: pit.cpp:31