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