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 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 34 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 35 | typedef std::list< shared_ptr<cs::Entry> > SkipListLayer; |
| 36 | typedef std::list<SkipListLayer*> SkipList; |
| 37 | |
| 38 | /** \brief represents Content Store |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 39 | */ |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 40 | class Cs : noncopyable |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 43 | explicit |
| 44 | Cs(int nMaxPackets = 65536); // ~500MB with average packet size = 8KB |
| 45 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 46 | ~Cs(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 47 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 48 | /** \brief inserts a Data packet |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 49 | * This method does not consider the payload of the Data packet. |
| 50 | * |
| 51 | * Packets are considered duplicate if the name matches. |
| 52 | * The new Data packet with the identical name, but a different payload |
| 53 | * is not placed in the Content Store |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 54 | * \return{ whether the Data is added } |
| 55 | */ |
| 56 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 57 | insert(const Data& data, bool isUnsolicited = false); |
| 58 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 59 | /** \brief finds the best match Data for an Interest |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 60 | * \return{ the best match, if any; otherwise 0 } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 61 | */ |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 62 | const Data* |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 63 | find(const Interest& interest) const; |
| 64 | |
| 65 | /** \brief deletes CS entry by the exact name |
| 66 | */ |
| 67 | void |
| 68 | erase(const Name& exactName); |
| 69 | |
| 70 | /** \brief sets maximum allowed size of Content Store (in packets) |
| 71 | */ |
| 72 | void |
| 73 | setLimit(size_t nMaxPackets); |
| 74 | |
| 75 | /** \brief returns maximum allowed size of Content Store (in packets) |
| 76 | * \return{ number of packets that can be stored in Content Store } |
| 77 | */ |
| 78 | size_t |
| 79 | getLimit() const; |
| 80 | |
| 81 | /** \brief returns current size of Content Store measured in packets |
| 82 | * \return{ number of packets located in Content Store } |
| 83 | */ |
| 84 | size_t |
| 85 | size() const; |
| 86 | |
| 87 | protected: |
| 88 | /** \brief removes one Data packet from Content Store based on replacement policy |
| 89 | * \return{ whether the Data was removed } |
| 90 | */ |
| 91 | bool |
| 92 | evictItem(); |
| 93 | |
| 94 | private: |
| 95 | /** \brief returns True if the Content Store is at its maximum capacity |
| 96 | * \return{ True if Content Store is full; otherwise False} |
| 97 | */ |
| 98 | bool |
| 99 | isFull() const; |
| 100 | |
| 101 | /** \brief Computes the layer where new Content Store Entry is placed |
| 102 | * |
| 103 | * Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh |
| 104 | * \return{ returns random layer (number) in a skip list} |
| 105 | */ |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 106 | size_t |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 107 | pickRandomLayer() const; |
| 108 | |
| 109 | /** \brief Inserts a new Content Store Entry in a skip list |
| 110 | * \return{ returns a pair containing a pointer to the CS Entry, |
| 111 | * and a flag indicating if the entry was newly created (True) or refreshed (False) } |
| 112 | */ |
| 113 | std::pair< shared_ptr<cs::Entry>, bool> |
| 114 | insertToSkipList(const Data& data, bool isUnsolicited = false); |
| 115 | |
| 116 | /** \brief Removes a specific CS Entry from all layers of a skip list |
| 117 | * \return{ returns True if CS Entry was succesfully removed and False if CS Entry was not found} |
| 118 | */ |
| 119 | bool |
| 120 | eraseFromSkipList(shared_ptr<cs::Entry> entry); |
| 121 | |
| 122 | /** \brief Prints contents of the skip list, starting from the top layer |
| 123 | */ |
| 124 | void |
| 125 | printSkipList() const; |
| 126 | |
| 127 | /** \brief Implements child selector (leftmost, rightmost, undeclared). |
| 128 | * Operates on the first layer of a skip list. |
| 129 | * |
| 130 | * startingPoint must be less than Interest Name. |
| 131 | * startingPoint can be equal to Interest Name only when the item is in the begin() position. |
| 132 | * |
| 133 | * Iterates toward greater Names, terminates when CS entry falls out of Interest prefix. |
| 134 | * When childSelector = leftmost, returns first CS entry that satisfies other selectors. |
| 135 | * When childSelector = rightmost, it goes till the end, and returns CS entry that satisfies |
| 136 | * other selectors. Returned CS entry is the leftmost child of the rightmost child. |
| 137 | * \return{ the best match, if any; otherwise 0 } |
| 138 | */ |
| 139 | const Data* |
| 140 | selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const; |
| 141 | |
| 142 | /** \brief checks if Content Store entry satisfies Interest selectors (MinSuffixComponents, |
| 143 | * MaxSuffixComponents, Implicit Digest, MustBeFresh) |
| 144 | * \return{ true if satisfies all selectors; false otherwise } |
| 145 | */ |
| 146 | bool |
| 147 | doesComplyWithSelectors(const Interest& interest, shared_ptr<cs::Entry> entry) const; |
| 148 | |
| 149 | /** \brief interprets minSuffixComponent and name lengths to understand if Interest contains |
| 150 | * implicit digest of the data |
| 151 | * \return{ True if Interest name contains digest; False otherwise } |
| 152 | */ |
| 153 | bool |
| 154 | recognizeInterestWithDigest(const Interest& interest, shared_ptr<cs::Entry> entry) const; |
| 155 | |
| 156 | private: |
| 157 | class StalenessComparator |
| 158 | { |
| 159 | public: |
| 160 | bool |
| 161 | operator() (shared_ptr<cs::Entry> entry1, shared_ptr<cs::Entry> entry2) |
| 162 | { |
| 163 | return entry1->getStaleTime() > entry2->getStaleTime(); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | SkipList m_skipList; |
| 168 | size_t m_nMaxPackets; //user defined maximum size of the Content Store in packets |
| 169 | |
| 170 | std::queue< shared_ptr<cs::Entry> > m_unsolicitedContent; //FIFO for unsolicited Data |
| 171 | std::queue< shared_ptr<cs::Entry> > m_contentByArrival; //FIFO index |
| 172 | std::priority_queue< shared_ptr<cs::Entry>, std::vector< shared_ptr<cs::Entry> >, StalenessComparator> m_contentByStaleness; //index by staleness time |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 175 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 176 | |
| 177 | #endif // NFD_TABLE_CS_HPP |