Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
RingFileCache< T > Class Template Reference

Persistent ring buffer cache with file-backed storage. More...

#include <ringfilecache.H>

Collaboration diagram for RingFileCache< T >:
[legend]

Classes

class  Iterator
 
struct  Pars
 
class  Pointer
 Defines a pointer to a specific location in the cache. More...
 

Public Member Functions

std::string to_string () const
 Human-readable dump of the cache internal state (debugging)
 
T read (const Pointer &ptr)
 Read the entry pointed to by ptr
 
void write (const Pointer &ptr, const T &item)
 Write item at an absolute position designated by ptr.
 
bool is_initialized () const
 True if the cache has been initialized with a valid parameters file.
 
size_t size () const noexcept
 Returns the number of entries stored in the cache.
 
size_t capacity () const noexcept
 Returns the maximum capacity.
 
size_t avail () const noexcept
 Returns the number of available entries.
 
size_t head_pos () const noexcept
 Returns the current head position.
 
size_t tail_pos () const noexcept
 return the current tail position
 
bool is_empty () const noexcept
 Returns true if the cache is empty.
 
bool is_full () const noexcept
 Returns true if the cache is full (no more entries can be inserted)
 
 RingFileCache (const std::string &pars_fname)
 Initialize a cache previously built.
 
 RingFileCache ()=default
 Default constructor.
 
void init (const std::string &pars_fname)
 Initialize a cache constructed with the default constructor.
 
bool put (const T &item)
 Insert an item into the cache.
 
bool read (T entries[], const size_t m=1)
 Read the m the oldest entries and load them in a contiguous array.
 
T read_first ()
 Read the oldest entry in the cache.
 
T read_last ()
 Read the youngest (most recently inserted) entry in the cache.
 
T youngest ()
 Alias for read_last().
 
T oldest ()
 Alias for read_first().
 
T oldest (size_t i)
 Read the i-th oldest element without removing it.
 
Array< Tread_all ()
 Read all entries in insertion order.
 
Array< Tread_from (const size_t pos, const size_t m)
 Read up to m entries starting from the pos-th oldest position.
 
Array< Tread_from (const Pointer &ptr, const size_t m)
 Read up to m entries starting from the position designated by ptr.
 
bool get (const size_t m=1) noexcept
 Extracts (deletes) from the cache the m oldest inserted items.
 
void empty () noexcept
 Empties the cache; all entries are logically deleted.
 
void flush ()
 Flushes the current cache state to disk.
 
void close ()
 Flushes state and closes the underlying streams.
 
 ~RingFileCache ()
 
void resize (const size_t sz)
 Resize the maximum capacity of the cache.
 
Iterator get_it ()
 Returns a forward iterator starting at the oldest entry.
 

Static Public Member Functions

static void create (const std::string &pars_file_name, const std::string &cache_file_name, const size_t num_entries)
 Create a brand new on-disk ring cache.
 
static bool test (const std::string &pars_fname)
 Tests if pars and cache files have been created.
 

Private Member Functions

void test_and_set_tail_pointer ()
 Position the writing pointer at the current tail.
 
bool is_valid_offset (const size_t offset) const noexcept
 True if offset is a valid logical offset from the head (0 <= offset < n)
 
bool is_valid_position (const size_t pos) const noexcept
 True if pos is a valid absolute position within the stored entries.
 
void test_and_set_head_pointer (const size_t num_entries=0)
 Position the read pointer at head + num_entries (with wraparound)
 
T read_entry ()
 Load an entry from the current stream position. WARNING: stream pointer not set here.
 
T read_entry (const size_t pos)
 Read the entry at logical position pos from head (with bounds check)
 
void write_entry (const T &item)
 Write an entry at the current stream position.
 
void validate_absolute_position (const size_t pos) const
 Throw out_of_range if pos >= dim.
 
T read_absolute (const size_t pos)
 Read entry at absolute file position (with bounds check)
 
void write_absolute (const size_t pos, const T &item)
 Write entry at absolute file position (with bounds check)
 
void read_pars (const std::string &pars_file_name)
 

Static Private Member Functions

static std::string state (std::fstream &ss)
 

Private Attributes

