|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Persistent ring buffer cache with file-backed storage. More...
#include <ringfilecache.H>
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< T > | read_all () |
| Read all entries in insertion order. | |
| Array< T > | read_from (const size_t pos, const size_t m) |
Read up to m entries starting from the pos-th oldest position. | |
| Array< T > | read_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) |
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:
The cache can be closed and reopened, preserving all data and state. Use save_pars() to ensure metadata is synchronized to disk.
| T | Element type (must be trivially copyable and default constructible) |
Definition at line 130 of file ringfilecache.H.
|
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.
| [in] | pars_fname | name of a cache parameters file |
| domain_error | if any file cannot be opened |
| std::ios_base::failure | if 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().
|
default |
Default constructor.
Provided to allow deferred initialization when file creation must be handled elsewhere.
|
inline |
Definition at line 907 of file ringfilecache.H.
References RingFileCache< T >::close().
|
inlinenoexcept |
Returns the number of available entries.
Definition at line 565 of file ringfilecache.H.
References RingFileCache< T >::dim, and RingFileCache< T >::n.
|
inlinenoexcept |
Returns the maximum capacity.
Definition at line 562 of file ringfilecache.H.
References RingFileCache< T >::dim.
Referenced by RingFileCache< T >::to_string().
|
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().
|
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.
| [in] | pars_file_name | Path of the parameters file to create. |
| [in] | cache_file_name | Path of the data file to create. |
| [in] | num_entries | Capacity (number of entries) of the cache. |
| std::domain_error | if any file cannot be opened/written. |
| std::bad_alloc | if 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.
|
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.
|
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().
Extracts (deletes) from the cache the m oldest inserted items.
| [in] | m | number of items to be extracted |
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().
|
inline |
Returns a forward iterator starting at the oldest entry.
Definition at line 1057 of file ringfilecache.H.
|
inlinenoexcept |
Returns the current head position.
Definition at line 568 of file ringfilecache.H.
References RingFileCache< T >::head.
Referenced by RingFileCache< T >::to_string().
Initialize a cache constructed with the default constructor.
| [in] | pars_fname | name of cache parameters file |
| domain_error | if a pars file is already opened |
| std::ios_base::failure | if 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().
|
inlinenoexcept |
Returns true if the cache is empty.
Definition at line 574 of file ringfilecache.H.
References RingFileCache< T >::n.
|
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.
|
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.
|
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.
|
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.
|
inline |
Alias for read_first().
| underflow_error | if the cache is empty |
Definition at line 784 of file ringfilecache.H.
References RingFileCache< T >::read_first().
Read the i-th oldest element without removing it.
| [in] | i | zero-based index from the oldest entry |
i from the head | overflow_error | if i >= size() |
Definition at line 792 of file ringfilecache.H.
References ah_overflow_error_if, RingFileCache< T >::n, and RingFileCache< T >::read_entry().
Insert an item into the cache.
The cache is FIFO: each new entry is appended at the tail.
| [in] | item | to be inserted into the queue |
true if the 'item' was correctly inserted; false otherwise| std::ios_base::failure | if 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 the entry pointed to by ptr
Definition at line 538 of file ringfilecache.H.
References ah_domain_error_if, RingFileCache< T >::Pointer::cache_ptr, RingFileCache< T >::Pointer::pos, and RingFileCache< T >::read_absolute().
Referenced by RingFileCache< T >::read_all(), and RingFileCache< T >::read_first().
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().
| [in] | m | the number of entries to be read from the queue head side |
| [in] | entries | an array where the read entries will be put. |
m entries were read; false otherwise, what only happens if m is greater than the current number of stored elements. | std::ios_base::failure | if there is a failure in any file. |
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 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 entries in insertion order.
Definition at line 805 of file ringfilecache.H.
References LocateFunctions< Container, Type >::base(), Aleph::maps(), RingFileCache< T >::n, and RingFileCache< T >::read().
|
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 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.
|
inline |
Read the oldest entry in the cache.
| underflow_error | if 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 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 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().
|
inline |
Read the youngest (most recently inserted) entry in the cache.
| underflow_error | if 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().
|
inlineprivate |
Definition at line 363 of file ringfilecache.H.
References ah_domain_error_if, RingFileCache< T >::cache_file_name, RingFileCache< T >::cache_stream, RingFileCache< T >::Pars::dim, RingFileCache< T >::dim, RingFileCache< T >::Pars::head, RingFileCache< T >::head, RingFileCache< T >::initialized, Aleph::maps(), RingFileCache< T >::Pars::n, RingFileCache< T >::n, RingFileCache< T >::pars_file_name, RingFileCache< T >::pars_stream, Ring_Max_Name_Size, RingFileCache< T >::Pars::size_cache_file, RingFileCache< T >::Pars::tail, and RingFileCache< T >::tail.
Referenced by RingFileCache< T >::RingFileCache(), and RingFileCache< T >::init().
Resize the maximum capacity of the cache.
Currently only size increase is implemented. Newly added slots are value-initialized.
| [in] | sz | new capacity |
| domain_error | if sz is lesser than the current capacity |
| std::ios_base::failure | if 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().
|
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().
|
inlinestaticprivate |
Definition at line 339 of file ringfilecache.H.
References Aleph::maps(), and RingFileCache< T >::state().
Referenced by RingFileCache< T >::state().
|
inlinenoexcept |
return the current tail position
Definition at line 571 of file ringfilecache.H.
References RingFileCache< T >::tail.
Referenced by RingFileCache< T >::to_string().
|
inlinestatic |
Tests if pars and cache files have been created.
| [in] | pars_fname | name of parameters file |
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.
|
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().
|
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().
|
inline |
Human-readable dump of the cache internal state (debugging)
Definition at line 261 of file ringfilecache.H.
References RingFileCache< T >::cache_stream, RingFileCache< T >::capacity(), RingFileCache< T >::head_pos(), Aleph::maps(), RingFileCache< T >::size(), and RingFileCache< T >::tail_pos().
|
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 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 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 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().
|
inline |
Alias for read_last().
| underflow_error | if the cache is empty |
Definition at line 777 of file ringfilecache.H.
References RingFileCache< T >::read_last().
|
friend |
Definition at line 277 of file ringfilecache.H.
|
private |
Definition at line 165 of file ringfilecache.H.
Referenced by RingFileCache< T >::create(), RingFileCache< T >::flush(), RingFileCache< T >::read_pars(), and RingFileCache< T >::test().
|
private |
Definition at line 168 of file ringfilecache.H.
Referenced by RingFileCache< T >::close(), RingFileCache< T >::create(), RingFileCache< T >::flush(), RingFileCache< T >::put(), RingFileCache< T >::read(), RingFileCache< T >::read_absolute(), RingFileCache< T >::read_entry(), RingFileCache< T >::read_entry(), RingFileCache< T >::read_last(), RingFileCache< T >::read_pars(), RingFileCache< T >::resize(), RingFileCache< T >::test(), RingFileCache< T >::test_and_set_head_pointer(), RingFileCache< T >::test_and_set_tail_pointer(), RingFileCache< T >::to_string(), RingFileCache< T >::write_absolute(), and RingFileCache< T >::write_entry().
|
private |
Definition at line 170 of file ringfilecache.H.
Referenced by RingFileCache< T >::avail(), RingFileCache< T >::capacity(), RingFileCache< T >::Pointer::decrease_pos(), RingFileCache< T >::flush(), RingFileCache< T >::get(), RingFileCache< T >::Pointer::get_pos_respect_to_head(), RingFileCache< T >::Pointer::increase_pos(), RingFileCache< T >::is_full(), RingFileCache< T >::put(), RingFileCache< T >::read(), RingFileCache< T >::read_entry(), RingFileCache< T >::read_last(), RingFileCache< T >::read_pars(), RingFileCache< T >::resize(), RingFileCache< T >::test_and_set_head_pointer(), RingFileCache< T >::validate_absolute_position(), and RingFileCache< T >::Pointer::validate_position().
|
private |
Definition at line 172 of file ringfilecache.H.
Referenced by RingFileCache< T >::flush(), RingFileCache< T >::get(), RingFileCache< T >::Pointer::get_pos_respect_to_head(), RingFileCache< T >::head_pos(), RingFileCache< T >::is_valid_position(), RingFileCache< T >::read(), RingFileCache< T >::read_entry(), RingFileCache< T >::read_pars(), RingFileCache< T >::resize(), and RingFileCache< T >::test_and_set_head_pointer().
Definition at line 163 of file ringfilecache.H.
Referenced by RingFileCache< T >::close(), RingFileCache< T >::is_initialized(), and RingFileCache< T >::read_pars().
|
private |
Definition at line 171 of file ringfilecache.H.
Referenced by RingFileCache< T >::avail(), RingFileCache< T >::empty(), RingFileCache< T >::flush(), RingFileCache< T >::get(), RingFileCache< T >::is_empty(), RingFileCache< T >::is_full(), RingFileCache< T >::is_valid_offset(), RingFileCache< T >::oldest(), RingFileCache< T >::put(), RingFileCache< T >::read(), RingFileCache< T >::read_all(), RingFileCache< T >::read_entry(), RingFileCache< T >::read_first(), RingFileCache< T >::read_last(), RingFileCache< T >::read_pars(), RingFileCache< T >::resize(), and RingFileCache< T >::size().
|
private |
Definition at line 164 of file ringfilecache.H.
Referenced by RingFileCache< T >::RingFileCache(), RingFileCache< T >::create(), RingFileCache< T >::init(), and RingFileCache< T >::read_pars().
|
private |
Definition at line 167 of file ringfilecache.H.
Referenced by RingFileCache< T >::close(), RingFileCache< T >::create(), RingFileCache< T >::flush(), RingFileCache< T >::init(), and RingFileCache< T >::read_pars().
|
private |
Definition at line 173 of file ringfilecache.H.
Referenced by RingFileCache< T >::flush(), RingFileCache< T >::is_valid_position(), RingFileCache< T >::put(), RingFileCache< T >::read_last(), RingFileCache< T >::read_pars(), RingFileCache< T >::resize(), RingFileCache< T >::tail_pos(), and RingFileCache< T >::test_and_set_tail_pointer().