blob: bdc39626dd9075b70006ae61c3ceacfd6ed09de9 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu404eafd2016-03-06 14:54:25 -08003 * Copyright (c) 2013-2016 Regents of the University of California.
Jiewen Tan99135962014-09-20 02:18:53 -07004 *
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_UTIL_IN_MEMORY_STORAGE_HPP
23#define NDN_UTIL_IN_MEMORY_STORAGE_HPP
24
25#include "../common.hpp"
26#include "../interest.hpp"
27#include "../data.hpp"
Yingdi Yu404eafd2016-03-06 14:54:25 -080028#include "scheduler.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070029#include "in-memory-storage-entry.hpp"
30
31#include <boost/multi_index/member.hpp>
32#include <boost/multi_index_container.hpp>
33#include <boost/multi_index/ordered_index.hpp>
34#include <boost/multi_index/sequenced_index.hpp>
35#include <boost/multi_index/identity.hpp>
36#include <boost/multi_index/mem_fun.hpp>
37
38#include <stack>
39#include <iterator>
Jiewen Tan99135962014-09-20 02:18:53 -070040
41namespace ndn {
42namespace util {
43
44/** @brief Represents in-memory storage
45 */
46class InMemoryStorage : noncopyable
47{
48public:
49 //multi_index_container to implement storage
50 class byFullName;
51
52 typedef boost::multi_index_container<
53 InMemoryStorageEntry*,
54 boost::multi_index::indexed_by<
55
56 // by Full Name
57 boost::multi_index::ordered_unique<
58 boost::multi_index::tag<byFullName>,
59 boost::multi_index::const_mem_fun<InMemoryStorageEntry, const Name&,
60 &InMemoryStorageEntry::getFullName>,
61 std::less<Name>
62 >
63
64 >
65 > Cache;
66
67 /** @brief Represents a self-defined const_iterator for the in-memory storage
68 *
69 * @note Don't try to instantiate this class directly, use InMemoryStorage::begin() instead.
70 */
71 class const_iterator : public std::iterator<std::input_iterator_tag, const Data>
72 {
73 public:
74 const_iterator(const Data* ptr, const Cache* cache,
75 Cache::index<byFullName>::type::iterator it);
76
77 const_iterator&
78 operator++();
79
80 const_iterator
81 operator++(int);
82
83 const Data&
84 operator*();
85
86 const Data*
87 operator->();
88
89 bool
90 operator==(const const_iterator& rhs);
91
92 bool
93 operator!=(const const_iterator& rhs);
94
95 private:
96 const Data* m_ptr;
97 const Cache* m_cache;
98 Cache::index<byFullName>::type::iterator m_it;
99 };
100
101 /** @brief Represents an error might be thrown during reduce the current capacity of the
102 * in-memory storage through function setCapacity(size_t nMaxPackets).
103 */
104 class Error : public std::runtime_error
105 {
106 public:
107 Error() : std::runtime_error("Cannot reduce the capacity of the in-memory storage!")
108 {
109 }
110 };
111
Yingdi Yu404eafd2016-03-06 14:54:25 -0800112 /** @brief Create a InMemoryStorage with up to @p limit entries
113 * The InMemoryStorage created through this method will ignore MustBeFresh in interest processing
114 */
Jiewen Tan99135962014-09-20 02:18:53 -0700115 explicit
116 InMemoryStorage(size_t limit = std::numeric_limits<size_t>::max());
117
Yingdi Yu404eafd2016-03-06 14:54:25 -0800118 /** @brief Create a InMemoryStorage with up to @p limit entries
119 * The InMemoryStorage created through this method will handle MustBeFresh in interest processing
120 */
121 explicit
122 InMemoryStorage(boost::asio::io_service& ioService,
123 size_t limit = std::numeric_limits<size_t>::max());
124
Jiewen Tan99135962014-09-20 02:18:53 -0700125 /** @note Please make sure to implement it to free m_freeEntries and evict
126 * all items in the derived class for anybody who wishes to inherit this class
127 */
128 virtual
129 ~InMemoryStorage();
130
131 /** @brief Inserts a Data packet
132 *
Yingdi Yu404eafd2016-03-06 14:54:25 -0800133 * @param data the packet to insert
134 * @param mustBeFreshProcessingWindow Beyond this time period after the data is inserted, the
135 * data can only be used to answer interest without MustBeFresh selector.
136 *
Jiewen Tan99135962014-09-20 02:18:53 -0700137 * @note Packets are considered duplicate if the name with implicit digest matches.
138 * The new Data packet with the identical name, but a different payload
139 * will be placed in the in-memory storage.
140 *
141 * @note It will invoke afterInsert(shared_ptr<InMemoryStorageEntry>).
142 */
143 void
Yingdi Yu404eafd2016-03-06 14:54:25 -0800144 insert(const Data& data, const time::milliseconds& mustBeFreshProcessingWindow = INFINITE_WINDOW);
Jiewen Tan99135962014-09-20 02:18:53 -0700145
146 /** @brief Finds the best match Data for an Interest
147 *
148 * @note It will invoke afterAccess(shared_ptr<InMemoryStorageEntry>).
149 * As currently it is impossible to determine whether a Name contains implicit digest or not,
150 * therefore this find function is not able to locate a packet according to an interest(
151 * including implicit digest) whose name is not the full name of the data matching the
152 * implicit digest.
153 *
154 * @return{ the best match, if any; otherwise a null shared_ptr }
155 */
156 shared_ptr<const Data>
157 find(const Interest& interest);
158
159 /** @brief Finds the best match Data for a Name with or without
160 * the implicit digest.
161 *
162 * If packets with the same name but different digests exist
163 * and the Name supplied is the one without implicit digest, a packet
164 * will be arbitrarily chosen to return.
165 *
166 * @note It will invoke afterAccess(shared_ptr<InMemoryStorageEntry>).
167 *
168 * @return{ the one matched the Name; otherwise a null shared_ptr }
169 */
170 shared_ptr<const Data>
171 find(const Name& name);
172
173 /** @brief Deletes in-memory storage entry by prefix by default.
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700174 * @param prefix Exact name of a prefix of the data to remove
175 * @param isPrefix If false, the function will only delete the
Jiewen Tan99135962014-09-20 02:18:53 -0700176 * entry completely matched with the prefix according to canonical ordering.
177 * For this case, user should substitute the prefix with full name.
178 *
179 * @note Please do not use this function directly in any derived class to erase
180 * entry in the cache, use eraseHelper instead.
181 * @note It will invoke beforeErase(shared_ptr<InMemoryStorageEntry>).
182 */
183 void
184 erase(const Name& prefix, const bool isPrefix = true);
185
186 /** @return{ maximum number of packets that can be allowed to store in in-memory storage }
187 */
188 size_t
189 getLimit() const
190 {
191 return m_limit;
192 }
193
194 /** @return{ number of packets stored in in-memory storage }
195 */
196 size_t
197 size() const
198 {
199 return m_nPackets;
200 }
201
202 /** @brief Returns begin iterator of the in-memory storage ordering by
203 * name with digest
204 *
205 * @return{ const_iterator pointing to the beginning of the m_cache }
206 */
207 InMemoryStorage::const_iterator
208 begin() const;
209
210 /** @brief Returns end iterator of the in-memory storage ordering by
211 * name with digest
212 *
213 * @return{ const_iterator pointing to the end of the m_cache }
214 */
215 InMemoryStorage::const_iterator
216 end() const;
217
Junxiao Shi99848502014-10-13 19:22:22 -0700218NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Jiewen Tan99135962014-09-20 02:18:53 -0700219 /** @brief Update the entry when the entry is returned by the find() function
220 * according to derived class implemented replacement policy
221 */
222 virtual void
223 afterAccess(InMemoryStorageEntry* entry);
224
225 /** @brief Update the entry or other data structures
226 * after a entry is successfully inserted
227 * according to derived class implemented replacement policy
228 */
229 virtual void
230 afterInsert(InMemoryStorageEntry* entry);
231
232 /** @brief Update the entry or other data structures
233 * before a entry is successfully erased
234 * according to derived class implemented replacement policy
235 */
236 virtual void
237 beforeErase(InMemoryStorageEntry* entry);
238
239 /** @brief Removes one Data packet from in-memory storage based on
240 * derived class implemented replacement policy
241 *
242 * Please do not use this function directly in any derived class to erase
243 * entry in the cache, use eraseHelper instead.
244 * @return{ whether the Data was removed }
245 */
246 virtual bool
247 evictItem() = 0;
248
Junxiao Shi99848502014-10-13 19:22:22 -0700249NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Jiewen Tan99135962014-09-20 02:18:53 -0700250 /** @brief sets current capacity of in-memory storage (in packets)
251 */
252 void
253 setCapacity(size_t nMaxPackets);
254
255 /** @brief returns current capacity of in-memory storage (in packets)
256 * @return{ number of packets that can be stored in application cache }
257 */
258 size_t
259 getCapacity() const
260 {
261 return m_capacity;
262 }
263
264 /** @brief returns true if the in-memory storage uses up the current capacity, false otherwise
265 */
266 bool
267 isFull() const
268 {
269 return size() >= m_capacity;
270 }
271
272 /** @brief deletes in-memory storage entries by the Name with implicit digest.
273 *
274 * This is the function one should use to erase entry in the cache
275 * in derived class.
276 * It won't invoke beforeErase(shared_ptr<Entry>).
277 */
278 void
279 eraseImpl(const Name& name);
280
281 /** @brief Prints contents of the in-memory storage
282 */
283 void
284 printCache(std::ostream& os) const;
285
Junxiao Shi99848502014-10-13 19:22:22 -0700286NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Jiewen Tan99135962014-09-20 02:18:53 -0700287 /** @brief free in-memory storage entries by an iterator pointing to that entry.
Alexander Afanasyev8e131fd2014-12-15 21:31:50 -0800288 @return An iterator pointing to the element that followed the last element erased.
289 */
290 Cache::iterator
291 freeEntry(Cache::iterator it);
Jiewen Tan99135962014-09-20 02:18:53 -0700292
293 /** @brief Implements child selector (leftmost, rightmost, undeclared).
294 * Operates on the first layer of a skip list.
295 *
296 * startingPoint must be less than Interest Name.
297 * startingPoint can be equal to Interest Name only
298 * when the item is in the begin() position.
299 *
300 * Iterates toward greater Names, terminates when application cache entry falls out of Interest
301 * prefix. When childSelector = leftmost, returns first application cache entry that satisfies
302 * other selectors. When childSelector = rightmost, it goes till the end, and returns application
303 * cache entry that satisfies other selectors. Returned application cache entry is the leftmost
304 * child of the rightmost child.
305 * @return{ the best match, if any; otherwise 0 }
306 */
307 InMemoryStorageEntry*
308 selectChild(const Interest& interest,
309 Cache::index<byFullName>::type::iterator startingPoint) const;
310
Yingdi Yu404eafd2016-03-06 14:54:25 -0800311 /** @brief Get the next iterator (include startingPoint) that satisfies MustBeFresh requirement
312 *
313 * @param startingPoint The iterator to start with.
314 * @return The next qualified iterator
315 */
316 Cache::index<byFullName>::type::iterator
317 findNextFresh(Cache::index<byFullName>::type::iterator startingPoint) const;
318
319private:
320 void
321 init();
322
323public:
324 static const time::milliseconds INFINITE_WINDOW;
325
326private:
327 static const time::milliseconds ZERO_WINDOW;
328
Jiewen Tan99135962014-09-20 02:18:53 -0700329private:
330 Cache m_cache;
331 /// user defined maximum capacity of the in-memory storage in packets
332 size_t m_limit;
333 /// current capacity of the in-memory storage in packets
334 size_t m_capacity;
335 /// current number of packets in in-memory storage
336 size_t m_nPackets;
337 /// memory pool
338 std::stack<InMemoryStorageEntry*> m_freeEntries;
Yingdi Yu404eafd2016-03-06 14:54:25 -0800339 /// scheduler
340 unique_ptr<Scheduler> m_scheduler;
Jiewen Tan99135962014-09-20 02:18:53 -0700341};
342
343} // namespace util
344} // namespace ndn
345
346#endif // NDN_UTIL_IN_MEMORY_STORAGE_HPP