bool initialized = false
 
std::string pars_file_name
 
std::string cache_file_name
 
std::fstream pars_stream
 
std::fstream cache_stream
 
size_t dim = 0
 
size_t n = 0
 
size_t head = 0
 
size_t tail = 0
 

Friends

std::ostream & operator<< (std::ostream &out, const RingFileCache &cache)
 

Detailed Description

template<typename T>
class RingFileCache< T >

Persistent ring buffer cache with file-backed storage.

Implements a circular buffer that stores elements on disk, providing persistent storage with bounded memory usage. The cache uses two files:

  • Data file: Binary storage of cache entries
  • Parameter file: Metadata (capacity, size, head, tail pointers)

Ring Buffer Semantics

  • Capacity: Fixed at construction
  • Insert: Writes to tail, advances tail pointer (wraps around)
  • Get: Reads from head, advances head pointer (wraps around)
  • Full: When tail catches up to head, oldest entry is overwritten

Persistence

The cache can be closed and reopened, preserving all data and state. Use save_pars() to ensure metadata is synchronized to disk.

Template Parameters
TElement type (must be trivially copyable and default constructible)
Complexity
  • Insert: O(1) + disk I/O
  • Get: O(1) + disk I/O
  • Space: O(capacity) on disk, O(1) in RAM
Thread Safety
Not thread-safe. Use external synchronization for concurrent access.

Definition at line 130 of file ringfilecache.H.

Constructor & Destructor Documentation

◆ RingFileCache() [1/2]

template<typename T >
RingFileCache< T >::RingFileCache ( const std::string &  pars_fname)
inline

Initialize a cache previously built.

The internal state of the cache is represented by a file specified with the parameters file called pars_file_name and passed during create() static method. The name of the cache is also passed to create(), but this one is stored in the parameters file.

So, in order to instantiate a cache, it is enough to pass the parameters file name.

Parameters
[in]pars_fnamename of a cache parameters file
Exceptions
domain_errorif any file cannot be opened
std::ios_base::failureif there is a failure in any file.

Definition at line 593 of file ringfilecache.H.

References RingFileCache< T >::pars_file_name, and RingFileCache< T >::read_pars().

◆ RingFileCache() [2/2]

template<typename T >
RingFileCache< T >::RingFileCache ( )
default

Default constructor.

Provided to allow deferred initialization when file creation must be handled elsewhere.

Warning
The constructed cache is in an invalid state
See also
init()

◆ ~RingFileCache()

template<typename T >
RingFileCache< T >::~RingFileCache ( )
inline

Definition at line 907 of file ringfilecache.H.

References RingFileCache< T >::close().

Member Function Documentation

◆ avail()

template<typename T >
size_t RingFileCache< T >::avail ( ) const
inlinenoexcept

Returns the number of available entries.

Definition at line 565 of file ringfilecache.H.

References RingFileCache< T >::dim, and RingFileCache< T >::n.

◆ capacity()

template<typename T >
size_t RingFileCache< T >::capacity ( ) const
inlinenoexcept

Returns the maximum capacity.

Definition at line 562 of file ringfilecache.H.

References RingFileCache< T >::dim.

Referenced by RingFileCache< T >::to_string().

◆ close()

template<typename T >
void RingFileCache< T >::close ( )
inline

Flushes state and closes the underlying streams.

Safe to call multiple times; subsequent calls after the first are no-ops.

Definition at line 897 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, RingFileCache< T >::flush(), RingFileCache< T >::initialized, Aleph::maps(), and RingFileCache< T >::pars_stream.

Referenced by RingFileCache< T >::~RingFileCache().

◆ create()

template<typename T >
static void RingFileCache< T >::create ( const std::string &  pars_file_name,
const std::string &  cache_file_name,
const size_t  num_entries 
)
inlinestatic

Create a brand new on-disk ring cache.

This must be called once to create and initialize the parameter and data files before any RingFileCache instance can be constructed. Two files are created:

  • pars_file_name: stores the cache metadata (dimension, head/tail, number of items) and the cache file name.
  • cache_file_name: stores the raw cached entries (preallocated to num_entries items).

