fib.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "fib.hpp"
27 #include "pit-entry.hpp"
28 #include "measurements-entry.hpp"
29 #include "core/asserts.hpp"
30 
31 namespace nfd {
32 namespace fib {
33 
35 
36 const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
37 
38 static inline bool
40 {
41  return nte.getFibEntry() != nullptr;
42 }
43 
44 Fib::Fib(NameTree& nameTree)
45  : m_nameTree(nameTree)
46  , m_nItems(0)
47 {
48 }
49 
50 template<typename K>
51 const Entry&
52 Fib::findLongestPrefixMatchImpl(const K& key) const
53 {
54  name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasFibEntry);
55  if (nte != nullptr) {
56  return *nte->getFibEntry();
57  }
58  return *s_emptyEntry;
59 }
60 
61 const Entry&
62 Fib::findLongestPrefixMatch(const Name& prefix) const
63 {
64  return this->findLongestPrefixMatchImpl(prefix);
65 }
66 
67 const Entry&
69 {
70  return this->findLongestPrefixMatchImpl(pitEntry);
71 }
72 
73 const Entry&
74 Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
75 {
76  return this->findLongestPrefixMatchImpl(measurementsEntry);
77 }
78 
79 Entry*
80 Fib::findExactMatch(const Name& prefix)
81 {
82  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
83  if (nte != nullptr)
84  return nte->getFibEntry();
85 
86  return nullptr;
87 }
88 
89 std::pair<Entry*, bool>
90 Fib::insert(const Name& prefix)
91 {
92  name_tree::Entry& nte = m_nameTree.lookup(prefix);
93  Entry* entry = nte.getFibEntry();
94  if (entry != nullptr) {
95  return std::make_pair(entry, false);
96  }
97 
98  nte.setFibEntry(make_unique<Entry>(prefix));
99  ++m_nItems;
100  return std::make_pair(nte.getFibEntry(), true);
101 }
102 
103 void
104 Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
105 {
106  BOOST_ASSERT(nte != nullptr);
107 
108  nte->setFibEntry(nullptr);
109  if (canDeleteNte) {
110  m_nameTree.eraseIfEmpty(nte);
111  }
112  --m_nItems;
113 }
114 
115 void
116 Fib::erase(const Name& prefix)
117 {
118  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
119  if (nte != nullptr) {
120  this->erase(nte);
121  }
122 }
123 
124 void
125 Fib::erase(const Entry& entry)
126 {
127  name_tree::Entry* nte = m_nameTree.getEntry(entry);
128  if (nte == nullptr) { // don't try to erase s_emptyEntry
129  BOOST_ASSERT(&entry == s_emptyEntry.get());
130  return;
131  }
132  this->erase(nte);
133 }
134 
135 void
136 Fib::removeNextHop(Entry& entry, const Face& face)
137 {
138  entry.removeNextHop(face);
139 
140  if (!entry.hasNextHops()) {
141  name_tree::Entry* nte = m_nameTree.getEntry(entry);
142  this->erase(nte, false);
143  }
144 }
145 
147 Fib::getRange() const
148 {
149  return m_nameTree.fullEnumerate(&nteHasFibEntry) |
150  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
151 }
152 
153 } // namespace fib
154 } // namespace nfd
Entry * findExactMatch(const Name &prefix)
performs an exact match lookup
Definition: fib.cpp:80
void erase(const Name &prefix)
Definition: fib.cpp:116
represents a Measurements entry
Fib(NameTree &nameTree)
Definition: fib.cpp:44
represents a FIB entry
Definition: fib-entry.hpp:51
std::pair< Entry *, bool > insert(const Name &prefix)
inserts a FIB entry for prefix
Definition: fib.cpp:90
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
Definition: fib.hpp:105
fib::Entry * getFibEntry() const
an Interest table entry
Definition: pit-entry.hpp:57
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
an entry in the name tree
boost::range_iterator< Range >::type const_iterator
Definition: fib.hpp:106
void removeNextHop(Entry &entry, const Face &face)
removes the NextHop record for face
Definition: fib.cpp:136
NFD_ASSERT_FORWARD_ITERATOR(Fib::const_iterator)
const Entry & findLongestPrefixMatch(const Name &prefix) const
performs a longest prefix match
Definition: fib.cpp:62
void removeNextHop(const Face &face)
removes a NextHop record
Definition: fib-entry.cpp:66
a functor to get a table entry from a name tree entry
static bool nteHasFibEntry(const name_tree::Entry &nte)
Definition: fib.cpp:39
bool hasNextHops() const
Definition: fib-entry.hpp:72