blob: 67cc6117a2d89bd7205e0b37089109250075fe01 [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"
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070034#include "cs-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
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040046typedef std::list<cs::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
53 operator()(const cs::Entry* entry1, const cs::Entry* entry2) const
54 {
55 return entry1->getStaleTime() < entry2->getStaleTime();
56 }
57};
58
59class UnsolicitedComparator
60{
61public:
62 bool
63 operator()(const cs::Entry* entry1, const cs::Entry* entry2) const
64 {
65 return entry1->isUnsolicited();
66 }
67};
68
69// tags
70class unsolicited;
71class byStaleness;
72class byArrival;
73
Davide Pesavento52a18f92014-04-10 00:55:01 +020074typedef boost::multi_index_container<
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040075 cs::Entry*,
Davide Pesavento52a18f92014-04-10 00:55:01 +020076 boost::multi_index::indexed_by<
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040077
78 // by arrival (FIFO)
Davide Pesavento52a18f92014-04-10 00:55:01 +020079 boost::multi_index::sequenced<
80 boost::multi_index::tag<byArrival>
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040081 >,
82
83 // index by staleness time
Davide Pesavento52a18f92014-04-10 00:55:01 +020084 boost::multi_index::ordered_non_unique<
85 boost::multi_index::tag<byStaleness>,
86 boost::multi_index::identity<cs::Entry*>,
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040087 StalenessComparator
88 >,
89
90 // unsolicited Data is in the front
Davide Pesavento52a18f92014-04-10 00:55:01 +020091 boost::multi_index::ordered_non_unique<
92 boost::multi_index::tag<unsolicited>,
93 boost::multi_index::identity<cs::Entry*>,
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040094 UnsolicitedComparator
95 >
96
97 >
98> CleanupIndex;
99
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800100/** \brief represents Content Store
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700101 */
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800102class Cs : noncopyable
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700103{
104public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105 explicit
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600106 Cs(size_t nMaxPackets = 10);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800107
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700108 ~Cs();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800109
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700110 /** \brief inserts a Data packet
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800111 * This method does not consider the payload of the Data packet.
112 *
113 * Packets are considered duplicate if the name matches.
114 * The new Data packet with the identical name, but a different payload
115 * is not placed in the Content Store
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700116 * \return{ whether the Data is added }
117 */
118 bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119 insert(const Data& data, bool isUnsolicited = false);
120
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700121 /** \brief finds the best match Data for an Interest
Alexander Afanasyevc9765df2014-01-25 19:24:27 -0800122 * \return{ the best match, if any; otherwise 0 }
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700123 */
Alexander Afanasyevc9765df2014-01-25 19:24:27 -0800124 const Data*
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800125 find(const Interest& interest) const;
126
127 /** \brief deletes CS entry by the exact name
128 */
129 void
130 erase(const Name& exactName);
131
132 /** \brief sets maximum allowed size of Content Store (in packets)
133 */
134 void
135 setLimit(size_t nMaxPackets);
136
137 /** \brief returns maximum allowed size of Content Store (in packets)
138 * \return{ number of packets that can be stored in Content Store }
139 */
140 size_t
141 getLimit() const;
142
143 /** \brief returns current size of Content Store measured in packets
144 * \return{ number of packets located in Content Store }
145 */
146 size_t
147 size() const;
148
149protected:
150 /** \brief removes one Data packet from Content Store based on replacement policy
151 * \return{ whether the Data was removed }
152 */
153 bool
154 evictItem();
155
156private:
157 /** \brief returns True if the Content Store is at its maximum capacity
158 * \return{ True if Content Store is full; otherwise False}
159 */
160 bool
161 isFull() const;
162
163 /** \brief Computes the layer where new Content Store Entry is placed
164 *
165 * Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh
166 * \return{ returns random layer (number) in a skip list}
167 */
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700168 size_t
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800169 pickRandomLayer() const;
170
171 /** \brief Inserts a new Content Store Entry in a skip list
172 * \return{ returns a pair containing a pointer to the CS Entry,
173 * and a flag indicating if the entry was newly created (True) or refreshed (False) }
174 */
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400175 std::pair<cs::Entry*, bool>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 insertToSkipList(const Data& data, bool isUnsolicited = false);
177
178 /** \brief Removes a specific CS Entry from all layers of a skip list
179 * \return{ returns True if CS Entry was succesfully removed and False if CS Entry was not found}
180 */
181 bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400182 eraseFromSkipList(cs::Entry* entry);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800183
184 /** \brief Prints contents of the skip list, starting from the top layer
185 */
186 void
187 printSkipList() const;
188
189 /** \brief Implements child selector (leftmost, rightmost, undeclared).
190 * Operates on the first layer of a skip list.
191 *
192 * startingPoint must be less than Interest Name.
193 * startingPoint can be equal to Interest Name only when the item is in the begin() position.
194 *
195 * Iterates toward greater Names, terminates when CS entry falls out of Interest prefix.
196 * When childSelector = leftmost, returns first CS entry that satisfies other selectors.
197 * When childSelector = rightmost, it goes till the end, and returns CS entry that satisfies
198 * other selectors. Returned CS entry is the leftmost child of the rightmost child.
199 * \return{ the best match, if any; otherwise 0 }
200 */
201 const Data*
202 selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const;
203
204 /** \brief checks if Content Store entry satisfies Interest selectors (MinSuffixComponents,
205 * MaxSuffixComponents, Implicit Digest, MustBeFresh)
206 * \return{ true if satisfies all selectors; false otherwise }
207 */
208 bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400209 doesComplyWithSelectors(const Interest& interest,
210 cs::Entry* entry,
211 bool doesInterestContainDigest) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800212
213 /** \brief interprets minSuffixComponent and name lengths to understand if Interest contains
214 * implicit digest of the data
215 * \return{ True if Interest name contains digest; False otherwise }
216 */
217 bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400218 recognizeInterestWithDigest(const Interest& interest, cs::Entry* entry) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800219
220private:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800221 SkipList m_skipList;
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400222 CleanupIndex m_cleanupIndex;
223 size_t m_nMaxPackets; // user defined maximum size of the Content Store in packets
224 size_t m_nPackets; // current number of packets in Content Store
225 std::queue<cs::Entry*> m_freeCsEntries; // memory pool
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700226};
227
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800228} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800229
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700230#endif // NFD_DAEMON_TABLE_CS_HPP