ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2024 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 <limits>
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
37namespace ndn {
38
41class InMemoryStorage : noncopyable
42{
43public:
44 // multi_index_container to implement storage
45 class byFullName;
46
47 using Cache = boost::multi_index_container<
49 boost::multi_index::indexed_by<
50 // by Full Name
51 boost::multi_index::ordered_unique<
52 boost::multi_index::tag<byFullName>,
53 boost::multi_index::const_mem_fun<InMemoryStorageEntry, const Name&,
55 std::less<Name>
56 >
57 >
58 >;
59
64 class const_iterator : public boost::forward_iterator_helper<const_iterator, const Data>
65 {
66 public:
67 const_iterator(const Data* ptr, const Cache* cache,
68 Cache::index<byFullName>::type::iterator it) noexcept
69 : m_ptr(ptr)
70 , m_cache(cache)
71 , m_it(it)
72 {
73 }
74
75 reference
76 operator*() const noexcept
77 {
78 return *m_ptr;
79 }
80
82 operator++();
83
84 friend bool
85 operator==(const const_iterator& lhs, const const_iterator& rhs) noexcept
86 {
87 return lhs.m_it == rhs.m_it;
88 }
89
90 private:
91 const Data* m_ptr;
92 const Cache* m_cache;
93 Cache::index<byFullName>::type::iterator m_it;
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
112 explicit
113 InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
114
119 explicit
120 InMemoryStorage(boost::asio::io_context& ioCtx,
121 size_t limit = std::numeric_limits<size_t>::max());
122
126 virtual
128
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
250
254 void
255 setCapacity(size_t nMaxPackets);
256
260 size_t
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
323private:
324 void
325 init();
326
327public:
328 static constexpr time::milliseconds INFINITE_WINDOW = -1_ms;
329
330private:
331 Cache m_cache;
333 size_t m_limit = 0;
335 size_t m_capacity = 0;
337 size_t m_nPackets = 0;
339 std::stack<InMemoryStorageEntry*> m_freeEntries;
341 unique_ptr<Scheduler> m_scheduler;
342};
343
344} // namespace ndn
345
346#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 const_iterator for the in-memory storage.
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it) noexcept
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs) noexcept
Represents in-memory storage.
static constexpr time::milliseconds INFINITE_WINDOW
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 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.
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...
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:45
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition common.hpp:48
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition common.hpp:49
::boost::chrono::milliseconds milliseconds
Definition time.hpp:52
Definition data.cpp:25
STL namespace.