table: erase NameTree entry when FIB/Measurements/StrategyChoice entry is erased

This commit also optimizes Measurements table to make use of NameTree shortcuts.

refs #1803

Change-Id: Ib0e465750ed5e8ff9ed129a926a7bc852db4e9e1
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index 3b17ab1..838dd9a 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -166,7 +166,7 @@
       // going out of this strategy's namespace
       return;
     }
-    this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
+    this->getMeasurements().extendLifetime(measurementsEntry, MEASUREMENTS_LIFETIME);
 
     shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
       this->getMeasurementsEntryInfo(measurementsEntry);
@@ -187,7 +187,7 @@
       // going out of this strategy's namespace
       return;
     }
-    this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
+    this->getMeasurements().extendLifetime(measurementsEntry, MEASUREMENTS_LIFETIME);
 
     shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
       this->getMeasurementsEntryInfo(measurementsEntry);
diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp
index 516de57..3672b66 100644
--- a/daemon/table/fib.cpp
+++ b/daemon/table/fib.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "fib.hpp"
 #include "pit-entry.hpp"
@@ -114,24 +115,28 @@
 }
 
 void
+Fib::erase(shared_ptr<name_tree::Entry> nameTreeEntry)
+{
+  nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
+  m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
+  --m_nItems;
+}
+
+void
 Fib::erase(const Name& prefix)
 {
   shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
-  if (static_cast<bool>(nameTreeEntry))
-  {
-    nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
-    --m_nItems;
+  if (static_cast<bool>(nameTreeEntry)) {
+    this->erase(nameTreeEntry);
   }
 }
 
 void
 Fib::erase(const fib::Entry& entry)
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry);
-  if (static_cast<bool>(nameTreeEntry))
-  {
-    nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
-    --m_nItems;
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(entry);
+  if (static_cast<bool>(nameTreeEntry)) {
+    this->erase(nameTreeEntry);
   }
 }
 
diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp
index e5aaf15..615462f 100644
--- a/daemon/table/fib.hpp
+++ b/daemon/table/fib.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_DAEMON_TABLE_FIB_HPP
 #define NFD_DAEMON_TABLE_FIB_HPP
@@ -128,6 +129,9 @@
   shared_ptr<fib::Entry>
   findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;
 
+  void
+  erase(shared_ptr<name_tree::Entry> nameTreeEntry);
+
 private:
   NameTree& m_nameTree;
   size_t m_nItems;
diff --git a/daemon/table/measurements-accessor.cpp b/daemon/table/measurements-accessor.cpp
index 8ecb0fd..81b1432 100644
--- a/daemon/table/measurements-accessor.cpp
+++ b/daemon/table/measurements-accessor.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "measurements-accessor.hpp"
 
diff --git a/daemon/table/measurements-accessor.hpp b/daemon/table/measurements-accessor.hpp
index 2db0ccb..2108435 100644
--- a/daemon/table/measurements-accessor.hpp
+++ b/daemon/table/measurements-accessor.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_DAEMON_TABLE_MEASUREMENTS_ACCESSOR_HPP
 #define NFD_DAEMON_TABLE_MEASUREMENTS_ACCESSOR_HPP
@@ -68,7 +69,7 @@
    *  The entry will be kept until at least now()+lifetime.
    */
   void
