Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CA::Tile_Cache< State, TileSide > Class Template Reference

Out-of-core 2D tile cache with LRU eviction. More...

#include <ca-streaming-storage.H>

Collaboration diagram for Aleph::CA::Tile_Cache< State, TileSide >:
[legend]

Classes

struct  Tile_Entry
 

Public Types

using state_type = State
 Cell value type.
 

Public Member Functions

 Tile_Cache (const std::array< ca_size_t, 2 > &extents, std::filesystem::path dir, const std::size_t capacity)
 Build a tile cache.
 
 Tile_Cache (const Tile_Cache &)=delete
 Copying is disabled.
 
Tile_Cache & operator= (const Tile_Cache &)=delete
 Copying is disabled, as in the copy constructor.
 
 Tile_Cache (Tile_Cache &&other) noexcept(std::is_nothrow_move_constructible_v< lru_list > and std::is_nothrow_move_constructible_v< key_map >)
 Take the tiles, the directory and the statistics of another cache, which is left as an empty cache, with no extents.
 
Tile_Cache & operator= (Tile_Cache &&other) noexcept
 Exchange this cache with another, which is left with the previous tiles of this one.
 
void swap (Tile_Cache &other) noexcept
 Exchange in O(1) the grids, the tiles, the directories and the statistics of this cache and another.
 
std::array< ca_size_t, 2 > extents () const noexcept
 
ca_size_t tiles_rows () const noexcept
 
ca_size_t tiles_cols () const noexcept
 
std::size_t capacity () const noexcept
 
std::size_t resident () const noexcept
 
Tile_Cache_Stats stats () const noexcept
 
State at (ca_size_t r, ca_size_t c)
 Read the cell at (r, c).
 
void set (ca_size_t r, ca_size_t c, const State &v)
 Write the cell at (r, c) and mark its tile dirty.
 
void flush ()
 Persist every dirty resident tile to disk and clear the dirty flag.
 

Static Public Attributes

static constexpr std::size_t tile_side = TileSide
 Tile side length (in cells).
 
static constexpr std::size_t tile_cells = TileSide * TileSide
 Number of cells per tile.
 

Private Types

using tile_buffer = std::array< State, tile_cells >
 
using lru_list = std::list< Tile_Entry >
 
using lru_iter = typename lru_list::iterator
 
using key_map = std::unordered_map< std::uint64_t, lru_iter >
 

Private Member Functions

std::filesystem::path tile_path (std::uint32_t row, std::uint32_t col) const
 
void read_tile_from_disk (std::uint32_t row, std::uint32_t col, tile_buffer &buf)
 Read tile from disk into buf.
 
void write_tile_to_disk (const Tile_Entry &entry)
 Persist tile to disk.
 
void touch (lru_iter it)
 Move it to the front of the LRU list (mark as most-recent).
 
lru_iter page_in (std::uint32_t row, std::uint32_t col)
 Page a tile in, evicting the LRU when full.
 
lru_iter resolve (ca_size_t r, ca_size_t c)
 Locate the tile that owns (r, c), paging it in if absent.
 

Static Private Member Functions

static std::size_t local_index (ca_size_t r, ca_size_t c) noexcept
 

Private Attributes

ca_size_t rows_ = 0
 
ca_size_t cols_ = 0
 
ca_size_t tiles_per_row_ = 0
 
ca_size_t tiles_per_col_ = 0
 
std::filesystem::path dir_
 
std::size_t capacity_ = 0
 
lru_list cache_
 
key_map index_
 
Tile_Cache_Stats stats_
 

Detailed Description

template<typename State, std::size_t TileSide>
class Aleph::CA::Tile_Cache< State, TileSide >

Out-of-core 2D tile cache with LRU eviction.

The cache models a logical rows × cols grid divided into TileSide × TileSide tiles. The grid extents must be multiples of TileSide. Up to capacity tiles are kept resident in RAM; cache misses page the tile in from disk (or zero-initialise it on first touch) and evict the least-recently-used tile if needed.

Template Parameters
Statecell value type. Must be trivially copyable.
TileSidecompile-time tile side length.

Definition at line 133 of file ca-streaming-storage.H.

Member Typedef Documentation

◆ key_map

template<typename State , std::size_t TileSide>
using Aleph::CA::Tile_Cache< State, TileSide >::key_map = std::unordered_map<std::uint64_t, lru_iter>
private

Definition at line 160 of file ca-streaming-storage.H.

◆ lru_iter

template<typename State , std::size_t TileSide>
using Aleph::CA::Tile_Cache< State, TileSide >::lru_iter = typename lru_list::iterator
private

Definition at line 159 of file ca-streaming-storage.H.

◆ lru_list

template<typename State , std::size_t TileSide>
using Aleph::CA::Tile_Cache< State, TileSide >::lru_list = std::list<Tile_Entry>
private

