NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
name-tree.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "name-tree.hpp"
27 #include "core/logger.hpp"
28 
29 #include <boost/concept/assert.hpp>
30 #include <boost/concept_check.hpp>
31 #include <type_traits>
32 
33 namespace nfd {
34 namespace name_tree {
35 
36 NFD_LOG_INIT("NameTree");
37 
38 NameTree::NameTree(size_t nBuckets)
39  : m_ht(HashtableOptions(nBuckets))
40 {
41 }
42 
43 Entry&
45 {
46  NFD_LOG_TRACE("lookup " << name);
47 
48  HashSequence hashes = computeHashes(name);
49  const Node* node = nullptr;
50  Entry* parent = nullptr;
51 
52  for (size_t prefixLen = 0; prefixLen <= name.size(); ++prefixLen) {
53  bool isNew = false;
54  std::tie(node, isNew) = m_ht.insert(name, prefixLen, hashes);
55 
56  if (isNew && parent != nullptr) {
57  node->entry.setParent(*parent);
58  }
59  parent = &node->entry;
60  }
61  return node->entry;
62 }
63 
64 Entry&
65 NameTree::lookup(const fib::Entry& fibEntry)
66 {
67  Entry* nte = this->getEntry(fibEntry);
68  if (nte == nullptr) {
69  // special case: Fib::s_emptyEntry is unattached
70  BOOST_ASSERT(fibEntry.getPrefix().empty());
71  return this->lookup(fibEntry.getPrefix());
72  }
73 
74  BOOST_ASSERT(nte->getFibEntry() == &fibEntry);
75  return *nte;
76 }
77 
78 Entry&
79 NameTree::lookup(const pit::Entry& pitEntry)
80 {
81  Entry* nte = this->getEntry(pitEntry);
82  BOOST_ASSERT(nte != nullptr);
83 
84  BOOST_ASSERT(std::count_if(nte->getPitEntries().begin(), nte->getPitEntries().end(),
85  [&pitEntry] (const shared_ptr<pit::Entry>& pitEntry1) {
86  return pitEntry1.get() == &pitEntry;
87  }) == 1);
88 
89  if (nte->getName().size() == pitEntry.getName().size()) {
90  return *nte;
91  }
92 
93  // special case: PIT entry whose Interest name ends with an implicit digest
94  // are attached to the name tree entry with one-shorter-prefix.
95  BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest());
96  BOOST_ASSERT(nte->getName() == pitEntry.getName().getPrefix(-1));
97  return this->lookup(pitEntry.getName());
98 }
99 
100 Entry&
101 NameTree::lookup(const measurements::Entry& measurementsEntry)
102 {
103  Entry* nte = this->getEntry(measurementsEntry);
104  BOOST_ASSERT(nte != nullptr);
105 
106  BOOST_ASSERT(nte->getMeasurementsEntry() == &measurementsEntry);
107  return *nte;
108 }
109 
110 Entry&
111 NameTree::lookup(const strategy_choice::Entry& strategyChoiceEntry)
112 {
113  Entry* nte = this->getEntry(strategyChoiceEntry);
114  BOOST_ASSERT(nte != nullptr);
115 
116  BOOST_ASSERT(nte->getStrategyChoiceEntry() == &strategyChoiceEntry);
117  return *nte;
118 }
119 
120 size_t
121 NameTree::eraseIfEmpty(Entry* entry, bool canEraseAncestors)
122 {
123  BOOST_ASSERT(entry != nullptr);
124 
125  size_t nErased = 0;
126  for (Entry* parent = nullptr; entry != nullptr && entry->isEmpty(); entry = parent) {
127  parent = entry->getParent();
128 
129  if (parent != nullptr) {
130  entry->unsetParent();
131  }
132 
133  m_ht.erase(getNode(*entry));
134  ++nErased;
135 
136  if (!canEraseAncestors) {
137  break;
138  }
139  }
140 
141  if (nErased == 0) {
142  NFD_LOG_TRACE("not-erase " << entry->getName());
143  }
144  return nErased;
145 }
146 
147 Entry*
149 {
150  const Node* node = m_ht.find(name, name.size());
151  return node == nullptr ? nullptr : &node->entry;
152 }
153 
154 Entry*
155 NameTree::findLongestPrefixMatch(const Name& name, const EntrySelector& entrySelector) const
156 {
157  HashSequence hashes = computeHashes(name);
158 
159  for (ssize_t prefixLen = name.size(); prefixLen >= 0; --prefixLen) {
160  const Node* node = m_ht.find(name, prefixLen, hashes);
161  if (node != nullptr && entrySelector(node->entry)) {
162  return &node->entry;
163  }
164  }
165 
166  return nullptr;
167 }
168 
169 Entry*
170 NameTree::findLongestPrefixMatch(const Entry& entry1, const EntrySelector& entrySelector) const
171 {
172  Entry* entry = const_cast<Entry*>(&entry1);
173  while (entry != nullptr) {
174  if (entrySelector(*entry)) {
175  return entry;
176  }
177  entry = entry->getParent();
178  }
179  return nullptr;
180 }
181 
182 template<typename ENTRY>
183 Entry*
184 NameTree::findLongestPrefixMatch(const ENTRY& tableEntry, const EntrySelector& entrySelector) const
185 {
186  const Entry* nte = this->getEntry(tableEntry);
187  BOOST_ASSERT(nte != nullptr);
188  return this->findLongestPrefixMatch(*nte, entrySelector);
189 }
190 
191 template Entry*
192 NameTree::findLongestPrefixMatch<fib::Entry>(const fib::Entry&, const EntrySelector&) const;
193 
194 template Entry*
195 NameTree::findLongestPrefixMatch<measurements::Entry>(const measurements::Entry&,
196  const EntrySelector&) const;
197 
198 template Entry*
199 NameTree::findLongestPrefixMatch<strategy_choice::Entry>(const strategy_choice::Entry&,
200  const EntrySelector&) const;
201 
202 Entry*
203 NameTree::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntrySelector& entrySelector) const
204 {
205  const Entry* nte = this->getEntry(pitEntry);
206  BOOST_ASSERT(nte != nullptr);
207 
208  if (nte->getName().size() < pitEntry.getName().size()) {
209  // special case: PIT entry whose Interest name ends with an implicit digest
210  // are attached to the name tree entry with one-shorter-prefix.
211  BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest());
212  BOOST_ASSERT(nte->getName() == pitEntry.getName().getPrefix(-1));
213  const Entry* exact = this->findExactMatch(pitEntry.getName());
214  if (exact != nullptr) {
215  nte = exact;
216  }
217  }
218 
219  return this->findLongestPrefixMatch(*nte, entrySelector);
220 }
221 
222 boost::iterator_range<NameTree::const_iterator>
223 NameTree::findAllMatches(const Name& name, const EntrySelector& entrySelector) const
224 {
225  // As we are using Name Prefix Hash Table, and the current LPM() is
226  // implemented as starting from full name, and reduce the number of
227  // components by 1 each time, we could use it here.
228  // For trie-like design, it could be more efficient by walking down the
229  // trie from the root node.
230 
231  Entry* entry = this->findLongestPrefixMatch(name, entrySelector);
232  return {Iterator(make_shared<PrefixMatchImpl>(*this, entrySelector), entry), end()};
233 }
234 
235 boost::iterator_range<NameTree::const_iterator>
236 NameTree::fullEnumerate(const EntrySelector& entrySelector) const
237 {
238  return {Iterator(make_shared<FullEnumerationImpl>(*this, entrySelector), nullptr), end()};
239 }
240 
241 boost::iterator_range<NameTree::const_iterator>
243  const EntrySubTreeSelector& entrySubTreeSelector) const
244 {
245  Entry* entry = this->findExactMatch(prefix);
246  return {Iterator(make_shared<PartialEnumerationImpl>(*this, entrySubTreeSelector), entry), end()};
247 }
248 
249 } // namespace name_tree
250 } // namespace nfd
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241
const Name & getPrefix() const
Definition: fib-entry.hpp:58
HashSequence computeHashes(const Name &name)
computes hash values for each prefix of name
const Component & at(ssize_t i) const
Get component at the specified index.
Definition: name.hpp:434
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
represents a FIB entry
Definition: fib-entry.hpp:51
const_iterator end() const
Definition: name-tree.hpp:241
std::pair< const Node *, bool > insert(const Name &name, size_t prefixLen, const HashSequence &hashes)
find or insert node for name.getPrefix(prefixLen)
Entry * findExactMatch(const Name &name) const
exact match lookup
Definition: name-tree.cpp:148
fib::Entry * getFibEntry() const
function< std::pair< bool, bool >const Entry &entry)> EntrySubTreeSelector
a predicate to accept or reject an Entry and its children
provides options for Hashtable
const Node * find(const Name &name, size_t prefixLen) const
find node for name.getPrefix(prefixLen)
void setParent(Entry &entry)
set parent of this entry
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
longest prefix matching
Definition: name-tree.cpp:155
represents a Measurements entry
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
enumerate all entries under a prefix
Definition: name-tree.cpp:242
Range findAllMatches(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
all-prefixes match lookup
Definition: name-tree.cpp:223
measurements::Entry * getMeasurementsEntry() const
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
an Interest table entry
Definition: pit-entry.hpp:57
Entry & lookup(const Name &name)
find or insert an entry with specified name
Definition: name-tree.cpp:44
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
Name abstraction to represent an absolute name.
Definition: name.hpp:46
represents a Strategy Choice entry
std::vector< HashValue > HashSequence
a sequence of hash values
function< bool(const Entry &entry)> EntrySelector
a predicate to accept or reject an Entry in find operations
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
enumerate all entries
Definition: name-tree.cpp:236
Node * getNode(const Entry &entry)
size_t size() const
Get the number of components.
Definition: name.hpp:400
NameTree(size_t nBuckets=1024)
Definition: name-tree.cpp:38
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
delete the entry if it is empty
Definition: name-tree.cpp:121
const Name & getName() const
Definition: pit-entry.hpp:77
bool empty() const
Check if name is emtpy.
Definition: name.hpp:390
Entry * getEntry(const ENTRY &tableEntry) const
Definition: name-tree.hpp:64
void unsetParent()
unset parent of this entry
strategy_choice::Entry * getStrategyChoiceEntry() const
Entry * getParent() const
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
const Name & getName() const
an entry in the name tree
void erase(Node *node)
delete node