Modifying implementation of content store to use new trie
diff --git a/examples/ccnx-simple.cc b/examples/ccnx-simple.cc
index d5a90b6..da8b1f6 100644
--- a/examples/ccnx-simple.cc
+++ b/examples/ccnx-simple.cc
@@ -80,9 +80,15 @@
   NS_LOG_INFO ("Installing CCNx applications");
   CcnxAppHelper consumerHelper ("ns3::CcnxConsumerCbr");
   // Consumer will request /prefix/0, /prefix/1, ...
-  consumerHelper.SetPrefix ("/prefix");
-  consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
+  consumerHelper.SetPrefix ("/prefix/0");
+  consumerHelper.SetAttribute ("Frequency", StringValue ("1")); // 10 interests a second
   ApplicationContainer consumers = consumerHelper.Install (nodes.Get (0)); // first node
+  consumers.Stop (Seconds (0.3));
+
+  consumerHelper.SetPrefix ("/prefix");
+  consumers = consumerHelper.Install (nodes.Get (0)); // first node
+  consumers.Start (Seconds (1));
+  consumers.Stop  (Seconds (1.3));
   
   CcnxAppHelper producerHelper ("ns3::CcnxProducer");
   // Producer will reply to all requests starting with /prefix
diff --git a/examples/trie.cc b/examples/trie.cc
index 36e52ac..4d82ba6 100644
--- a/examples/trie.cc
+++ b/examples/trie.cc
@@ -70,7 +70,6 @@
   x.erase (item.first);
 
   x.insert (n1, ns3::Create<Integer> (1));
-  x.find (n1);
   x.insert (n4, ns3::Create<Integer> (4));
 
   // std::cout << x.getTrie ();
diff --git a/model/ccnx-content-store-lru.cc b/model/ccnx-content-store-lru.cc
index 8c0d777..5546d84 100644
--- a/model/ccnx-content-store-lru.cc
+++ b/model/ccnx-content-store-lru.cc
@@ -33,8 +33,6 @@
 
 NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreLru);
 
-using namespace __ccnx_private;
-
 TypeId 
 CcnxContentStoreLru::GetTypeId (void)
 {
@@ -57,6 +55,7 @@
 CcnxContentStoreLru::SetMaxSize (uint32_t maxSize)
 {
   m_maxSize = maxSize;
+  m_contentStore.getPolicy ().set_max_size (maxSize);
 }
 
 uint32_t
@@ -73,12 +72,6 @@
  * \ingroup ccnx
  * \brief Typedef for hash index of content store container
  */
-struct CcnxContentStoreByPrefix
-{
-  typedef
-  CcnxContentStoreLruContainer::type::index<i_prefix>::type
-  type;
-};
 
 CcnxContentStoreLru::CcnxContentStoreLru ()
   : m_maxSize (100)
@@ -92,50 +85,45 @@
 CcnxContentStoreLru::Lookup (Ptr<const CcnxInterestHeader> interest)
 {
   NS_LOG_FUNCTION (this << interest->GetName ());
-  CcnxContentStoreLruContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (interest->GetName ());
-  if (it != m_contentStore.end ())
-    {
-      // promote entry to the top
-      m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
-                                           m_contentStore.project<i_mru> (it));
 
-      m_cacheHitsTrace (interest, it->GetHeader ());
-      
-      // return fully formed CCNx packet
-      return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader (), it->GetPacket ());
+  /// @todo Change to search with predicate
+  CcnxContentStoreLruContainer::iterator node =
+    m_contentStore.deepest_prefix_match (interest->GetName ());
+  
+  if (node != m_contentStore.end ())
+    {
+      m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
+
+      NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
+      return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (),
+                                node->payload ()->GetHeader (),
+                                node->payload ()->GetPacket ());
     }
-  m_cacheMissesTrace (interest);
-  return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
+  else
+    {
+      NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
+      m_cacheMissesTrace (interest);
+      return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
+    }
 }   
     
 bool 
 CcnxContentStoreLru::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
 {
   NS_LOG_FUNCTION (this << header->GetName ());
-  CcnxContentStoreLruContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (header->GetName ());
-  if (it == m_contentStore.end ())
-    { // add entry to the top
-      m_contentStore.get<i_mru> ().push_front (CcnxContentStoreEntry (header, packet));
-      if (m_contentStore.size () > m_maxSize)
-        m_contentStore.get<i_mru> ().pop_back ();
-      return false;
-    }
-  else
-    {
-      /// @todo Wrong!!! Record should be updated and relocated, not just relocated
-      // promote entry to the top
-      m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
-                                             m_contentStore.project<i_mru> (it));
-      return true;
-    }
+
+  return
+    m_contentStore
+    .insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet))
+    .second;
 }
     
 void 
