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