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