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 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 30 | #include "cs.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 31 | #include "core/logger.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 32 | #include "core/random.hpp" |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/util/crypto.hpp> |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 37 | #include <boost/random/bernoulli_distribution.hpp> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 38 | |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 39 | /// max skip list layers |
| 40 | static const size_t SKIPLIST_MAX_LAYERS = 32; |
| 41 | /// probability for an entry in layer N to appear also in layer N+1 |
| 42 | static const double SKIPLIST_PROBABILITY = 0.25; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 43 | |
| 44 | NFD_LOG_INIT("ContentStore"); |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 46 | namespace nfd { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 47 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 48 | Cs::Cs(size_t nMaxPackets) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 49 | : m_nMaxPackets(nMaxPackets) |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 50 | , m_nPackets(0) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 51 | { |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 52 | SkipListLayer* zeroLayer = new SkipListLayer(); |
| 53 | m_skipList.push_back(zeroLayer); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 54 | |
| 55 | for (size_t i = 0; i < m_nMaxPackets; i++) |
| 56 | m_freeCsEntries.push(new cs::Entry()); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 57 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 58 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 59 | Cs::~Cs() |
| 60 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 61 | // evict all items from CS |
| 62 | while (evictItem()) |
| 63 | ; |
| 64 | |
| 65 | BOOST_ASSERT(m_freeCsEntries.size() == m_nMaxPackets); |
| 66 | |
| 67 | while (!m_freeCsEntries.empty()) |
| 68 | { |
| 69 | delete m_freeCsEntries.front(); |
| 70 | m_freeCsEntries.pop(); |
| 71 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | size_t |
| 75 | Cs::size() const |
| 76 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 77 | return m_nPackets; // size of the first layer in a skip list |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | Cs::setLimit(size_t nMaxPackets) |
| 82 | { |
Alexander Afanasyev | 281b916 | 2014-06-08 10:15:20 +0300 | [diff] [blame] | 83 | size_t oldNMaxPackets = m_nMaxPackets; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 84 | m_nMaxPackets = nMaxPackets; |
| 85 | |
Alexander Afanasyev | 281b916 | 2014-06-08 10:15:20 +0300 | [diff] [blame] | 86 | while (size() > m_nMaxPackets) { |
| 87 | evictItem(); |
| 88 | } |
| 89 | |
| 90 | if (m_nMaxPackets >= oldNMaxPackets) { |
| 91 | for (size_t i = oldNMaxPackets; i < m_nMaxPackets; i++) { |
| 92 | m_freeCsEntries.push(new cs::Entry()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 93 | } |
Alexander Afanasyev | 281b916 | 2014-06-08 10:15:20 +0300 | [diff] [blame] | 94 | } |
| 95 | else { |
| 96 | for (size_t i = oldNMaxPackets; i > m_nMaxPackets; i--) { |
| 97 | delete m_freeCsEntries.front(); |
| 98 | m_freeCsEntries.pop(); |
| 99 | } |
| 100 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | size_t |
| 104 | Cs::getLimit() const |
| 105 | { |
| 106 | return m_nMaxPackets; |
| 107 | } |
| 108 | |
| 109 | //Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 110 | std::pair<cs::Entry*, bool> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 111 | Cs::insertToSkipList(const Data& data, bool isUnsolicited) |
| 112 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 113 | NFD_LOG_TRACE("insertToSkipList() " << data.getFullName() << ", " |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 114 | << "skipList size " << size()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 115 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 116 | BOOST_ASSERT(m_cleanupIndex.size() <= size()); |
| 117 | BOOST_ASSERT(m_freeCsEntries.size() > 0); |
| 118 | |
| 119 | // take entry for the memory pool |
| 120 | cs::Entry* entry = m_freeCsEntries.front(); |
| 121 | m_freeCsEntries.pop(); |
| 122 | m_nPackets++; |
| 123 | entry->setData(data, isUnsolicited); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 124 | |
| 125 | bool insertInFront = false; |
| 126 | bool isIterated = false; |
| 127 | SkipList::reverse_iterator topLayer = m_skipList.rbegin(); |
| 128 | SkipListLayer::iterator updateTable[SKIPLIST_MAX_LAYERS]; |
| 129 | SkipListLayer::iterator head = (*topLayer)->begin(); |
| 130 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 131 | if (!(*topLayer)->empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 132 | { |
| 133 | //start from the upper layer towards bottom |
| 134 | int layer = m_skipList.size() - 1; |
| 135 | for (SkipList::reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit) |
| 136 | { |
| 137 | //if we didn't do any iterations on the higher layers, start from the begin() again |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 138 | if (!isIterated) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 139 | head = (*rit)->begin(); |
| 140 | |
| 141 | updateTable[layer] = head; |
| 142 | |
| 143 | if (head != (*rit)->end()) |
| 144 | { |
| 145 | // it can happen when begin() contains the element in front of which we need to insert |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 146 | if (!isIterated && ((*head)->getFullName() >= entry->getFullName())) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 147 | { |
| 148 | --updateTable[layer]; |
| 149 | insertInFront = true; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | SkipListLayer::iterator it = head; |
| 154 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 155 | while ((*it)->getFullName() < entry->getFullName()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 156 | { |
| 157 | head = it; |
| 158 | updateTable[layer] = it; |
| 159 | isIterated = true; |
| 160 | |
| 161 | ++it; |
| 162 | if (it == (*rit)->end()) |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (layer > 0) |
| 169 | head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer |
| 170 | |
| 171 | layer--; |
| 172 | } |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | updateTable[0] = (*topLayer)->begin(); //initialization |
| 177 | } |
| 178 | |
| 179 | head = updateTable[0]; |
| 180 | ++head; // look at the next slot to check if it contains a duplicate |
| 181 | |
| 182 | bool isCsEmpty = (size() == 0); |
| 183 | bool isInBoundaries = (head != (*m_skipList.begin())->end()); |
| 184 | bool isNameIdentical = false; |
| 185 | if (!isCsEmpty && isInBoundaries) |
| 186 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 187 | isNameIdentical = (*head)->getFullName() == entry->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | //check if this is a duplicate packet |
| 191 | if (isNameIdentical) |
| 192 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 193 | NFD_LOG_TRACE("Duplicate name (with digest)"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 195 | (*head)->setData(data, isUnsolicited); //updates stale time |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 196 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 197 | // new entry not needed, returning to the pool |
| 198 | entry->release(); |
| 199 | m_freeCsEntries.push(entry); |
| 200 | m_nPackets--; |
| 201 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 202 | return std::make_pair(*head, false); |
| 203 | } |
| 204 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 205 | NFD_LOG_TRACE("Not a duplicate"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 207 | size_t randomLayer = pickRandomLayer(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 208 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 209 | while (m_skipList.size() < randomLayer + 1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 210 | { |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 211 | SkipListLayer* newLayer = new SkipListLayer(); |
| 212 | m_skipList.push_back(newLayer); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 214 | updateTable[(m_skipList.size() - 1)] = newLayer->begin(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 217 | size_t layer = 0; |
| 218 | for (SkipList::iterator i = m_skipList.begin(); |
| 219 | i != m_skipList.end() && layer <= randomLayer; ++i) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 220 | { |
| 221 | if (updateTable[layer] == (*i)->end() && !insertInFront) |
| 222 | { |
| 223 | (*i)->push_back(entry); |
| 224 | SkipListLayer::iterator last = (*i)->end(); |
| 225 | --last; |
| 226 | entry->setIterator(layer, last); |
| 227 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 228 | NFD_LOG_TRACE("pushback " << &(*last)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 229 | } |
| 230 | else if (updateTable[layer] == (*i)->end() && insertInFront) |
| 231 | { |
| 232 | (*i)->push_front(entry); |
| 233 | entry->setIterator(layer, (*i)->begin()); |
| 234 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 235 | NFD_LOG_TRACE("pushfront "); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 239 | NFD_LOG_TRACE("insertafter"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 240 | ++updateTable[layer]; // insert after |
| 241 | SkipListLayer::iterator position = (*i)->insert(updateTable[layer], entry); |
| 242 | entry->setIterator(layer, position); // save iterator where item was inserted |
| 243 | } |
| 244 | layer++; |
| 245 | } |
| 246 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 247 | return std::make_pair(entry, true); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | bool |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 251 | Cs::insert(const Data& data, bool isUnsolicited) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 252 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 253 | NFD_LOG_TRACE("insert() " << data.getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 254 | |
| 255 | if (isFull()) |
| 256 | { |
| 257 | evictItem(); |
| 258 | } |
| 259 | |
| 260 | //pointer and insertion status |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 261 | std::pair<cs::Entry*, bool> entry = insertToSkipList(data, isUnsolicited); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 262 | |
| 263 | //new entry |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 264 | if (static_cast<bool>(entry.first) && (entry.second == true)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 265 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 266 | m_cleanupIndex.push_back(entry.first); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 272 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 273 | size_t |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 274 | Cs::pickRandomLayer() const |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 275 | { |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 276 | static boost::random::bernoulli_distribution<> dist(SKIPLIST_PROBABILITY); |
| 277 | // TODO rewrite using geometry_distribution |
| 278 | size_t layer; |
| 279 | for (layer = 0; layer < SKIPLIST_MAX_LAYERS; ++layer) { |
| 280 | if (!dist(getGlobalRng())) { |
| 281 | break; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 282 | } |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 283 | } |
| 284 | return layer; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool |
| 288 | Cs::isFull() const |
| 289 | { |
| 290 | if (size() >= m_nMaxPackets) //size of the first layer vs. max size |
| 291 | return true; |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 297 | Cs::eraseFromSkipList(cs::Entry* entry) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 298 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 299 | NFD_LOG_TRACE("eraseFromSkipList() " << entry->getFullName()); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 300 | NFD_LOG_TRACE("SkipList size " << size()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 301 | |
| 302 | bool isErased = false; |
| 303 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 304 | const std::map<int, std::list<cs::Entry*>::iterator>& iterators = entry->getIterators(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 305 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 306 | if (!iterators.empty()) |
| 307 | { |
| 308 | int layer = 0; |
| 309 | |
| 310 | for (SkipList::iterator it = m_skipList.begin(); it != m_skipList.end(); ) |
| 311 | { |
| 312 | std::map<int, std::list<cs::Entry*>::iterator>::const_iterator i = iterators.find(layer); |
| 313 | |
| 314 | if (i != iterators.end()) |
| 315 | { |
| 316 | (*it)->erase(i->second); |
| 317 | entry->removeIterator(layer); |
| 318 | isErased = true; |
| 319 | |
| 320 | //remove layers that do not contain any elements (starting from the second layer) |
| 321 | if ((layer != 0) && (*it)->empty()) |
| 322 | { |
| 323 | delete *it; |
| 324 | it = m_skipList.erase(it); |
| 325 | } |
| 326 | else |
| 327 | ++it; |
| 328 | |
| 329 | layer++; |
| 330 | } |
| 331 | else |
| 332 | break; |
| 333 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 336 | //delete entry; |
| 337 | if (isErased) |
| 338 | { |
| 339 | entry->release(); |
| 340 | m_freeCsEntries.push(entry); |
| 341 | m_nPackets--; |
| 342 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 343 | |
| 344 | return isErased; |
| 345 | } |
| 346 | |
| 347 | bool |
| 348 | Cs::evictItem() |
| 349 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 350 | NFD_LOG_TRACE("evictItem()"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 351 | |
Alexander Afanasyev | 4cf4170 | 2014-10-19 23:46:10 -0400 | [diff] [blame] | 352 | if (!m_cleanupIndex.get<unsolicited>().empty()) { |
| 353 | CleanupIndex::index<unsolicited>::type::const_iterator firstSolicited = |
| 354 | m_cleanupIndex.get<unsolicited>().upper_bound(false); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 355 | |
Alexander Afanasyev | 4cf4170 | 2014-10-19 23:46:10 -0400 | [diff] [blame] | 356 | if (firstSolicited != m_cleanupIndex.get<unsolicited>().begin()) { |
| 357 | --firstSolicited; |
| 358 | BOOST_ASSERT((*firstSolicited)->isUnsolicited()); |
| 359 | NFD_LOG_TRACE("Evict from unsolicited queue"); |
| 360 | |
| 361 | eraseFromSkipList(*firstSolicited); |
| 362 | m_cleanupIndex.get<unsolicited>().erase(firstSolicited); |
| 363 | return true; |
| 364 | } |
| 365 | else { |
| 366 | BOOST_ASSERT(!(*m_cleanupIndex.get<unsolicited>().begin())->isUnsolicited()); |
| 367 | } |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 368 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 369 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 370 | if (!m_cleanupIndex.get<byStaleness>().empty() && |
| 371 | (*m_cleanupIndex.get<byStaleness>().begin())->getStaleTime() < time::steady_clock::now()) |
| 372 | { |
| 373 | NFD_LOG_TRACE("Evict from staleness queue"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 374 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 375 | eraseFromSkipList(*m_cleanupIndex.get<byStaleness>().begin()); |
| 376 | m_cleanupIndex.get<byStaleness>().erase(m_cleanupIndex.get<byStaleness>().begin()); |
| 377 | return true; |
| 378 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 379 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 380 | if (!m_cleanupIndex.get<byArrival>().empty()) |
| 381 | { |
| 382 | NFD_LOG_TRACE("Evict from arrival queue"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 383 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 384 | eraseFromSkipList(*m_cleanupIndex.get<byArrival>().begin()); |
| 385 | m_cleanupIndex.get<byArrival>().erase(m_cleanupIndex.get<byArrival>().begin()); |
| 386 | return true; |
| 387 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | const Data* |
| 393 | Cs::find(const Interest& interest) const |
| 394 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 395 | NFD_LOG_TRACE("find() " << interest.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 396 | |
| 397 | bool isIterated = false; |
| 398 | SkipList::const_reverse_iterator topLayer = m_skipList.rbegin(); |
| 399 | SkipListLayer::iterator head = (*topLayer)->begin(); |
| 400 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 401 | if (!(*topLayer)->empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 402 | { |
| 403 | //start from the upper layer towards bottom |
| 404 | int layer = m_skipList.size() - 1; |
| 405 | for (SkipList::const_reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit) |
| 406 | { |
| 407 | //if we didn't do any iterations on the higher layers, start from the begin() again |
| 408 | if (!isIterated) |
| 409 | head = (*rit)->begin(); |
| 410 | |
| 411 | if (head != (*rit)->end()) |
| 412 | { |
| 413 | // it happens when begin() contains the element we want to find |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 414 | if (!isIterated && (interest.getName().isPrefixOf((*head)->getFullName()))) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 415 | { |
| 416 | if (layer > 0) |
| 417 | { |
| 418 | layer--; |
| 419 | continue; // try lower layer |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | isIterated = true; |
| 424 | } |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | SkipListLayer::iterator it = head; |
| 429 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 430 | while ((*it)->getFullName() < interest.getName()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 431 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 432 | NFD_LOG_TRACE((*it)->getFullName() << " < " << interest.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 433 | head = it; |
| 434 | isIterated = true; |
| 435 | |
| 436 | ++it; |
| 437 | if (it == (*rit)->end()) |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if (layer > 0) |
| 444 | { |
| 445 | head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer |
| 446 | } |
| 447 | else //if we reached the first layer |
| 448 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 449 | if (isIterated) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 450 | return selectChild(interest, head); |
| 451 | } |
| 452 | |
| 453 | layer--; |
| 454 | } |
| 455 | } |
| 456 | |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 457 | return 0; |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 460 | const Data* |
| 461 | Cs::selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const |
| 462 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 463 | BOOST_ASSERT(startingPoint != (*m_skipList.begin())->end()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 464 | |
| 465 | if (startingPoint != (*m_skipList.begin())->begin()) |
| 466 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 467 | BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 470 | NFD_LOG_TRACE("selectChild() " << interest.getChildSelector() << " " |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 471 | << (*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 472 | |
| 473 | bool hasLeftmostSelector = (interest.getChildSelector() <= 0); |
| 474 | bool hasRightmostSelector = !hasLeftmostSelector; |
| 475 | |
| 476 | if (hasLeftmostSelector) |
| 477 | { |
| 478 | bool doesInterestContainDigest = recognizeInterestWithDigest(interest, *startingPoint); |
| 479 | bool isInPrefix = false; |
| 480 | |
| 481 | if (doesInterestContainDigest) |
| 482 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 483 | isInPrefix = interest.getName().getPrefix(-1).isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 484 | } |
| 485 | else |
| 486 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 487 | isInPrefix = interest.getName().isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | if (isInPrefix) |
| 491 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 492 | if (doesComplyWithSelectors(interest, *startingPoint, doesInterestContainDigest)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 493 | { |
| 494 | return &(*startingPoint)->getData(); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | //iterate to the right |
| 500 | SkipListLayer::iterator rightmost = startingPoint; |
| 501 | if (startingPoint != (*m_skipList.begin())->end()) |
| 502 | { |
| 503 | SkipListLayer::iterator rightmostCandidate = startingPoint; |
| 504 | Name currentChildPrefix(""); |
| 505 | |
| 506 | while (true) |
| 507 | { |
| 508 | ++rightmostCandidate; |
| 509 | |
| 510 | bool isInBoundaries = (rightmostCandidate != (*m_skipList.begin())->end()); |
| 511 | bool isInPrefix = false; |
| 512 | bool doesInterestContainDigest = false; |
| 513 | if (isInBoundaries) |
| 514 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 515 | doesInterestContainDigest = recognizeInterestWithDigest(interest, |
| 516 | *rightmostCandidate); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 517 | |
| 518 | if (doesInterestContainDigest) |
| 519 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 520 | isInPrefix = interest.getName().getPrefix(-1) |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 521 | .isPrefixOf((*rightmostCandidate)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 522 | } |
| 523 | else |
| 524 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 525 | isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | if (isInPrefix) |
| 530 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 531 | if (doesComplyWithSelectors(interest, *rightmostCandidate, doesInterestContainDigest)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 532 | { |
| 533 | if (hasLeftmostSelector) |
| 534 | { |
| 535 | return &(*rightmostCandidate)->getData(); |
| 536 | } |
| 537 | |
| 538 | if (hasRightmostSelector) |
| 539 | { |
| 540 | if (doesInterestContainDigest) |
| 541 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 542 | // get prefix which is one component longer than Interest name |
| 543 | // (without digest) |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 544 | const Name& childPrefix = (*rightmostCandidate)->getFullName() |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 545 | .getPrefix(interest.getName().size()); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 546 | NFD_LOG_TRACE("Child prefix" << childPrefix); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 547 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 548 | if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 549 | { |
| 550 | currentChildPrefix = childPrefix; |
| 551 | rightmost = rightmostCandidate; |
| 552 | } |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | // get prefix which is one component longer than Interest name |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 557 | const Name& childPrefix = (*rightmostCandidate)->getFullName() |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 558 | .getPrefix(interest.getName().size() + 1); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 559 | NFD_LOG_TRACE("Child prefix" << childPrefix); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 560 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 561 | if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 562 | { |
| 563 | currentChildPrefix = childPrefix; |
| 564 | rightmost = rightmostCandidate; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | else |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (rightmost != startingPoint) |
| 576 | { |
| 577 | return &(*rightmost)->getData(); |
| 578 | } |
| 579 | |
| 580 | if (hasRightmostSelector) // if rightmost was not found, try starting point |
| 581 | { |
| 582 | bool doesInterestContainDigest = recognizeInterestWithDigest(interest, *startingPoint); |
| 583 | bool isInPrefix = false; |
| 584 | |
| 585 | if (doesInterestContainDigest) |
| 586 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 587 | isInPrefix = interest.getName().getPrefix(-1).isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 588 | } |
| 589 | else |
| 590 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 591 | isInPrefix = interest.getName().isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if (isInPrefix) |
| 595 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 596 | if (doesComplyWithSelectors(interest, *startingPoint, doesInterestContainDigest)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 597 | { |
| 598 | return &(*startingPoint)->getData(); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 607 | Cs::doesComplyWithSelectors(const Interest& interest, |
| 608 | cs::Entry* entry, |
| 609 | bool doesInterestContainDigest) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 610 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 611 | NFD_LOG_TRACE("doesComplyWithSelectors()"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 612 | |
| 613 | /// \todo The following detection is not correct |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 614 | /// 1. If Interest name ends with 32-octet component doesn't mean that this component is |
| 615 | /// digest |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 616 | /// 2. Only min/max selectors (both 0) can be specified, all other selectors do not |
| 617 | /// make sense for interests with digest (though not sure if we need to enforce this) |
| 618 | |
| 619 | if (doesInterestContainDigest) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 620 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 621 | if (interest.getName().get(-1) != entry->getFullName().get(-1)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 622 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 623 | NFD_LOG_TRACE("violates implicit digest"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 628 | if (!doesInterestContainDigest) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 629 | { |
| 630 | if (interest.getMinSuffixComponents() >= 0) |
| 631 | { |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 632 | size_t minDataNameLength = interest.getName().size() + interest.getMinSuffixComponents(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 633 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 634 | bool isSatisfied = (minDataNameLength <= entry->getFullName().size()); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 635 | if (!isSatisfied) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 636 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 637 | NFD_LOG_TRACE("violates minComponents"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (interest.getMaxSuffixComponents() >= 0) |
| 643 | { |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 644 | size_t maxDataNameLength = interest.getName().size() + interest.getMaxSuffixComponents(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 645 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 646 | bool isSatisfied = (maxDataNameLength >= entry->getFullName().size()); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 647 | if (!isSatisfied) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 648 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 649 | NFD_LOG_TRACE("violates maxComponents"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 655 | if (interest.getMustBeFresh() && entry->getStaleTime() < time::steady_clock::now()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 656 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 657 | NFD_LOG_TRACE("violates mustBeFresh"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 658 | return false; |
| 659 | } |
| 660 | |
Ilya Moiseenko | 9c9231b | 2014-04-10 11:05:12 -0400 | [diff] [blame] | 661 | if (!interest.getPublisherPublicKeyLocator().empty()) |
| 662 | { |
| 663 | if (entry->getData().getSignature().getType() == ndn::Signature::Sha256WithRsa) |
| 664 | { |
| 665 | ndn::SignatureSha256WithRsa rsaSignature(entry->getData().getSignature()); |
| 666 | if (rsaSignature.getKeyLocator() != interest.getPublisherPublicKeyLocator()) |
| 667 | { |
| 668 | NFD_LOG_TRACE("violates publisher key selector"); |
| 669 | return false; |
| 670 | } |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | NFD_LOG_TRACE("violates publisher key selector"); |
| 675 | return false; |
| 676 | } |
| 677 | } |
| 678 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 679 | if (doesInterestContainDigest) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 680 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 681 | const ndn::name::Component& lastComponent = entry->getFullName().get(-1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 682 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 683 | if (!lastComponent.empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 684 | { |
| 685 | if (interest.getExclude().isExcluded(lastComponent)) |
| 686 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 687 | NFD_LOG_TRACE("violates exclusion"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 688 | return false; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | else |
| 693 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 694 | if (entry->getFullName().size() >= interest.getName().size() + 1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 695 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 696 | const ndn::name::Component& nextComponent = entry->getFullName() |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 697 | .get(interest.getName().size()); |
| 698 | if (!nextComponent.empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 699 | { |
| 700 | if (interest.getExclude().isExcluded(nextComponent)) |
| 701 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 702 | NFD_LOG_TRACE("violates exclusion"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 703 | return false; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 709 | NFD_LOG_TRACE("complies"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
| 713 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 714 | Cs::recognizeInterestWithDigest(const Interest& interest, cs::Entry* entry) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 715 | { |
| 716 | // only when min selector is not specified or specified with value of 0 |
| 717 | // and Interest's name length is exactly the length of the name of CS entry |
| 718 | if (interest.getMinSuffixComponents() <= 0 && |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 719 | interest.getName().size() == (entry->getFullName().size())) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 720 | { |
| 721 | const ndn::name::Component& last = interest.getName().get(-1); |
| 722 | if (last.value_size() == ndn::crypto::SHA256_DIGEST_SIZE) |
| 723 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 724 | NFD_LOG_TRACE("digest recognized"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 725 | return true; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | void |
| 733 | Cs::erase(const Name& exactName) |
| 734 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 735 | NFD_LOG_TRACE("insert() " << exactName << ", " |
| 736 | << "skipList size " << size()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 737 | |
| 738 | bool isIterated = false; |
| 739 | SkipListLayer::iterator updateTable[SKIPLIST_MAX_LAYERS]; |
| 740 | SkipList::reverse_iterator topLayer = m_skipList.rbegin(); |
| 741 | SkipListLayer::iterator head = (*topLayer)->begin(); |
| 742 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 743 | if (!(*topLayer)->empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 744 | { |
| 745 | //start from the upper layer towards bottom |
| 746 | int layer = m_skipList.size() - 1; |
| 747 | for (SkipList::reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit) |
| 748 | { |
| 749 | //if we didn't do any iterations on the higher layers, start from the begin() again |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 750 | if (!isIterated) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 751 | head = (*rit)->begin(); |
| 752 | |
| 753 | updateTable[layer] = head; |
| 754 | |
| 755 | if (head != (*rit)->end()) |
| 756 | { |
| 757 | // it can happen when begin() contains the element we want to remove |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 758 | if (!isIterated && ((*head)->getFullName() == exactName)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 759 | { |
Alexander Afanasyev | 8934c42 | 2015-01-05 15:58:46 -0800 | [diff] [blame^] | 760 | cs::Entry* entryToDelete = *head; |
| 761 | NFD_LOG_TRACE("Found target " << entryToDelete->getFullName()); |
| 762 | eraseFromSkipList(entryToDelete); |
| 763 | // head can become invalid after eraseFromSkipList |
| 764 | m_cleanupIndex.remove(entryToDelete); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | else |
| 768 | { |
| 769 | SkipListLayer::iterator it = head; |
| 770 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 771 | while ((*it)->getFullName() < exactName) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 772 | { |
| 773 | head = it; |
| 774 | updateTable[layer] = it; |
| 775 | isIterated = true; |
| 776 | |
| 777 | ++it; |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 778 | if (it == (*rit)->end()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if (layer > 0) |
| 785 | head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer |
| 786 | |
| 787 | layer--; |
| 788 | } |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | head = updateTable[0]; |
| 796 | ++head; // look at the next slot to check if it contains the item we want to remove |
| 797 | |
| 798 | bool isCsEmpty = (size() == 0); |
| 799 | bool isInBoundaries = (head != (*m_skipList.begin())->end()); |
| 800 | bool isNameIdentical = false; |
| 801 | if (!isCsEmpty && isInBoundaries) |
| 802 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 803 | NFD_LOG_TRACE("Identical? " << (*head)->getFullName()); |
| 804 | isNameIdentical = (*head)->getFullName() == exactName; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | if (isNameIdentical) |
| 808 | { |
Alexander Afanasyev | 8934c42 | 2015-01-05 15:58:46 -0800 | [diff] [blame^] | 809 | cs::Entry* entryToDelete = *head; |
| 810 | NFD_LOG_TRACE("Found target " << entryToDelete->getFullName()); |
| 811 | eraseFromSkipList(entryToDelete); |
| 812 | // head can become invalid after eraseFromSkipList |
| 813 | m_cleanupIndex.remove(entryToDelete); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 814 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | void |
| 818 | Cs::printSkipList() const |
| 819 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 820 | NFD_LOG_TRACE("print()"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 821 | //start from the upper layer towards bottom |
| 822 | int layer = m_skipList.size() - 1; |
| 823 | for (SkipList::const_reverse_iterator rit = m_skipList.rbegin(); rit != m_skipList.rend(); ++rit) |
| 824 | { |
| 825 | for (SkipListLayer::iterator it = (*rit)->begin(); it != (*rit)->end(); ++it) |
| 826 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 827 | NFD_LOG_TRACE("Layer " << layer << " " << (*it)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 828 | } |
| 829 | layer--; |
| 830 | } |
| 831 | } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 832 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 833 | } //namespace nfd |