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 
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
234 
239  virtual void
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 
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:
327  static const time::milliseconds INFINITE_WINDOW;
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_IMS_IN_MEMORY_STORAGE_HPP
ndn::InMemoryStorage::begin
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
Definition: in-memory-storage.cpp:356
ndn::InMemoryStorage::selectChild
InMemoryStorageEntry * selectChild(const Interest &interest, Cache::index< byFullName >::type::iterator startingPoint) const
Implements child selector (leftmost, rightmost, undeclared).
Definition: in-memory-storage.cpp:262
ndn::InMemoryStorage::insert
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
Definition: in-memory-storage.cpp:161
ndn::InMemoryStorage::find
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
Definition: in-memory-storage.cpp:217
ndn::InMemoryStorage::Cache
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
Definition: in-memory-storage.hpp:46
in-memory-storage-entry.hpp
ndn::InMemoryStorage::const_iterator::operator++
const_iterator & operator++()
Definition: in-memory-storage.cpp:39
ndn::InMemoryStorage::const_iterator::difference_type
std::ptrdiff_t difference_type
Definition: in-memory-storage.hpp:72
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
ndn::InMemoryStorage::erase
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
Definition: in-memory-storage.cpp:321
ndn::InMemoryStorage::size
size_t size() const
Definition: in-memory-storage.hpp:200
ndn::InMemoryStorage::eraseImpl
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
Definition: in-memory-storage.cpp:346
scheduler.hpp
ndn::InMemoryStorage::findNextFresh
Cache::index< byFullName >::type::iterator findNextFresh(Cache::index< byFullName >::type::iterator startingPoint) const
Get the next iterator (include startingPoint) that satisfies MustBeFresh requirement.
Definition: in-memory-storage.cpp:251
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::InMemoryStorageEntry
Represents an in-memory storage entry.
Definition: in-memory-storage-entry.hpp:34
ndn::InMemoryStorage::const_iterator::operator->
pointer operator->()
Definition: in-memory-storage.cpp:67
ndn::InMemoryStorage::isFull
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise
Definition: in-memory-storage.hpp:270
ndn::DummyIoService
Definition: asio-fwd.hpp:28
ndn::InMemoryStorage::InMemoryStorage
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...
Definition: in-memory-storage.cpp:84
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
ndn::InMemoryStorage::const_iterator::operator!=
bool operator!=(const const_iterator &rhs)
Definition: in-memory-storage.cpp:79
ndn::InMemoryStorage::INFINITE_WINDOW
static const time::milliseconds INFINITE_WINDOW
Definition: in-memory-storage.hpp:327
ndn::InMemoryStorage::evictItem
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy.
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::InMemoryStorage::const_iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: in-memory-storage.hpp:70
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::InMemoryStorage::const_iterator::operator*
reference operator*()
Definition: in-memory-storage.cpp:61
ndn::tlv::Data
@ Data
Definition: tlv.hpp:66
ndn::InMemoryStorageEntry::getFullName
const Name & getFullName() const
Returns the full name (including implicit digest) of the Data packet stored in the in-memory storage ...
Definition: in-memory-storage-entry.hpp:57
ndn::InMemoryStorage::printCache
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
Definition: in-memory-storage.cpp:385
ndn::InMemoryStorage::getLimit
size_t getLimit() const
Definition: in-memory-storage.hpp:192
ndn::InMemoryStorage::end
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
Definition: in-memory-storage.cpp:363
ndn::InMemoryStorage::Error::Error
Error()
Definition: in-memory-storage.hpp:109
ndn::InMemoryStorage::~InMemoryStorage
virtual ~InMemoryStorage()
Definition: in-memory-storage.cpp:114
ndn::InMemoryStorage::const_iterator::const_iterator
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
Definition: in-memory-storage.cpp:30
ndn::name
Definition: name-component-types.hpp:33
ndn::InMemoryStorage::const_iterator
Represents a self-defined const_iterator for the in-memory storage.
Definition: in-memory-storage.hpp:68
ndn::InMemoryStorage::beforeErase
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
Definition: in-memory-storage.cpp:375
ndn::InMemoryStorage::const_iterator::operator==
bool operator==(const const_iterator &rhs)
Definition: in-memory-storage.cpp:73
ndn::InMemoryStorage::afterInsert
virtual void afterInsert(InMemoryStorageEntry *entry)
Update the entry or other data structures after a entry is successfully inserted according to derived...
Definition: in-memory-storage.cpp:370
ndn::InMemoryStorage::getCapacity
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)
Definition: in-memory-storage.hpp:262
ndn::InMemoryStorage
Represents in-memory storage.
Definition: in-memory-storage.hpp:43
ndn::InMemoryStorage::Error
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
Definition: in-memory-storage.hpp:107
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34