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-2021 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_CXX_IMS_IN_MEMORY_STORAGE_HPP
23 #define NDN_CXX_IMS_IN_MEMORY_STORAGE_HPP
24 
27 
28 #include <iterator>
29 #include <stack>
30 
31 #include <boost/multi_index_container.hpp>
32 #include <boost/multi_index/identity.hpp>
33 #include <boost/multi_index/mem_fun.hpp>
34 #include <boost/multi_index/member.hpp>
35 #include <boost/multi_index/ordered_index.hpp>
36 #include <boost/multi_index/sequenced_index.hpp>
37 
38 namespace ndn {
39 
42 class InMemoryStorage : noncopyable
43 {
44 public:
45  // multi_index_container to implement storage
46  class byFullName;
47 
48  typedef boost::multi_index_container<
50  boost::multi_index::indexed_by<
51 
52  // by Full Name
53  boost::multi_index::ordered_unique<
54  boost::multi_index::tag<byFullName>,
55  boost::multi_index::const_mem_fun<InMemoryStorageEntry, const Name&,
57  std::less<Name>
58  >
59 
60  >
61  > Cache;
62 
68  {
69  public:
70  using iterator_category = std::input_iterator_tag;
71  using value_type = const Data;
72  using difference_type = std::ptrdiff_t;
73  using pointer = value_type*;
75 
76  const_iterator(const Data* ptr, const Cache* cache,
77  Cache::index<byFullName>::type::iterator it);
78 
80  operator++();
81 
83  operator++(int);
84 
85  reference
86  operator*();
87 
88  pointer
89  operator->();
90 
91  bool
92  operator==(const const_iterator& rhs);
93 
94  bool
95  operator!=(const const_iterator& rhs);
96 
97  private:
98  const Data* m_ptr;
99  const Cache* m_cache;
100  Cache::index<byFullName>::type::iterator m_it;
101  };
102 
106  class Error : public std::runtime_error
107  {
108  public:
110  : std::runtime_error("Cannot reduce the capacity of the in-memory storage!")
111  {
112  }
113  };
114 
118  explicit
119  InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
120 
124  explicit
125  InMemoryStorage(DummyIoService& ioService,
126  size_t limit = std::numeric_limits<size_t>::max());
127 
131  virtual
133 
146  void
147  insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow = INFINITE_WINDOW);
148 
159  shared_ptr<const Data>
160  find(const Interest& interest);
161 
173  shared_ptr<const Data>
174  find(const Name& name);
175 
186  void
187  erase(const Name& prefix, const bool isPrefix = true);
188 
191  size_t
192  getLimit() const
193  {
194  return m_limit;
195  }
196 
199  size_t
200  size() const
201  {
202  return m_nPackets;
203  }
204 
211  begin() const;
212 
219  end() const;
220 
225  virtual void
226  afterAccess(InMemoryStorageEntry* entry);
227 
232  virtual void
233  afterInsert(InMemoryStorageEntry* entry);
234 
239  virtual void
240  beforeErase(InMemoryStorageEntry* entry);
241 
249  virtual bool
250  evictItem() = 0;
251 
255  void
256  setCapacity(size_t nMaxPackets);
257 
261  size_t
262  getCapacity() const
263  {
264  return m_capacity;
265  }
266 
269  bool
270  isFull() const
271  {
272  return size() >= m_capacity;
273  }
274 
281  void
282  eraseImpl(const Name& name);
283 
286  void
287  printCache(std::ostream& os) const;
288 
293  Cache::iterator
294  freeEntry(Cache::iterator it);
295 
310  InMemoryStorageEntry*
311  selectChild(const Interest& interest,
312  Cache::index<byFullName>::type::iterator startingPoint) const;
313 
319  Cache::index<byFullName>::type::iterator
320  findNextFresh(Cache::index<byFullName>::type::iterator startingPoint) const;
321 
322 private:
323  void
324  init();
325 
326 public:
328 
329 private:
330  static const time::milliseconds ZERO_WINDOW;
331 
332 private:
333  Cache m_cache;
335  size_t m_limit;
337  const size_t m_initCapacity = 16;
339  size_t m_capacity;
341  size_t m_nPackets;
343  std::stack<InMemoryStorageEntry*> m_freeEntries;
345  unique_ptr<scheduler::Scheduler> m_scheduler;
346 };
347 
348 } // namespace ndn
349 
350 #endif // NDN_CXX_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 ...
STL namespace.
Represents an Interest packet.
Definition: interest.hpp:48
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:47
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
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:48
Represents an absolute name.
Definition: name.hpp:41
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:37
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)
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48