Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 3 | * 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 Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 11 | * 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 Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 25 | * \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 Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 30 | #ifndef NFD_DAEMON_TABLE_CS_HPP |
| 31 | #define NFD_DAEMON_TABLE_CS_HPP |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 32 | |
| 33 | #include "common.hpp" |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 34 | #include "cs-entry.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 35 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 36 | #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 Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 42 | #include <queue> |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 43 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 44 | namespace nfd { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 45 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 46 | typedef std::list<cs::Entry*> SkipListLayer; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 47 | typedef std::list<SkipListLayer*> SkipList; |
| 48 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 49 | class StalenessComparator |
| 50 | { |
| 51 | public: |
| 52 | bool |
| 53 | operator()(const cs::Entry* entry1, const cs::Entry* entry2) const |
| 54 | { |
| 55 | return entry1->getStaleTime() < entry2->getStaleTime(); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | class UnsolicitedComparator |
| 60 | { |
| 61 | public: |
| 62 | bool |
| 63 | operator()(const cs::Entry* entry1, const cs::Entry* entry2) const |
| 64 | { |
| 65 | return entry1->isUnsolicited(); |
| 66 | } |
Alexander Afanasyev | 4cf4170 | 2014-10-19 23:46:10 -0400 | [diff] [blame^] | 67 | |
| 68 | bool |
| 69 | operator()(bool isUnsolicited, const cs::Entry* entry) const |
| 70 | { |
| 71 | if (isUnsolicited) |
| 72 | return true; |
| 73 | else |
| 74 | return !entry->isUnsolicited(); |
| 75 | } |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // tags |
| 79 | class unsolicited; |
| 80 | class byStaleness; |
| 81 | class byArrival; |
| 82 | |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 83 | typedef boost::multi_index_container< |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 84 | cs::Entry*, |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 85 | boost::multi_index::indexed_by< |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 86 | |
| 87 | // by arrival (FIFO) |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 88 | boost::multi_index::sequenced< |
| 89 | boost::multi_index::tag<byArrival> |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 90 | >, |
| 91 | |
| 92 | // index by staleness time |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 93 | boost::multi_index::ordered_non_unique< |
| 94 | boost::multi_index::tag<byStaleness>, |
| 95 | boost::multi_index::identity<cs::Entry*>, |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 96 | StalenessComparator |
| 97 | >, |
| 98 | |
| 99 | // unsolicited Data is in the front |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 100 | boost::multi_index::ordered_non_unique< |
| 101 | boost::multi_index::tag<unsolicited>, |
| 102 | boost::multi_index::identity<cs::Entry*>, |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 103 | UnsolicitedComparator |
| 104 | > |
| 105 | |
| 106 | > |
| 107 | > CleanupIndex; |
| 108 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 109 | /** \brief represents Content Store |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 110 | */ |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 111 | class Cs : noncopyable |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 112 | { |
| 113 | public: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 114 | explicit |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 115 | Cs(size_t nMaxPackets = 10); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 116 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 117 | ~Cs(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 118 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 119 | /** \brief inserts a Data packet |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 120 | * 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 Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 125 | * \return{ whether the Data is added } |
| 126 | */ |
| 127 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 128 | insert(const Data& data, bool isUnsolicited = false); |
| 129 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 130 | /** \brief finds the best match Data for an Interest |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 131 | * \return{ the best match, if any; otherwise 0 } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 132 | */ |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 133 | const Data* |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 134 | 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 | |
| 158 | protected: |
| 159 | /** \brief removes one Data packet from Content Store based on replacement policy |
| 160 | * \return{ whether the Data was removed } |
| 161 | */ |
| 162 | bool |
| 163 | evictItem(); |
| 164 | |
| 165 | private: |
| 166 | /** \brief returns True if the Content Store is at its maximum capacity |
| 167 | * \return{ True if Content Store is full; otherwise False} |
| 168 | */ |
| 169 | bool |
| 170 | isFull() const; |
| 171 | |
| 172 | /** \brief Computes the layer where new Content Store Entry is placed |
| 173 | * |
| 174 | * Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh |
| 175 | * \return{ returns random layer (number) in a skip list} |
| 176 | */ |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 177 | size_t |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 178 | pickRandomLayer() const; |
| 179 | |
| 180 | /** \brief Inserts a new Content Store Entry in a skip list |
| 181 | * \return{ returns a pair containing a pointer to the CS Entry, |
| 182 | * and a flag indicating if the entry was newly created (True) or refreshed (False) } |
| 183 | */ |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 184 | std::pair<cs::Entry*, bool> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 185 | insertToSkipList(const Data& data, bool isUnsolicited = false); |
| 186 | |
| 187 | /** \brief Removes a specific CS Entry from all layers of a skip list |
| 188 | * \return{ returns True if CS Entry was succesfully removed and False if CS Entry was not found} |
| 189 | */ |
| 190 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 191 | eraseFromSkipList(cs::Entry* entry); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 192 | |
| 193 | /** \brief Prints contents of the skip list, starting from the top layer |
| 194 | */ |
| 195 | void |
| 196 | printSkipList() const; |
| 197 | |
| 198 | /** \brief Implements child selector (leftmost, rightmost, undeclared). |
| 199 | * Operates on the first layer of a skip list. |
| 200 | * |
| 201 | * startingPoint must be less than Interest Name. |
| 202 | * startingPoint can be equal to Interest Name only when the item is in the begin() position. |
| 203 | * |
| 204 | * Iterates toward greater Names, terminates when CS entry falls out of Interest prefix. |
| 205 | * When childSelector = leftmost, returns first CS entry that satisfies other selectors. |
| 206 | * When childSelector = rightmost, it goes till the end, and returns CS entry that satisfies |
| 207 | * other selectors. Returned CS entry is the leftmost child of the rightmost child. |
| 208 | * \return{ the best match, if any; otherwise 0 } |
| 209 | */ |
| 210 | const Data* |
| 211 | selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const; |
| 212 | |
| 213 | /** \brief checks if Content Store entry satisfies Interest selectors (MinSuffixComponents, |
| 214 | * MaxSuffixComponents, Implicit Digest, MustBeFresh) |
| 215 | * \return{ true if satisfies all selectors; false otherwise } |
| 216 | */ |
| 217 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 218 | doesComplyWithSelectors(const Interest& interest, |
| 219 | cs::Entry* entry, |
| 220 | bool doesInterestContainDigest) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 221 | |
| 222 | /** \brief interprets minSuffixComponent and name lengths to understand if Interest contains |
| 223 | * implicit digest of the data |
| 224 | * \return{ True if Interest name contains digest; False otherwise } |
| 225 | */ |
| 226 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 227 | recognizeInterestWithDigest(const Interest& interest, cs::Entry* entry) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 228 | |
| 229 | private: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 230 | SkipList m_skipList; |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 231 | CleanupIndex m_cleanupIndex; |
| 232 | size_t m_nMaxPackets; // user defined maximum size of the Content Store in packets |
| 233 | size_t m_nPackets; // current number of packets in Content Store |
| 234 | std::queue<cs::Entry*> m_freeCsEntries; // memory pool |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 237 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 238 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 239 | #endif // NFD_DAEMON_TABLE_CS_HPP |