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