-  extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime);
+  extendLifetime(shared_ptr<measurements::Entry> entry, const time::nanoseconds& lifetime);
 
 private:
   /** \brief perform access control to Measurements entry
@@ -109,7 +110,8 @@
 }
 
 inline void
-MeasurementsAccessor::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime)
+MeasurementsAccessor::extendLifetime(shared_ptr<measurements::Entry> entry,
+                                     const time::nanoseconds& lifetime)
 {
   m_measurements.extendLifetime(entry, lifetime);
 }
diff --git a/daemon/table/measurements.cpp b/daemon/table/measurements.cpp
index a63c602..4943bf4 100644
--- a/daemon/table/measurements.cpp
+++ b/daemon/table/measurements.cpp
@@ -100,7 +100,15 @@
     return shared_ptr<measurements::Entry>();
   }
 
-  return this->get(child->getName().getPrefix(-1));
+  shared_ptr<name_tree::Entry> nameTreeChild = m_nameTree.get(*child);
+  shared_ptr<name_tree::Entry> nameTreeEntry = nameTreeChild->getParent();
+  if (static_cast<bool>(nameTreeEntry)) {
+    return this->get(nameTreeEntry);
+  }
+  else {
+    BOOST_ASSERT(nameTreeChild->getPrefix().size() == 0); // root entry has no parent
+    return shared_ptr<measurements::Entry>();
+  }
 }
 
 shared_ptr<measurements::Entry>
@@ -124,33 +132,38 @@
 }
 
 void
-Measurements::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime)
+Measurements::extendLifetime(shared_ptr<measurements::Entry> entry,
+                             const time::nanoseconds& lifetime)
 {
-  shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName());
-  if (static_cast<bool>(ret))
-  {
-    time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
-    if (ret->m_expiry >= expiry) // has longer lifetime, not extending
-      return;
-    scheduler::cancel(entry.m_cleanup);
-    entry.m_expiry = expiry;
-    entry.m_cleanup = scheduler::schedule(lifetime,
-                         bind(&Measurements::cleanup, this, ret));
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*entry);
+  if (!static_cast<bool>(nameTreeEntry) ||
+      nameTreeEntry->getMeasurementsEntry().get() != entry.get()) {
+    // entry is already gone; it is a dangling reference
+    return;
   }
+
+  time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
+  if (entry->m_expiry >= expiry) {
+    // has longer lifetime, not extending
+    return;
+  }
+
+  scheduler::cancel(entry->m_cleanup);
+  entry->m_expiry = expiry;
+  entry->m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, entry));
 }
 
 void
 Measurements::cleanup(shared_ptr<measurements::Entry> entry)
 {
-  BOOST_ASSERT(entry);
+  BOOST_ASSERT(static_cast<bool>(entry));
 
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName());
-  if (static_cast<bool>(nameTreeEntry))
-  {
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*entry);
+  if (static_cast<bool>(nameTreeEntry)) {
     nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>());
+    m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
     m_nItems--;
   }
-
 }
 
 } // namespace nfd
diff --git a/daemon/table/measurements.hpp b/daemon/table/measurements.hpp
index 1ace0b9..7478c58 100644
--- a/daemon/table/measurements.hpp
+++ b/daemon/table/measurements.hpp
@@ -86,7 +86,7 @@
    *  The entry will be kept until at least now()+lifetime.
    */
   void
-  extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime);
+  extendLifetime(shared_ptr<measurements::Entry> entry, const time::nanoseconds& lifetime);
 
   size_t
   size() const;
diff --git a/daemon/table/name-tree-entry.cpp b/daemon/table/name-tree-entry.cpp
index 0e4d821..017dbfe 100644
--- a/daemon/table/name-tree-entry.cpp
+++ b/daemon/table/name-tree-entry.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,9 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
-
-// Name Tree Entry (i.e., Name Prefix Entry)
+ */
 
 #include "name-tree-entry.hpp"
 
@@ -41,7 +40,7 @@
   // resolve hash collisions
   // So before erasing a single node, make sure its m_next == 0
   // See eraseEntryIfEmpty in name-tree.cpp
-  if (m_next)
+  if (m_next != 0)
     delete m_next;
 }
 
@@ -55,6 +54,42 @@
 {
 }
 
+bool
+Entry::isEmpty() const
+{
+  return m_children.empty() &&
+         !static_cast<bool>(m_fibEntry) &&
+         m_pitEntries.empty() &&
+         !static_cast<bool>(m_measurementsEntry) &&
+         !static_cast<bool>(m_strategyChoiceEntry);
+}
+
+void
+Entry::setFibEntry(shared_ptr<fib::Entry> fibEntry)
+{
+  if (static_cast<bool>(fibEntry)) {
+    BOOST_ASSERT(!static_cast<bool>(fibEntry->m_nameTreeEntry));
+  }
+
+  if (static_cast<bool>(m_fibEntry)) {
+    m_fibEntry->m_nameTreeEntry.reset();
+  }
+  m_fibEntry = fibEntry;
+  if (static_cast<bool>(m_fibEntry)) {
+    m_fibEntry->m_nameTreeEntry = this->shared_from_this();
+  }
+}
+
+void
+Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
+{
+  BOOST_ASSERT(static_cast<bool>(pitEntry));
+  BOOST_ASSERT(!static_cast<bool>(pitEntry->m_nameTreeEntry));
+
+  m_pitEntries.push_back(pitEntry);
+  pitEntry->m_nameTreeEntry = this->shared_from_this();
+}
+
 void
 Entry::erasePitEntry(shared_ptr<pit::Entry> pitEntry)
 {
@@ -70,5 +105,37 @@
   pitEntry->m_nameTreeEntry.reset();
 }
 
