NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 
66  class const_iterator : public std::iterator<std::input_iterator_tag, const Data>
67  {
68  public:
69  const_iterator(const Data* ptr, const Cache* cache,
71 
73  operator++();
74 
76  operator++(int);
77 
78  const Data&
79  operator*();
80 
81  const Data*
82  operator->();
83 
84  bool
85  operator==(const const_iterator& rhs);
86 
87  bool
88  operator!=(const const_iterator& rhs);
89 
90  private:
91  const Data* m_ptr;
92  const Cache* m_cache;
94  };
95 
99  class Error : public std::runtime_error
100  {
101  public:
103  : std::runtime_error("Cannot reduce the capacity of the in-memory storage!")
104  {
105  }
106  };
107 
111  explicit
112  InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
113 
117  explicit
118  InMemoryStorage(boost::asio::io_service& ioService,
119  size_t limit = std::numeric_limits<size_t>::max());
120 
124  virtual
126 
139  void
140  insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow = INFINITE_WINDOW);
141 
152  shared_ptr<const Data>
153  find(const Interest& interest);
154 
166  shared_ptr<const Data>
167  find(const Name& name);
168 
179  void
180  erase(const Name& prefix, const bool isPrefix = true);
181 
184  size_t
185  getLimit() const
186  {
187  return m_limit;
188  }
189 
192  size_t
193  size() const
194  {
195  return m_nPackets;
196  }
197 
204  begin() const;
205 
212  end() const;
213 
218  virtual void
219  afterAccess(InMemoryStorageEntry* entry);
220 
225  virtual void
226  afterInsert(InMemoryStorageEntry* entry);
227 
232  virtual void
233  beforeErase(InMemoryStorageEntry* entry);
234 
242  virtual bool
243  evictItem() = 0;
244 
248  void
249  setCapacity(size_t nMaxPackets);
250 
254  size_t
255  getCapacity() const
256  {
257  return m_capacity;
258  }
259 
262  bool
263  isFull() const
264  {
265  return size() >= m_capacity;
266  }
267 
274  void
275  eraseImpl(const Name& name);
276 
279  void
280  printCache(std::ostream& os) const;
281 
287  freeEntry(Cache::iterator it);
288 
303  InMemoryStorageEntry*
304  selectChild(const Interest& interest,
305  Cache::index<byFullName>::type::iterator startingPoint) const;
306 
314 
315 private:
316  void
317  init();
318 
319 public:
321 
322 private:
323  static const time::milliseconds ZERO_WINDOW;
324 
325 private:
326  Cache m_cache;
328  size_t m_limit;
330  size_t m_capacity;
332  size_t m_nPackets;
334  std::stack<InMemoryStorageEntry*> m_freeEntries;
336  unique_ptr<Scheduler> m_scheduler;
337 };
338 
339 } // namespace ndn
340 
341 #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.
Represents in-memory storage.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy...
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise ...
STL namespace.
represents an Interest packet
Definition: interest.hpp:42
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)
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)
const Name & getFullName() const
Returns the full name (including implicit digest) of the Data packet stored in the in-memory storage ...
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 ...
InMemoryStorageEntry * selectChild(const Interest &interest, Cache::index< byFullName >::type::iterator startingPoint) const
Implements child selector (leftmost, rightmost, undeclared).
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...
Represents a self-defined const_iterator for the in-memory storage.
Represents an absolute name.
Definition: name.hpp:42
Represents an in-memory storage entry.
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
Represents a Data packet.
Definition: data.hpp:35
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
Cache::index< byFullName >::type::iterator findNextFresh(Cache::index< byFullName >::type::iterator startingPoint) const
Get the next iterator (include startingPoint) that satisfies MustBeFresh requirement.
static const time::milliseconds INFINITE_WINDOW