blob: 2c1b65759f395c74c1e04e95c15d418278b0234c [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -070025 * \author Ilya Moiseenko <http://ilyamoiseenko.com/>
26 * \author Junxiao Shi <http://www.cs.arizona.edu/people/shijunxiao/>
27 * \author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070028 */
29
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070030#include "cs.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060031#include "core/logger.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070032#include "core/random.hpp"
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -070033
Alexander Afanasyev4a771362014-04-24 21:29:33 -070034#include <ndn-cxx/util/crypto.hpp>
Junxiao Shiaf6569a2014-06-14 00:01:34 -070035#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -070036
Junxiao Shiaf6569a2014-06-14 00:01:34 -070037#include <boost/random/bernoulli_distribution.hpp>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080038
Junxiao Shiaf6569a2014-06-14 00:01:34 -070039/// max skip list layers
40static const size_t SKIPLIST_MAX_LAYERS = 32;
41/// probability for an entry in layer N to appear also in layer N+1
42static const double SKIPLIST_PROBABILITY = 0.25;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080043
44NFD_LOG_INIT("ContentStore");
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080045
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080046namespace nfd {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070047
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060048Cs::Cs(size_t nMaxPackets)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080049 : m_nMaxPackets(nMaxPackets)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040050 , m_nPackets(0)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070051{
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052 SkipListLayer* zeroLayer = new SkipListLayer();
53 m_skipList.push_back(zeroLayer);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040054
55 for (size_t i = 0; i < m_nMaxPackets; i++)
56 m_freeCsEntries.push(new cs::Entry());
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070057}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080058
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070059Cs::~Cs()
60{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040061 // evict all items from CS
62 while (evictItem())
63 ;
64
65 BOOST_ASSERT(m_freeCsEntries.size() == m_nMaxPackets);
66
67 while (!m_freeCsEntries.empty())
68 {
69 delete m_freeCsEntries.front();
70 m_freeCsEntries.pop();
71 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072}
73
74size_t
75Cs::size() const
76{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040077 return m_nPackets; // size of the first layer in a skip list
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080078}
79
80void
81Cs::setLimit(size_t nMaxPackets)
82{
Alexander Afanasyev281b9162014-06-08 10:15:20 +030083 size_t oldNMaxPackets = m_nMaxPackets;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080084 m_nMaxPackets = nMaxPackets;
85
Alexander Afanasyev281b9162014-06-08 10:15:20 +030086 while (size() > m_nMaxPackets) {
87 evictItem();
88 }
89
90 if (m_nMaxPackets >= oldNMaxPackets) {
91 for (size_t i = oldNMaxPackets; i < m_nMaxPackets; i++) {
92 m_freeCsEntries.push(new cs::Entry());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080093 }
Alexander Afanasyev281b9162014-06-08 10:15:20 +030094 }
95 else {
96 for (size_t i = oldNMaxPackets; i > m_nMaxPackets; i--) {
97 delete m_freeCsEntries.front();
98 m_freeCsEntries.pop();
99 }
100 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800101}
102
103size_t
104Cs::getLimit() const
105{
106 return m_nMaxPackets;
107}
108
109//Reference: "Skip Lists: A Probabilistic Alternative to Balanced Trees" by W.Pugh
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400110std::pair<cs::Entry*, bool>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800111Cs::insertToSkipList(const Data& data, bool isUnsolicited)
112{
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700113 NFD_LOG_TRACE("insertToSkipList() " << data.getFullName() << ", "
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700114 << "skipList size " << size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800115
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400116 BOOST_ASSERT(m_cleanupIndex.size() <= size());
117 BOOST_ASSERT(m_freeCsEntries.size() > 0);
118
119 // take entry for the memory pool
120 cs::Entry* entry = m_freeCsEntries.front();
121 m_freeCsEntries.pop();
122 m_nPackets++;
123 entry->setData(data, isUnsolicited);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800124
125 bool insertInFront = false;
126 bool isIterated = false;
127 SkipList::reverse_iterator topLayer = m_skipList.rbegin();
128 SkipListLayer::iterator updateTable[SKIPLIST_MAX_LAYERS];
129 SkipListLayer::iterator head = (*topLayer)->begin();
130
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400131 if (!(*topLayer)->empty())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800132 {
133 //start from the upper layer towards bottom
134 int layer = m_skipList.size() - 1;
135 for (SkipList::reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit)
136 {
137 //if we didn't do any iterations on the higher layers, start from the begin() again
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400138 if (!isIterated)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139 head = (*rit)->begin();
140
141 updateTable[layer] = head;
142
143 if (head != (*rit)->end())
144 {
145 // it can happen when begin() contains the element in front of which we need to insert
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700146 if (!isIterated && ((*head)->getFullName() >= entry->getFullName()))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800147 {
148 --updateTable[layer];
149 insertInFront = true;
150 }
151 else
152 {
153 SkipListLayer::iterator it = head;
154
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700155 while ((*it)->getFullName() < entry->getFullName())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800156 {
157 head = it;
158 updateTable[layer] = it;
159 isIterated = true;
160
161 ++it;
162 if (it == (*rit)->end())
163 break;
164 }
165 }
166 }
167
168 if (layer > 0)
169 head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer
170
171 layer--;
172 }
173 }
174 else
175 {
176 updateTable[0] = (*topLayer)->begin(); //initialization
177 }
178
179 head = updateTable[0];
180 ++head; // look at the next slot to check if it contains a duplicate
181
182 bool isCsEmpty = (size() == 0);
183 bool isInBoundaries = (head != (*m_skipList.begin())->end());
184 bool isNameIdentical = false;
185 if (!isCsEmpty && isInBoundaries)
186 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700187 isNameIdentical = (*head)->getFullName() == entry->getFullName();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800188 }
189
190 //check if this is a duplicate packet
191 if (isNameIdentical)
192 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700193 NFD_LOG_TRACE("Duplicate name (with digest)");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800194
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700195 (*head)->setData(data, isUnsolicited); //updates stale time
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800196
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400197 // new entry not needed, returning to the pool
198 entry->release();
199 m_freeCsEntries.push(entry);
200 m_nPackets--;
201
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202 return std::make_pair(*head, false);
203 }
204
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700205 NFD_LOG_TRACE("Not a duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800206
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700207 size_t randomLayer = pickRandomLayer();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400209 while (m_skipList.size() < randomLayer + 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800210 {
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700211 SkipListLayer* newLayer = new SkipListLayer();
212 m_skipList.push_back(newLayer);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800213
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700214 updateTable[(m_skipList.size() - 1)] = newLayer->begin();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800215 }
216
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700217 size_t layer = 0;
218 for (SkipList::iterator i = m_skipList.begin();
219 i != m_skipList.end() && layer <= randomLayer; ++i)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800220 {
221 if (updateTable[layer] == (*i)->end() && !insertInFront)
222 {
223 (*i)->push_back(entry);
224 SkipListLayer::iterator last = (*i)->end();
225 --last;
226 entry->setIterator(layer, last);
227
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700228 NFD_LOG_TRACE("pushback " << &(*last));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800229 }
230 else if (updateTable[layer] == (*i)->end() && insertInFront)
231 {
232 (*i)->push_front(entry);
233 entry->setIterator(layer, (*i)->begin());
234
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700235 NFD_LOG_TRACE("pushfront ");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800236 }
237 else
238 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700239 NFD_LOG_TRACE("insertafter");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240 ++updateTable[layer]; // insert after
241 SkipListLayer::iterator position = (*i)->insert(updateTable[layer], entry);
242 entry->setIterator(layer, position); // save iterator where item was inserted
243 }
244 layer++;
245 }
246
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800247 return std::make_pair(entry, true);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700248}
249
250bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800251Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700252{
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700253 NFD_LOG_TRACE("insert() " << data.getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800254
255 if (isFull())
256 {
257 evictItem();
258 }
259
260 //pointer and insertion status
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400261 std::pair<cs::Entry*, bool> entry = insertToSkipList(data, isUnsolicited);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800262
263 //new entry
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400264 if (static_cast<bool>(entry.first) && (entry.second == true))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800265 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400266 m_cleanupIndex.push_back(entry.first);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800267 return true;
268 }
269
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700270 return false;
271}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800272
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700273size_t
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800274Cs::pickRandomLayer() const
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700275{
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700276 static boost::random::bernoulli_distribution<> dist(SKIPLIST_PROBABILITY);
277 // TODO rewrite using geometry_distribution
278 size_t layer;
279 for (layer = 0; layer < SKIPLIST_MAX_LAYERS; ++layer) {
280 if (!dist(getGlobalRng())) {
281 break;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800282 }
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700283 }
284 return layer;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800285}
286
287bool
288Cs::isFull() const
289{
290 if (size() >= m_nMaxPackets) //size of the first layer vs. max size
291 return true;
292
293 return false;
294}
295
296bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400297Cs::eraseFromSkipList(cs::Entry* entry)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800298{
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700299 NFD_LOG_TRACE("eraseFromSkipList() " << entry->getFullName());
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700300 NFD_LOG_TRACE("SkipList size " << size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800301
302 bool isErased = false;
303
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400304 const std::map<int, std::list<cs::Entry*>::iterator>& iterators = entry->getIterators();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800305
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400306 if (!iterators.empty())
307 {
308 int layer = 0;
309
310 for (SkipList::iterator it = m_skipList.begin(); it != m_skipList.end(); )
311 {
312 std::map<int, std::list<cs::Entry*>::iterator>::const_iterator i = iterators.find(layer);
313
314 if (i != iterators.end())
315 {
316 (*it)->erase(i->second);
317 entry->removeIterator(layer);
318 isErased = true;
319
320 //remove layers that do not contain any elements (starting from the second layer)
321 if ((layer != 0) && (*it)->empty())
322 {
323 delete *it;
324 it = m_skipList.erase(it);
325 }
326 else
327 ++it;
328
329 layer++;
330 }
331 else
332 break;
333 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800334 }
335
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400336 //delete entry;
337 if (isErased)
338 {
339 entry->release();
340 m_freeCsEntries.push(entry);
341 m_nPackets--;
342 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800343
344 return isErased;
345}
346
347bool
348Cs::evictItem()
349{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700350 NFD_LOG_TRACE("evictItem()");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800351
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400352 if (!m_cleanupIndex.get<unsolicited>().empty() &&
353 (*m_cleanupIndex.get<unsolicited>().begin())->isUnsolicited())
354 {
355 NFD_LOG_TRACE("Evict from unsolicited queue");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800356
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400357 eraseFromSkipList(*m_cleanupIndex.get<unsolicited>().begin());
358 m_cleanupIndex.get<unsolicited>().erase(m_cleanupIndex.get<unsolicited>().begin());
359 return true;
360 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800361
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400362 if (!m_cleanupIndex.get<byStaleness>().empty() &&
363 (*m_cleanupIndex.get<byStaleness>().begin())->getStaleTime() < time::steady_clock::now())
364 {
365 NFD_LOG_TRACE("Evict from staleness queue");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800366
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400367 eraseFromSkipList(*m_cleanupIndex.get<byStaleness>().begin());
368 m_cleanupIndex.get<byStaleness>().erase(m_cleanupIndex.get<byStaleness>().begin());
369 return true;
370 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800371
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400372 if (!m_cleanupIndex.get<byArrival>().empty())
373 {
374 NFD_LOG_TRACE("Evict from arrival queue");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800375
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400376 eraseFromSkipList(*m_cleanupIndex.get<byArrival>().begin());
377 m_cleanupIndex.get<byArrival>().erase(m_cleanupIndex.get<byArrival>().begin());
378 return true;
379 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800380
381 return false;
382}
383
384const Data*
385Cs::find(const Interest& interest) const
386{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700387 NFD_LOG_TRACE("find() " << interest.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800388
389 bool isIterated = false;
390 SkipList::const_reverse_iterator topLayer = m_skipList.rbegin();
391 SkipListLayer::iterator head = (*topLayer)->begin();
392
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400393 if (!(*topLayer)->empty())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800394 {
395 //start from the upper layer towards bottom
396 int layer = m_skipList.size() - 1;
397 for (SkipList::const_reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit)
398 {
399 //if we didn't do any iterations on the higher layers, start from the begin() again
400 if (!isIterated)
401 head = (*rit)->begin();
402
403 if (head != (*rit)->end())
404 {
405 // it happens when begin() contains the element we want to find
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700406 if (!isIterated && (interest.getName().isPrefixOf((*head)->getFullName())))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800407 {
408 if (layer > 0)
409 {
410 layer--;
411 continue; // try lower layer
412 }
413 else
414 {
415 isIterated = true;
416 }
417 }
418 else
419 {
420 SkipListLayer::iterator it = head;
421
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700422 while ((*it)->getFullName() < interest.getName())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800423 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700424 NFD_LOG_TRACE((*it)->getFullName() << " < " << interest.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800425 head = it;
426 isIterated = true;
427
428 ++it;
429 if (it == (*rit)->end())
430 break;
431 }
432 }
433 }
434
435 if (layer > 0)
436 {
437 head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer
438 }
439 else //if we reached the first layer
440 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400441 if (isIterated)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800442 return selectChild(interest, head);
443 }
444
445 layer--;
446 }
447 }
448
Alexander Afanasyevc9765df2014-01-25 19:24:27 -0800449 return 0;
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700450}
451
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800452const Data*
453Cs::selectChild(const Interest& interest, SkipListLayer::iterator startingPoint) const
454{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400455 BOOST_ASSERT(startingPoint != (*m_skipList.begin())->end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800456
457 if (startingPoint != (*m_skipList.begin())->begin())
458 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700459 BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800460 }
461
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400462 NFD_LOG_TRACE("selectChild() " << interest.getChildSelector() << " "
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700463 << (*startingPoint)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800464
465 bool hasLeftmostSelector = (interest.getChildSelector() <= 0);
466 bool hasRightmostSelector = !hasLeftmostSelector;
467
468 if (hasLeftmostSelector)
469 {
470 bool doesInterestContainDigest = recognizeInterestWithDigest(interest, *startingPoint);
471 bool isInPrefix = false;
472
473 if (doesInterestContainDigest)
474 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700475 isInPrefix = interest.getName().getPrefix(-1).isPrefixOf((*startingPoint)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800476 }
477 else
478 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700479 isInPrefix = interest.getName().isPrefixOf((*startingPoint)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800480 }
481
482 if (isInPrefix)
483 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400484 if (doesComplyWithSelectors(interest, *startingPoint, doesInterestContainDigest))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800485 {
486 return &(*startingPoint)->getData();
487 }
488 }
489 }
490
491 //iterate to the right
492 SkipListLayer::iterator rightmost = startingPoint;
493 if (startingPoint != (*m_skipList.begin())->end())
494 {
495 SkipListLayer::iterator rightmostCandidate = startingPoint;
496 Name currentChildPrefix("");
497
498 while (true)
499 {
500 ++rightmostCandidate;
501
502 bool isInBoundaries = (rightmostCandidate != (*m_skipList.begin())->end());
503 bool isInPrefix = false;
504 bool doesInterestContainDigest = false;
505 if (isInBoundaries)
506 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400507 doesInterestContainDigest = recognizeInterestWithDigest(interest,
508 *rightmostCandidate);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800509
510 if (doesInterestContainDigest)
511 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400512 isInPrefix = interest.getName().getPrefix(-1)
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700513 .isPrefixOf((*rightmostCandidate)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800514 }
515 else
516 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700517 isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800518 }
519 }
520
521 if (isInPrefix)
522 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400523 if (doesComplyWithSelectors(interest, *rightmostCandidate, doesInterestContainDigest))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800524 {
525 if (hasLeftmostSelector)
526 {
527 return &(*rightmostCandidate)->getData();
528 }
529
530 if (hasRightmostSelector)
531 {
532 if (doesInterestContainDigest)
533 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400534 // get prefix which is one component longer than Interest name
535 // (without digest)
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700536 const Name& childPrefix = (*rightmostCandidate)->getFullName()
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400537 .getPrefix(interest.getName().size());
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700538 NFD_LOG_TRACE("Child prefix" << childPrefix);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800539
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400540 if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800541 {
542 currentChildPrefix = childPrefix;
543 rightmost = rightmostCandidate;
544 }
545 }
546 else
547 {
548 // get prefix which is one component longer than Interest name
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700549 const Name& childPrefix = (*rightmostCandidate)->getFullName()
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400550 .getPrefix(interest.getName().size() + 1);
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700551 NFD_LOG_TRACE("Child prefix" << childPrefix);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800552
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400553 if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800554 {
555 currentChildPrefix = childPrefix;
556 rightmost = rightmostCandidate;
557 }
558 }
559 }
560 }
561 }
562 else
563 break;
564 }
565 }
566
567 if (rightmost != startingPoint)
568 {
569 return &(*rightmost)->getData();
570 }
571
572 if (hasRightmostSelector) // if rightmost was not found, try starting point
573 {
574 bool doesInterestContainDigest = recognizeInterestWithDigest(interest, *startingPoint);
575 bool isInPrefix = false;
576
577 if (doesInterestContainDigest)
578 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700579 isInPrefix = interest.getName().getPrefix(-1).isPrefixOf((*startingPoint)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800580 }
581 else
582 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700583 isInPrefix = interest.getName().isPrefixOf((*startingPoint)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800584 }
585
586 if (isInPrefix)
587 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400588 if (doesComplyWithSelectors(interest, *startingPoint, doesInterestContainDigest))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800589 {
590 return &(*startingPoint)->getData();
591 }
592 }
593 }
594
595 return 0;
596}
597
598bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400599Cs::doesComplyWithSelectors(const Interest& interest,
600 cs::Entry* entry,
601 bool doesInterestContainDigest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800602{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700603 NFD_LOG_TRACE("doesComplyWithSelectors()");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800604
605 /// \todo The following detection is not correct
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700606 /// 1. If Interest name ends with 32-octet component doesn't mean that this component is
607 /// digest
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400608 /// 2. Only min/max selectors (both 0) can be specified, all other selectors do not
609 /// make sense for interests with digest (though not sure if we need to enforce this)
610
611 if (doesInterestContainDigest)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800612 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700613 if (interest.getName().get(-1) != entry->getFullName().get(-1))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800614 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700615 NFD_LOG_TRACE("violates implicit digest");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800616 return false;
617 }
618 }
619
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400620 if (!doesInterestContainDigest)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800621 {
622 if (interest.getMinSuffixComponents() >= 0)
623 {
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700624 size_t minDataNameLength = interest.getName().size() + interest.getMinSuffixComponents();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800625
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700626 bool isSatisfied = (minDataNameLength <= entry->getFullName().size());
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400627 if (!isSatisfied)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800628 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700629 NFD_LOG_TRACE("violates minComponents");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800630 return false;
631 }
632 }
633
634 if (interest.getMaxSuffixComponents() >= 0)
635 {
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700636 size_t maxDataNameLength = interest.getName().size() + interest.getMaxSuffixComponents();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800637
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700638 bool isSatisfied = (maxDataNameLength >= entry->getFullName().size());
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400639 if (!isSatisfied)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800640 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700641 NFD_LOG_TRACE("violates maxComponents");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800642 return false;
643 }
644 }
645 }
646
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700647 if (interest.getMustBeFresh() && entry->getStaleTime() < time::steady_clock::now())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800648 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700649 NFD_LOG_TRACE("violates mustBeFresh");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800650 return false;
651 }
652
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400653 if (!interest.getPublisherPublicKeyLocator().empty())
654 {
655 if (entry->getData().getSignature().getType() == ndn::Signature::Sha256WithRsa)
656 {
657 ndn::SignatureSha256WithRsa rsaSignature(entry->getData().getSignature());
658 if (rsaSignature.getKeyLocator() != interest.getPublisherPublicKeyLocator())
659 {
660 NFD_LOG_TRACE("violates publisher key selector");
661 return false;
662 }
663 }
664 else
665 {
666 NFD_LOG_TRACE("violates publisher key selector");
667 return false;
668 }
669 }
670
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400671 if (doesInterestContainDigest)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800672 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700673 const ndn::name::Component& lastComponent = entry->getFullName().get(-1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800674
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400675 if (!lastComponent.empty())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800676 {
677 if (interest.getExclude().isExcluded(lastComponent))
678 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700679 NFD_LOG_TRACE("violates exclusion");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800680 return false;
681 }
682 }
683 }
684 else
685 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700686 if (entry->getFullName().size() >= interest.getName().size() + 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800687 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700688 const ndn::name::Component& nextComponent = entry->getFullName()
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400689 .get(interest.getName().size());
690 if (!nextComponent.empty())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800691 {
692 if (interest.getExclude().isExcluded(nextComponent))
693 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700694 NFD_LOG_TRACE("violates exclusion");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800695 return false;
696 }
697 }
698 }
699 }
700
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700701 NFD_LOG_TRACE("complies");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800702 return true;
703}
704
705bool
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400706Cs::recognizeInterestWithDigest(const Interest& interest, cs::Entry* entry) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800707{
708 // only when min selector is not specified or specified with value of 0
709 // and Interest's name length is exactly the length of the name of CS entry
710 if (interest.getMinSuffixComponents() <= 0 &&
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700711 interest.getName().size() == (entry->getFullName().size()))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800712 {
713 const ndn::name::Component& last = interest.getName().get(-1);
714 if (last.value_size() == ndn::crypto::SHA256_DIGEST_SIZE)
715 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700716 NFD_LOG_TRACE("digest recognized");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800717 return true;
718 }
719 }
720
721 return false;
722}
723
724void
725Cs::erase(const Name& exactName)
726{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700727 NFD_LOG_TRACE("insert() " << exactName << ", "
728 << "skipList size " << size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800729
730 bool isIterated = false;
731 SkipListLayer::iterator updateTable[SKIPLIST_MAX_LAYERS];
732 SkipList::reverse_iterator topLayer = m_skipList.rbegin();
733 SkipListLayer::iterator head = (*topLayer)->begin();
734
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400735 if (!(*topLayer)->empty())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800736 {
737 //start from the upper layer towards bottom
738 int layer = m_skipList.size() - 1;
739 for (SkipList::reverse_iterator rit = topLayer; rit != m_skipList.rend(); ++rit)
740 {
741 //if we didn't do any iterations on the higher layers, start from the begin() again
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400742 if (!isIterated)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800743 head = (*rit)->begin();
744
745 updateTable[layer] = head;
746
747 if (head != (*rit)->end())
748 {
749 // it can happen when begin() contains the element we want to remove
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700750 if (!isIterated && ((*head)->getFullName() == exactName))
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800751 {
752 eraseFromSkipList(*head);
753 return;
754 }
755 else
756 {
757 SkipListLayer::iterator it = head;
758
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700759 while ((*it)->getFullName() < exactName)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800760 {
761 head = it;
762 updateTable[layer] = it;
763 isIterated = true;
764
765 ++it;
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400766 if (it == (*rit)->end())
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800767 break;
768 }
769 }
770 }
771
772 if (layer > 0)
773 head = (*head)->getIterators().find(layer - 1)->second; // move HEAD to the lower layer
774
775 layer--;
776 }
777 }
778 else
779 {
780 return;
781 }
782
783 head = updateTable[0];
784 ++head; // look at the next slot to check if it contains the item we want to remove
785
786 bool isCsEmpty = (size() == 0);
787 bool isInBoundaries = (head != (*m_skipList.begin())->end());
788 bool isNameIdentical = false;
789 if (!isCsEmpty && isInBoundaries)
790 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700791 NFD_LOG_TRACE("Identical? " << (*head)->getFullName());
792 isNameIdentical = (*head)->getFullName() == exactName;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800793 }
794
795 if (isNameIdentical)
796 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700797 NFD_LOG_TRACE("Found target " << (*head)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800798 eraseFromSkipList(*head);
799 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800800}
801
802void
803Cs::printSkipList() const
804{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700805 NFD_LOG_TRACE("print()");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800806 //start from the upper layer towards bottom
807 int layer = m_skipList.size() - 1;
808 for (SkipList::const_reverse_iterator rit = m_skipList.rbegin(); rit != m_skipList.rend(); ++rit)
809 {
810 for (SkipListLayer::iterator it = (*rit)->begin(); it != (*rit)->end(); ++it)
811 {
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700812 NFD_LOG_TRACE("Layer " << layer << " " << (*it)->getFullName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800813 }
814 layer--;
815 }
816}
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700817
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800818} //namespace nfd