-CcnxContentStoreLru::Print() const
+CcnxContentStoreLru::Print () const
 {
-  BOOST_FOREACH (const CcnxContentStoreEntry &entry, m_contentStore.get<i_mru> ())
+  BOOST_FOREACH (const CcnxContentStoreLruContainer::parent_trie &item, m_contentStore.getPolicy ())
     {
-      NS_LOG_INFO (entry.GetName ());
+      NS_LOG_INFO (item.payload ()->GetName ());
     }
 }
 
diff --git a/model/ccnx-content-store-lru.h b/model/ccnx-content-store-lru.h
index b510646..277290a 100644
--- a/model/ccnx-content-store-lru.h
+++ b/model/ccnx-content-store-lru.h
@@ -23,45 +23,22 @@
 
 #include "ccnx-content-store.h"
 
-#include <boost/multi_index_container.hpp>
-#include <boost/multi_index/tag.hpp>
-#include <boost/multi_index/ordered_index.hpp>
-#include <boost/multi_index/sequenced_index.hpp>
-#include <boost/multi_index/hashed_index.hpp>
-#include <boost/multi_index/mem_fun.hpp>
-
 #include "ccnx.h"
 #include "ccnx-name-components-hash-helper.h"
 
+#include "../utils/trie.h"
+
 namespace  ns3
 {
 /**
  * \ingroup ccnx
- * \brief Typedef for content store container implemented as a Boost.MultiIndex container
- *
- * - First index (tag<prefix>) is a unique hash index based on NDN prefix of the stored content.
- * - Second index (tag<mru>) is a sequential index used to maintain up to m_maxSize most recent used (MRU) entries in the content store
- *
- * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
+ * \brief Typedef for content store container implemented as trie with configurable replacement policy
  */
-struct CcnxContentStoreLruContainer
-{
-  /// @cond include_hidden
-  typedef
-  boost::multi_index::multi_index_container<
-    CcnxContentStoreEntry,
-    boost::multi_index::indexed_by<
-      boost::multi_index::hashed_unique<
-        boost::multi_index::tag<__ccnx_private::i_prefix>,
-        boost::multi_index::const_mem_fun<CcnxContentStoreEntry,
-                                          const CcnxNameComponents&,
-                                          &CcnxContentStoreEntry::GetName>,
-        CcnxPrefixHash>,
-      boost::multi_index::sequenced<boost::multi_index::tag<__ccnx_private::i_mru> >
-      >
-    > type;
-  /// @endcond
-};
+typedef indexed_trie<CcnxNameComponents,
+                     CcnxContentStoreEntry, smart_pointer_payload_traits<CcnxContentStoreEntry>,
+                     lru_policy_traits<CcnxNameComponents,
+                                       CcnxContentStoreEntry,
+                                       smart_pointer_payload_traits<CcnxContentStoreEntry> > > CcnxContentStoreLruContainer;
 
 /**
  * \ingroup ccnx
@@ -116,7 +93,7 @@
    * \brief Content store implemented as a Boost.MultiIndex container
    * \internal
    */
-  CcnxContentStoreLruContainer::type m_contentStore;
+  CcnxContentStoreLruContainer m_contentStore;
 };
 
 } //namespace ns3
diff --git a/model/ccnx-content-store.h b/model/ccnx-content-store.h
index 97615db..07888e7 100644
--- a/model/ccnx-content-store.h
+++ b/model/ccnx-content-store.h
@@ -47,7 +47,7 @@
  *
  * GetFullyFormedCcnxPacket method provided as a convenience
  */
