|
Aleph-w 7.10.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. | |
| 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_ |
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::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_.
|
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.
|
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.
| [in,out] | other | cache to move from. |
Definition at line 320 of file ca-streaming-storage.H.
|
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 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_.
|
inlinenoexcept |
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().
|
inlinenoexcept |
Definition at line 360 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 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().
|
inlinestaticprivatenoexcept |
Definition at line 260 of file ca-streaming-storage.H.
References r.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::at(), and Aleph::CA::Tile_Cache< State, TileSide >::set().
|
delete |
Copying is disabled, as in the copy constructor.
|
inlinenoexcept |
Exchange this cache with another, which is left with the previous tiles of this one.
| [in,out] | other | cache to move from. |
Definition at line 335 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::swap().
|
inlineprivate |
Page a tile in, evicting the LRU when full.
Definition at line 223 of file ca-streaming-storage.H.
References 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< State, TileSide >::Tile_Entry::dirty, 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, 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 372 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::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 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().
|
inlinenoexcept |
Definition at line 374 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::stats_.
|
inlinenoexcept |
Exchange in O(1) the grids, the tiles, the directories and the statistics of this cache and another.
| [in,out] | other | cache to exchange with. |
Definition at line 345 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::cache_, 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 >::index_, Aleph::CA::Tile_Cache< State, TileSide >::rows_, Aleph::CA::Tile_Cache< State, TileSide >::stats_, Aleph::CA::Tile_Cache< State, TileSide >::swap(), Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_col_, and Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_row_.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::operator=(), and Aleph::CA::Tile_Cache< State, TileSide >::swap().
|
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 368 of file ca-streaming-storage.H.
References Aleph::CA::Tile_Cache< State, TileSide >::tiles_per_col_.
|
inlinenoexcept |
Definition at line 366 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::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 |
Definition at line 168 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::flush(), Aleph::CA::Tile_Cache< State, TileSide >::page_in(), Aleph::CA::Tile_Cache< State, TileSide >::resident(), Aleph::CA::Tile_Cache< State, TileSide >::swap(), and Aleph::CA::Tile_Cache< State, TileSide >::touch().
|
private |
Definition at line 167 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::capacity(), Aleph::CA::Tile_Cache< State, TileSide >::page_in(), and Aleph::CA::Tile_Cache< State, TileSide >::swap().
|
private |
Definition at line 163 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache(), Aleph::CA::Tile_Cache< State, TileSide >::at(), Aleph::CA::Tile_Cache< State, TileSide >::extents(), Aleph::CA::Tile_Cache< State, TileSide >::set(), and Aleph::CA::Tile_Cache< State, TileSide >::swap().
|
private |
Definition at line 166 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache(), Aleph::CA::Tile_Cache< State, TileSide >::swap(), 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(), Aleph::CA::Tile_Cache< State, TileSide >::resolve(), and Aleph::CA::Tile_Cache< State, TileSide >::swap().
|
private |
Definition at line 162 of file ca-streaming-storage.H.
Referenced by Aleph::CA::Tile_Cache< State, TileSide >::Tile_Cache(), Aleph::CA::Tile_Cache< State, TileSide >::at(), Aleph::CA::Tile_Cache< State, TileSide >::extents(), Aleph::CA::Tile_Cache< State, TileSide >::set(), and Aleph::CA::Tile_Cache< State, TileSide >::swap().
|
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(), Aleph::CA::Tile_Cache< State, TileSide >::swap(), 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(), Aleph::CA::Tile_Cache< State, TileSide >::swap(), 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(), Aleph::CA::Tile_Cache< State, TileSide >::swap(), and Aleph::CA::Tile_Cache< State, TileSide >::tiles_rows().