ndn::InMemoryStorageLfu Class Reference

Provides an in-memory storage with Least Frequently Used (LFU) replacement policy. More...

#include <ndn-cxx/ims/in-memory-storage-lfu.hpp>

+ Inheritance diagram for ndn::InMemoryStorageLfu:
+ Collaboration diagram for ndn::InMemoryStorageLfu:

Public Types

using Cache = 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 > > > >
 

Public Member Functions

 InMemoryStorageLfu (boost::asio::io_context &ioCtx, size_t limit=16)
 
 InMemoryStorageLfu (size_t limit=16)
 
InMemoryStorage::const_iterator begin () const
 Returns begin iterator of the in-memory storage ordering by name with digest. More...
 
InMemoryStorage::const_iterator end () const
 Returns end iterator of the in-memory storage ordering by name with digest. More...
 
void erase (const Name &prefix, const bool isPrefix=true)
 Deletes in-memory storage entry by prefix by default. More...
 
shared_ptr< const Datafind (const Interest &interest)
 Finds the best match Data for an Interest. More...
 
shared_ptr< const Datafind (const Name &name)
 Finds the best match Data for a Name with or without implicit digest. More...
 
size_t getLimit () const
 
void insert (const Data &data, const time::milliseconds &mustBeFreshProcessingWindow=INFINITE_WINDOW)
 Inserts a Data packet. More...
 
size_t size () const
 

Static Public Attributes

static constexpr time::milliseconds INFINITE_WINDOW = -1_ms
 

Protected Member Functions

void afterAccess (InMemoryStorageEntry *entry) override
 Update the entry when the entry is returned by the find() function, increment the frequency according to LFU. More...
 
void afterInsert (InMemoryStorageEntry *entry) override
 Update the entry after a entry is successfully inserted, add it to the cleanupIndex. More...
 
void beforeErase (InMemoryStorageEntry *entry) override
 Update the entry or other data structures before a entry is successfully erased, erase it from the cleanupIndex. More...
 
void eraseImpl (const Name &name)
 Deletes in-memory storage entries by the Name with implicit digest. More...
 
bool evictItem () override
 Removes one Data packet from in-memory storage based on LFU, i.e. More...
 
size_t getCapacity () const
 Returns current capacity of in-memory storage (in packets). More...
 
bool isFull () const
 Returns true if the in-memory storage uses up the current capacity, false otherwise. More...
 
void printCache (std::ostream &os) const
 Prints contents of the in-memory storage. More...
 
void setCapacity (size_t nMaxPackets)
 Sets current capacity of in-memory storage (in packets). More...
 

Detailed Description

Provides an in-memory storage with Least Frequently Used (LFU) replacement policy.

Note
The frequency right now is usage count.

Definition at line 39 of file in-memory-storage-lfu.hpp.

Member Typedef Documentation

◆ Cache

using ndn::InMemoryStorage::Cache = 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> > > >
inherited

Definition at line 47 of file in-memory-storage.hpp.

Constructor & Destructor Documentation

◆ InMemoryStorageLfu() [1/2]

ndn::InMemoryStorageLfu::InMemoryStorageLfu ( size_t  limit = 16)
explicit

Definition at line 26 of file in-memory-storage-lfu.cpp.

◆ InMemoryStorageLfu() [2/2]

ndn::InMemoryStorageLfu::InMemoryStorageLfu ( boost::asio::io_context &  ioCtx,
size_t  limit = 16 
)
explicit

Definition at line 31 of file in-memory-storage-lfu.cpp.

Member Function Documentation

◆ afterAccess()

void ndn::InMemoryStorageLfu::afterAccess ( InMemoryStorageEntry entry)
overrideprotectedvirtual

Update the entry when the entry is returned by the find() function, increment the frequency according to LFU.

Reimplemented from ndn::InMemoryStorage.

Definition at line 68 of file in-memory-storage-lfu.cpp.

◆ afterInsert()

void ndn::InMemoryStorageLfu::afterInsert ( InMemoryStorageEntry entry)
overrideprotectedvirtual

Update the entry after a entry is successfully inserted, add it to the cleanupIndex.

Reimplemented from ndn::InMemoryStorage.

Definition at line 37 of file in-memory-storage-lfu.cpp.

◆ beforeErase()

void ndn::InMemoryStorageLfu::beforeErase ( InMemoryStorageEntry entry)
overrideprotectedvirtual

Update the entry or other data structures before a entry is successfully erased, erase it from the cleanupIndex.

Reimplemented from ndn::InMemoryStorage.

Definition at line 60 of file in-memory-storage-lfu.cpp.

◆ begin()

InMemoryStorage::const_iterator ndn::InMemoryStorage::begin ( ) const
inherited

Returns begin iterator of the in-memory storage ordering by name with digest.

Returns
const_iterator pointing to the beginning of the m_cache

Definition at line 313 of file in-memory-storage.cpp.

◆ end()

InMemoryStorage::const_iterator ndn::InMemoryStorage::end ( ) const
inherited

Returns end iterator of the in-memory storage ordering by name with digest.

Returns
const_iterator pointing to the end of the m_cache

