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