|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Out-of-core 2D tile cache with LRU eviction. More...
#include <ca-streaming-storage.H>
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. | |
| 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_ |
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.
| State | cell value type. Must be trivially copyable. |
| TileSide | compile-time tile side length. |
Definition at line 133 of file ca-streaming-storage.H.
|
private |
Definition at line 160 of file ca-streaming-storage.H.
|
private |
Definition at line 159 of file ca-streaming-storage.H.
|
private |
Definition at line 158 of file ca-streaming-storage.H.
| using Aleph::CA::Tile_Cache< State, TileSide >::state_type = State |
Cell value type.
Definition at line 141 of file ca-streaming-storage.H.
|
private |
Definition at line 148 of file ca-streaming-storage.H.
|
inline |
Build a tile cache.
Creates dir if it does not exist. Tile files are written into this directory on demand.
| [in] | extents | {rows, cols} of the logical grid. Both must be > 0 and multiples of TileSide. |
| [in] | dir | directory backing the tiles on disk. |
| [in] | capacity | maximum number of tiles kept resident. |
| std::domain_error | if capacity == 0 or extents are not multiples of TileSide. |
| std::filesystem::filesystem_error | if the directory cannot be created. |
Definition at line 282 of file ca-streaming-storage.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), 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_.
|
inline |
Read the cell at (r, c).
Pages the owning tile in if it is not resident. Marks the tile as most-recently-used.
| std::out_of_range | if (r, c) is outside the grid. |
Definition at line 327 of file ca-streaming-storage.H.
References ah_out_of_range_error_if, Aleph::blossom_maximum_cardinality_matching(), 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_.
|
inlinenoexcept |
Definition at line 311 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::capacity_.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache().
|
inlinenoexcept |
Definition at line 301 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::cols_, and Aleph::CA::Tile_Cache< State, TileSide >::rows_.
|
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.
| std::runtime_error | on stream write failure. |
Definition at line 362 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().
|
inlinestaticprivatenoexcept |
Definition at line 260 of file ca-streaming-storage.H.
References Aleph::blossom_maximum_cardinality_matching(), and r.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::at(), and Aleph::CA::Tile_Cache< State, TileSide >::set().
|
inlineprivate |
Page a tile in, evicting the LRU when full.
Definition at line 223 of file ca-streaming-storage.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Tile_Cache< State, TileSide >::cache_, Aleph::CA::Tile_Cache< State, TileSide >::capacity_, col, Aleph::CA::Tile_Cache< State, TileSide >::Tile_Entry::col, Aleph::CA::Tile_Cache< State, TileSide >::Tile_Entry::data, Aleph::CA::Tile_Cache_Stats::evictions, Aleph::CA::Tile_Cache< State, TileSide >::index_, Aleph::CA::Tile_Cache< State, TileSide >::read_tile_from_disk(), row, Aleph::CA::Tile_Cache< State, TileSide >::Tile_Entry::row, Aleph::CA::Tile_Cache< State, TileSide >::stats_, Aleph::CA::ca_stream_detail::tile_key(), and Aleph::CA::Tile_Cache< State, TileSide >::write_tile_to_disk().
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::resolve().
|
inlineprivate |
Read tile from disk into buf.
Initialises buf to State{} when the file does not yet exist (first touch).
Definition at line 180 of file ca-streaming-storage.H.
References ah_runtime_error_if, Aleph::blossom_maximum_cardinality_matching(), col, Aleph::CA::Tile_Cache_Stats::reads, row, Aleph::CA::Tile_Cache< State, TileSide >::stats_, Aleph::CA::Tile_Cache< State, TileSide >::tile_cells, and Aleph::CA::Tile_Cache< State, TileSide >::tile_path().
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::page_in().
|
inlinenoexcept |
Definition at line 313 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::cache_.
|
inlineprivate |
Locate the tile that owns (r, c), paging it in if absent.
Definition at line 245 of file ca-streaming-storage.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Tile_Cache_Stats::hits, Aleph::CA::Tile_Cache< State, TileSide >::index_, Aleph::CA::Tile_Cache_Stats::misses, Aleph::CA::Tile_Cache< State, TileSide >::page_in(), r, Aleph::CA::Tile_Cache< State, TileSide >::stats_, Aleph::CA::ca_stream_detail::tile_key(), and Aleph::CA::Tile_Cache< State, TileSide >::touch().
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::at(), and Aleph::CA::Tile_Cache< State, TileSide >::set().
|
inline |
Write the cell at (r, c) and mark its tile dirty.
| std::out_of_range | if (r, c) is outside the grid. |
Definition at line 343 of file ca-streaming-storage.H.
References ah_out_of_range_error_if, Aleph::blossom_maximum_cardinality_matching(), 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().
|
inlinenoexcept |
Definition at line 315 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::stats_.
|
inlineprivate |
Definition at line 172 of file ca-streaming-storage.H.
References col, Aleph::CA::Tile_Cache< State, TileSide >::dir_, and row.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::read_tile_from_disk(), and Aleph::CA::Tile_Cache< State, TileSide >::write_tile_to_disk().
|
inlinenoexcept |
Definition at line 309 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_col_.
|
inlinenoexcept |
Definition at line 307 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_row_.
|
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().
|
inlineprivate |
Persist tile to disk.
Definition at line 200 of file ca-streaming-storage.H.
References ah_runtime_error_if, Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Tile_Cache< State, TileSide >::Tile_Entry::col, Aleph::CA::Tile_Cache< State, TileSide >::Tile_Entry::data, out, Aleph::CA::Tile_Cache< State, TileSide >::Tile_Entry::row, Aleph::CA::Tile_Cache< State, TileSide >::stats_, Aleph::CA::Tile_Cache< State, TileSide >::tile_cells, Aleph::CA::Tile_Cache< State, TileSide >::tile_path(), and Aleph::CA::Tile_Cache_Stats::writes.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::flush(), and Aleph::CA::Tile_Cache< State, TileSide >::page_in().
|
private |
|
private |
Definition at line 167 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::capacity(), and Aleph::CA::Tile_Cache< State, TileSide >::page_in().
|
private |
|
private |
Definition at line 166 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache(), and Aleph::CA::Tile_Cache< State, TileSide >::tile_path().
|
private |
Definition at line 169 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::page_in(), and Aleph::CA::Tile_Cache< State, TileSide >::resolve().
|
private |
|
private |
Definition at line 170 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::page_in(), Aleph::CA::Tile_Cache< State, TileSide >::read_tile_from_disk(), Aleph::CA::Tile_Cache< State, TileSide >::resolve(), Aleph::CA::Tile_Cache< State, TileSide >::stats(), and Aleph::CA::Tile_Cache< State, TileSide >::write_tile_to_disk().
|
staticconstexpr |
Number of cells per tile.
Definition at line 145 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::read_tile_from_disk(), and Aleph::CA::Tile_Cache< State, TileSide >::write_tile_to_disk().
|
staticconstexpr |
Tile side length (in cells).
Definition at line 143 of file ca-streaming-storage.H.
|
private |
Definition at line 165 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache(), and Aleph::CA::Tile_Cache< State, TileSide >::tiles_cols().
|
private |
Definition at line 164 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache(), and Aleph::CA::Tile_Cache< State, TileSide >::tiles_rows().