Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [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 |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 9 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | * \author Ilya Moiseenko <iliamo@ucla.edu> |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 27 | #ifndef NFD_TABLE_CS_HPP |
| 28 | #define NFD_TABLE_CS_HPP |
| 29 | |
| 30 | #include "common.hpp" |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 31 | #include "cs-entry.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 32 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 33 | #include <boost/multi_index/member.hpp> |
| 34 | #include <boost/multi_index_container.hpp> |
| 35 | #include <boost/multi_index/ordered_index.hpp> |
| 36 | #include <boost/multi_index/sequenced_index.hpp> |
| 37 | #include <boost/multi_index/identity.hpp> |
| 38 | |
| 39 | using namespace ::boost; |
| 40 | using namespace ::boost::multi_index; |
| 41 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 42 | namespace nfd { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 43 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 44 | typedef std::list<cs::Entry*> SkipListLayer; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 45 | typedef std::list<SkipListLayer*> SkipList; |
| 46 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 47 | class StalenessComparator |
| 48 | { |
| 49 | public: |
| 50 | bool |
| 51 | operator()(const cs::Entry* entry1, const cs::Entry* entry2) const |
| 52 | { |
| 53 | return entry1->getStaleTime() < entry2->getStaleTime(); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | class UnsolicitedComparator |
| 58 | { |
| 59 | public: |
| 60 | bool |
| 61 | operator()(const cs::Entry* entry1, const cs::Entry* entry2) const |
| 62 | { |
| 63 | return entry1->isUnsolicited(); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | // tags |
| 68 | class unsolicited; |
| 69 | class byStaleness; |
| 70 | class byArrival; |
| 71 | |
| 72 | typedef multi_index_container< |
| 73 | cs::Entry*, |
| 74 | indexed_by< |
| 75 | |
| 76 | // by arrival (FIFO) |
| 77 | sequenced< |
| 78 | tag<byArrival> |
| 79 | >, |
| 80 | |
| 81 | // index by staleness time |
| 82 | ordered_non_unique< |
| 83 | tag<byStaleness>, |
| 84 | identity<cs::Entry*>, |
| 85 | StalenessComparator |
| 86 | >, |
| 87 | |
| 88 | // unsolicited Data is in the front |
| 89 | ordered_non_unique< |
| 90 | tag<unsolicited>, |
| 91 | identity<cs::Entry*>, |
| 92 | UnsolicitedComparator |
| 93 | > |
| 94 | |
| 95 | > |
| 96 | > CleanupIndex; |
| 97 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | /** \brief represents Content Store |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 99 | */ |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 100 | class Cs : noncopyable |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 101 | { |
| 102 | public: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 103 | explicit |
| 104 | Cs(int nMaxPackets = 65536); // ~500MB with average packet size = 8KB |
| 105 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 106 | ~Cs(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 107 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 108 | /** \brief inserts a Data packet |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 109 | * This method does not consider the payload of the Data packet. |
| 110 | * |
| 111 | * Packets are considered duplicate if the name matches. |
| 112 | * The new Data packet with the identical name, but a different payload |
| 113 | * is not placed in the Content Store |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 114 | * \return{ whether the Data is added } |
| 115 | */ |
| 116 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 117 | insert(const Data& data, bool isUnsolicited = false); |
| 118 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 119 | /** \brief finds the best match Data for an Interest |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 120 | * \return{ the best match, if any; otherwise 0 } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 121 | */ |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 122 | const Data* |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 123 | find(const Interest& interest) const; |
| 124 | |
| 125 | /** \brief deletes CS entry by the exact name |
| 126 | */ |
| 127 | void |
| 128 | erase(const Name& exactName); |
| 129 | |
| 130 | /** \brief sets maximum allowed size of Content Store (in packets) |
| 131 | */ |
| 132 | void |
| 133 | setLimit(size_t nMaxPackets); |
| 134 | |
| 135 | /** \brief returns maximum allowed size of Content Store (in packets) |
| 136 | * \return{ number of packets that can be stored in Content Store } |
| 137 | */ |
| 138 | size_t |
| 139 | getLimit() const; |
| 140 | |
| 141 | /** \brief returns current size of Content Store measured in packets |
| 142 | * \return{ number of packets located in Content Store } |
| 143 | */ |
| 144 | size_t |
| 145 | size() const; |
| 146 | |
| 147 | protected: |
| 148 | /** \brief removes one Data packet from Content Store based on replacement policy |
| 149 | * \return{ whether the Data was removed } |
| 150 | */ |
| 151 | bool |
| 152 | evictItem(); |
| 153 | |
| 154 | private: |
| 155 | /** \brief returns True if the Content Store is at its maximum capacity |
| 156 | * \return{ True if Content Store is full; otherwise False} |
| 157 | */ |
| 158 | bool |
| 159 | isFull() const; |
| 160 | |
| 161 | /** \brief Computes the layer where new Content Store Entry is placed |
| 162 | * |
| 163 | * Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh |
| 164 | * \return{ returns random layer (number) in a skip list} |
| 165 | */ |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 166 | size_t |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 167 | pickRandomLayer() const; |
| 168 | |
| 169 | /** \brief Inserts a new Content Store Entry in a skip list |
| 170 | * \return{ returns a pair containing a pointer to the CS Entry, |
| 171 | * and a flag indicating if the entry was newly created (True) or refreshed (False) } |
| 172 | */ |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 173 | std::pair<cs::Entry*, bool> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 174 | insertToSkipList(const Data& data, bool isUnsolicited = false); |
| 175 | |
| 176 | /** \brief Removes a specific CS Entry from all layers of a skip list |
| 177 | * \return{ returns True if CS Entry was succesfully removed and False if CS Entry was not found} |
| 178 | */ |
| 179 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 180 | eraseFromSkipList(cs::Entry* entry); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 181 | |
| 182 | /** \brief Prints contents of the skip list, starting from the top layer |
| 183 | */ |
| 184 | void |
| 185 | printSkipList() const; |
| 186 | |
| 187 | /** \brief Implements child selector (leftmost, rightmost, undeclared). |
| 188 | * Operates on the first layer of a skip list. |
| 189 | * |
| 190 | * startingPoint must be less than Interest Name. |
| 191 | * startingPoint can be equal to Interest Name only when the item is in the begin() position. |
| 192 | * |
| 193 | * Iterates toward greater Names, terminates when CS entry falls out of Interest prefix. |
| 194 | * When childSelector = leftmost, returns first CS entry that satisfies other selectors. |
| 195 | * When childSelector = rightmost, it goes till the end, and returns CS entry that satisfies |
| 196 | * other selectors. Returned CS entry is the leftmost child of the rightmost child. |
| 197 | * \return{ the best match, if any; otherwise 0 } |
| 198 | */ |
| 199 | const Data* |
| 200 | selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const; |
| 201 | |
| 202 | /** \brief checks if Content Store entry satisfies Interest selectors (MinSuffixComponents, |
| 203 | * MaxSuffixComponents, Implicit Digest, MustBeFresh) |
| 204 | * \return{ true if satisfies all selectors; false otherwise } |
| 205 | */ |
| 206 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 207 | doesComplyWithSelectors(const Interest& interest, |
| 208 | cs::Entry* entry, |
| 209 | bool doesInterestContainDigest) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 210 | |
| 211 | /** \brief interprets minSuffixComponent and name lengths to understand if Interest contains |
| 212 | * implicit digest of the data |
| 213 | * \return{ True if Interest name contains digest; False otherwise } |
| 214 | */ |
| 215 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 216 | recognizeInterestWithDigest(const Interest& interest, cs::Entry* entry) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 217 | |
| 218 | private: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 219 | SkipList m_skipList; |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 220 | CleanupIndex m_cleanupIndex; |
| 221 | size_t m_nMaxPackets; // user defined maximum size of the Content Store in packets |
| 222 | size_t m_nPackets; // current number of packets in Content Store |
| 223 | std::queue<cs::Entry*> m_freeCsEntries; // memory pool |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 226 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 227 | |
| 228 | #endif // NFD_TABLE_CS_HPP |