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()
103 m_capacity = m_initCapacity;
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++) {
110 m_freeEntries.push(
new InMemoryStorageEntry());
117 auto 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 = std::max(capacity, m_initCapacity);
136 if (
size() > m_capacity) {
137 ssize_t nAllowedFailures =
size() - m_capacity;
138 while (
size() > m_capacity) {
139 if (!
evictItem() && --nAllowedFailures < 0) {
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);
165 if (it != m_cache.get<byFullName>().end())
170 if (
isFull() && !doesReachLimit) {
177 if (
isFull() && doesReachLimit) {
182 BOOST_ASSERT(m_freeEntries.size() > 0);
188 if (m_scheduler !=
nullptr && mustBeFreshProcessingWindow > ZERO_WINDOW) {
191 m_cache.insert(entry);
197 shared_ptr<const Data>
200 auto it = m_cache.get<byFullName>().lower_bound(name);
203 if (it == m_cache.get<byFullName>().end()) {
213 return ((*it)->getData()).shared_from_this();
216 shared_ptr<const Data>
220 auto it = m_cache.get<byFullName>().
find(interest.
getName());
223 if (it != m_cache.get<byFullName>().end()) {
224 return ((*it)->getData()).shared_from_this();
229 it = m_cache.get<byFullName>().lower_bound(interest.
getName());
231 if (it == m_cache.get<byFullName>().end()) {
236 if (it != m_cache.get<byFullName>().begin()) {
241 if (ret ==
nullptr) {
247 return ret->
getData().shared_from_this();
250 InMemoryStorage::Cache::index<InMemoryStorage::byFullName>::type::iterator
251 InMemoryStorage::findNextFresh(Cache::index<byFullName>::type::iterator it)
const
253 for (; it != m_cache.get<byFullName>().
end(); it++) {
254 if ((*it)->isFresh())
261 InMemoryStorageEntry*
262 InMemoryStorage::selectChild(
const Interest& interest,
263 Cache::index<byFullName>::type::iterator startingPoint)
const
265 BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end());
267 if (startingPoint != m_cache.get<byFullName>().begin()) {
268 BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName());
272 if (interest.getMustBeFresh()) {
273 startingPoint = findNextFresh(startingPoint);
276 if (startingPoint == m_cache.get<byFullName>().end()) {
280 if (interest.matchesData((*startingPoint)->getData())) {
281 return *startingPoint;
285 auto rightmostCandidate = startingPoint;
287 ++rightmostCandidate;
289 if (interest.getMustBeFresh()) {
290 rightmostCandidate = findNextFresh(rightmostCandidate);
293 bool isInPrefix =
false;
294 if (rightmostCandidate != m_cache.get<byFullName>().end()) {
295 isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName());
298 if (interest.matchesData((*rightmostCandidate)->getData())) {
299 return *rightmostCandidate;
310 InMemoryStorage::Cache::iterator
311 InMemoryStorage::freeEntry(Cache::iterator it)
315 m_freeEntries.push(*it);
317 return m_cache.erase(it);
324 auto it = m_cache.get<byFullName>().lower_bound(prefix);
325 while (it != m_cache.get<byFullName>().end() && prefix.
isPrefixOf((*it)->getName())) {
332 auto it = m_cache.get<byFullName>().
find(prefix);
333 if (it == m_cache.get<byFullName>().end())
341 if (m_freeEntries.size() > (2 *
size()))
348 auto it = m_cache.get<byFullName>().
find(name);
349 if (it == m_cache.get<byFullName>().end())
358 auto it = m_cache.get<byFullName>().
begin();
365 auto it = m_cache.get<byFullName>().
end();
388 for (
const auto& elem : m_cache.get<byFullName>())
389 os << elem->getFullName() << std::endl;
Represents a Data packet.
const Name & getFullName() const
Get full name including implicit digest.
Represents an in-memory storage entry.
void scheduleMarkStale(Scheduler &sched, time::nanoseconds after)
Schedule an event to mark this entry as non-fresh.
const Data & getData() const
Returns the Data packet stored in the in-memory storage entry.
void setData(const Data &data)
Changes the content of in-memory storage entry.
Represents an error might be thrown during reduce the current capacity of the in-memory storage throu...
Represents a self-defined const_iterator for the in-memory storage.
const_iterator & operator++()
bool operator!=(const const_iterator &rhs)
const_iterator(const Data *ptr, const Cache *cache, Cache::index< byFullName >::type::iterator it)
bool operator==(const const_iterator &rhs)
InMemoryStorage(size_t limit=std::numeric_limits< size_t >::max())
Create a InMemoryStorage with up to limit entries.
void erase(const Name &prefix, const bool isPrefix=true)
Deletes in-memory storage entry by prefix by default.
virtual void afterAccess(InMemoryStorageEntry *entry)
Update the entry when the entry is returned by the find() function according to derived class impleme...
void insert(const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
Inserts a Data packet.
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
void printCache(std::ostream &os) const
Prints contents of the in-memory storage.
virtual void beforeErase(InMemoryStorageEntry *entry)
Update the entry or other data structures before a entry is successfully erased according to derived ...
shared_ptr< const Data > find(const Interest &interest)
Finds the best match Data for an Interest.
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.
size_t getCapacity() const
Returns current capacity of in-memory storage (in packets).
InMemoryStorage::const_iterator begin() const
Returns begin iterator of the in-memory storage ordering by name with digest.
void eraseImpl(const Name &name)
Deletes in-memory storage entries by the Name with implicit digest.
virtual void afterInsert(InMemoryStorageEntry *entry)
Update the entry or other data structures after a entry is successfully inserted according to derived...
static const time::milliseconds INFINITE_WINDOW
void setCapacity(size_t nMaxPackets)
Sets current capacity of in-memory storage (in packets).
virtual ~InMemoryStorage()
Represents an Interest packet.
const Name & getName() const noexcept
Represents an absolute name.
bool isPrefixOf(const Name &other) const noexcept
Check if this name is a prefix of another name.
boost::chrono::milliseconds milliseconds