-class CcnxContentStoreEntry
+class CcnxContentStoreEntry : public SimpleRefCount<CcnxContentStoreEntry>
 {
 public:
   /**
diff --git a/utils/trie.h b/utils/trie.h
index e45ee70..21bc362 100644
--- a/utils/trie.h
+++ b/utils/trie.h
@@ -340,7 +340,7 @@
     return this;
 
   typedef trie<FullKey, Payload, PayloadTraits> trie;
-  BOOST_FOREACH (const trie &subnode, children_)
+  BOOST_FOREACH (trie &subnode, children_)
     {
       iterator value = subnode.find ();
       if (value != 0)
@@ -373,7 +373,8 @@
 
 template<typename FullKey, typename Payload, typename PayloadTraits, typename PolicyHook>
 inline void
-trie<FullKey, Payload, PayloadTraits, PolicyHook>::erase ()
+trie<FullKey, Payload, PayloadTraits, PolicyHook>
+::erase ()
 {
   payload_ = PayloadTraits::empty_payload;
   prune ();
@@ -381,7 +382,8 @@
 
 template<typename FullKey, typename Payload, typename PayloadTraits, typename PolicyHook>
 inline void
-trie<FullKey, Payload, PayloadTraits, PolicyHook>::prune ()
+trie<FullKey, Payload, PayloadTraits, PolicyHook>
+::prune ()
 {
   if (payload_ == 0 && children_.size () == 0)
     {
@@ -414,7 +416,8 @@
 
 template<typename FullKey, typename Payload, typename PayloadTraits, typename PolicyHook>
 inline void
-trie<FullKey, Payload, PayloadTraits, PolicyHook>::PrintStat (std::ostream &os) const
+trie<FullKey, Payload, PayloadTraits, PolicyHook>
+::PrintStat (std::ostream &os) const
 {
   os << "# " << key_ << ((payload_ != 0)?"*":"") << ": " << children_.size() << " children" << std::endl;
   for (size_t bucket = 0, maxbucket = children_.bucket_count ();
@@ -548,7 +551,7 @@
     else
       {
         item.first->set_payload (payload);
-        // policy_traits::update (*item.first);
+        policy_.update (s_iterator_to (item.first));
       }
     
     return item;
@@ -564,27 +567,133 @@
   }
 
   /**
-   * @brief Perform the longest prefix match
-   * @param key the key for which to perform the longest prefix match
-   *
-   * For subsequent direct searches, policy_.lookup () should be called manually
-   *
-   * @return ->second is true if prefix in ->first is longer than key
-   *         ->third is always last node searched
+   * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
    */
-  inline boost::tuple< iterator, bool, iterator >
-  find (const FullKey &key)
+  inline iterator
+  longest_prefix_match (const FullKey &key)
   {
     boost::tuple< iterator, bool, iterator > item = trie_.find (key);
     if (item.template get<0> () != trie_.end ())
       {
         policy_.lookup (s_iterator_to (item.template get<0> ()));
       }
-    return boost::make_tuple (s_iterator_to (item.template get<0> ()),
-                              item.template get<1> (),
-                              s_iterator_to (item.template get<2> ()));
+    return item.template get<0> ();
   }
 
+  /**
+   * @brief Find a node that has prefix at least as the key (cache lookup)
+   */
+  inline iterator
+  deepest_prefix_match (const FullKey &key)
+  {
+    iterator foundItem, lastItem;
+    bool reachLast;
+    boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+    // guard in case we don't have anything in the trie
+    if (lastItem == trie_.end ())
+      return trie_.end ();
+    
+    if (reachLast)
+      {
+        if (foundItem == trie_.end ())
+          {
+            foundItem = lastItem->find (); // should be something
+          }
+        policy_.lookup (s_iterator_to (foundItem));
+        return foundItem;
+      }
+    else
+      { // couldn't find a node that has prefix at least as key
+        return trie_.end ();
+      }
+  }
+
+  /**
+   * @brief Find a node that has prefix at least as the key
+   */
+  template<class Predicate>
+  inline iterator
+  deepest_prefix_match (const FullKey &key, Predicate pred)
+  {
+    iterator foundItem, lastItem;
+    bool reachLast;
+    boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+    // guard in case we don't have anything in the trie
+    if (lastItem == trie_.end ())
+      return trie_.end ();
+    
+    if (reachLast)
+      {
+        foundItem = lastItem->find_if (pred); // may or may not find something
+        if (foundItem == trie_.end ())
+          {
+            return trie_.end ();
+          }
+        policy_.lookup (s_iterator_to (foundItem));
+        return foundItem;
+      }
+    else
+      { // couldn't find a node that has prefix at least as key
+        return trie_.end ();
+      }
+  }
+  
+  // /**
+  //  * @brief Perform the longest prefix match
+  //  * @param key the key for which to perform the longest prefix match
+  //  *
+  //  * @return ->second is true if prefix in ->first is longer than key
+  //  *         ->third is always last node searched
+  //  */
+  // inline boost::tuple< iterator, bool, iterator >
+  // find (const FullKey &key)
+  // {
+  //   boost::tuple< iterator, bool, iterator > item = trie_.find (key);
+  //   if (item.template get<0> () != trie_.end ())
+  //     {
+  //       policy_.lookup (s_iterator_to (item.template get<0> ()));
+  //     }
+  //   return boost::make_tuple (s_iterator_to (item.template get<0> ()),
+  //                             item.template get<1> (),
+  //                             s_iterator_to (item.template get<2> ()));
+  // }
+
+  // /**
+  //  * @brief Find next payload of the sub-trie
+  //  * @param start Start for the search (root for the sub-trie)
+  //  * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
+  //  */
+  // inline iterator
+  // find (iterator start)
+  // {
+  //   iterator item = start->find ();
+  //   if (item != trie_.end ())
+  //     {
+  //       policy_.lookup (s_iterator_to (item));
+  //     }
+  //   return item;
+  // }
+
+  // /**
+  //  * @brief Find next payload of the sub-trie satisfying the predicate
+  //  * @param start Start for the search (root for the sub-trie)
+  //  * @param pred predicate
+  //  * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
+  //  */
+  // template<class Predicate>
+  // inline iterator
+  // find_if (iterator start, Predicate pred)
+  // {
+  //   iterator item = start->find (pred);
+  //   if (item != trie_.end ())
+  //     {
+  //       policy_.lookup (s_iterator_to (item));
+  //     }
+  //   return item;
+  // }
+
   iterator end ()
   {
     return 0;