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 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 352 | if (!m_cleanupIndex.get<unsolicited>().empty() && |
| 353 | (*m_cleanupIndex.get<unsolicited>().begin())->isUnsolicited()) |
| 354 | { |
| 355 | NFD_LOG_TRACE("Evict from unsolicited queue"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 356 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 357 | eraseFromSkipList(*m_cleanupIndex.get<unsolicited>().begin()); |
| 358 | m_cleanupIndex.get<unsolicited>().erase(m_cleanupIndex.get<unsolicited>().begin()); |
| 359 | return true; |
| 360 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 361 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 362 | if (!m_cleanupIndex.get<byStaleness>().empty() && |
| 363 | (*m_cleanupIndex.get<byStaleness>().begin())->getStaleTime() < time::steady_clock::now()) |
| 364 | { |
| 365 | NFD_LOG_TRACE("Evict from staleness queue"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 366 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 367 | eraseFromSkipList(*m_cleanupIndex.get<byStaleness>().begin()); |
| 368 | m_cleanupIndex.get<byStaleness>().erase(m_cleanupIndex.get<byStaleness>().begin()); |
| 369 | return true; |
| 370 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 371 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 372 | if (!m_cleanupIndex.get<byArrival>().empty()) |
| 373 | { |
| 374 | NFD_LOG_TRACE("Evict from arrival queue"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 375 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 376 | eraseFromSkipList(*m_cleanupIndex.get<byArrival>().begin()); |
| 377 | m_cleanupIndex.get<byArrival>().erase(m_cleanupIndex.get<byArrival>().begin()); |
| 378 | return true; |
| 379 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 380 | |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | const Data* |
| 385 | Cs::find(const Interest& interest) const |
| 386 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 387 | NFD_LOG_TRACE("find() " << interest.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 388 | |
| 389 | bool isIterated = false; |
| 390 | SkipList::const_reverse_iterator topLayer = m_skipList.rbegin(); |
| 391 | SkipListLayer::iterator head = (*topLayer)->begin(); |
| 392 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 393 | if (!(*topLayer)->empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 394 | { |
| 395 | //start from the upper layer towards bottom |
| 396 | int layer = m_skipList.size() - 1; |
| 397 | for (SkipList::const_reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit) |
| 398 | { |
| 399 | //if we didn't do any iterations on the higher layers, start from the begin() again |
| 400 | if (!isIterated) |
| 401 | head = (*rit)->begin(); |
| 402 | |
| 403 | if (head != (*rit)->end()) |
| 404 | { |
| 405 | // it happens when begin() contains the element we want to find |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 406 | if (!isIterated && (interest.getName().isPrefixOf((*head)->getFullName()))) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 407 | { |
| 408 | if (layer > 0) |
| 409 | { |
| 410 | layer--; |
| 411 | continue; // try lower layer |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | isIterated = true; |
| 416 | } |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | SkipListLayer::iterator it = head; |
| 421 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 422 | while ((*it)->getFullName() < interest.getName()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 423 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 424 | NFD_LOG_TRACE((*it)->getFullName() << " < " << interest.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 425 | head = it; |
| 426 | isIterated = true; |
| 427 | |
| 428 | ++it; |
| 429 | if (it == (*rit)->end()) |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (layer > 0) |
| 436 | { |
| 437 | head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer |
| 438 | } |
| 439 | else //if we reached the first layer |
| 440 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 441 | if (isIterated) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 442 | return selectChild(interest, head); |
| 443 | } |
| 444 | |
| 445 | layer--; |
| 446 | } |
| 447 | } |
| 448 | |
Alexander Afanasyev | c9765df | 2014-01-25 19:24:27 -0800 | [diff] [blame] | 449 | return 0; |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 452 | const Data* |
| 453 | Cs::selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const |
| 454 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 455 | BOOST_ASSERT(startingPoint != (*m_skipList.begin())->end()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 456 | |
| 457 | if (startingPoint != (*m_skipList.begin())->begin()) |
| 458 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 459 | BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 462 | NFD_LOG_TRACE("selectChild() " << interest.getChildSelector() << " " |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 463 | << (*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 464 | |
| 465 | bool hasLeftmostSelector = (interest.getChildSelector() <= 0); |
| 466 | bool hasRightmostSelector = !hasLeftmostSelector; |
| 467 | |
| 468 | if (hasLeftmostSelector) |
| 469 | { |
| 470 | bool doesInterestContainDigest = recognizeInterestWithDigest(interest, *startingPoint); |
| 471 | bool isInPrefix = false; |
| 472 | |
| 473 | if (doesInterestContainDigest) |
| 474 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 475 | isInPrefix = interest.getName().getPrefix(-1).isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 476 | } |
| 477 | else |
| 478 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 479 | isInPrefix = interest.getName().isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | if (isInPrefix) |
| 483 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 484 | if (doesComplyWithSelectors(interest, *startingPoint, doesInterestContainDigest)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 485 | { |
| 486 | return &(*startingPoint)->getData(); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | //iterate to the right |
| 492 | SkipListLayer::iterator rightmost = startingPoint; |
| 493 | if (startingPoint != (*m_skipList.begin())->end()) |
| 494 | { |
| 495 | SkipListLayer::iterator rightmostCandidate = startingPoint; |
| 496 | Name currentChildPrefix(""); |
| 497 | |
| 498 | while (true) |
| 499 | { |
| 500 | ++rightmostCandidate; |
| 501 | |
| 502 | bool isInBoundaries = (rightmostCandidate != (*m_skipList.begin())->end()); |
| 503 | bool isInPrefix = false; |
| 504 | bool doesInterestContainDigest = false; |
| 505 | if (isInBoundaries) |
| 506 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 507 | doesInterestContainDigest = recognizeInterestWithDigest(interest, |
| 508 | *rightmostCandidate); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 509 | |
| 510 | if (doesInterestContainDigest) |
| 511 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 512 | isInPrefix = interest.getName().getPrefix(-1) |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 513 | .isPrefixOf((*rightmostCandidate)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 514 | } |
| 515 | else |
| 516 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 517 | isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | if (isInPrefix) |
| 522 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 523 | if (doesComplyWithSelectors(interest, *rightmostCandidate, doesInterestContainDigest)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 524 | { |
| 525 | if (hasLeftmostSelector) |
| 526 | { |
| 527 | return &(*rightmostCandidate)->getData(); |
| 528 | } |
| 529 | |
| 530 | if (hasRightmostSelector) |
| 531 | { |
| 532 | if (doesInterestContainDigest) |
| 533 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 534 | // get prefix which is one component longer than Interest name |
| 535 | // (without digest) |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 536 | const Name& childPrefix = (*rightmostCandidate)->getFullName() |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 537 | .getPrefix(interest.getName().size()); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 538 | NFD_LOG_TRACE("Child prefix" << childPrefix); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 539 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 540 | if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 541 | { |
| 542 | currentChildPrefix = childPrefix; |
| 543 | rightmost = rightmostCandidate; |
| 544 | } |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | // get prefix which is one component longer than Interest name |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 549 | const Name& childPrefix = (*rightmostCandidate)->getFullName() |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 550 | .getPrefix(interest.getName().size() + 1); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 551 | NFD_LOG_TRACE("Child prefix" << childPrefix); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 552 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 553 | if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 554 | { |
| 555 | currentChildPrefix = childPrefix; |
| 556 | rightmost = rightmostCandidate; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | else |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (rightmost != startingPoint) |
| 568 | { |
| 569 | return &(*rightmost)->getData(); |
| 570 | } |
| 571 | |
| 572 | if (hasRightmostSelector) // if rightmost was not found, try starting point |
| 573 | { |
| 574 | bool doesInterestContainDigest = recognizeInterestWithDigest(interest, *startingPoint); |
| 575 | bool isInPrefix = false; |
| 576 | |
| 577 | if (doesInterestContainDigest) |
| 578 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 579 | isInPrefix = interest.getName().getPrefix(-1).isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 580 | } |
| 581 | else |
| 582 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 583 | isInPrefix = interest.getName().isPrefixOf((*startingPoint)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | if (isInPrefix) |
| 587 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 588 | if (doesComplyWithSelectors(interest, *startingPoint, doesInterestContainDigest)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 589 | { |
| 590 | return &(*startingPoint)->getData(); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 599 | Cs::doesComplyWithSelectors(const Interest& interest, |
| 600 | cs::Entry* entry, |
| 601 | bool doesInterestContainDigest) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 602 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 603 | NFD_LOG_TRACE("doesComplyWithSelectors()"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 604 | |
| 605 | /// \todo The following detection is not correct |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 606 | /// 1. If Interest name ends with 32-octet component doesn't mean that this component is |
| 607 | /// digest |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 608 | /// 2. Only min/max selectors (both 0) can be specified, all other selectors do not |
| 609 | /// make sense for interests with digest (though not sure if we need to enforce this) |
| 610 | |
| 611 | if (doesInterestContainDigest) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 612 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 613 | if (interest.getName().get(-1) != entry->getFullName().get(-1)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 614 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 615 | NFD_LOG_TRACE("violates implicit digest"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 616 | return false; |
| 617 | } |
| 618 | } |
| 619 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 620 | if (!doesInterestContainDigest) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 621 | { |
| 622 | if (interest.getMinSuffixComponents() >= 0) |
| 623 | { |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 624 | size_t minDataNameLength = interest.getName().size() + interest.getMinSuffixComponents(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 625 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 626 | bool isSatisfied = (minDataNameLength <= entry->getFullName().size()); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 627 | if (!isSatisfied) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 628 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 629 | NFD_LOG_TRACE("violates minComponents"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if (interest.getMaxSuffixComponents() >= 0) |
| 635 | { |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 636 | size_t maxDataNameLength = interest.getName().size() + interest.getMaxSuffixComponents(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 637 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 638 | bool isSatisfied = (maxDataNameLength >= entry->getFullName().size()); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 639 | if (!isSatisfied) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 640 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 641 | NFD_LOG_TRACE("violates maxComponents"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 647 | if (interest.getMustBeFresh() && entry->getStaleTime() < time::steady_clock::now()) |
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 mustBeFresh"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
Ilya Moiseenko | 9c9231b | 2014-04-10 11:05:12 -0400 | [diff] [blame] | 653 | if (!interest.getPublisherPublicKeyLocator().empty()) |
| 654 | { |
| 655 | if (entry->getData().getSignature().getType() == ndn::Signature::Sha256WithRsa) |
| 656 | { |
| 657 | ndn::SignatureSha256WithRsa rsaSignature(entry->getData().getSignature()); |
| 658 | if (rsaSignature.getKeyLocator() != interest.getPublisherPublicKeyLocator()) |
| 659 | { |
| 660 | NFD_LOG_TRACE("violates publisher key selector"); |
| 661 | return false; |
| 662 | } |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | NFD_LOG_TRACE("violates publisher key selector"); |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 671 | if (doesInterestContainDigest) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 672 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 673 | const ndn::name::Component& lastComponent = entry->getFullName().get(-1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 674 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 675 | if (!lastComponent.empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 676 | { |
| 677 | if (interest.getExclude().isExcluded(lastComponent)) |
| 678 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 679 | NFD_LOG_TRACE("violates exclusion"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | else |
| 685 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 686 | if (entry->getFullName().size() >= interest.getName().size() + 1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 687 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 688 | const ndn::name::Component& nextComponent = entry->getFullName() |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 689 | .get(interest.getName().size()); |
| 690 | if (!nextComponent.empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 691 | { |
| 692 | if (interest.getExclude().isExcluded(nextComponent)) |
| 693 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 694 | NFD_LOG_TRACE("violates exclusion"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 695 | return false; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 701 | NFD_LOG_TRACE("complies"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 702 | return true; |
| 703 | } |
| 704 | |
| 705 | bool |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 706 | Cs::recognizeInterestWithDigest(const Interest& interest, cs::Entry* entry) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 707 | { |
| 708 | // only when min selector is not specified or specified with value of 0 |
| 709 | // and Interest's name length is exactly the length of the name of CS entry |
| 710 | if (interest.getMinSuffixComponents() <= 0 && |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 711 | interest.getName().size() == (entry->getFullName().size())) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 712 | { |
| 713 | const ndn::name::Component& last = interest.getName().get(-1); |
| 714 | if (last.value_size() == ndn::crypto::SHA256_DIGEST_SIZE) |
| 715 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 716 | NFD_LOG_TRACE("digest recognized"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | void |
| 725 | Cs::erase(const Name& exactName) |
| 726 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 727 | NFD_LOG_TRACE("insert() " << exactName << ", " |
| 728 | << "skipList size " << size()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 729 | |
| 730 | bool isIterated = false; |
| 731 | SkipListLayer::iterator updateTable[SKIPLIST_MAX_LAYERS]; |
| 732 | SkipList::reverse_iterator topLayer = m_skipList.rbegin(); |
| 733 | SkipListLayer::iterator head = (*topLayer)->begin(); |
| 734 | |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 735 | if (!(*topLayer)->empty()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 736 | { |
| 737 | //start from the upper layer towards bottom |
| 738 | int layer = m_skipList.size() - 1; |
| 739 | for (SkipList::reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit) |
| 740 | { |
| 741 | //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] | 742 | if (!isIterated) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 743 | head = (*rit)->begin(); |
| 744 | |
| 745 | updateTable[layer] = head; |
| 746 | |
| 747 | if (head != (*rit)->end()) |
| 748 | { |
| 749 | // it can happen when begin() contains the element we want to remove |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 750 | if (!isIterated && ((*head)->getFullName() == exactName)) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 751 | { |
| 752 | eraseFromSkipList(*head); |
| 753 | return; |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | SkipListLayer::iterator it = head; |
| 758 | |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 759 | while ((*it)->getFullName() < exactName) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 760 | { |
| 761 | head = it; |
| 762 | updateTable[layer] = it; |
| 763 | isIterated = true; |
| 764 | |
| 765 | ++it; |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 766 | if (it == (*rit)->end()) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | if (layer > 0) |
| 773 | head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer |
| 774 | |
| 775 | layer--; |
| 776 | } |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | head = updateTable[0]; |
| 784 | ++head; // look at the next slot to check if it contains the item we want to remove |
| 785 | |
| 786 | bool isCsEmpty = (size() == 0); |
| 787 | bool isInBoundaries = (head != (*m_skipList.begin())->end()); |
| 788 | bool isNameIdentical = false; |
| 789 | if (!isCsEmpty && isInBoundaries) |
| 790 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 791 | NFD_LOG_TRACE("Identical? " << (*head)->getFullName()); |
| 792 | isNameIdentical = (*head)->getFullName() == exactName; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | if (isNameIdentical) |
| 796 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 797 | NFD_LOG_TRACE("Found target " << (*head)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 798 | eraseFromSkipList(*head); |
| 799 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | void |
| 803 | Cs::printSkipList() const |
| 804 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 805 | NFD_LOG_TRACE("print()"); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 806 | //start from the upper layer towards bottom |
| 807 | int layer = m_skipList.size() - 1; |
| 808 | for (SkipList::const_reverse_iterator rit = m_skipList.rbegin(); rit != m_skipList.rend(); ++rit) |
| 809 | { |
| 810 | for (SkipListLayer::iterator it = (*rit)->begin(); it != (*rit)->end(); ++it) |
| 811 | { |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 812 | NFD_LOG_TRACE("Layer " << layer << " " << (*it)->getFullName()); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 813 | } |
| 814 | layer--; |
| 815 | } |
| 816 | } |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 817 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 818 | } //namespace nfd |