blob: 0d989cdfea23737e2c758c17a9e5df6cd215e8fb [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -070025 * \author Ilya Moiseenko <http://ilyamoiseenko.com/>
26 * \author Junxiao Shi <http://www.cs.arizona.edu/people/shijunxiao/>
27 * \author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070028 */
29
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070030#ifndef NFD_DAEMON_TABLE_CS_HPP
31#define NFD_DAEMON_TABLE_CS_HPP
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080032
33#include "common.hpp"
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080034#include "cs-skip-list-entry.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080035
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040036#include <boost/multi_index/member.hpp>
37#include <boost/multi_index_container.hpp>
38#include <boost/multi_index/ordered_index.hpp>
39#include <boost/multi_index/sequenced_index.hpp>
40#include <boost/multi_index/identity.hpp>
41
Davide Pesavento52a18f92014-04-10 00:55:01 +020042#include <queue>
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040043
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080044namespace nfd {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070045
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080046typedef std::list<cs::skip_list::Entry*> SkipListLayer;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047typedef std::list<SkipListLayer*> SkipList;
48
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040049class StalenessComparator
50{
51public:
52 bool
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080053 operator()(const cs::skip_list::Entry* entry1, const cs::skip_list::Entry* entry2) const
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040054 {
55 return entry1->getStaleTime() < entry2->getStaleTime();
56 }
57};
58
59class UnsolicitedComparator
60{
61public:
62 bool
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080063 operator()(const cs::skip_list::Entry* entry1, const cs::skip_list::Entry* entry2) const
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040064 {
65 return entry1->isUnsolicited();
66 }
Alexander Afanasyev4cf41702014-10-19 23:46:10 -040067
68 bool
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080069 operator()(bool isUnsolicited, const cs::skip_list::Entry* entry) const
Alexander Afanasyev4cf41702014-10-19 23:46:10 -040070 {
71 if (isUnsolicited)
72 return true;
73 else
74 return !entry->isUnsolicited();
75 }
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040076};
77
78// tags
79class unsolicited;
80class byStaleness;
81class byArrival;
82
Davide Pesavento52a18f92014-04-10 00:55:01 +020083typedef boost::multi_index_container<
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080084 cs::skip_list::Entry*,
Davide Pesavento52a18f92014-04-10 00:55:01 +020085 boost::multi_index::indexed_by<
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040086
87 // by arrival (FIFO)
Davide Pesavento52a18f92014-04-10 00:55:01 +020088 boost::multi_index::sequenced<
89 boost::multi_index::tag<byArrival>
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040090 >,
91
92 // index by staleness time
Davide Pesavento52a18f92014-04-10 00:55:01 +020093 boost::multi_index::ordered_non_unique<
94 boost::multi_index::tag<byStaleness>,
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -080095 boost::multi_index::identity<cs::skip_list::Entry*>,
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040096 StalenessComparator
97 >,
98
99 // unsolicited Data is in the front
Davide Pesavento52a18f92014-04-10 00:55:01 +0200100 boost::multi_index::ordered_non_unique<
101 boost::multi_index::tag<unsolicited>,
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800102 boost::multi_index::identity<cs::skip_list::Entry*>,
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400103 UnsolicitedComparator
104 >
105
106 >
107> CleanupIndex;
108
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800109/** \brief represents Content Store
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700110 */
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800111class Cs : noncopyable
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700112{
113public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800114 explicit
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600115 Cs(size_t nMaxPackets = 10);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800116
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700117 ~Cs();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700119 /** \brief inserts a Data packet
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800120 * This method does not consider the payload of the Data packet.
121 *
122 * Packets are considered duplicate if the name matches.
123 * The new Data packet with the identical name, but a different payload
124 * is not placed in the Content Store
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700125 * \return{ whether the Data is added }
126 */
127 bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800128 insert(const Data& data, bool isUnsolicited = false);
129
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700130 /** \brief finds the best match Data for an Interest
Alexander Afanasyevc9765df2014-01-25 19:24:27 -0800131 * \return{ the best match, if any; otherwise 0 }
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700132 */
Alexander Afanasyevc9765df2014-01-25 19:24:27 -0800133 const Data*
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800134 find(const Interest& interest) const;
135
136 /** \brief deletes CS entry by the exact name
137 */
138 void
139 erase(const Name& exactName);
140
141 /** \brief sets maximum allowed size of Content Store (in packets)
142 */
143 void
144 setLimit(size_t nMaxPackets);
145
146 /** \brief returns maximum allowed size of Content Store (in packets)
147 * \return{ number of packets that can be stored in Content Store }
148 */
149 size_t
150 getLimit() const;
151
152 /** \brief returns current size of Content Store measured in packets
153 * \return{ number of packets located in Content Store }
154 */
155 size_t
156 size() const;
157
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800158public: // enumeration
159 class const_iterator;
160
161 /** \brief returns an iterator pointing to the first CS entry
162 * \note Iteration order is implementation-specific and is undefined
163 * \note The returned iterator may get invalidated if CS is modified
164 */
165 const_iterator
166 begin() const;
167
168 /** \brief returns an iterator referring to the past-the-end CS entry
169 * \note The returned iterator may get invalidated if CS is modified
170 */
171 const_iterator
172 end() const;
173
174 class const_iterator : public std::iterator<std::forward_iterator_tag, const cs::Entry>
175 {
176 public:
177 const_iterator() = default;
178
179 const_iterator(SkipListLayer::const_iterator it);
180
181 ~const_iterator();
182
183 reference
184 operator*() const;
185
186 pointer
187 operator->() const;
188
189 const_iterator&
190 operator++();
191
192 const_iterator
193 operator++(int);
194
195 bool
196 operator==(const const_iterator& other) const;
197
198 bool
199 operator!=(const const_iterator& other) const;
200
201 private:
202 SkipListLayer::const_iterator m_skipListIterator;
203 };
204
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800205protected:
206 /** \brief removes one Data packet from Content Store based on replacement policy
207 * \return{ whether the Data was removed }
208 */
209 bool
210 evictItem();
211
212private:
213 /** \brief returns True if the Content Store is at its maximum capacity
214 * \return{ True if Content Store is full; otherwise False}
215 */
216 bool
217 isFull() const;
218
219 /** \brief Computes the layer where new Content Store Entry is placed
220 *
221 * Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh
222 * \return{ returns random layer (number) in a skip list}
223 */
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700224 size_t
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225 pickRandomLayer() const;
226
227 /** \brief Inserts a new Content Store Entry in a skip list
228 * \return{ returns a pair containing a pointer to the CS Entry,
229 * and a flag indicating if the entry was newly created (True) or refreshed (False) }
230 */
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800231 std::pair<cs::skip_list::Entry*, bool>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800232 insertToSkipList(const Data& data, bool isUnsolicited = false);
233
234 /** \brief Removes a specific CS Entry from all layers of a skip list
235 * \return{ returns True if CS Entry was succesfully removed and False if CS Entry was not found}
236 */
237 bool
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800238 eraseFromSkipList(cs::skip_list::Entry* entry);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800239
240 /** \brief Prints contents of the skip list, starting from the top layer
241 */
242 void
243 printSkipList() const;
244
245 /** \brief Implements child selector (leftmost, rightmost, undeclared).
246 * Operates on the first layer of a skip list.
247 *
248 * startingPoint must be less than Interest Name.
249 * startingPoint can be equal to Interest Name only when the item is in the begin() position.
250 *
251 * Iterates toward greater Names, terminates when CS entry falls out of Interest prefix.
252 * When childSelector = leftmost, returns first CS entry that satisfies other selectors.
253 * When childSelector = rightmost, it goes till the end, and returns CS entry that satisfies
254 * other selectors. Returned CS entry is the leftmost child of the rightmost child.
255 * \return{ the best match, if any; otherwise 0 }
256 */
257 const Data*
258 selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const;
259
260 /** \brief checks if Content Store entry satisfies Interest selectors (MinSuffixComponents,
261 * MaxSuffixComponents, Implicit Digest, MustBeFresh)
262 * \return{ true if satisfies all selectors; false otherwise }
263 */
264 bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400265 doesComplyWithSelectors(const Interest& interest,
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800266 cs::skip_list::Entry* entry,
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400267 bool doesInterestContainDigest) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800268
269 /** \brief interprets minSuffixComponent and name lengths to understand if Interest contains
270 * implicit digest of the data
271 * \return{ True if Interest name contains digest; False otherwise }
272 */
273 bool
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800274 recognizeInterestWithDigest(const Interest& interest, cs::skip_list::Entry* entry) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800275
276private:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800277 SkipList m_skipList;
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400278 CleanupIndex m_cleanupIndex;
279 size_t m_nMaxPackets; // user defined maximum size of the Content Store in packets
280 size_t m_nPackets; // current number of packets in Content Store
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800281 std::queue<cs::skip_list::Entry*> m_freeCsEntries; // memory pool
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700282};
283
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800284inline Cs::const_iterator
285Cs::begin() const
286{
287 return const_iterator(m_skipList.front()->begin());
288}
289
290inline Cs::const_iterator
291Cs::end() const
292{
293 return const_iterator(m_skipList.front()->end());
294}
295
296inline
297Cs::const_iterator::const_iterator(SkipListLayer::const_iterator it)
298 : m_skipListIterator(it)
299{
300}
301
302inline
303Cs::const_iterator::~const_iterator()
304{
305}
306
307inline Cs::const_iterator&
308Cs::const_iterator::operator++()
309{
310 ++m_skipListIterator;
311 return *this;
312}
313
314inline Cs::const_iterator
315Cs::const_iterator::operator++(int)
316{
317 Cs::const_iterator temp(*this);
318 ++(*this);
319 return temp;
320}
321
322inline Cs::const_iterator::reference
323Cs::const_iterator::operator*() const
324{
325 return *(this->operator->());
326}
327
328inline Cs::const_iterator::pointer
329Cs::const_iterator::operator->() const
330{
331 return *m_skipListIterator;
332}
333
334inline bool
335Cs::const_iterator::operator==(const Cs::const_iterator& other) const
336{
337 return m_skipListIterator == other.m_skipListIterator;
338}
339
340inline bool
341Cs::const_iterator::operator!=(const Cs::const_iterator& other) const
342{
343 return !(*this == other);
344}
345
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800346} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800347
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700348#endif // NFD_DAEMON_TABLE_CS_HPP