The internal state saved in pars_file_name includes the name of the cache file so that a subsequent constructor only needs the parameters file path.

Parameters
[in]pars_file_namePath of the parameters file to create.
[in]cache_file_namePath of the data file to create.
[in]num_entriesCapacity (number of entries) of the cache.
Exceptions
std::domain_errorif any file cannot be opened/written.
std::bad_allocif memory allocation fails.

Definition at line 303 of file ringfilecache.H.

References ah_bad_alloc_if, ah_domain_error_if, RingFileCache< T >::cache_file_name, RingFileCache< T >::cache_stream, Aleph::DynList< T >::get(), Aleph::HTList::head, Aleph::init, Aleph::maps(), RingFileCache< T >::pars_file_name, RingFileCache< T >::pars_stream, Ring_Max_Name_Size, Aleph::HTList::size(), and Aleph::HTList::tail.

◆ empty()

template<typename T >
void RingFileCache< T >::empty ( )
inlinenoexcept

Empties the cache; all entries are logically deleted.

The underlying file storage is not modified; only the head/tail pointers are reset.

Definition at line 868 of file ringfilecache.H.

References RingFileCache< T >::get(), and RingFileCache< T >::n.

◆ flush()

template<typename T >
void RingFileCache< T >::flush ( )
inline

Flushes the current cache state to disk.

Use this method wisely: frequent calls can be expensive, but the persistence guarantees of this class rely on writing both the parameters and data streams.

Definition at line 876 of file ringfilecache.H.

References RingFileCache< T >::cache_file_name, RingFileCache< T >::cache_stream, RingFileCache< T >::Pars::dim, RingFileCache< T >::dim, RingFileCache< T >::Pars::head, RingFileCache< T >::head, Aleph::maps(), RingFileCache< T >::Pars::n, RingFileCache< T >::n, RingFileCache< T >::pars_stream, RingFileCache< T >::Pars::size_cache_file, RingFileCache< T >::Pars::tail, and RingFileCache< T >::tail.

Referenced by RingFileCache< T >::close(), and RingFileCache< T >::resize().

◆ get()

template<typename T >
bool RingFileCache< T >::get ( const size_t  m = 1)
inlinenoexcept

Extracts (deletes) from the cache the m oldest inserted items.

Parameters
[in]mnumber of items to be extracted
Returns
true if the m items were extracted

Definition at line 852 of file ringfilecache.H.

References RingFileCache< T >::dim, RingFileCache< T >::head, and RingFileCache< T >::n.

Referenced by RingFileCache< T >::empty().

◆ get_it()

template<typename T >
Iterator RingFileCache< T >::get_it ( )
inline

Returns a forward iterator starting at the oldest entry.

Returns
an Iterator positioned at the head of the cache

Definition at line 1057 of file ringfilecache.H.

◆ head_pos()

template<typename T >
size_t RingFileCache< T >::head_pos ( ) const
inlinenoexcept

Returns the current head position.

Definition at line 568 of file ringfilecache.H.

References RingFileCache< T >::head.

Referenced by RingFileCache< T >::to_string().

◆ init()

template<typename T >
void RingFileCache< T >::init ( const std::string &  pars_fname)
inline

Initialize a cache constructed with the default constructor.

Warning
Behavior is undefined if a valid cache has already been constructed.
Parameters
[in]pars_fnamename of cache parameters file
Exceptions
domain_errorif a pars file is already opened
std::ios_base::failureif there is a failure in any file.

Definition at line 646 of file ringfilecache.H.

References ah_domain_error_if, Aleph::maps(), RingFileCache< T >::pars_file_name, RingFileCache< T >::pars_stream, and RingFileCache< T >::read_pars().

◆ is_empty()

template<typename T >
bool RingFileCache< T >::is_empty ( ) const
inlinenoexcept

Returns true if the cache is empty.

Definition at line 574 of file ringfilecache.H.

References RingFileCache< T >::n.

◆ is_full()

template<typename T >
bool RingFileCache< T >::is_full ( ) const
inlinenoexcept

Returns true if the cache is full (no more entries can be inserted)

Definition at line 577 of file ringfilecache.H.

