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 
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,
76  Cache::index<byFullName>::type::iterator it);
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;
99  Cache::index<byFullName>::type::iterator m_it;
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(boost::asio::io_service& 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
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 
292  Cache::iterator
293  freeEntry(Cache::iterator it);
294 
310  selectChild(const Interest& interest,
311  Cache::index<byFullName>::type::iterator startingPoint) const;
312 
318  Cache::index<byFullName>::type::iterator
319  findNextFresh(Cache::index<byFullName>::type::iterator startingPoint) const;
320 
321 private:
322  void
323  init();
324 
325 public:
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_CXX_IMS_IN_MEMORY_STORAGE_HPP
Represents a Data packet.
Definition: data.hpp:38
Represents an in-memory storage entry.
const Name & getFullName() const
Returns the full name (including implicit digest) of the Data packet stored in the in-memory storage ...
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
Represents a self-defined const_iterator for the in-memory storage.
bool operator!=(const const_iterator &rhs)
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
bool operator==(const const_iterator &rhs)
std::input_iterator_tag iterator_category
Represents in-memory storage.
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...
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
virtual void afterAccess(InMemoryStorageEntry *entry)
Update the entry when the entry is returned by the find() function according to derived class impleme...
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
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
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy.
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
virtual void afterInsert(InMemoryStorageEntry *entry)
Update the entry or other data structures after a entry is successfully inserted according to derived...
static const time::milliseconds INFINITE_WINDOW
void setCapacity(size_t nMaxPackets)
sets current capacity of in-memory storage (in packets)
Represents an Interest packet.
Definition: interest.hpp:50
Represents an absolute name.
Definition: name.hpp:46
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ Data
Definition: tlv.hpp:66
Definition: data.cpp:25