+void
+Entry::setMeasurementsEntry(shared_ptr<measurements::Entry> measurementsEntry)
+{
+  if (static_cast<bool>(measurementsEntry)) {
+    BOOST_ASSERT(!static_cast<bool>(measurementsEntry->m_nameTreeEntry));
+  }
+
+  if (static_cast<bool>(m_measurementsEntry)) {
+    m_measurementsEntry->m_nameTreeEntry.reset();
+  }
+  m_measurementsEntry = measurementsEntry;
+  if (static_cast<bool>(m_measurementsEntry)) {
+    m_measurementsEntry->m_nameTreeEntry = this->shared_from_this();
+  }
+}
+
+void
+Entry::setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry)
+{
+  if (static_cast<bool>(strategyChoiceEntry)) {
+    BOOST_ASSERT(!static_cast<bool>(strategyChoiceEntry->m_nameTreeEntry));
+  }
+
+  if (static_cast<bool>(m_strategyChoiceEntry)) {
+    m_strategyChoiceEntry->m_nameTreeEntry.reset();
+  }
+  m_strategyChoiceEntry = strategyChoiceEntry;
+  if (static_cast<bool>(m_strategyChoiceEntry)) {
+    m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
+  }
+}
+
 } // namespace name_tree
 } // namespace nfd
diff --git a/daemon/table/name-tree-entry.hpp b/daemon/table/name-tree-entry.hpp
index 48c9af7..d0e3587 100644
--- a/daemon/table/name-tree-entry.hpp
+++ b/daemon/table/name-tree-entry.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,9 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
-
-// Name Tree Entry (i.e., Name Prefix Entry)
+ */
 
 #ifndef NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP
 #define NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP
@@ -95,6 +94,7 @@
   bool
   isEmpty() const;
 
+public: // attached table entries
   void
   setFibEntry(shared_ptr<fib::Entry> fibEntry);
 
@@ -187,37 +187,12 @@
   return !m_children.empty();
 }
 
-inline bool
-Entry::isEmpty() const
-{
-  return m_children.empty() &&
-         !static_cast<bool>(m_fibEntry) &&
-         m_pitEntries.empty() &&
-         !static_cast<bool>(m_measurementsEntry);
-}
-
 inline shared_ptr<fib::Entry>
 Entry::getFibEntry() const
 {
   return m_fibEntry;
 }
 
-inline void
-Entry::setFibEntry(shared_ptr<fib::Entry> fibEntry)
-{
-  if (static_cast<bool>(fibEntry)) {
-    BOOST_ASSERT(!static_cast<bool>(fibEntry->m_nameTreeEntry));
-  }
-
-  if (static_cast<bool>(m_fibEntry)) {
-    m_fibEntry->m_nameTreeEntry.reset();
-  }
-  m_fibEntry = fibEntry;
-  if (static_cast<bool>(m_fibEntry)) {
-    m_fibEntry->m_nameTreeEntry = this->shared_from_this();
-  }
-}
-
 inline bool
 Entry::hasPitEntries() const
 {
@@ -230,60 +205,18 @@
   return m_pitEntries;
 }
 
-inline void
-Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
-{
-  BOOST_ASSERT(static_cast<bool>(pitEntry));
-  BOOST_ASSERT(!static_cast<bool>(pitEntry->m_nameTreeEntry));
-
-  m_pitEntries.push_back(pitEntry);
-  pitEntry->m_nameTreeEntry = this->shared_from_this();
-}
-
 inline shared_ptr<measurements::Entry>
 Entry::getMeasurementsEntry() const
 {
   return m_measurementsEntry;
 }
 