References RingFileCache< T >::dim, and RingFileCache< T >::n.

◆ is_initialized()

template<typename T >
bool RingFileCache< T >::is_initialized ( ) const
inline

True if the cache has been initialized with a valid parameters file.

Definition at line 556 of file ringfilecache.H.

References RingFileCache< T >::initialized.

◆ is_valid_offset()

template<typename T >
bool RingFileCache< T >::is_valid_offset ( const size_t  offset) const
inlineprivatenoexcept

True if offset is a valid logical offset from the head (0 <= offset < n)

Definition at line 182 of file ringfilecache.H.

References RingFileCache< T >::n, and offset.

◆ is_valid_position()

template<typename T >
bool RingFileCache< T >::is_valid_position ( const size_t  pos) const
inlineprivatenoexcept

True if pos is a valid absolute position within the stored entries.

Definition at line 188 of file ringfilecache.H.

References RingFileCache< T >::head, Aleph::maps(), and RingFileCache< T >::tail.

◆ oldest() [1/2]

template<typename T >
T RingFileCache< T >::oldest ( )
inline

Alias for read_first().

Returns
the oldest (first inserted) entry
Exceptions
underflow_errorif the cache is empty

Definition at line 784 of file ringfilecache.H.

References RingFileCache< T >::read_first().

◆ oldest() [2/2]

template<typename T >
T RingFileCache< T >::oldest ( size_t  i)
inline

Read the i-th oldest element without removing it.

Parameters
[in]izero-based index from the oldest entry
Returns
the entry at position i from the head
Exceptions
overflow_errorif i >= size()

Definition at line 792 of file ringfilecache.H.

References ah_overflow_error_if, RingFileCache< T >::n, and RingFileCache< T >::read_entry().

◆ put()

template<typename T >
bool RingFileCache< T >::put ( const T item)
inline

Insert an item into the cache.

The cache is FIFO: each new entry is appended at the tail.

Parameters
[in]itemto be inserted into the queue
Returns
true if the 'item' was correctly inserted; false otherwise
Exceptions
std::ios_base::failureif there is a failure in any file.

Definition at line 665 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, RingFileCache< T >::dim, Aleph::maps(), RingFileCache< T >::n, RingFileCache< T >::tail, RingFileCache< T >::test_and_set_tail_pointer(), and RingFileCache< T >::write_entry().

◆ read() [1/2]

◆ read() [2/2]

template<typename T >
bool RingFileCache< T >::read ( T  entries[],
const size_t  m = 1 
)
inline

Read the m the oldest entries and load them in a contiguous array.

read() reads the m the oldest entries located on the queue head side and loads them in the contiguous array entries. Of course, entries must be large enough to contain the m entries.

This method does not alter the internal state of the cache, no element is removed.

The typical usage pattern is to read with this method and, afterward (if the caller is satisfied), remove them with get().

Parameters
[in]mthe number of entries to be read from the queue head side
[in]entriesan array where the read entries will be put.
Returns
true if the m entries were read; false otherwise, what only happens if m is greater than the current number of stored elements.
Exceptions
std::ios_base::failureif there is a failure in any file.
See also
get()

Definition at line 712 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, RingFileCache< T >::dim, RingFileCache< T >::head, Aleph::maps(), RingFileCache< T >::n, and RingFileCache< T >::test_and_set_head_pointer().

◆ read_absolute()

template<typename T >
T RingFileCache< T >::read_absolute ( const size_t  pos)
inlineprivate

Read entry at absolute file position (with bounds check)

Definition at line 242 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, Aleph::maps(), and RingFileCache< T >::validate_absolute_position().

Referenced by RingFileCache< T >::read().

◆ read_all()

template<typename T >
Array< T > RingFileCache< T >::read_all ( )
inline

Read all entries in insertion order.

Returns
a dynamic and contiguous array with all the entries of the cache. If the cache is empty, the returned array is empty.

Definition at line 805 of file ringfilecache.H.

References LocateFunctions< Container, Type >::base(), Aleph::maps(), RingFileCache< T >::n, and RingFileCache< T >::read().

◆ read_entry() [1/2]

template<typename T >
T RingFileCache< T >::read_entry ( )
inlineprivate

