Implementing content store using Boost.MultiIndex container
Adding .SetGroupName("Ccnx") call to Ccnx-related objects (probably
missed some). This call is advisory, but it is nice to give users
information about Ccnx-related objects (this information is accessible
via --PrintGroup=Ccnx command line argument)
diff --git a/model/ccnx-content-store.h b/model/ccnx-content-store.h
index 58e060e..ee29f81 100644
--- a/model/ccnx-content-store.h
+++ b/model/ccnx-content-store.h
@@ -18,60 +18,327 @@
* Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
*/
-#ifndef ccnx_content_store_h
-#define ccnx_content_store_h
+#ifndef CCNX_CONTENT_STORE_H
+#define CCNX_CONTENT_STORE_H
-#include <ns3/system-mutex.h>
+#include "ns3/object.h"
+#include "ns3/ptr.h"
+
#include <list>
#include <string>
-#include "hash-helper.h"
+#include <iostream>
-using namespace std;
+#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 "hash-helper.h"
+#include "ccnx-content-object-header.h"
+#include "ccnx-interest-header.h"
+#include "name-components.h"
namespace ns3
{
-
-class CsEntry;
-typedef list<CsEntry*>::iterator CsLruIterator;
-
-//structure for CS entry
-struct CsEntry
-{
- string contentName;
- int contentSize;
-
- CsLruIterator lruPosition;
-};
-
-typedef string_key_hash_t<CsEntry>::iterator CsIterator;
-typedef string_key_hash_t<CsEntry>::iterator CsRangeIterator;
-
-class CcnxContentStore
+class Packet;
+
+/**
+ * \ingroup ccnx
+ * \brief NDN content store entry
+ *
+ * Content store entry stores separately pseudo header and content of
+ * ContentObject packet. It is responsibility of the caller to
+ * construct a fully formed CcnxPacket by calling Copy(), AddHeader(),
+ * AddTail() on the packet received by GetPacket() method.
+ *
+ * GetFullyFormedCcnxPacket method provided as a convenience
+ */
+class CcnxContentStoreEntry
{
public:
- CcnxContentStore( int max_size=NDN_CONTENT_STORE_SIZE );
- virtual ~CcnxContentStore( );
+ /**
+ * \brief Construct content store entry
+ *
+ * \param header Parsed CcnxContentObject header
+ * \param packet Original CCNx packet
+ *
+ * The constructor will make a copy of the supplied packet and calls
+ * RemoveHeader and RemoveTail on the copy.
+ */
+ CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
+
+ /**
+ * \brief Get prefix of the stored entry
+ * \returns prefix of the stored entry
+ */
+ inline const Name::Components&
+ GetName () const;
+
+ /**
+ * \brief Get CcnxContentObjectHeader of the stored entry
+ * \returns CcnxContentObjectHeader of the stored entry
+ */
+ inline Ptr<const CcnxContentObjectHeader>
+ GetHeader () const;
+
+ /**
+ * \brief Get content of the stored entry
+ * \returns content of the stored entry
+ */
+ inline Ptr<const Packet>
+ GetPacket () const;
+
+ /**
+ * \brief Convenience method to create a fully formed CCNx packet from stored header and content
+ * \returns A read-write copy of the packet with CcnxContentObjectHeader and CcxnContentObjectTail
+ */
+ Ptr<Packet>
+ GetFullyFormedCcnxPacket () const;
+
+// Copy constructor is required by the container. Though, we're
+// storing only two pointers, so shouldn't be a problem
+// private:
+// CcnxContentStoreEntry (const CcnxContentStoreEntry &); ///< disabled copy constructor
+// CcnxContentStoreEntry& operator= (const CcnxContentStoreEntry&); ///< disabled copy operator
+
+private:
+ Ptr<CcnxContentObjectHeader> m_header; ///< \brief non-modifiable CcnxContentObjectHeader
+ Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet
+
+ static CcnxContentObjectTail m_tail; ///< \internal for optimization purposes
+};
+
+/// \brief Private namespace for CCNx implementation
+namespace __ccnx_private
+{
+class hash {}; ///< tag for Boost.Multiindex container (hashed prefix)
+class mru {}; ///< tag for Boost.Multiindex container (most recent used index)
+#ifdef _DEBUG
+class ordered {}; ///< tag for Boost.MultiIndex container (ordered by prefix)
+#endif
+};
+
+/**
+ * \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
+ * - Third index (tag<ordered>) is just a helper to provide stored prefixes in ordered way. Should be disabled in production build
+ *
+ * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
+ */
+struct CcnxContentStoreContainer
+{
+ typedef
+ boost::multi_index::multi_index_container<
+ CcnxContentStoreEntry,
+ boost::multi_index::indexed_by<
+ boost::multi_index::hashed_unique<
+ boost::multi_index::tag<__ccnx_private::hash>,
+ boost::multi_index::const_mem_fun<CcnxContentStoreEntry,
+ const Name::Components&,
+ &CcnxContentStoreEntry::GetName>,
+ CcnxPrefixHash>,
+ boost::multi_index::sequenced<boost::multi_index::tag<__ccnx_private::mru> >
+#ifdef _DEBUG
+ ,
+ boost::multi_index::ordered_unique<
+ boost::multi_index::tag<__ccnx_private::ordered>,
+ boost::multi_index::const_mem_fun<CcnxContentStoreEntry,
+ const Name::Components&,
+ &CcnxContentStoreEntry::GetName>
+ >
+#endif
+ >
+ > type;
+};
+
+/**
+ * \ingroup ccnx
+ * \brief Typedef for hash index of content store container
+ */
+struct CcnxContentStoreByPrefix
+{
+ typedef
+ CcnxContentStoreContainer::type::index<__ccnx_private::hash>::type
+ type;
+};
+
+/**
+ * \ingroup ccnx
+ * \brief Typedef for MRU index of content store container
+ */
+struct CcnxContentStoreByMru
+{
+ typedef
+ CcnxContentStoreContainer::type::index<__ccnx_private::mru>::type
+ type;
+};
+
+#ifdef _DEBUG
+#define DUMP_INDEX_TAG ordered
+#define DUMP_INDEX CcnxContentStoreOrderedPrefix
+/**
+ * \ingroup ccnx
+ * \brief Typedef for ordered index of content store container
+ */
+struct CcnxContentStoreOrderedPrefix
+{
+ typedef
+ CcnxContentStoreContainer::type::index<__ccnx_private::ordered>::type
+ type;
+};
+#else
+#define DUMP_INDEX_TAG mru
+#define DUMP_INDEX CcnxContentStoreByMru
+#endif
+
+/**
+ * \ingroup ccnx
+ * \brief NDN content store entry
+ */
+class CcnxContentStore : public Object
+{
+public:
+ /**
+ * \brief Interface ID
+ *
+ * \return interface ID
+ */
+ static TypeId GetTypeId ();
+
+ // typedef string_key_hash_t<CsEntry>::iterator CsIterator;
+ // typedef string_key_hash_t<CsEntry>::iterator CsRangeIterator;
+
+ /**
+ * Default constructor
+ */
+ CcnxContentStore( );
+ virtual ~CcnxContentStore( );
- // Find corresponding CS entry for the given content name
- CsEntry* Lookup( const string prefix );
- //bool isValid( const CsIterator &it ) { return it!=_cs.end(); }
+ /**
+ * \brief Find corresponding CS entry for the given interest
+ *
+ * \param interest Interest for which matching content store entry
+ * will be searched
+ *
+ * If an entry is found, it is promoted to the top of most recent
+ * used entries index, \see m_contentStore
+ */
+ Ptr<Packet>
+ Lookup (Ptr<const CcnxInterestHeader> interest);
- // Add new content to the content store. Old content will be replaced
- void Add( const string contentName, int contentSize );
-
- // Dump content store entries
- void Dump( );
+ /**
+ * \brief Add a new content to the content store.
+ *
+ * \param header Fully parsed CcnxContentObjectHeader
+ * \param packet Fully formed CCNx packet to add to content store
+ * (will be copied and stripped down of headers)
+ *
+ * If entry with the same prefix exists, the old entry will be
+ * promoted to the top of the MRU hash
+ */
+ void
+ Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
+
+ /**
+ * \brief Set maximum size of content store
+ *
+ * \param size size in packets
+ */
+ inline void
+ SetMaxSize (uint32_t maxSize);
+
+ /**
+ * \brief Get maximum size of content store
+ *
+ * \returns size in packets
+ */
+ inline uint32_t
+ GetMaxSize () const;
+
+ /**
+ * \brief Print out content store entries
+ *
+ * Debug build provides dumping of content store entries in
+ * lexicographical order of corresponding prefixes
+ *
+ * Release build dumps everything in MRU order
+ */
+ void Print () const;
protected:
- //move the given CS entry to the head of the list
- void Promote( CsEntry &entry );
-
+ // /**
+ // * \brief Move the given CS entry to the head of the list
+ // *
+ // * \param entry Content Store entry
+ // */
+ // void Promote( CsEntry &entry );
+
+ /**
+ * \todo Alex: DoDispose and NotifyNewAggregate are seem to be very
+ * important, but I'm not yet sure what exactly they are supposed to
+ * do
+ */
+ // virtual void DoDispose ();
+ // virtual void NotifyNewAggregate ();
+
private:
- int m_maxSize; // maximum number of entries in cache
-
- string_key_hash_t<CsEntry> m_contentStore; // actual content store
- list<CsEntry*> m_LRU; // LRU index of the content store
- SystemMutex m_csMutex; // just to make sure we are not
+ CcnxContentStore (const CcnxContentStore &o); ///< Disabled copy constructor
+ CcnxContentStore& operator= (const CcnxContentStore &o); ///< Disabled copy operator
+
+private:
+ int m_maxSize; ///< \brief maximum number of entries in cache \internal
+ // string_key_hash_t<CsEntry> m_contentStore; ///< \brief actual content store \internal
+
+ /**
+ * \brief Content store implemented as a Boost.MultiIndex container
+ * \internal
+ */
+ CcnxContentStoreContainer::type m_contentStore;
};
+
+inline std::ostream&
+operator<< (std::ostream &os, const CcnxContentStore &cs)
+{
+ cs.Print ();
+ return os;
}
-#endif
+
+const Name::Components&
+CcnxContentStoreEntry::GetName () const
+{
+ return m_header->GetName ();
+}
+
+Ptr<const CcnxContentObjectHeader>
+CcnxContentStoreEntry::GetHeader () const
+{
+ return m_header;
+}
+
+Ptr<const Packet>
+CcnxContentStoreEntry::GetPacket () const
+{
+ return m_packet;
+}
+
+
+inline void
+CcnxContentStore::SetMaxSize (uint32_t maxSize)
+{
+ m_maxSize = maxSize;
+}
+
+inline uint32_t
+CcnxContentStore::GetMaxSize () const
+{
+ return m_maxSize;
+}
+
+} //namespace ns3
+
+#endif // CCNX_CONTENT_STORE_H