vitrio 0.1.4
High-throughput image I/O for Cryo-Electron Microscopy (CryoEM)
Loading...
Searching...
No Matches
Public Member Functions | List of all members
vitrio::caching_image_reader_provider Class Referencefinal

A provider that keeps the readers it was last asked for. More...

#include <vitrio/caching_image_reader_provider.hpp>

Inheritance diagram for vitrio::caching_image_reader_provider:
Inheritance graph
[legend]
Collaboration diagram for vitrio::caching_image_reader_provider:
Collaboration graph
[legend]

Public Member Functions

 caching_image_reader_provider (std::shared_ptr< image_reader_provider > backing, std::size_t capacity)
 Construct a cache in front of another provider.
 
std::size_t get_capacity () const noexcept
 Get the number of readers this may keep open at once.
 
std::size_t get_reader_count () const noexcept
 Get how many readers are kept open.
 
std::shared_ptr< const image_reader > acquire (const std::string &key) override
 Get the reader a key names.
 
- Public Member Functions inherited from vitrio::image_reader_provider
 image_reader_provider (const image_reader_provider &other)=delete
 
 image_reader_provider (image_reader_provider &&other)=delete
 
image_reader_provider & operator= (const image_reader_provider &other)=delete
 
image_reader_provider & operator= (image_reader_provider &&other)=delete
 

Detailed Description

A provider that keeps the readers it was last asked for.

Opening a file costs a system call and a header read, which asking for the same few files over and over repeats for nothing. This keeps a bounded number of readers open, each under the key it was asked for with, and evicts the least recently asked for when it is full. A key it holds is served without opening anything, and becomes the last to be evicted.

It is a decorator over another provider, not a second opener: on a miss it asks its backing provider and keeps what it gets, so it caches the readers of any provider alike.

The capacity is a file descriptor budget, which is why it is stated in entries rather than in bytes.

Thread safety
acquire may be called concurrently. The lock is not held while the backing provider opens a file, so two concurrent calls missing on one key may both open it and one of the two readers is kept; that wastes an open and never costs correctness, since either reader serves equally. Handing out shared_ptr means the one not kept stays alive as long as it is read through, and so does an evicted one.

Constructor & Destructor Documentation

◆ caching_image_reader_provider()

vitrio::caching_image_reader_provider::caching_image_reader_provider ( std::shared_ptr< image_reader_provider >  backing,
std::size_t  capacity 
)

Construct a cache in front of another provider.

Parameters
backingThe provider asked on a miss.
capacityMost readers to keep open at once. Must be greater than zero.
Exceptions
std::invalid_argumentIf backing is null or capacity is zero.

Member Function Documentation

◆ acquire()

std::shared_ptr< const image_reader > vitrio::caching_image_reader_provider::acquire ( const std::string &  key)
overridevirtual

Get the reader a key names.

Shared ownership rather than a reference, so that a reader the provider stops keeping stays alive for as long as it is still read through.

Parameters
keyKey of the file to read, such as its path.
Returns
std::shared_ptr<const image_reader> The reader, never null.
Exceptions
image_file_errorIf the file does not exist or can not be read.
unsupported_operation_errorIf no format can read the file.
image_file_format_errorIf the file is malformed or truncated.

Implements vitrio::image_reader_provider.

◆ get_capacity()

std::size_t vitrio::caching_image_reader_provider::get_capacity ( ) const
noexcept

Get the number of readers this may keep open at once.

Returns
std::size_t The capacity, in entries.

◆ get_reader_count()

std::size_t vitrio::caching_image_reader_provider::get_reader_count ( ) const
noexcept

Get how many readers are kept open.

Returns
std::size_t The number of entries, never above get_capacity.

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