34 if (m_it != m_cache->get<byFullName>().end()) {
35 m_ptr = &((*m_it)->getData());
52 m_scheduler = make_unique<Scheduler>(ioCtx);
57 InMemoryStorage::init()
62 if (m_limit != std::numeric_limits<size_t>::max() && m_capacity > m_limit) {
66 for (
size_t i = 0; i < m_capacity; i++) {
67 m_freeEntries.push(
new InMemoryStorageEntry());
74 auto it = m_cache.begin();
75 while (it != m_cache.end()) {
79 BOOST_ASSERT(m_freeEntries.size() == m_capacity);
81 while (!m_freeEntries.empty()) {
82 delete m_freeEntries.top();
90 size_t oldCapacity = m_capacity;
93 if (
size() > m_capacity) {
94 ssize_t nAllowedFailures =
size() - m_capacity;
95 while (
size() > m_capacity) {
96 if (!
evictItem() && --nAllowedFailures < 0) {
102 if (m_capacity >= oldCapacity) {
103 for (
size_t i = oldCapacity; i < m_capacity; i++) {
108 for (
size_t i = oldCapacity; i > m_capacity; i--) {
109 delete m_freeEntries.top();
114 BOOST_ASSERT(
size() + m_freeEntries.size() == m_capacity);
122 if (it != m_cache.get<byFullName>().end())
127 if (
isFull() && !doesReachLimit) {
134 if (
isFull() && doesReachLimit) {
139 BOOST_ASSERT(m_freeEntries.size() > 0);
145 if (m_scheduler !=
nullptr && mustBeFreshProcessingWindow >=
ZERO_WINDOW) {
148 m_cache.insert(entry);
154 shared_ptr<const Data>
157 auto it = m_cache.get<byFullName>().lower_bound(name);
160 if (it == m_cache.get<byFullName>().end()) {
170 return ((*it)->getData()).shared_from_this();
173 shared_ptr<const Data>
177 auto it = m_cache.get<byFullName>().
find(interest.
getName());
180 if (it != m_cache.get<byFullName>().end()) {
181 return ((*it)->getData()).shared_from_this();
186 it = m_cache.get<byFullName>().lower_bound(interest.
getName());
188 if (it == m_cache.get<byFullName>().end()) {
193 if (it != m_cache.get<byFullName>().begin()) {
198 if (ret ==
nullptr) {
204 return ret->
getData().shared_from_this();
207 InMemoryStorage::Cache::index<InMemoryStorage::byFullName>::type::iterator
208 InMemoryStorage::findNextFresh(Cache::index<byFullName>::type::iterator it)
const
210 for (; it != m_cache.get<byFullName>().
end(); it++) {
211 if ((*it)->isFresh())
218 InMemoryStorageEntry*
219 InMemoryStorage::selectChild(
const Interest& interest,
220 Cache::index<byFullName>::type::iterator startingPoint)
const
222 BOOST_ASSERT(startingPoint != m_cache.get<byFullName>().end());
224 if (startingPoint != m_cache.get<byFullName>().begin()) {
225 BOOST_ASSERT((*startingPoint)->getFullName() < interest.getName());
229 if (interest.getMustBeFresh()) {
230 startingPoint = findNextFresh(startingPoint);
233 if (startingPoint == m_cache.get<byFullName>().end()) {
237 if (interest.matchesData((*startingPoint)->getData())) {
238 return *startingPoint;
242 auto rightmostCandidate = startingPoint;
244 ++rightmostCandidate;
246 if (interest.getMustBeFresh()) {
247 rightmostCandidate = findNextFresh(rightmostCandidate);
250 bool isInPrefix =
false;
251 if (rightmostCandidate != m_cache.get<byFullName>().end()) {
252 isInPrefix = interest.getName().isPrefixOf((*rightmostCandidate)->getFullName());
255 if (interest.matchesData((*rightmostCandidate)->getData())) {
256 return *rightmostCandidate;
267 InMemoryStorage::Cache::iterator
268 InMemoryStorage::freeEntry(Cache::iterator it)
272 m_freeEntries.push(*it);
274 return m_cache.erase(it);
281 auto it = m_cache.get<byFullName>().lower_bound(prefix);
282 while (it != m_cache.get<byFullName>().end() && prefix.
isPrefixOf((*it)->getName())) {
289 auto it = m_cache.get<byFullName>().
find(prefix);
290 if (it == m_cache.get<byFullName>().end())
298 if (m_freeEntries.size() > (2 *
size()))
305 auto it = m_cache.get<byFullName>().
find(name);
306 if (it == m_cache.get<byFullName>().end())
315 auto it = m_cache.get<byFullName>().
begin();
322 auto it = m_cache.get<byFullName>().
end();
345 for (
const auto& elem : m_cache.get<byFullName>())
346 os << elem->getFullName() << std::endl;
Represents a Data packet.
const Name & getFullName() const
Get the 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 const_iterator for the in-memory storage.
const_iterator & operator++()
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.
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...
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
Get the Interest name.
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
constexpr time::milliseconds ZERO_WINDOW
constexpr size_t MIN_CAPACITY