CcnxContentStore with BOOST hash
diff --git a/model/ccnx-content-store.cc b/model/ccnx-content-store.cc
new file mode 100644
index 0000000..31a9fb3
--- /dev/null
+++ b/model/ccnx-content-store.cc
@@ -0,0 +1,103 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
+ */
+
+#include "ccnx-content-store.h"
+#include "ns3/log.h"
+
+
+NS_LOG_COMPONENT_DEFINE ("CcnxContentStore");
+
+namespace ns3
+{
+
+CcnxContentStore::CcnxContentStore( int maxSize )
+ : m_maxSize(maxSize) { }
+
+CcnxContentStore::~CcnxContentStore( )
+ { }
+
+//Find corresponding CS entry for the given content name
+CsEntry*
+CcnxContentStore::Lookup(const string prefix )
+{
+ CriticalSection section(m_csMutex);
+
+ CsEntry *result = &(m_contentStore.at(prefix));
+
+ if(result != NULL)
+ Promote (*result);
+
+ return result;
+}
+
+//move the given CS entry to the head of the list
+void
+CcnxContentStore::Promote(CsEntry &ce )
+{
+ // should not lock mutex. Otherwise deadlocks will be welcome
+ if( m_LRU.front() == &ce ) return;
+
+ //assert( *(ce.lruPosition)==&ce ); // should point to the same object
+
+ // swaping positions in _lru
+ m_LRU.erase( ce.lruPosition );
+ m_LRU.push_front( &ce );
+ ce.lruPosition = m_LRU.begin( );
+
+ //assert( *(ce.lruPosition)==&ce ); // should point to the same object
+}
+
+//Add entry to content store, if content store is full, use LRU replacement
+void
+CcnxContentStore::Add( const string contentName, int contentSize )
+{
+ CriticalSection section(m_csMutex);
+
+ m_contentStore.erase(m_contentStore.find(contentName));
+
+ if((int)m_contentStore.size() == m_maxSize )
+ {
+ CsEntry *entry = m_LRU.back();
+ m_contentStore.erase(m_contentStore.find(entry->contentName));
+ m_LRU.pop_back( );
+ }
+
+ CsEntry ce;
+ ce.contentName = contentName;
+ ce.contentSize = contentSize;
+
+ m_contentStore[contentName] = ce;
+
+ CsEntry *ce_in_hash = &(m_contentStore.at(contentName));
+ m_LRU.push_front( ce_in_hash );
+ ce_in_hash->lruPosition = m_LRU.begin( );
+}
+
+void
+CcnxContentStore::Dump()
+{
+ CriticalSection section(m_csMutex);
+
+ BOOST_FOREACH(string_key_hash_t<CsEntry>::value_type i, m_contentStore)
+ {
+ NS_LOG_INFO ("Key = " << i.first << " Value = " << i.second.contentName);
+ }
+}
+}
\ No newline at end of file
diff --git a/model/ccnx-content-store.h b/model/ccnx-content-store.h
new file mode 100644
index 0000000..58e060e
--- /dev/null
+++ b/model/ccnx-content-store.h
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
+ */
+
+#ifndef ccnx_content_store_h
+#define ccnx_content_store_h
+
+#include <ns3/system-mutex.h>
+#include <list>
+#include <string>
+#include "hash-helper.h"
+
+using namespace std;
+
+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
+{
+public:
+ CcnxContentStore( int max_size=NDN_CONTENT_STORE_SIZE );
+ 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(); }
+
+ // 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( );
+
+protected:
+ //move the given CS entry to the head of the list
+ void Promote( CsEntry &entry );
+
+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
+};
+}
+#endif
diff --git a/model/hash-helper.h b/model/hash-helper.h
new file mode 100644
index 0000000..880a217
--- /dev/null
+++ b/model/hash-helper.h
@@ -0,0 +1,65 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
+ */
+
+#ifndef ccnx_hash_helper_h
+#define ccnx_hash_helper_h
+
+#include <string>
+#include <boost/unordered_map.hpp>
+#include <boost/functional/hash.hpp>
+#include <boost/foreach.hpp>
+
+//size of content store
+#define NDN_CONTENT_STORE_SIZE 100
+//maximum length of content name
+#define NDN_MAX_NAME_LENGTH 30
+
+//using namespace std;
+
+#define KEY(x) x->first
+#define VALUE(x) x->second
+
+
+/*template<typename T>
+struct hash : public std::unary_function<T, std::size_t> {
+ std::size_t operator()(T const&) const;
+};*/
+
+struct string_hash : public std::unary_function<std::string, std::size_t>
+{
+ inline std::size_t operator( )( std::string str ) const
+ {
+ std::size_t hash = str.size() + 23;
+ for( std::string::const_iterator it = str.begin( ); it!=str.end(); it++ )
+ {
+ hash = ((hash << 6) ^ (hash >> 27)) + static_cast<std::size_t>( *it );
+ }
+
+ return boost::hash_value(hash); //hash;
+ }
+};
+
+// A collision-chaining hash table mapping strings to ints.
+template<typename Value>
+class string_key_hash_t : public boost::unordered_map<std::string,Value, string_hash, std::equal_to<std::string>,std::allocator<std::string> >
+{
+};
+
+#endif
\ No newline at end of file