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-2022 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 
118  explicit
119  InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
120 
125  explicit
126  InMemoryStorage(boost::asio::io_service& ioService,
127  size_t limit = std::numeric_limits<size_t>::max());
128 
132  virtual
134 
147  void
148  insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow = INFINITE_WINDOW);
149 
160  shared_ptr<const Data>
161  find(const Interest& interest);
162 
173  shared_ptr<const Data>
174  find(const Name& name);
175 
187  void
188  erase(const Name& prefix, const bool isPrefix = true);
189 
192  size_t
193  getLimit() const
194  {
195  return m_limit;
196  }
197 
200  size_t
201  size() const
202  {
203  return m_nPackets;
204  }
205 
211  begin() const;
212 
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 
312  selectChild(const Interest& interest,
313  Cache::index<byFullName>::type::iterator startingPoint) const;
314 
320  Cache::index<byFullName>::type::iterator
321  findNextFresh(Cache::index<byFullName>::type::iterator startingPoint) const;
322 
323 private:
324  void
325  init();
326 
327 public:
329 
330 private:
331  static const time::milliseconds ZERO_WINDOW;
332 
333 private:
334  Cache m_cache;
336  size_t m_limit;
338  const size_t m_initCapacity = 16;
340  size_t m_capacity;
342  size_t m_nPackets;
344  std::stack<InMemoryStorageEntry*> m_freeEntries;
346  unique_ptr<Scheduler> m_scheduler;
347 };
348 
349 } // namespace ndn
350 
351 #endif // NDN_CXX_IMS_IN_MEMORY_STORAGE_HPP
Represents a Data packet.
Definition: data.hpp:39
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.
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:44
#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:69
Definition: data.cpp:25