28 #include "../security/signature-sha256-with-rsa.hpp" 34 const time::milliseconds InMemoryStorage::ZERO_WINDOW(0);
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++) {
124 while (it != m_cache.end()) {
128 BOOST_ASSERT(m_freeEntries.size() == m_capacity);
130 while (!m_freeEntries.empty()) {
131 delete m_freeEntries.top();
137 InMemoryStorage::setCapacity(
size_t capacity)
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);
171 if (it != m_cache.get<byFullName>().end())
176 if (
isFull() && !doesReachLimit) {
179 setCapacity(newCapacity);
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>
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>
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>();
263 for (; it != m_cache.get<byFullName>().
end(); it++) {
264 if ((*it)->isFresh())
275 BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end());
277 if (startingPoint != m_cache.get<byFullName>().begin())
279 BOOST_ASSERT((*startingPoint)->getFullName() < interest.
getName());
283 bool hasRightmostSelector = !hasLeftmostSelector;
289 if (startingPoint == m_cache.get<byFullName>().end()) {
293 if (hasLeftmostSelector)
295 if (interest.
matchesData((*startingPoint)->getData()))
297 return *startingPoint;
303 if (startingPoint != m_cache.get<byFullName>().end())
306 Name currentChildPrefix(
"");
310 ++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()
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;
371 m_freeEntries.push(*it);
373 return m_cache.erase(it);
382 while (it != m_cache.get<byFullName>().end() && prefix.
isPrefixOf((*it)->getName())) {
391 if (it == m_cache.get<byFullName>().end())
399 if (m_freeEntries.size() > (2 *
size()))
408 if (it == m_cache.get<byFullName>().end())
427 const Data* ptr = NULL;
451 const Cache::index<byFullName>::type& cacheIndex = m_cache.get<byFullName>();
453 it != cacheIndex.end(); it++)
454 os << (*it)->getFullName() << std::endl;
const Data & getData() const
Returns the Data packet stored in the in-memory storage entry.
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
bool isFull() const
returns true if the in-memory storage uses up the current capacity, false otherwise ...
Copyright (c) 2011-2015 Regents of the University of California.
const Data * operator->()
int getChildSelector() const
int getMustBeFresh() const
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.
represents an Interest packet
bool operator!=(const const_iterator &rhs)
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.
Table::const_iterator iterator
const_iterator & operator++()
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
InMemoryStorage::const_iterator end() const
Returns end iterator of the in-memory storage ordering by name with digest.
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)
Cache::index< byFullName >::type::iterator findNextFresh(Cache::index< byFullName >::type::iterator startingPoint) const
Get the next iterator (include startingPoint) that satisfies MustBeFresh requirement.
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Represents an in-memory storage entry.
Name abstraction to represent an absolute name.
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...
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
const Name & getFullName() const
Get full name of Data packet, including the implicit digest.
size_t size() const
Get the number of components.
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
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 empty() const
Check if name is emtpy.
static const time::milliseconds INFINITE_WINDOW
InMemoryStorageEntry * selectChild(const Interest &interest, Cache::index< byFullName >::type::iterator startingPoint) const
Implements child selector (leftmost, rightmost, undeclared).
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
const Name & getName() const
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.