ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
ndn::ContentStore Class Reference

Content Store. More...

#include <ndn/cs.hpp>

Classes

struct  Stats
 CS statistics. More...
 

Public Member Functions

 ContentStore ()=default
 Default constructor.
 
 ~ContentStore ()
 Destructor.
 
 ContentStore (const ContentStore &)=delete
 
ContentStoreoperator= (const ContentStore &)=delete
 
 ContentStore (ContentStore &&)=delete
 
ContentStoreoperator= (ContentStore &&)=delete
 
Error init (size_t maxEntries=CS_DEFAULT_ENTRIES)
 Initialize the Content Store.
 
Data management
Error insert (const Data &data, TimeMs now)
 Insert Data into the cache.
 
const CsEntryfind (const Name &name, bool mustBeFresh=false, TimeMs now=0) const
 Search the cache by Name.
 
void remove (const Name &name)
 Remove the entry with the specified Name.
 
void evictStale (TimeMs now)
 Remove stale entries.
 
Statistics
size_t size () const
 Get the current number of entries.
 
size_t capacity () const
 Get the maximum number of entries.
 
const Statsstats () const
 Get statistics.
 

Detailed Description

Content Store.

One of the core components of the forwarder. Caches received Data and can respond to subsequent Interests without forwarding to the network.

Uses an LRU (Least Recently Used) replacement policy.

// Cache Data
Data data;
data.setName("/sensor/temperature");
data.setContent("25.5 C");
data.setFreshnessPeriod(10000); // Valid for 10 seconds
cs.insert(data, currentTimeMs());
// Search cache for Data
if (auto* entry = cs.find(interestName, mustBeFresh, now)) {
// Cache hit
sendData(entry->data());
}
Content Store.
Definition cs.hpp:92
Error insert(const Data &data, TimeMs now)
Insert Data into the cache.
Definition cs.cpp:61
const CsEntry * find(const Name &name, bool mustBeFresh=false, TimeMs now=0) const
Search the cache by Name.
Definition cs.cpp:115
NDN Data packet.
Definition data.hpp:49
Data & setFreshnessPeriod(uint32_t periodMs)
Set the FreshnessPeriod.
Definition data.cpp:39
Error setContent(const uint8_t *data, size_t size)
Set binary data as content.
Definition data.cpp:26
Data & setName(const Name &name)
Set the Name (supports method chaining)
Definition data.cpp:12
TimeMs currentTimeMs()
Get current time (milliseconds)
Definition common.cpp:7

Definition at line 92 of file cs.hpp.

Constructor & Destructor Documentation

◆ ~ContentStore()

ndn::ContentStore::~ContentStore ( )

Destructor.

Frees memory allocated on PSRAM.

Definition at line 26 of file cs.cpp.

Member Function Documentation

◆ capacity()

size_t ndn::ContentStore::capacity ( ) const
inline

Get the maximum number of entries.

Returns
Maximum number of entries

Definition at line 181 of file cs.hpp.

◆ evictStale()

void ndn::ContentStore::evictStale ( TimeMs  now)

Remove stale entries.

Removes all entries that have exceeded their staleTime.

Parameters
nowCurrent time (milliseconds)

Definition at line 144 of file cs.cpp.

References ndn::ContentStore::Stats::evictions.

Referenced by ndn::Forwarder::processEvents().

◆ find()

const CsEntry * ndn::ContentStore::find ( const Name name,
bool  mustBeFresh = false,
TimeMs  now = 0 
) const

Search the cache by Name.

Parameters
nameName to search for
mustBeFreshIf true, only return fresh Data
nowCurrent time (used when mustBeFresh=true)
Returns
Pointer to the entry if found, nullptr otherwise

Definition at line 115 of file cs.cpp.

◆ init()

Error ndn::ContentStore::init ( size_t  maxEntries = CS_DEFAULT_ENTRIES)

Initialize the Content Store.

Allocates the entry array on PSRAM. This function must be called exactly once.

Parameters
maxEntriesMaximum number of entries (default: CS_DEFAULT_ENTRIES)
Returns
Error::Success on success, Error::NoMemory on PSRAM allocation failure, Error::AlreadyExists if already initialized

Definition at line 33 of file cs.cpp.

Referenced by ndn::Forwarder::init().

◆ insert()

Error ndn::ContentStore::insert ( const Data data,
TimeMs  now 
)

Insert Data into the cache.

Overwrites if an entry with the same Name exists. If the cache is full, the oldest entry is evicted using the LRU policy.

Parameters
dataData to cache
nowCurrent time (milliseconds)
Returns
Error::Success on success

Definition at line 61 of file cs.cpp.

References ndn::ContentStore::Stats::evictions, ndn::Data::freshnessPeriod(), ndn::ContentStore::Stats::insertions, and ndn::Data::name().

Referenced by ndn::Forwarder::putData().

◆ remove()

void ndn::ContentStore::remove ( const Name name)

Remove the entry with the specified Name.

Parameters
nameName of the entry to remove

Definition at line 133 of file cs.cpp.

◆ size()

size_t ndn::ContentStore::size ( ) const
inline

Get the current number of entries.

Returns
Number of entries

Definition at line 175 of file cs.hpp.

◆ stats()

const Stats & ndn::ContentStore::stats ( ) const
inline

Get statistics.

Returns
Const reference to statistics

Definition at line 197 of file cs.hpp.


The documentation for this class was generated from the following files: