From 78c84d1ce5d89015f5cc20ab784cf9bdfca6355b Mon Sep 17 00:00:00 2001 From: Haowei Yuan Date: Thu, 27 Feb 2014 15:35:13 -0600 Subject: [PATCH] table: PIT on NameTree ref# 1194 Change-Id: I32f103374885445725fc5deeebda60b1b1e54c2f --- daemon/fw/forwarder.cpp | 8 ++-- daemon/fw/forwarder.hpp | 3 +- daemon/table/pit.cpp | 97 +++++++++++++++++++++++++++++++++-------- daemon/table/pit.hpp | 30 ++++++++++--- tests/table/pit.cpp | 48 ++++++++++++++++---- 5 files changed, 149 insertions(+), 37 deletions(-) diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index a605ef0d..cc40f240 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -16,6 +16,8 @@ const Name Forwarder::s_localhostName("ndn:/localhost"); Forwarder::Forwarder() : m_lastFaceId(0) + , m_nameTree(1024) // "1024" could be made as one configurable parameter of the forwarder. + , m_pit(m_nameTree) { m_strategy = make_shared(boost::ref(*this)); } @@ -189,8 +191,8 @@ Forwarder::onInterestUnsatisfied(shared_ptr pitEntry) // invoke PIT unsatisfied callback // TODO - // PIT delete - m_pit.remove(pitEntry); + // PIT erase + m_pit.erase(pitEntry); } void @@ -322,7 +324,7 @@ Forwarder::setStragglerTimer(shared_ptr pitEntry) time::Duration stragglerTime = time::milliseconds(100); pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, - bind(&Pit::remove, &m_pit, pitEntry)); + bind(&Pit::erase, &m_pit, pitEntry)); } void diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp index 66229995..3df2d41d 100644 --- a/daemon/fw/forwarder.hpp +++ b/daemon/fw/forwarder.hpp @@ -117,7 +117,8 @@ PROTECTED_WITH_TESTS_ELSE_PRIVATE: private: FaceId m_lastFaceId; std::map > m_faces; - + + NameTree m_nameTree; Fib m_fib; Pit m_pit; Cs m_cs; diff --git a/daemon/table/pit.cpp b/daemon/table/pit.cpp index 168a64e4..6f5acf33 100644 --- a/daemon/table/pit.cpp +++ b/daemon/table/pit.cpp @@ -4,12 +4,24 @@ * See COPYING for copyright and distribution information. */ +/** + * KNOWN ISSUES + * + * - To remove a PIT entry, we need to first perform a lookup on NameTree + * to locate its NameTree Entry, and then call NameTreeEntry->deletePitEntry() + * function. Alternatively, we could store a pointer at each PIT-Entry, which + * would speed up this procedure with the cost of additional memory space. Maybe + * this could also be part of the PIT/FIB/Measurement shortcut, where all of these + * entries have pointers to their NameTreeEntry. Which could be part of task + * #1202, shortcuts between FIB, PIT, Measurements. + * + */ + #include "pit.hpp" -#include namespace nfd { -Pit::Pit() +Pit::Pit(NameTree& nameTree) : m_nameTree(nameTree), m_nItems(0) { } @@ -43,34 +55,83 @@ predicate_PitEntry_similar_Interest(shared_ptr entry, std::pair, bool> Pit::insert(const Interest& interest) { - std::list >::iterator it = std::find_if( - m_table.begin(), m_table.end(), - bind(&predicate_PitEntry_similar_Interest, _1, interest)); - if (it != m_table.end()) return std::make_pair(*it, false); - - shared_ptr entry = make_shared(interest); - m_table.push_back(entry); - return std::make_pair(entry, true); + // - first lookup() the Interest Name in the NameTree, which will creates all + // the intermedia nodes, starting from the shortest prefix. + // - if it is guaranteed that this Interest already has a NameTree Entry, we + // could use findExactMatch() instead. + // - Alternatively, we could try to do findExactMatch() first, if not found, then + // do lookup(). + shared_ptr nameTreeEntry = m_nameTree.lookup(interest.getName()); + + BOOST_ASSERT(static_cast(nameTreeEntry)); + + std::vector >& pitEntries = nameTreeEntry->getPitEntries(); + + // then check if this Interest is already in the PIT entries + std::vector >::iterator it = std::find_if(pitEntries.begin(), pitEntries.end(), bind(&predicate_PitEntry_similar_Interest, _1, interest)); + + if (it != pitEntries.end()) + { + return std::make_pair(*it, false); + } + else + { + shared_ptr entry = make_shared(interest); + nameTreeEntry->insertPitEntry(entry); + + // Increase m_nItmes only if we create a new PIT Entry + m_nItems++; + + return std::make_pair(entry, true); + } } shared_ptr Pit::findAllDataMatches(const Data& data) const { shared_ptr result = make_shared(); - for (std::list >::const_iterator it = m_table.begin(); - it != m_table.end(); ++it) { - shared_ptr entry = *it; - if (entry->getInterest().matchesName(data.getName())) { - result->push_back(entry); + + shared_ptr nameTreeEntry; + + // NOTE: We are using findLongestPrefixMatch() here. + // The reason is that findLongestPrefixMatch() starts with the full name + // and then remove one component each time, which is the type of behavior we would like + // to use here. + // If it could be guranteed that the quering Data packet always has a Name Tree + // Entry, we could also use findExactMatch(). + for (nameTreeEntry = m_nameTree.findLongestPrefixMatch(data.getName()); + static_cast(nameTreeEntry); + nameTreeEntry = nameTreeEntry->getParent()) + { + std::vector >& pitEntries = nameTreeEntry->getPitEntries(); + for (size_t i = 0; i < pitEntries.size(); i++) + { + if (pitEntries[i]->getInterest().matchesName(data.getName())) + result->push_back(pitEntries[i]); + } } - } + return result; } void -Pit::remove(shared_ptr pitEntry) +Pit::erase(shared_ptr pitEntry) { - m_table.remove(pitEntry); + // first get the NPE + // If pit-entry.hpp stores a NameTree Entry for each PIT, we could also use the get() method + // directly, saving one hash computation. + shared_ptr nameTreeEntry = m_nameTree.findExactMatch(pitEntry->getName()); + + BOOST_ASSERT(static_cast(nameTreeEntry)); + + // erase this PIT entry + if (static_cast(nameTreeEntry)) + { + nameTreeEntry->erasePitEntry(pitEntry); + m_nameTree.eraseEntryIfEmpty(nameTreeEntry); + + m_nItems--; + } } } // namespace nfd diff --git a/daemon/table/pit.hpp b/daemon/table/pit.hpp index 9dd2e1e2..9ea5aac6 100644 --- a/daemon/table/pit.hpp +++ b/daemon/table/pit.hpp @@ -7,7 +7,9 @@ #ifndef NFD_TABLE_PIT_HPP #define NFD_TABLE_PIT_HPP +#include "name-tree.hpp" #include "pit-entry.hpp" + namespace nfd { namespace pit { @@ -27,9 +29,16 @@ typedef std::vector > DataMatchResult; class Pit : noncopyable { public: - Pit(); - + explicit + Pit(NameTree& nameTree); + ~Pit(); + + /** + * \brief Get the number of items stored in the PIT. + */ + size_t + size() const; /** \brief inserts a FIB entry for prefix * If an entry for exact same prefix exists, that entry is returned. @@ -43,15 +52,24 @@ public: */ shared_ptr findAllDataMatches(const Data& data) const; - - /// removes a PIT entry + + /** + * \brief Erase a PIT Entry + */ void - remove(shared_ptr pitEntry); + erase(shared_ptr pitEntry); private: - std::list > m_table; + NameTree& m_nameTree; + size_t m_nItems; }; +inline size_t +Pit::size() const +{ + return m_nItems; +} + } // namespace nfd #endif // NFD_TABLE_PIT_HPP diff --git a/tests/table/pit.cpp b/tests/table/pit.cpp index 5fc50cdb..916db810 100644 --- a/tests/table/pit.cpp +++ b/tests/table/pit.cpp @@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(EntryInOutRecords) BOOST_CHECK(entry.getInterest().getName().equals(name)); BOOST_CHECK(entry.getName().equals(name)); - + const pit::InRecordCollection& inRecords1 = entry.getInRecords(); BOOST_CHECK_EQUAL(inRecords1.size(), 0); const pit::OutRecordCollection& outRecords1 = entry.getOutRecords(); @@ -157,62 +157,86 @@ BOOST_AUTO_TEST_CASE(Insert) // different Name+exclude1 Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0); - Pit pit; + NameTree nameTree(16); + Pit pit(nameTree); + std::pair, bool> insertResult; + BOOST_CHECK_EQUAL(pit.size(), 0); + insertResult = pit.insert(interestA); BOOST_CHECK_EQUAL(insertResult.second, true); - + BOOST_CHECK_EQUAL(pit.size(), 1); + insertResult = pit.insert(interestB); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 2); insertResult = pit.insert(interestC); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 3); insertResult = pit.insert(interestD); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 4); insertResult = pit.insert(interestE); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 5); insertResult = pit.insert(interestF); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 6); insertResult = pit.insert(interestG); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 7); + insertResult = pit.insert(interestH); BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ - + BOOST_CHECK_EQUAL(pit.size(), 7); + insertResult = pit.insert(interestI); BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ + BOOST_CHECK_EQUAL(pit.size(), 7); insertResult = pit.insert(interestJ); BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ + BOOST_CHECK_EQUAL(pit.size(), 7); insertResult = pit.insert(interestK); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 8); } -BOOST_AUTO_TEST_CASE(Remove) +BOOST_AUTO_TEST_CASE(Erase) { Interest interest(Name("ndn:/z88Admz6A2")); - Pit pit; + NameTree nameTree(16); + Pit pit(nameTree); + std::pair, bool> insertResult; + BOOST_CHECK_EQUAL(pit.size(), 0); + insertResult = pit.insert(interest); BOOST_CHECK_EQUAL(insertResult.second, true); + BOOST_CHECK_EQUAL(pit.size(), 1); insertResult = pit.insert(interest); BOOST_CHECK_EQUAL(insertResult.second, false); + BOOST_CHECK_EQUAL(pit.size(), 1); - pit.remove(insertResult.first); + pit.erase(insertResult.first); + BOOST_CHECK_EQUAL(pit.size(), 0); insertResult = pit.insert(interest); BOOST_CHECK_EQUAL(insertResult.second, true); -} + BOOST_CHECK_EQUAL(pit.size(), 1); +} BOOST_AUTO_TEST_CASE(FindAllDataMatches) { @@ -224,11 +248,17 @@ BOOST_AUTO_TEST_CASE(FindAllDataMatches) Interest interestAB(nameAB); Interest interestD (nameD ); - Pit pit; + NameTree nameTree(16); + Pit pit(nameTree); + + BOOST_CHECK_EQUAL(pit.size(), 0); + pit.insert(interestA ); pit.insert(interestAB); pit.insert(interestD ); + BOOST_CHECK_EQUAL(pit.size(), 3); + Data data(nameABC); shared_ptr matches = pit.findAllDataMatches(data);