NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_IMS_IN_MEMORY_STORAGE_HPP
23 #define NDN_IMS_IN_MEMORY_STORAGE_HPP
24 
26 
27 #include <iterator>
28 #include <stack>
29 
30 #include <boost/multi_index_container.hpp>
31 #include <boost/multi_index/identity.hpp>
32 #include <boost/multi_index/mem_fun.hpp>
33 #include <boost/multi_index/member.hpp>
34 #include <boost/multi_index/ordered_index.hpp>
35 #include <boost/multi_index/sequenced_index.hpp>
36 
37 namespace ndn {
38 
41 class InMemoryStorage : noncopyable
42 {
43 public:
44  // multi_index_container to implement storage
45  class byFullName;
46 
47  typedef boost::multi_index_container<
49  boost::multi_index::indexed_by<
50 
51  // by Full Name
52  boost::multi_index::ordered_unique<
53  boost::multi_index::tag<byFullName>,
54  boost::multi_index::const_mem_fun<InMemoryStorageEntry, const Name&,
56  std::less<Name>
57  >
58 
59  >
60  > Cache;
61 
67  {
68  public:
69  using iterator_category = std::input_iterator_tag;
70  using value_type = const Data;
71  using difference_type = std::ptrdiff_t;
72  using pointer = value_type*;
74 
75  const_iterator(const Data* ptr, const Cache* cache,
77 
79  operator++();
80 
82  operator++(int);
83 
84  reference
85  operator*();
86 
87  pointer
88  operator->();
89 
90  bool
91  operator==(const const_iterator& rhs);
92 
93  bool
94  operator!=(const const_iterator& rhs);
95 
96  private:
97  const Data* m_ptr;
98  const Cache* m_cache;
100  };
101 
105  class Error : public std::runtime_error
106  {
107  public:
109  : std::runtime_error("Cannot reduce the capacity of the in-memory storage!")
110  {
111  }
112  };
113 
117  explicit
118  InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
119 
123  explicit
124  InMemoryStorage(DummyIoService& ioService,
125  size_t limit = std::numeric_limits<size_t>::max());
126 
130  virtual
132 
145  void
146  insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow = INFINITE_WINDOW);
147 
158  shared_ptr<const Data>
159  find(const Interest& interest);
160 
172  shared_ptr<const Data>
173  find(const Name& name);
174 
185  void
186  erase(const Name& prefix, const bool isPrefix = true);
187 
190  size_t
191  getLimit() const
192  {
193  return m_limit;
194  }
195 
198  size_t
199  size() const
200  {
201  return m_nPackets;
202  }
203 
210  begin() const;
211 
218  end() const;
219 
224  virtual void
225  afterAccess(InMemoryStorageEntry* entry);
226 
231  virtual void
233 
238  virtual void
240 
248  virtual bool
249  evictItem() = 0;
250 
254  void
255  setCapacity(size_t nMaxPackets);
256 
260  size_t
261  getCapacity() const
262  {
263  return m_capacity;
264  }
265 
268  bool
269  isFull() const
270  {
271  return size() >= m_capacity;
272  }
273 
280  void
281  eraseImpl(const Name& name);
282 
285  void
286  printCache(std::ostream& os) const;
287 
293  freeEntry(Cache::iterator it);
294 
310  selectChild(const Interest& interest,
311  Cache::index<byFullName>::type::iterator startingPoint) const;
312 
320 
321 private:
322  void
323  init();
324 
325 public:
326  static const time::milliseconds INFINITE_WINDOW;
327 
328 private:
329  static const time::milliseconds ZERO_WINDOW;
330 
331 private:
332  Cache m_cache;
334  size_t m_limit;
336  const size_t m_initCapacity = 16;
338  size_t m_capacity;
340  size_t m_nPackets;
342  std::stack<InMemoryStorageEntry*> m_freeEntries;
344  unique_ptr<Scheduler> m_scheduler;
345 };
346 
347 } // namespace ndn
348 
349 #endif // NDN_IMS_IN_MEMORY_STORAGE_HPP
Copyright (c) 2011-2015 Regents of the University of California.
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
Cache::index< byFullName >::type::iterator findNextFresh(Cache::index< byFullName >::type::iterator startingPoint) const
Get the next iterator (include startingPoint) that satisfies MustBeFresh requirement.
Represents in-memory storage.
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy.
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
const Name & getFullName() const
Returns the full name (including implicit digest) of the Data packet stored in the in-memory storage ...
Represents an Interest packet.
Definition: interest.hpp:44
bool operator==(const const_iterator &rhs)
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
bool operator!=(const const_iterator &rhs)
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
InMemoryStorage(size_t limit=std::numeric_limits< size_t >::max())
Create a InMemoryStorage with up to limit entries The InMemoryStorage created through this method wil...
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
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).
Represents a self-defined const_iterator for the in-memory storage.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
Represents an absolute name.
Definition: name.hpp:43
Represents an in-memory storage entry.
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
std::input_iterator_tag iterator_category
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
Represents a Data packet.
Definition: data.hpp:35
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
static const time::milliseconds INFINITE_WINDOW
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)