Definition at line 158 of file ca-streaming-storage.H.

◆ state_type

template<typename State , std::size_t TileSide>
using Aleph::CA::Tile_Cache< State, TileSide >::state_type = State

Cell value type.

Definition at line 141 of file ca-streaming-storage.H.

◆ tile_buffer

template<typename State , std::size_t TileSide>
using Aleph::CA::Tile_Cache< State, TileSide >::tile_buffer = std::array<State, tile_cells>
private

Definition at line 148 of file ca-streaming-storage.H.

Constructor & Destructor Documentation

◆ Tile_Cache() [1/3]

template<typename State , std::size_t TileSide>
Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache ( const std::array< ca_size_t, 2 > &  extents,
std::filesystem::path  dir,
const std::size_t  capacity 
)
inline

Build a tile cache.

Creates dir if it does not exist. Tile files are written into this directory on demand.

Parameters
[in]extents{rows, cols} of the logical grid. Both must be > 0 and multiples of TileSide.
[in]dirdirectory backing the tiles on disk.
[in]capacitymaximum number of tiles kept resident.
Exceptions
std::domain_errorif capacity == 0 or extents are not multiples of TileSide.
std::filesystem::filesystem_errorif the directory cannot be created.

Definition at line 282 of file ca-streaming-storage.H.

References ah_domain_error_if, Aleph::CA::Tile_Cache< State, TileSide >::capacity(), Aleph::CA::Tile_Cache< State, TileSide >::cols_, Aleph::CA::Tile_Cache< State, TileSide >::dir_, Aleph::CA::Tile_Cache< State, TileSide >::rows_, Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_col_, and Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_row_.

◆ Tile_Cache() [2/3]

template<typename State , std::size_t TileSide>
Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache ( const Tile_Cache< State, TileSide > &  )
delete

Copying is disabled.

The implicit copy kept in its index the iterators into the tiles of the original, so the copy spliced the nodes of another list; both caches would also write the same tile files.

◆ Tile_Cache() [3/3]

template<typename State , std::size_t TileSide>
Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache ( Tile_Cache< State, TileSide > &&  other)
inlinenoexcept

Take the tiles, the directory and the statistics of another cache, which is left as an empty cache, with no extents.

The implicit move took the tiles and the directory but copied the extents: the moved-from cache then read and wrote tiles with no directory, in the current one.

Parameters
[in,out]othercache to move from.

Definition at line 320 of file ca-streaming-storage.H.

Member Function Documentation

◆ at()

template<typename State , std::size_t TileSide>
State Aleph::CA::Tile_Cache< State, TileSide >::at ( ca_size_t  r,
ca_size_t  c 
)
inline

Read the cell at (r, c).

Pages the owning tile in if it is not resident. Marks the tile as most-recently-used.

Parameters
[in]rrow index in [0, rows()).
[in]ccolumn index in [0, cols()).
Returns
cell value.
Exceptions
std::out_of_rangeif (r, c) is outside the grid.

Definition at line 386 of file ca-streaming-storage.H.

References ah_out_of_range_error_if, Aleph::CA::Tile_Cache< State, TileSide >::cols_, Aleph::CA::Tile_Cache< State, TileSide >::local_index(), r, Aleph::CA::Tile_Cache< State, TileSide >::resolve(), and Aleph::CA::Tile_Cache< State, TileSide >::rows_.

◆ capacity()

template<typename State , std::size_t TileSide>
std::size_t Aleph::CA::Tile_Cache< State, TileSide >::capacity ( ) const
inlinenoexcept
Returns
resident tile capacity (LRU upper bound).

Definition at line 370 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::capacity_.

Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache().

◆ extents()

template<typename State , std::size_t TileSide>
std::array< ca_size_t, 2 > Aleph::CA::Tile_Cache< State, TileSide >::extents ( ) const
inlinenoexcept

◆ flush()

template<typename State , std::size_t TileSide>
void Aleph::CA::Tile_Cache< State, TileSide >::flush ( )
inline

Persist every dirty resident tile to disk and clear the dirty flag.

After flush the on-disk image is consistent with what the cache currently holds. Resident tiles stay in the cache so the next access remains a hit.

Exceptions
std::runtime_erroron stream write failure.

Definition at line 421 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::cache_, and Aleph::CA::Tile_Cache< State, TileSide >::write_tile_to_disk().

◆ local_index()

template<typename State , std::size_t TileSide>
static std::size_t Aleph::CA::Tile_Cache< State, TileSide >::local_index ( ca_size_t  r,
ca_size_t  c 
)
inlinestaticprivatenoexcept

◆ operator=() [1/2]

template<typename State , std::size_t TileSide>
Tile_Cache & Aleph::CA::Tile_Cache< State, TileSide >::operator= ( const Tile_Cache< State, TileSide > &  )
delete

