27 #include "../security/signature-sha256-with-rsa.hpp"
33 const time::milliseconds InMemoryStorage::ZERO_WINDOW(0);
36 Cache::index<byFullName>::type::iterator it)
47 if (m_it != m_cache->get<byFullName>().end()) {
48 m_ptr = &((*m_it)->getData());
80 return m_it == rhs.m_it;
86 return m_it != rhs.m_it;
100 m_scheduler = make_unique<Scheduler>(ioService);
105 InMemoryStorage::init()
110 if (m_limit != std::numeric_limits<size_t>::max() && m_capacity > m_limit) {
111 m_capacity = m_limit;
114 for (
size_t i = 0; i < m_capacity; i++) {
115 m_freeEntries.push(
new InMemoryStorageEntry());
122 Cache::iterator it = m_cache.begin();
123 while (it != m_cache.end()) {
127 BOOST_ASSERT(m_freeEntries.size() == m_capacity);
129 while (!m_freeEntries.empty()) {
130 delete m_freeEntries.top();
138 size_t oldCapacity = m_capacity;
139 m_capacity = capacity;
141 if (
size() > m_capacity) {
142 ssize_t nAllowedFailures =
size() - m_capacity;
143 while (
size() > m_capacity) {
144 if (!
evictItem() && --nAllowedFailures < 0) {
145 BOOST_THROW_EXCEPTION(
Error());
150 if (m_capacity >= oldCapacity) {
151 for (
size_t i = oldCapacity; i < m_capacity; i++) {
156 for (
size_t i = oldCapacity; i > m_capacity; i--) {
157 delete m_freeEntries.top();
162 BOOST_ASSERT(
size() + m_freeEntries.size() == m_capacity);
169 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(data.
getFullName());
170 if (it != m_cache.get<byFullName>().end())
175 if (
isFull() && !doesReachLimit) {
182 if (
isFull() && doesReachLimit) {
187 BOOST_ASSERT(m_freeEntries.size() > 0);
192 entry->setData(data);
193 if (m_scheduler !=
nullptr && mustBeFreshProcessingWindow > ZERO_WINDOW) {
194 auto eventId = make_unique<scheduler::ScopedEventId>(*m_scheduler);
195 *eventId = m_scheduler->scheduleEvent(mustBeFreshProcessingWindow,
197 entry->setMarkStaleEventId(std::move(eventId));
199 m_cache.insert(entry);
205 shared_ptr<const Data>
208 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().lower_bound(name);
211 if (it == m_cache.get<byFullName>().end()) {
212 return shared_ptr<const Data>();
217 return shared_ptr<const Data>();
221 return ((*it)->getData()).shared_from_this();
224 shared_ptr<const Data>
228 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>()
232 if (it != m_cache.get<byFullName>().end()) {
233 return ((*it)->getData()).shared_from_this();
238 it = m_cache.get<byFullName>().lower_bound(interest.
getName());
240 if (it == m_cache.get<byFullName>().end()) {
241 return shared_ptr<const Data>();
245 if (it != m_cache.get<byFullName>().begin())
252 return ret->
getData().shared_from_this();
255 return shared_ptr<const Data>();
259 InMemoryStorage::Cache::index<InMemoryStorage::byFullName>::type::iterator
260 InMemoryStorage::findNextFresh(Cache::index<byFullName>::type::iterator it)
const
262 for (; it != m_cache.get<byFullName>().
end(); it++) {
263 if ((*it)->isFresh())
270 InMemoryStorageEntry*
271 InMemoryStorage::selectChild(
const Interest& interest,
272 Cache::index<byFullName>::type::iterator startingPoint)
const
274 BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end());
276 if (startingPoint != m_cache.get<byFullName>().begin())
278 BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName());
281 bool hasLeftmostSelector = (interest.getChildSelector() <= 0);
282 bool hasRightmostSelector = !hasLeftmostSelector;
285 if (interest.getMustBeFresh())
286 startingPoint = findNextFresh(startingPoint);
288 if (startingPoint == m_cache.get<byFullName>().end()) {
292 if (hasLeftmostSelector)
294 if (interest.matchesData((*startingPoint)->getData()))
296 return *startingPoint;
301 Cache::index<byFullName>::type::iterator rightmost = startingPoint;
302 if (startingPoint != m_cache.get<byFullName>().end())
304 Cache::index<byFullName>::type::iterator rightmostCandidate = startingPoint;
305 Name currentChildPrefix(
"");
309 ++rightmostCandidate;
311 if (interest.getMustBeFresh())
312 rightmostCandidate = findNextFresh(rightmostCandidate);
314 bool isInBoundaries = (rightmostCandidate != m_cache.get<byFullName>().
end());
315 bool isInPrefix =
false;
318 isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName());
323 if (interest.matchesData((*rightmostCandidate)->getData()))
325 if (hasLeftmostSelector)
327 return *rightmostCandidate;
330 if (hasRightmostSelector)
333 const Name& childPrefix = (*rightmostCandidate)->getFullName()
334 .getPrefix(interest.getName().size() + 1);
336 if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix))
338 currentChildPrefix = childPrefix;
339 rightmost = rightmostCandidate;
349 if (rightmost != startingPoint)
354 if (hasRightmostSelector)
356 if (interest.matchesData((*startingPoint)->getData()))
358 return *startingPoint;
365 InMemoryStorage::Cache::iterator
366 InMemoryStorage::freeEntry(Cache::iterator it)
370 m_freeEntries.push(*it);
372 return m_cache.erase(it);
379 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().lower_bound(prefix);
381 while (it != m_cache.get<byFullName>().end() && prefix.
isPrefixOf((*it)->getName())) {
388 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(prefix);
390 if (it == m_cache.get<byFullName>().end())
398 if (m_freeEntries.size() > (2 *
size()))
405 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(name);
407 if (it == m_cache.get<byFullName>().end())
416 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
begin();
424 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
end();
426 const Data* ptr = NULL;
450 const Cache::index<byFullName>::type& cacheIndex = m_cache.get<byFullName>();
451 for (Cache::index<byFullName>::type::iterator it = cacheIndex.begin();
452 it != cacheIndex.end(); it++)
453 os << (*it)->getFullName() << std::endl;
virtual void afterAccess(InMemoryStorageEntry *entry)
Update the entry when the entry is returned by the find() function according to derived class impleme...
const Name & getName() const
Copyright (c) 2013-2016 Regents of the University of California.
const Data * operator->()
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise ...
void markStale()
Disable the data from satisfying interest with MustBeFresh.
InMemoryStorage(size_t limit=std::numeric_limits< size_t >::max())
Create a InMemoryStorage with up to limit entries The InMemoryStorage created through this method wil...
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
Represents a self-defined const_iterator for the in-memory storage.
const Data & getData() const
Returns the Data packet stored in the in-memory storage entry.
represents an Interest packet
bool operator!=(const const_iterator &rhs)
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy...
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
const_iterator & operator++()
boost::multi_index_container< InMemoryStorageEntry *, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< byFullName >, boost::multi_index::const_mem_fun< InMemoryStorageEntry, const Name &,&InMemoryStorageEntry::getFullName >, std::less< Name > > > > Cache
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)
Represents an in-memory storage entry.
Name abstraction to represent an absolute name.
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
virtual ~InMemoryStorage()
bool operator==(const const_iterator &rhs)
virtual void afterInsert(InMemoryStorageEntry *entry)
Update the entry or other data structures after a entry is successfully inserted according to derived...
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
static const time::milliseconds INFINITE_WINDOW
void setCapacity(size_t nMaxPackets)
sets current capacity of in-memory storage (in packets)
const Name & getFullName() const
Get full name of Data packet, including the implicit digest.
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.