Load an entry from the current stream position. WARNING: stream pointer not set here.

Definition at line 204 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, and Aleph::maps().

Referenced by RingFileCache< T >::oldest(), and RingFileCache< T >::read_last().

◆ read_entry() [2/2]

template<typename T >
T RingFileCache< T >::read_entry ( const size_t  pos)
inlineprivate

Read the entry at logical position pos from head (with bounds check)

Definition at line 213 of file ringfilecache.H.

References ah_range_error_if, RingFileCache< T >::cache_stream, RingFileCache< T >::dim, RingFileCache< T >::head, Aleph::maps(), and RingFileCache< T >::n.

◆ read_first()

template<typename T >
T RingFileCache< T >::read_first ( )
inline

Read the oldest entry in the cache.

Returns
the oldest (first inserted) entry
Exceptions
underflow_errorif the cache is empty

Definition at line 745 of file ringfilecache.H.

References ah_underflow_error_if, Aleph::maps(), RingFileCache< T >::n, RingFileCache< T >::read(), and RingFileCache< T >::test_and_set_head_pointer().

Referenced by RingFileCache< T >::oldest().

◆ read_from() [1/2]

template<typename T >
Array< T > RingFileCache< T >::read_from ( const Pointer ptr,
const size_t  m 
)
inline

Read up to m entries starting from the position designated by ptr.

The returned array size indicates how many entries were effectively read.

Definition at line 837 of file ringfilecache.H.

References Aleph::DynList< T >::append(), RingFileCache< T >::Iterator::get_curr_ne(), RingFileCache< T >::Pointer::get_pos_respect_to_head(), RingFileCache< T >::Iterator::has_curr(), Aleph::maps(), and RingFileCache< T >::Iterator::next_ne().

◆ read_from() [2/2]

template<typename T >
Array< T > RingFileCache< T >::read_from ( const size_t  pos,
const size_t  m 
)
inline

Read up to m entries starting from the pos-th oldest position.

The returned array size indicates how many entries were effectively read.

Definition at line 822 of file ringfilecache.H.

References Aleph::DynList< T >::append(), RingFileCache< T >::Iterator::get_curr(), RingFileCache< T >::Iterator::has_curr(), Aleph::maps(), and RingFileCache< T >::Iterator::next_ne().

◆ read_last()

template<typename T >
T RingFileCache< T >::read_last ( )
inline

Read the youngest (most recently inserted) entry in the cache.

Returns
the youngest (last inserted) entry
Exceptions
underflow_errorif the cache is empty

Definition at line 760 of file ringfilecache.H.

References ah_underflow_error_if, RingFileCache< T >::cache_stream, RingFileCache< T >::dim, RingFileCache< T >::n, RingFileCache< T >::read_entry(), and RingFileCache< T >::tail.

Referenced by RingFileCache< T >::youngest().

◆ read_pars()

◆ resize()

template<typename T >
void RingFileCache< T >::resize ( const size_t  sz)
inline

Resize the maximum capacity of the cache.

Currently only size increase is implemented. Newly added slots are value-initialized.

Parameters
[in]sznew capacity
Exceptions
domain_errorif sz is lesser than the current capacity
std::ios_base::failureif there is a failure in any file

Definition at line 918 of file ringfilecache.H.

References ah_domain_error_if, RingFileCache< T >::cache_stream, RingFileCache< T >::dim, RingFileCache< T >::flush(), Aleph::DynList< T >::get(), RingFileCache< T >::head, Aleph::init, Aleph::maps(), RingFileCache< T >::n, RingFileCache< T >::tail, RingFileCache< T >::test_and_set_tail_pointer(), and RingFileCache< T >::write_entry().

◆ size()

template<typename T >
size_t RingFileCache< T >::size ( ) const
inlinenoexcept

Returns the number of entries stored in the cache.

Definition at line 559 of file ringfilecache.H.

References RingFileCache< T >::n.

Referenced by RingFileCache< T >::to_string().

◆ state()

template<typename T >
static std::string RingFileCache< T >::state ( std::fstream &  ss)
inlinestaticprivate

Definition at line 339 of file ringfilecache.H.