-inline void
-Entry::setMeasurementsEntry(shared_ptr<measurements::Entry> measurementsEntry)
-{
-  if (static_cast<bool>(measurementsEntry)) {
-    BOOST_ASSERT(!static_cast<bool>(measurementsEntry->m_nameTreeEntry));
-  }
-
-  if (static_cast<bool>(m_measurementsEntry)) {
-    m_measurementsEntry->m_nameTreeEntry.reset();
-  }
-  m_measurementsEntry = measurementsEntry;
-  if (static_cast<bool>(m_measurementsEntry)) {
-    m_measurementsEntry->m_nameTreeEntry = this->shared_from_this();
-  }
-}
-
 inline shared_ptr<strategy_choice::Entry>
 Entry::getStrategyChoiceEntry() const
 {
   return m_strategyChoiceEntry;
 }
 
-inline void
-Entry::setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry)
-{
-  if (static_cast<bool>(strategyChoiceEntry)) {
-    BOOST_ASSERT(!static_cast<bool>(strategyChoiceEntry->m_nameTreeEntry));
-  }
-
-  if (static_cast<bool>(m_strategyChoiceEntry)) {
-    m_strategyChoiceEntry->m_nameTreeEntry.reset();
-  }
-  m_strategyChoiceEntry = strategyChoiceEntry;
-  if (static_cast<bool>(m_strategyChoiceEntry)) {
-    m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
-  }
-}
-
 } // namespace name_tree
 } // namespace nfd
 
diff --git a/daemon/table/name-tree.hpp b/daemon/table/name-tree.hpp
index 2f404bf..027f852 100644
--- a/daemon/table/name-tree.hpp
+++ b/daemon/table/name-tree.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,9 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
-
-// Name Tree (Name Prefix Hash Table)
+ */
 
 #ifndef NFD_DAEMON_TABLE_NAME_TREE_HPP
 #define NFD_DAEMON_TABLE_NAME_TREE_HPP
@@ -121,17 +120,12 @@
   shared_ptr<name_tree::Entry>
   findExactMatch(const Name& prefix) const;
 
-  shared_ptr<name_tree::Entry>
-  findExactMatch(const fib::Entry& fibEntry) const;
-
   /**
-   * \brief Erase a Name Tree Entry if this entry is empty.
-   * \details If a Name Tree Entry contains no Children, no FIB, no PIT, and
-   * no Measurements entries, then it can be erased. In addition, its parent entry
-   * will also be examined by following the parent pointer until all empty entries
-   * are erased.
-   * \param entry The pointer to the entry to be erased. The entry pointer should
-   * returned by the findExactMatch(), lookup(), or findLongestPrefixMatch() functions.
+   * \brief Delete a Name Tree Entry if this entry is empty.
+   * \param entry The entry to be deleted if empty.
+   * \note This function must be called after a table entry is detached from Name Tree
+   *       entry. The function deletes a Name Tree entry if nothing is attached to it and
+   *       it has no children, then repeats the same process on its ancestors.
    */
   bool
   eraseEntryIfEmpty(shared_ptr<name_tree::Entry> entry);
@@ -297,12 +291,6 @@
 }
 
 inline shared_ptr<name_tree::Entry>
-NameTree::findExactMatch(const fib::Entry& fibEntry) const
-{
-  return fibEntry.m_nameTreeEntry;
-}
-
-inline shared_ptr<name_tree::Entry>
 NameTree::get(const fib::Entry& fibEntry) const
 {
   return fibEntry.m_nameTreeEntry;
diff --git a/daemon/table/strategy-choice.cpp b/daemon/table/strategy-choice.cpp
index 34c583e..aa5db1b 100644
--- a/daemon/table/strategy-choice.cpp
+++ b/daemon/table/strategy-choice.cpp
@@ -21,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "strategy-choice.hpp"
 #include "core/logger.hpp"
@@ -120,6 +120,7 @@
   this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this());
 
   nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>());
+  m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
   --m_nItems;
 }
 
diff --git a/tests/daemon/table/fib.cpp b/tests/daemon/table/fib.cpp
index bdf2fca..d40f15d 100644
--- a/tests/daemon/table/fib.cpp
+++ b/tests/daemon/table/fib.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "table/fib.hpp"
 #include "tests/daemon/face/dummy-face.hpp"
@@ -288,7 +289,7 @@
 }
 
 void
-validateRemove(Fib& fib, const Name& target)
+validateErase(Fib& fib, const Name& target)
 {
   fib.erase(target);
 
@@ -299,14 +300,14 @@
     }
 }
 
