NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
in-memory-storage.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_UTIL_IN_MEMORY_STORAGE_HPP
23 #define NDN_UTIL_IN_MEMORY_STORAGE_HPP
24 
25 #include "../common.hpp"
26 #include "../interest.hpp"
27 #include "../data.hpp"
28 
30 
31 #include <boost/multi_index/member.hpp>
32 #include <boost/multi_index_container.hpp>
33 #include <boost/multi_index/ordered_index.hpp>
34 #include <boost/multi_index/sequenced_index.hpp>
35 #include <boost/multi_index/identity.hpp>
36 #include <boost/multi_index/mem_fun.hpp>
37 
38 #include <stack>
39 #include <iterator>
40 
41 namespace ndn {
42 namespace util {
43 
46 class InMemoryStorage : noncopyable
47 {
48 public:
49  //multi_index_container to implement storage
50  class byFullName;
51 
52  typedef boost::multi_index_container<
54  boost::multi_index::indexed_by<
55 
56  // by Full Name
57  boost::multi_index::ordered_unique<
58  boost::multi_index::tag<byFullName>,
59  boost::multi_index::const_mem_fun<InMemoryStorageEntry, const Name&,
61  std::less<Name>
62  >
63 
64  >
65  > Cache;
66 
71  class const_iterator : public std::iterator<std::input_iterator_tag, const Data>
72  {
73  public:
74  const_iterator(const Data* ptr, const Cache* cache,
76 
78  operator++();
79 
81  operator++(int);
82 
83  const Data&
84  operator*();
85 
86  const Data*
87  operator->();
88 
89  bool
90  operator==(const const_iterator& rhs);
91 
92  bool
93  operator!=(const const_iterator& rhs);
94 
95  private:
96  const Data* m_ptr;
97  const Cache* m_cache;
99  };
100 
104  class Error : public std::runtime_error
105  {
106  public:
107  Error() : std::runtime_error("Cannot reduce the capacity of the in-memory storage!")
108  {
109  }
110  };
111 
112  explicit
113  InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
114 
118  virtual
120 
129  void
130  insert(const Data& data);
131 
142  shared_ptr<const Data>
143  find(const Interest& interest);
144 
156  shared_ptr<const Data>
157  find(const Name& name);
158 
168  void
169  erase(const Name& prefix, const bool isPrefix = true);
170 
173  size_t
174  getLimit() const
175  {
176  return m_limit;
177  }
178 
181  size_t
182  size() const
183  {
184  return m_nPackets;
185  }
186 
193  begin() const;
194 
201  end() const;
202 
207  virtual void
208  afterAccess(InMemoryStorageEntry* entry);
209 
214  virtual void
215  afterInsert(InMemoryStorageEntry* entry);
216 
221  virtual void
222  beforeErase(InMemoryStorageEntry* entry);
223 
231  virtual bool
232  evictItem() = 0;
233 
237  void
238  setCapacity(size_t nMaxPackets);
239 
243  size_t
244  getCapacity() const
245  {
246  return m_capacity;
247  }
248 
251  bool
252  isFull() const
253  {
254  return size() >= m_capacity;
255  }
256 
263  void
264  eraseImpl(const Name& name);
265 
268  void
269  printCache(std::ostream& os) const;
270 
276  freeEntry(Cache::iterator it);
277 
292  InMemoryStorageEntry*
293  selectChild(const Interest& interest,
294  Cache::index<byFullName>::type::iterator startingPoint) const;
295 
296 private:
297  Cache m_cache;
299  size_t m_limit;
301  size_t m_capacity;
303  size_t m_nPackets;
305  std::stack<InMemoryStorageEntry*> m_freeEntries;
306 };
307 
308 } // namespace util
309 } // namespace ndn
310 
311 #endif // NDN_UTIL_IN_MEMORY_STORAGE_HPP
Copyright (c) 2011-2015 Regents of the University of California.
void insert(const Data &data)
Inserts a Data packet.
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise ...
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
InMemoryStorage(size_t limit=std::numeric_limits< size_t >::max())
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
Represents a self-defined const_iterator for the in-memory storage.
STL namespace.
represents an Interest packet
Definition: interest.hpp:45
bool operator!=(const const_iterator &rhs)
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy...
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Represents in-memory storage.
boost::multi_index_container< InMemoryStorageEntry *, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< byFullName >, boost::multi_index::const_mem_fun< InMemoryStorageEntry, const Name &,&InMemoryStorageEntry::getFullName >, std::less< Name > > > > Cache
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)
const Name & getFullName() const
Returns the full name (including implicit digest) of the Data packet stored in the in-memory storage ...
Represents an in-memory storage entry.
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
bool operator==(const const_iterator &rhs)
virtual void afterInsert(InMemoryStorageEntry *entry)
Update the entry or other data structures after a entry is successfully inserted according to derived...
InMemoryStorageEntry * selectChild(const Interest &interest, Cache::index< byFullName >::type::iterator startingPoint) const
Implements child selector (leftmost, rightmost, undeclared).
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
represents a Data packet
Definition: data.hpp:39
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.