References Aleph::maps(), and RingFileCache< T >::state().

Referenced by RingFileCache< T >::state().

◆ tail_pos()

template<typename T >
size_t RingFileCache< T >::tail_pos ( ) const
inlinenoexcept

return the current tail position

Definition at line 571 of file ringfilecache.H.

References RingFileCache< T >::tail.

Referenced by RingFileCache< T >::to_string().

◆ test()

template<typename T >
static bool RingFileCache< T >::test ( const std::string &  pars_fname)
inlinestatic

Tests if pars and cache files have been created.

Parameters
[in]pars_fnamename of parameters file
Returns
true if pars_fname exists and its associated cache file exists too.

Definition at line 615 of file ringfilecache.H.

References RingFileCache< T >::cache_file_name, RingFileCache< T >::cache_stream, Aleph::DynList< T >::get(), Aleph::maps(), Ring_Max_Name_Size, and RingFileCache< T >::Pars::size_cache_file.

◆ test_and_set_head_pointer()

template<typename T >
void RingFileCache< T >::test_and_set_head_pointer ( const size_t  num_entries = 0)
inlineprivate

Position the read pointer at head + num_entries (with wraparound)

Definition at line 197 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, RingFileCache< T >::dim, RingFileCache< T >::head, and Aleph::maps().

Referenced by RingFileCache< T >::read(), and RingFileCache< T >::read_first().

◆ test_and_set_tail_pointer()

template<typename T >
void RingFileCache< T >::test_and_set_tail_pointer ( )
inlineprivate

Position the writing pointer at the current tail.

Definition at line 176 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, and RingFileCache< T >::tail.

Referenced by RingFileCache< T >::put(), and RingFileCache< T >::resize().

◆ to_string()

template<typename T >
std::string RingFileCache< T >::to_string ( ) const
inline

◆ validate_absolute_position()

template<typename T >
void RingFileCache< T >::validate_absolute_position ( const size_t  pos) const
inlineprivate

Throw out_of_range if pos >= dim.

Definition at line 233 of file ringfilecache.H.

References ah_out_of_range_error, and RingFileCache< T >::dim.

Referenced by RingFileCache< T >::read_absolute(), and RingFileCache< T >::write_absolute().

◆ write()

template<typename T >
void RingFileCache< T >::write ( const Pointer ptr,
const T item 
)
inline

Write item at an absolute position designated by ptr.

Definition at line 547 of file ringfilecache.H.

References ah_domain_error_if, RingFileCache< T >::Pointer::cache_ptr, RingFileCache< T >::Pointer::pos, and RingFileCache< T >::write_absolute().

◆ write_absolute()

template<typename T >
void RingFileCache< T >::write_absolute ( const size_t  pos,
const T item 
)
inlineprivate

Write entry at absolute file position (with bounds check)

Definition at line 252 of file ringfilecache.H.

References RingFileCache< T >::cache_stream, and RingFileCache< T >::validate_absolute_position().

Referenced by RingFileCache< T >::write().

◆ write_entry()

template<typename T >
void RingFileCache< T >::write_entry ( const T item)
inlineprivate

Write an entry at the current stream position.

Definition at line 227 of file ringfilecache.H.

References RingFileCache< T >::cache_stream.

Referenced by RingFileCache< T >::put(), and RingFileCache< T >::resize().

◆ youngest()

template<typename T >
T RingFileCache< T >::youngest ( )
inline

Alias for read_last().

Returns
the youngest (last inserted) entry
Exceptions
underflow_errorif the cache is empty

Definition at line 777 of file ringfilecache.H.

References RingFileCache< T >::read_last().

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream &  out,
const RingFileCache< T > &  cache 
)
friend

Definition at line 277 of file ringfilecache.H.

Member Data Documentation

◆ cache_file_name

template<typename T >
std::string RingFileCache< T >::cache_file_name
private

◆ cache_stream

◆ dim

◆ head

◆ initialized

template<typename T >
bool RingFileCache< T >::initialized = false
private

◆ n

◆ pars_file_name

template<typename T >
std::string RingFileCache< T >::pars_file_name
private

◆ pars_stream

◆ tail


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