Definition at line 320 of file in-memory-storage.cpp.

◆ erase()

void ndn::InMemoryStorage::erase ( const Name prefix,
const bool  isPrefix = true 
)
inherited

Deletes in-memory storage entry by prefix by default.

Parameters
prefixExact name of a prefix of the data to remove
isPrefixIf false, the function will only delete the entry completely matched with the prefix according to canonical ordering. For this case, user should substitute the prefix with full name.
Warning
Please do not use this function directly in any derived class to erase an entry from the cache, use eraseImpl() instead.
Note
It will invoke beforeErase(shared_ptr<InMemoryStorageEntry>).

Definition at line 278 of file in-memory-storage.cpp.

◆ eraseImpl()

void ndn::InMemoryStorage::eraseImpl ( const Name name)
protectedinherited

Deletes in-memory storage entries by the Name with implicit digest.

This is the function one should use to erase entry in the cache in derived class. It won't invoke beforeErase(shared_ptr<Entry>).

Definition at line 303 of file in-memory-storage.cpp.

◆ evictItem()

bool ndn::InMemoryStorageLfu::evictItem ( )
overrideprotectedvirtual

Removes one Data packet from in-memory storage based on LFU, i.e.

evict the least frequently accessed Data packet

Returns
{ whether the Data was removed }

Implements ndn::InMemoryStorage.

Definition at line 47 of file in-memory-storage-lfu.cpp.

◆ find() [1/2]

shared_ptr< const Data > ndn::InMemoryStorage::find ( const Interest interest)
inherited

Finds the best match Data for an Interest.

Note
It will invoke afterAccess(shared_ptr<InMemoryStorageEntry>). As currently it is impossible to determine whether a Name contains implicit digest or not, therefore this find function is not able to locate a packet according to an interest( including implicit digest) whose name is not the full name of the data matching the implicit digest.
Returns
the best match, if any; otherwise a null shared_ptr

Definition at line 174 of file in-memory-storage.cpp.

◆ find() [2/2]

shared_ptr< const Data > ndn::InMemoryStorage::find ( const Name name)
inherited

Finds the best match Data for a Name with or without implicit digest.

If packets with the same name but different digests exist and the Name supplied is the one without implicit digest, a packet will be arbitrarily chosen to return.

Note
It will invoke afterAccess(shared_ptr<InMemoryStorageEntry>).
Returns
the one matched the Name; otherwise a null shared_ptr

Definition at line 155 of file in-memory-storage.cpp.

◆ getCapacity()

size_t ndn::InMemoryStorage::getCapacity ( ) const
inlineprotectedinherited

Returns current capacity of in-memory storage (in packets).

Returns
number of packets that can be stored in application cache

Definition at line 261 of file in-memory-storage.hpp.

◆ getLimit()

size_t ndn::InMemoryStorage::getLimit ( ) const
inlineinherited
Returns
Maximum number of packets that can be allowed to store in in-memory storage.

Definition at line 193 of file in-memory-storage.hpp.

◆ insert()

void ndn::InMemoryStorage::insert ( const Data data,
const time::milliseconds mustBeFreshProcessingWindow = INFINITE_WINDOW 
)
inherited

Inserts a Data packet.

Parameters
datathe packet to insert, must be signed and have wire encoding
mustBeFreshProcessingWindowBeyond this time period, the inserted data can only be used to answer interest without MustBeFresh. The value of mustBeFreshProcessingWindow is an application decision and it may or may not correspond to FreshnessPeriod.
Note
InMemoryStorage does not use the inserted data packet's FreshnessPeriod value. If the packet needs to be marked "stale" after application-defined period of time, the application must supply proper mustBeFreshProcessingWindow value.
Packets are considered duplicate if the name with implicit digest matches. The new Data packet with the identical name, but a different payload will be placed in the in-memory storage.
It will invoke afterInsert(shared_ptr<InMemoryStorageEntry>).

Definition at line 118 of file in-memory-storage.cpp.

◆ isFull()

bool ndn::InMemoryStorage::isFull ( ) const
inlineprotectedinherited

Returns true if the in-memory storage uses up the current capacity, false otherwise.

Definition at line 269 of file in-memory-storage.hpp.

◆ printCache()

void ndn::InMemoryStorage::printCache ( std::ostream &  os) const
protectedinherited

Prints contents of the in-memory storage.

Definition at line 342 of file in-memory-storage.cpp.

◆ setCapacity()

void ndn::InMemoryStorage::setCapacity ( size_t  nMaxPackets)
protectedinherited

Sets current capacity of in-memory storage (in packets).

Definition at line 88 of file in-memory-storage.cpp.

◆ size()

size_t ndn::InMemoryStorage::size ( ) const
inlineinherited
Returns
Number of packets stored in in-memory storage.

Definition at line 201 of file in-memory-storage.hpp.

Member Data Documentation

◆ INFINITE_WINDOW

constexpr time::milliseconds ndn::InMemoryStorage::INFINITE_WINDOW = -1_ms
staticconstexprinherited

Definition at line 328 of file in-memory-storage.hpp.