Copying is disabled, as in the copy constructor.

◆ operator=() [2/2]

template<typename State , std::size_t TileSide>
Tile_Cache & Aleph::CA::Tile_Cache< State, TileSide >::operator= ( Tile_Cache< State, TileSide > &&  other)
inlinenoexcept

Exchange this cache with another, which is left with the previous tiles of this one.

Parameters
[in,out]othercache to move from.
Returns
this cache.

Definition at line 335 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::swap().

◆ page_in()

◆ read_tile_from_disk()

template<typename State , std::size_t TileSide>
void Aleph::CA::Tile_Cache< State, TileSide >::read_tile_from_disk ( std::uint32_t  row,
std::uint32_t  col,
tile_buffer &  buf 
)
inlineprivate

◆ resident()

template<typename State , std::size_t TileSide>
std::size_t Aleph::CA::Tile_Cache< State, TileSide >::resident ( ) const
inlinenoexcept
Returns
number of tiles currently resident.

Definition at line 372 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::cache_.

◆ resolve()

◆ set()

template<typename State , std::size_t TileSide>
void Aleph::CA::Tile_Cache< State, TileSide >::set ( ca_size_t  r,
ca_size_t  c,
const State &  v 
)
inline

Write the cell at (r, c) and mark its tile dirty.

Parameters
[in]rrow index in [0, rows()).
[in]ccolumn index in [0, cols()).
[in]vnew cell value.
Exceptions
std::out_of_rangeif (r, c) is outside the grid.

Definition at line 402 of file ca-streaming-storage.H.

References ah_out_of_range_error_if, Aleph::CA::Tile_Cache< State, TileSide >::cols_, Aleph::CA::Tile_Cache< State, TileSide >::local_index(), r, Aleph::CA::Tile_Cache< State, TileSide >::resolve(), and Aleph::CA::Tile_Cache< State, TileSide >::rows_.

Referenced by TEST().

◆ stats()

template<typename State , std::size_t TileSide>
Tile_Cache_Stats Aleph::CA::Tile_Cache< State, TileSide >::stats ( ) const
inlinenoexcept
Returns
cache statistics snapshot.

Definition at line 374 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::stats_.

◆ swap()

◆ tile_path()

template<typename State , std::size_t TileSide>
std::filesystem::path Aleph::CA::Tile_Cache< State, TileSide >::tile_path ( std::uint32_t  row,
std::uint32_t  col 
) const
inlineprivate

◆ tiles_cols()

template<typename State , std::size_t TileSide>
ca_size_t Aleph::CA::Tile_Cache< State, TileSide >::tiles_cols ( ) const
inlinenoexcept
Returns
number of tiles along axis 1.

Definition at line 368 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_col_.

◆ tiles_rows()

template<typename State , std::size_t TileSide>
ca_size_t Aleph::CA::Tile_Cache< State, TileSide >::tiles_rows ( ) const
inlinenoexcept
Returns
number of tiles along axis 0.

Definition at line 366 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_row_.

◆ touch()

template<typename State , std::size_t TileSide>
void Aleph::CA::Tile_Cache< State, TileSide >::touch ( lru_iter  it)
inlineprivate

Move it to the front of the LRU list (mark as most-recent).

Definition at line 216 of file ca-streaming-storage.H.

References Aleph::CA::Tile_Cache< State, TileSide >::cache_.

Referenced by Aleph::CA::Tile_Cache< State, TileSide >::resolve().

◆ write_tile_to_disk()

Member Data Documentation

◆ cache_

◆ capacity_

template<typename State , std::size_t TileSide>
std::size_t Aleph::CA::Tile_Cache< State, TileSide >::capacity_ = 0
private

◆ cols_

◆ dir_

template<typename State , std::size_t TileSide>
std::filesystem::path Aleph::CA::Tile_Cache< State, TileSide >::dir_
private

◆ index_

template<typename State , std::size_t TileSide>
key_map Aleph::CA::Tile_Cache< State, TileSide >::index_
private

◆ rows_

◆ stats_

◆ tile_cells

template<typename State , std::size_t TileSide>
constexpr std::size_t Aleph::CA::Tile_Cache< State, TileSide >::tile_cells = TileSide * TileSide
staticconstexpr

◆ tile_side

template<typename State , std::size_t TileSide>
constexpr std::size_t Aleph::CA::Tile_Cache< State, TileSide >::tile_side = TileSide
staticconstexpr

Tile side length (in cells).

Definition at line 143 of file ca-streaming-storage.H.

◆ tiles_per_col_

template<typename State , std::size_t TileSide>
ca_size_t Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_col_ = 0
private

◆ tiles_per_row_

template<typename State , std::size_t TileSide>
ca_size_t Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_row_ = 0
private

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