Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
Euclid::FilePool::FileManager Class Referenceabstract

#include <FileManager.h>

Inheritance diagram for Euclid::FilePool::FileManager:
Inheritance graph
[legend]
Collaboration diagram for Euclid::FilePool::FileManager:
Collaboration graph
[legend]

Public Types

using FileId = intptr_t
 Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
 

Public Member Functions

 FileManager ()
 Constructor.
 
virtual ~FileManager ()
 Destructor.
 
std::shared_ptr< FileHandlergetFileHandler (const boost::filesystem::path &path)
 
template<typename TFD >
std::pair< FileId, TFD > open (const boost::filesystem::path &path, bool write, std::function< bool(FileId)> request_close)
 
template<typename TFD >
void close (FileId id, TFD &fd)
 
void closeAll ()
 
virtual void notifyUsed (FileId id)
 
bool hasHandler (const boost::filesystem::path &path) const
 
- Public Member Functions inherited from std::enable_shared_from_this< FileManager >
enable_shared_from_this (T... args)
 
operator= (T... args)
 
shared_from_this (T... args)
 
~enable_shared_from_this (T... args)
 

Static Public Member Functions

static std::shared_ptr< FileManagergetDefault ()
 

Protected Types

using Clock = std::chrono::steady_clock
 
using Timestamp = Clock::time_point
 

Protected Member Functions

virtual void notifyIntentToOpen (bool write)=0
 
virtual void notifyOpenedFile (FileId)=0
 
virtual void notifyClosedFile (FileId)=0
 

Protected Attributes

std::mutex m_mutex
 
std::map< boost::filesystem::path, std::weak_ptr< FileHandler > > m_handlers
 
std::map< FileId, std::unique_ptr< FileMetadata > > m_files
 

Detailed Description

Provide an open/close interface to FileHandler. Concrete policies must inherit this interface and implement the notify* methods.

Definition at line 59 of file FileManager.h.

Member Typedef Documentation

◆ Clock

Definition at line 159 of file FileManager.h.

◆ FileId

Opaque FileId, its concrete type should only be assumed to be copyable and hashable.

Definition at line 65 of file FileManager.h.

◆ Timestamp

using Euclid::FilePool::FileManager::Timestamp = Clock::time_point
protected

Definition at line 160 of file FileManager.h.

Constructor & Destructor Documentation

◆ FileManager()

Euclid::FilePool::FileManager::FileManager ( )

Constructor.

Definition at line 64 of file FileManager.cpp.

◆ ~FileManager()

Euclid::FilePool::FileManager::~FileManager ( )
virtual

Destructor.

Definition at line 66 of file FileManager.cpp.

Member Function Documentation

◆ close()

template<typename TFD >
void Euclid::FilePool::FileManager::close ( FileId  id,
TFD &  fd 
)

Close a file

Parameters
idThe id returned by open

Referenced by Euclid::FilePool::FileHandler::TypedFdWrapper< TFD >::close().

◆ closeAll()

void Euclid::FilePool::FileManager::closeAll ( )

Close all closeable file descriptors

Warning
Concrete implementations must call this on their destructors.

Definition at line 75 of file FileManager.cpp.

References std::back_inserter(), std::map< K, T >::begin(), std::map< K, T >::end(), m_files, m_mutex, and std::transform().

Referenced by Euclid::FilePool::LRUFileManager::~LRUFileManager().

Here is the call graph for this function:

◆ getDefault()

std::shared_ptr< FileManager > Euclid::FilePool::FileManager::getDefault ( )
static
Returns
A default FileManager implementation

Definition at line 59 of file FileManager.cpp.

◆ getFileHandler()

std::shared_ptr< FileHandler > Euclid::FilePool::FileManager::getFileHandler ( const boost::filesystem::path &  path)

Get a file handler

Template Parameters
TFDFile descriptor type
Parameters
pathFile path
Returns
A FileHandler for the given file and with the requested file descriptor type

