28 const time::milliseconds InMemoryStorage::ZERO_WINDOW(0);
31 Cache::index<byFullName>::type::iterator it)
42 if (m_it != m_cache->get<byFullName>().end()) {
43 m_ptr = &((*m_it)->getData());
75 return m_it == rhs.m_it;
81 return m_it != rhs.m_it;
95 m_scheduler = make_unique<Scheduler>(ioService);
100 InMemoryStorage::init()
105 if (m_limit != std::numeric_limits<size_t>::max() && m_capacity > m_limit) {
106 m_capacity = m_limit;
109 for (
size_t i = 0; i < m_capacity; i++) {
117 Cache::iterator it = m_cache.begin();
118 while (it != m_cache.end()) {
122 BOOST_ASSERT(m_freeEntries.size() == m_capacity);
124 while (!m_freeEntries.empty()) {
125 delete m_freeEntries.top();
133 size_t oldCapacity = m_capacity;
134 m_capacity = capacity;
136 if (
size() > m_capacity) {
137 ssize_t nAllowedFailures =
size() - m_capacity;
138 while (
size() > m_capacity) {
139 if (!
evictItem() && --nAllowedFailures < 0) {
140 BOOST_THROW_EXCEPTION(
Error());
145 if (m_capacity >= oldCapacity) {
146 for (
size_t i = oldCapacity; i < m_capacity; i++) {
151 for (
size_t i = oldCapacity; i > m_capacity; i--) {
152 delete m_freeEntries.top();
157 BOOST_ASSERT(
size() + m_freeEntries.size() == m_capacity);
164 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(data.
getFullName());
165 if (it != m_cache.get<byFullName>().end())
170 if (
isFull() && !doesReachLimit) {
177 if (
isFull() && doesReachLimit) {
182 BOOST_ASSERT(m_freeEntries.size() > 0);
187 entry->setData(data);
188 if (m_scheduler !=
nullptr && mustBeFreshProcessingWindow > ZERO_WINDOW) {
189 auto eventId = make_unique<util::scheduler::ScopedEventId>(*m_scheduler);
190 *eventId = m_scheduler->scheduleEvent(mustBeFreshProcessingWindow,
192 entry->setMarkStaleEventId(std::move(eventId));
194 m_cache.insert(entry);
200 shared_ptr<const Data>
203 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().lower_bound(name);
206 if (it == m_cache.get<byFullName>().end()) {
207 return shared_ptr<const Data>();
212 return shared_ptr<const Data>();
216 return ((*it)->getData()).shared_from_this();
219 shared_ptr<const Data>
223 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>()
227 if (it != m_cache.get<byFullName>().end()) {
228 return ((*it)->getData()).shared_from_this();
233 it = m_cache.get<byFullName>().lower_bound(interest.
getName());
235 if (it == m_cache.get<byFullName>().end()) {
236 return shared_ptr<const Data>();
240 if (it != m_cache.get<byFullName>().begin())
247 return ret->
getData().shared_from_this();
250 return shared_ptr<const Data>();
254 InMemoryStorage::Cache::index<InMemoryStorage::byFullName>::type::iterator
255 InMemoryStorage::findNextFresh(Cache::index<byFullName>::type::iterator it)
const 257 for (; it != m_cache.get<byFullName>().
end(); it++) {
258 if ((*it)->isFresh())
266 InMemoryStorage::selectChild(
const Interest& interest,
267 Cache::index<byFullName>::type::iterator startingPoint)
const 269 BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end());
271 if (startingPoint != m_cache.get<byFullName>().begin())
273 BOOST_ASSERT((*startingPoint)->getFullName() < interest.
getName());
277 bool hasRightmostSelector = !hasLeftmostSelector;
281 startingPoint = findNextFresh(startingPoint);
283 if (startingPoint == m_cache.get<byFullName>().end()) {
287 if (hasLeftmostSelector)
289 if (interest.
matchesData((*startingPoint)->getData()))
291 return *startingPoint;
296 Cache::index<byFullName>::type::iterator rightmost = startingPoint;
297 if (startingPoint != m_cache.get<byFullName>().end())
299 Cache::index<byFullName>::type::iterator rightmostCandidate = startingPoint;
300 Name currentChildPrefix(
"");
304 ++rightmostCandidate;
307 rightmostCandidate = findNextFresh(rightmostCandidate);
309 bool isInBoundaries = (rightmostCandidate != m_cache.get<byFullName>().
end());
310 bool isInPrefix =
false;
313 isInPrefix = interest.
getName().
isPrefixOf((*rightmostCandidate)->getFullName());
318 if (interest.
matchesData((*rightmostCandidate)->getData()))
320 if (hasLeftmostSelector)
322 return *rightmostCandidate;
325 if (hasRightmostSelector)
328 const Name& childPrefix = (*rightmostCandidate)->getFullName()
331 if (currentChildPrefix.
empty() || (childPrefix != currentChildPrefix))
333 currentChildPrefix = childPrefix;
334 rightmost = rightmostCandidate;
344 if (rightmost != startingPoint)
349 if (hasRightmostSelector)
351 if (interest.
matchesData((*startingPoint)->getData()))
353 return *startingPoint;
360 InMemoryStorage::Cache::iterator
361 InMemoryStorage::freeEntry(Cache::iterator it)
365 m_freeEntries.push(*it);
367 return m_cache.erase(it);
374 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().lower_bound(prefix);
376 while (it != m_cache.get<byFullName>().end() && prefix.
isPrefixOf((*it)->getName())) {
383 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(prefix);
385 if (it == m_cache.get<byFullName>().end())
393 if (m_freeEntries.size() > (2 *
size()))
400 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
find(name);
402 if (it == m_cache.get<byFullName>().end())
411 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
begin();
419 Cache::index<byFullName>::type::iterator it = m_cache.get<byFullName>().
end();
443 const Cache::index<byFullName>::type& cacheIndex = m_cache.get<byFullName>();
444 for (Cache::index<byFullName>::type::iterator it = cacheIndex.begin();
445 it != cacheIndex.end(); it++)
446 os << (*it)->getFullName() << std::endl;
const Name & getName() const
Copyright (c) 2013-2017 Regents of the University of California.
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
void setCapacity(size_t nMaxPackets)
sets current capacity of in-memory storage (in packets)
virtual bool evictItem()=0
Removes one Data packet from in-memory storage based on derived class implemented replacement policy...
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise ...
const Data * operator->()
const_iterator & operator++()
virtual ~InMemoryStorage()
void markStale()
Disable the data from satisfying interest with MustBeFresh.
represents an Interest packet
bool operator==(const const_iterator &rhs)
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
bool operator!=(const const_iterator &rhs)
size_t getCapacity() const
returns current capacity of in-memory storage (in packets)
int getChildSelector() const
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
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...
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
int getMustBeFresh() const
virtual void afterAccess(InMemoryStorageEntry *entry)
Update the entry when the entry is returned by the find() function according to derived class impleme...
virtual void afterInsert(InMemoryStorageEntry *entry)
Update the entry or other data structures after a entry is successfully inserted according to derived...
Represents a self-defined const_iterator for the in-memory storage.
size_t size() const
Get number of components.
Represents an absolute name.
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Represents an in-memory storage entry.
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
const Data & getData() const
Returns the Data packet stored in the in-memory storage entry.
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
bool empty() const
Check if name is empty.
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
const Name & getFullName() const
Get full name including implicit digest.
Represents a Data packet.
void eraseImpl(const Name &name)
deletes in-memory storage entries by the Name with implicit digest.
static const time::milliseconds INFINITE_WINDOW