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