28 #include "../security/signature-sha256-with-rsa.hpp"
34 const time::milliseconds InMemoryStorage::ZERO_WINDOW(0);
37 Cache::index<byFullName>::type::iterator it)
48 if (m_it != m_cache->get<byFullName>().end()) {
49 m_ptr = &((*m_it)->getData());
81 return m_it == rhs.m_it;
87 return m_it != rhs.m_it;
101 m_scheduler = make_unique<Scheduler>(ioService);
106 InMemoryStorage::init()
111 if (m_limit != std::numeric_limits<size_t>::max() && m_capacity > m_limit) {
112 m_capacity = m_limit;
115 for (
size_t i = 0; i < m_capacity; i++) {
116 m_freeEntries.push(
new InMemoryStorageEntry());
123 Cache::iterator it = m_cache.begin();
124 while (it != m_cache.end()) {
128 BOOST_ASSERT(m_freeEntries.size() == m_capacity);
130 while (!m_freeEntries.empty()) {
131 delete m_freeEntries.top();
139 size_t oldCapacity = m_capacity;
140 m_capacity = capacity;
142 if (
size() > m_capacity) {
143 ssize_t nAllowedFailures =
size() - m_capacity;
144 while (
size() > m_capacity) {
145 if (!
evictItem() && --nAllowedFailures < 0) {
146 BOOST_THROW_EXCEPTION(
Error());
151 if (m_capacity >= oldCapacity) {
152 for (
size_t i = oldCapacity; i < m_capacity; i++) {
157 for (
size_t i = oldCapacity; i > m_capacity; i--) {
158 delete m_freeEntries.top();
163 BOOST_ASSERT(
size() + m_freeEntries.size() == m_capacity);
170 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(data.
getFullName());
171 if (it != m_cache.get<byFullName>().end())
176 if (
isFull() && !doesReachLimit) {
183 if (
isFull() && doesReachLimit) {
188 BOOST_ASSERT(m_freeEntries.size() > 0);
193 entry->setData(data);
194 if (m_scheduler !=
nullptr && mustBeFreshProcessingWindow > ZERO_WINDOW) {
195 auto eventId = make_unique<scheduler::ScopedEventId>(*m_scheduler);
196 *eventId = m_scheduler->scheduleEvent(mustBeFreshProcessingWindow,
198 entry->setMarkStaleEventId(std::move(eventId));
200 m_cache.insert(entry);
206 shared_ptr<const Data>
209 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().lower_bound(name);
212 if (it == m_cache.get<byFullName>().end()) {
213 return shared_ptr<const Data>();
218 return shared_ptr<const Data>();
222 return ((*it)->getData()).shared_from_this();
225 shared_ptr<const Data>
229 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>()
233 if (it != m_cache.get<byFullName>().end()) {
234 return ((*it)->getData()).shared_from_this();
239 it = m_cache.get<byFullName>().lower_bound(interest.
getName());
241 if (it == m_cache.get<byFullName>().end()) {
242 return shared_ptr<const Data>();
246 if (it != m_cache.get<byFullName>().begin())
253 return ret->
getData().shared_from_this();
256 return shared_ptr<const Data>();
260 InMemoryStorage::Cache::index<InMemoryStorage::byFullName>::type::iterator
261 InMemoryStorage::findNextFresh(Cache::index<byFullName>::type::iterator it)
const
263 for (; it != m_cache.get<byFullName>().
end(); it++) {
264 if ((*it)->isFresh())
271 InMemoryStorageEntry*
272 InMemoryStorage::selectChild(
const Interest& interest,
273 Cache::index<byFullName>::type::iterator startingPoint)
const
275 BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end());
277 if (startingPoint != m_cache.get<byFullName>().begin())
279 BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName());
282 bool hasLeftmostSelector = (interest.getChildSelector() <= 0);
283 bool hasRightmostSelector = !hasLeftmostSelector;
286 if (interest.getMustBeFresh())
287 startingPoint = findNextFresh(startingPoint);
289 if (startingPoint == m_cache.get<byFullName>().end()) {
293 if (hasLeftmostSelector)
295 if (interest.matchesData((*startingPoint)->getData()))
297 return *startingPoint;
302 Cache::index<byFullName>::type::iterator rightmost = startingPoint;
303 if (startingPoint != m_cache.get<byFullName>().end())
305 Cache::index<byFullName>::type::iterator rightmostCandidate = startingPoint;
306 Name currentChildPrefix(
"");
310 ++rightmostCandidate;
312 if (interest.getMustBeFresh())
313 rightmostCandidate = findNextFresh(rightmostCandidate);
315 bool isInBoundaries = (rightmostCandidate != m_cache.get<byFullName>().
end());
316 bool isInPrefix =
false;
319 isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName());
324 if (interest.matchesData((*rightmostCandidate)->getData()))
326 if (hasLeftmostSelector)
328 return *rightmostCandidate;
331 if (hasRightmostSelector)
334 const Name& childPrefix = (*rightmostCandidate)->getFullName()
335 .getPrefix(interest.getName().size() + 1);
337 if (currentChildPrefix.empty() || (childPrefix != currentChildPrefix))
339 currentChildPrefix = childPrefix;
340 rightmost = rightmostCandidate;
350 if (rightmost != startingPoint)
355 if (hasRightmostSelector)
357 if (interest.matchesData((*startingPoint)->getData()))
359 return *startingPoint;
366 InMemoryStorage::Cache::iterator
367 InMemoryStorage::freeEntry(Cache::iterator it)
371 m_freeEntries.push(*it);
373 return m_cache.erase(it);
380 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().lower_bound(prefix);
382 while (it != m_cache.get<byFullName>().end() && prefix.
isPrefixOf((*it)->getName())) {
389 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(prefix);
391 if (it == m_cache.get<byFullName>().end())
399 if (m_freeEntries.size() > (2 *
size()))
406 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(name);
408 if (it == m_cache.get<byFullName>().end())
417 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
begin();
425 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
end();
427 const Data* ptr = NULL;
451 const Cache::index<byFullName>::type& cacheIndex = m_cache.get<byFullName>();
452 for (Cache::index<byFullName>::type::iterator it = cacheIndex.begin();
453 it != cacheIndex.end(); it++)
454 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.