Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, 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/>. |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | /** \file |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 27 | * |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 28 | * \brief implements the ContentStore |
| 29 | * |
| 30 | * This ContentStore implementation consists of two data structures, |
| 31 | * a Table, and a set of cleanup queues. |
| 32 | * |
| 33 | * The Table is a container (std::set) sorted by full Names of stored Data packets. |
| 34 | * Data packets are wrapped in Entry objects. |
| 35 | * Each Entry contain the Data packet itself, |
| 36 | * and a few addition attributes such as the staleness of the Data packet. |
| 37 | * |
| 38 | * The cleanup queues are three doubly linked lists which stores Table iterators. |
| 39 | * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively. |
| 40 | * Table iterator is placed into, removed from, and moved between suitable queues |
| 41 | * whenever an Entry is added, removed, or has other attribute changes. |
| 42 | * The Table iterator of an Entry should be in exactly one queue at any moment. |
| 43 | * Within each queue, the iterators are kept in first-in-first-out order. |
| 44 | * Eviction procedure exhausts the first queue before moving onto the next queue, |
| 45 | * in the order of unsolicited, stale, and fresh queue. |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 46 | */ |
| 47 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 48 | #ifndef NFD_DAEMON_TABLE_CS_HPP |
| 49 | #define NFD_DAEMON_TABLE_CS_HPP |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 50 | |
| 51 | #include "common.hpp" |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 52 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 53 | namespace nfd { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 54 | namespace cs { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 56 | class Entry; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 57 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 58 | /** \brief represents the ContentStore |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 59 | */ |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 60 | class Cs : noncopyable |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 61 | { |
| 62 | public: |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 63 | explicit |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 64 | Cs(size_t nMaxPackets = 10); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 65 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 66 | ~Cs(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 67 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 68 | /** \brief inserts a Data packet |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 69 | * \return true |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 70 | */ |
| 71 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 72 | insert(const Data& data, bool isUnsolicited = false); |
| 73 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 74 | /** \brief finds the best matching Data packet |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 75 | */ |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 76 | const Data* |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 77 | find(const Interest& interest) const; |
| 78 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 79 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 80 | erase(const Name& exactName) |
| 81 | { |
| 82 | BOOST_ASSERT_MSG(false, "not implemented"); |
| 83 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 84 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 85 | /** \brief changes capacity (in number of packets) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 86 | */ |
| 87 | void |
| 88 | setLimit(size_t nMaxPackets); |
| 89 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 90 | /** \return capacity (in number of packets) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 91 | */ |
| 92 | size_t |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 93 | getLimit() const |
| 94 | { |
| 95 | return m_limit; |
| 96 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 97 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 98 | /** \return number of stored packets |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 99 | */ |
| 100 | size_t |
| 101 | size() const; |
| 102 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 103 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 104 | void |
| 105 | dump(); |
| 106 | |
| 107 | public: |
| 108 | typedef std::set<Entry> Table; |
| 109 | typedef Table::const_iterator TableIt; |
| 110 | typedef std::list<TableIt> Queue; |
| 111 | typedef Queue::iterator QueueIt; |
| 112 | enum QueueType { |
| 113 | QUEUE_UNSOLICITED, |
| 114 | QUEUE_STALE, |
| 115 | QUEUE_FIFO, |
| 116 | QUEUE_UBOUND, |
| 117 | QUEUE_NONE = QUEUE_UBOUND |
| 118 | }; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 119 | |
| 120 | private: |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 121 | /** \brief find leftmost match in [first,last) |
| 122 | * \return the leftmost match, or last if not found |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 123 | */ |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 124 | TableIt |
| 125 | findLeftmost(const Interest& interest, TableIt left, TableIt right) const; |
| 126 | |
| 127 | /** \brief find rightmost match in [first,last) |
| 128 | * \return the rightmost match, or last if not found |
| 129 | */ |
| 130 | TableIt |
| 131 | findRightmost(const Interest& interest, TableIt first, TableIt last) const; |
| 132 | |
| 133 | /** \brief find rightmost match among entries with exact Names in [first,last) |
| 134 | * \return the rightmost match, or last if not found |
| 135 | */ |
| 136 | TableIt |
| 137 | findRightmostAmongExact(const Interest& interest, TableIt first, TableIt last) const; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 138 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 139 | /** \brief attaches an entry to a suitable cleanup queue |
| 140 | * \note if the entry is already attached to a queue, it's automatically detached |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 141 | */ |
| 142 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 143 | attachQueue(TableIt it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 144 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 145 | /** \brief detaches an entry from its current cleanup queue |
| 146 | * \warning if the entry is unattached, this will cause undefined behavior |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 147 | */ |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 148 | void |
| 149 | detachQueue(TableIt it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 150 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 151 | /** \brief moves an entry from FIFO queue to STALE queue |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 152 | */ |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 153 | void |
| 154 | moveToStaleQueue(TableIt it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 155 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 156 | /** \brief picks an entry for eviction |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 157 | */ |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 158 | std::tuple<TableIt, std::string/*reason*/> |
| 159 | evictPick(); |
| 160 | |
| 161 | /** \brief evicts zero or more entries so that number of stored entries |
| 162 | * is not greater than capacity |
| 163 | */ |
| 164 | void |
| 165 | evict(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 166 | |
| 167 | private: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 168 | size_t m_limit; |
| 169 | Table m_table; |
| 170 | Queue m_queues[QUEUE_UBOUND]; |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 173 | } // namespace cs |
| 174 | |
| 175 | using cs::Cs; |
| 176 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 177 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 179 | #endif // NFD_DAEMON_TABLE_CS_HPP |