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