If there is already a FileHandler<TFD> for the given path, this will return the same shared pointer as already in use. The FileHandler is thread-safe, so this is OK. The path is normalized (no symlinks and no '.' or '..'), so this holds true even if the same file is specified in different manners.

Warning
The above is not true for hardlinks. If the same file is referenced by different paths that are hardlinks to the same file, it will return different handlers, so there will be no read/write protection in place.
Exceptions
Elements::ExceptionIf there is already a FileHandler with a different file descriptor type.

Definition at line 93 of file FileManager.cpp.

References std::weak_ptr< T >::lock(), m_handlers, m_mutex, std::enable_shared_from_this< FileManager >::shared_from_this(), and weakly_canonical().

Here is the call graph for this function:

◆ hasHandler()

bool Euclid::FilePool::FileManager::hasHandler ( const boost::filesystem::path &  path) const
Returns
True if the path has an associated handler

Definition at line 120 of file FileManager.cpp.

References m_handlers, m_mutex, and weakly_canonical().

Here is the call graph for this function:

◆ notifyClosedFile()

virtual void Euclid::FilePool::FileManager::notifyClosedFile ( FileId  )
protectedpure virtual

◆ notifyIntentToOpen()

virtual void Euclid::FilePool::FileManager::notifyIntentToOpen ( bool  write)
protectedpure virtual

◆ notifyOpenedFile()

virtual void Euclid::FilePool::FileManager::notifyOpenedFile ( FileId  )
protectedpure virtual

◆ notifyUsed()

void Euclid::FilePool::FileManager::notifyUsed ( FileId  id)
virtual

Notify that the given file has been/is going to be used. This will update the book-keeping data used to decide what to close when.

Reimplemented in Euclid::FilePool::LRUFileManager.

Definition at line 68 of file FileManager.cpp.

References std::chrono::steady_clock::now().

Here is the call graph for this function:

◆ open()

template<typename TFD >
std::pair< FileId, TFD > Euclid::FilePool::FileManager::open ( const boost::filesystem::path &  path,
bool  write,
std::function< bool(FileId)>  request_close 
)

Open a file

Template Parameters
TFDFile descriptor type.
Parameters
pathFile path
writeTrue if the file is to be opened in write mode
request_closeThe manager will call this function when it needs to close the file descriptor, so whoever called open can put everything in order. The callback can return "false" if the given FileId can not be closed (i.e. it is still in use). The callback is responsible for calling close.
Returns
A pair FileId, FileDescriptor
Note
An specialization of OpenCloseTrait must exists for TFD.
Warning
Files will be closed when the FileManager is destroyed. Make sure that the information required by the callback lives longer than the FileManager. You do not need to care about this when using FileHandlers

Why the callback? The FilePool is designed to have single ownership of the file descriptors, so it can work with non-copyable file types. Files are either owned by the FileHandler (if not in use) or by the FileAccessor (if in use). Therefore, it can only let the owner know the file should be closed. Again, this caveat is unimportant if the access is done via FileHandlers.

Member Data Documentation

◆ m_files

std::map<FileId, std::unique_ptr<FileMetadata> > Euclid::FilePool::FileManager::m_files
protected

Map a file id to its metadata

Definition at line 176 of file FileManager.h.

Referenced by closeAll(), and Euclid::FilePool::LRUFileManager::notifyIntentToOpen().

◆ m_handlers

std::map<boost::filesystem::path, std::weak_ptr<FileHandler> > Euclid::FilePool::FileManager::m_handlers
protected

Map path / handler

The value is a std::weak_ptr because we are not really interested on keeping a handler alive if no one is using it. However, if someone has a handler pointing to a file alive, and someone else wants a handler to the same file, it should get the same handler.

Definition at line 171 of file FileManager.h.

Referenced by getFileHandler(), and hasHandler().

◆ m_mutex

std::mutex Euclid::FilePool::FileManager::m_mutex
mutableprotected

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