-BOOST_AUTO_TEST_CASE(Remove)
+BOOST_AUTO_TEST_CASE(Erase)
 {
   NameTree emptyNameTree;
   Fib emptyFib(emptyNameTree);
 
   emptyFib.erase("/does/not/exist"); // crash test
 
-  validateRemove(emptyFib, "/");
+  validateErase(emptyFib, "/");
 
   emptyFib.erase("/still/does/not/exist"); // crash test
 
@@ -320,19 +321,19 @@
   // check if we remove the right thing and leave
   // everything else alone
 
-  validateRemove(fib, "/A/B");
+  validateErase(fib, "/A/B");
   validateFindExactMatch(fib, "/A");
   validateFindExactMatch(fib, "/A/B/C");
   validateFindExactMatch(fib, "/");
 
-  validateRemove(fib, "/A/B/C");
+  validateErase(fib, "/A/B/C");
   validateFindExactMatch(fib, "/A");
   validateFindExactMatch(fib, "/");
 
-  validateRemove(fib, "/A");
+  validateErase(fib, "/A");
   validateFindExactMatch(fib, "/");
 
-  validateRemove(fib, "/");
+  validateErase(fib, "/");
   validateNoExactMatch(fib, "/");
 
   NameTree gapNameTree;
@@ -345,6 +346,17 @@
   validateFindExactMatch(gapFib, "/X/Y/Z");
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  NameTree nameTree;
+  Fib fib(nameTree);
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  fib.insert("ndn:/A/B/C");
+  fib.erase("ndn:/A/B/C");
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_CASE(Iterator)
 {
   NameTree nameTree;
diff --git a/tests/daemon/table/measurements.cpp b/tests/daemon/table/measurements.cpp
index c2afb96..30b71c4 100644
--- a/tests/daemon/table/measurements.cpp
+++ b/tests/daemon/table/measurements.cpp
@@ -84,8 +84,8 @@
                CHECK2 < EXTEND_C &&
                EXTEND_C < CHECK3);
 
-  measurements.extendLifetime(*entryA, EXTEND_A);
-  measurements.extendLifetime(*entryC, EXTEND_C);
+  measurements.extendLifetime(entryA, EXTEND_A);
+  measurements.extendLifetime(entryC, EXTEND_C);
   // remaining lifetime:
   //   A = initial lifetime, because it's extended by less duration
   //   B = initial lifetime
@@ -115,6 +115,22 @@
   BOOST_CHECK_EQUAL(measurements.size(), 0);
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  LimitedIo limitedIo;
+  NameTree nameTree;
+  Measurements measurements(nameTree);
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  shared_ptr<measurements::Entry> entry = measurements.get("/A");
+  BOOST_CHECK_EQUAL(
+    limitedIo.run(LimitedIo::UNLIMITED_OPS,
+                  Measurements::getInitialLifetime() + time::milliseconds(10)),
+    LimitedIo::EXCEED_TIME);
+  BOOST_CHECK_EQUAL(measurements.size(), 0);
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
diff --git a/tests/daemon/table/pit.cpp b/tests/daemon/table/pit.cpp
index 52013a6..566421f 100644
--- a/tests/daemon/table/pit.cpp
+++ b/tests/daemon/table/pit.cpp
@@ -352,6 +352,18 @@
 
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  NameTree nameTree;
+  Pit pit(nameTree);
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
+  shared_ptr<pit::Entry> entry = pit.insert(*interest).first;
+  pit.erase(entry);
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_CASE(FindAllDataMatches)
 {
   Name nameA   ("ndn:/A");
diff --git a/tests/daemon/table/strategy-choice.cpp b/tests/daemon/table/strategy-choice.cpp
index e8f4ca3..0f48538 100644
--- a/tests/daemon/table/strategy-choice.cpp
+++ b/tests/daemon/table/strategy-choice.cpp
@@ -21,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "table/strategy-choice.hpp"
 #include "tests/daemon/fw/dummy-strategy.hpp"
@@ -188,6 +188,28 @@
   BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  Forwarder forwarder;
+  NameTree& nameTree = forwarder.getNameTree();
+  StrategyChoice& table = forwarder.getStrategyChoice();
+
+  Name nameP("ndn:/strategy/P");
+  Name nameQ("ndn:/strategy/Q");
+  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
+  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+  table.install(strategyP);
+  table.install(strategyQ);
+
+  table.insert("ndn:/", nameP);
+
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  table.insert("ndn:/A/B", nameQ);
+  table.erase("ndn:/A/B");
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests