RTC Toolkit  1.0.0
fileRepository.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_FILEREPOSITORY_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_FILEREPOSITORY_HPP
14 
15 #include <exception>
16 #include <functional>
17 #include <mutex>
18 #include <thread>
19 
20 #include <boost/filesystem.hpp>
21 #include <mal/utility/Uri.hpp>
22 
25 
26 namespace rtctk::componentFramework {
27 
29 public:
30  explicit PathMissingException(const std::string& path);
31 };
32 
34 public:
35  explicit FileFormatError(const boost::filesystem::path& filepath);
36 };
37 
39 public:
40  explicit InvalidUriInFileError(const boost::filesystem::path& filepath, const std::string& uri);
41 };
42 
44 public:
45  explicit DirCreationError(const boost::filesystem::path& path);
46 };
47 
68 class FileRepository : virtual public RepositoryIf {
69 public:
70  FileRepository() = delete;
71  FileRepository(const FileRepository&) = delete;
73  explicit FileRepository(const elt::mal::Uri& datauri, const int dirdepth);
74  virtual ~FileRepository() noexcept = default;
75 
77  void CreateDataPoint(const DataPointPath& path, const std::type_info& type) override;
78 
79  void DeleteDataPoint(const DataPointPath& path) override;
80  const std::type_info& GetDataPointType(const DataPointPath& path) const override;
81  size_t GetDataPointSize(const DataPointPath& path) const override;
82  bool DataPointExists(const DataPointPath& path) const override;
83  std::pair<StringList, StringList> GetChildren(const DataPointPath& path) const override;
84  Response SendReadRequest(const Request& request) const override;
85  Response SendWriteRequest(const Request& request) override;
86 
94  inline size_t GetFitsWriteThreshold() const {
95  std::scoped_lock lck(m_mutex);
96  return m_fits_write_threshold;
97  }
98 
109  inline void SetFitsWriteThreshold(size_t value) {
110  std::scoped_lock lck(m_mutex);
111  m_fits_write_threshold = value;
112  }
113 
114 private:
130  template <typename F>
131  Response ProcessRequest(const Request& request, F selector) const;
132 
141  template <typename T>
142  void ReadData(const DataPointPath& path, T& buffer, const std::string& typestr) const;
143 
152  template <typename T>
153  void WriteData(const DataPointPath& path, const T& buffer, const std::string& typestr) const;
154 
156  struct IoFunctionMapEntry {
157  using ReadFunction =
158  std::function<void(const FileRepository*, const DataPointPath&, void*)>;
159  using WriteFunction =
160  std::function<void(const FileRepository*, const DataPointPath&, const void*)>;
161 
162  ReadFunction m_read_function;
163  WriteFunction m_write_function;
164  };
165 
167  using IoFunctionMap = std::map<std::type_index, IoFunctionMapEntry>;
168 
177  template <typename T>
178  static auto MakeMapEntry(const std::string& typestr);
179 
186  mutable std::mutex m_mutex;
187  boost::filesystem::path m_datapath;
188  std::string m_defaultfile;
189  int m_dirdepth;
190  size_t m_fits_write_threshold;
191 
193  static const IoFunctionMap S_IO_FUNCTIONS;
194 };
195 
196 } // namespace rtctk::componentFramework
197 
198 #endif // RTCTK_COMPONENTFRAMEWORK_FILEREPOSITORY_HPP
rtctk::componentFramework::FileRepository::DeleteDataPoint
void DeleteDataPoint(const DataPointPath &path) override
Deletes a datapoint.
Definition: fileRepository.cpp:869
exceptions.hpp
Provides macros and utilities for exception handling.
rtctk::componentFramework::InvalidUriInFileError::InvalidUriInFileError
InvalidUriInFileError(const boost::filesystem::path &filepath, const std::string &uri)
Definition: fileRepository.cpp:771
rtctk::componentFramework::DirCreationError::DirCreationError
DirCreationError(const boost::filesystem::path &path)
Definition: fileRepository.cpp:778
rtctk::componentFramework::PathMissingException
Definition: fileRepository.hpp:28
rtctk::componentFramework::FileRepository::FileRepository
FileRepository(const FileRepository &)=delete
rtctk::componentFramework::RepositoryIf::Request
A request object to pass information about datapoints that should be read (written) from (to) the rep...
Definition: repositoryIf.hpp:45
rtctk::componentFramework::FileRepository
Implements a file based repository that stores datapoints in local YAML and FITS files.
Definition: fileRepository.hpp:68
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::FileFormatError
Definition: fileRepository.hpp:33
rtctk::componentFramework::PathMissingException::PathMissingException
PathMissingException(const std::string &path)
Definition: fileRepository.cpp:763
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::componentFramework::FileRepository::operator=
FileRepository & operator=(const FileRepository &)=delete
rtctk::componentFramework::FileRepository::GetFitsWriteThreshold
size_t GetFitsWriteThreshold() const
Definition: fileRepository.hpp:94
rtctk::componentFramework::FileRepository::GetDataPointType
const std::type_info & GetDataPointType(const DataPointPath &path) const override
Fetches the type of the datapoint.
Definition: fileRepository.cpp:913
rtctk::componentFramework::FileRepository::FileRepository
FileRepository()=delete
repositoryIf.hpp
Header file for RepositoryIf and related base classes.
rtctk::componentFramework::FileRepository::CreateDataPoint
virtual void CreateDataPoint(const DataPointPath &path, const std::type_info &type)=0
Creates a new datapoint in the repository with a specified type.
rtctk::componentFramework::RepositoryIf::StringList
std::vector< std::string > StringList
Definition: repositoryIf.hpp:37
rtctk::componentFramework::FileRepository::SetFitsWriteThreshold
void SetFitsWriteThreshold(size_t value)
Sets the threshold for storing numerical matrices and vectors in FITS files rather than the YAML file...
Definition: fileRepository.hpp:109
rtctk::componentFramework::RepositoryIf
Abstract interface providing basic read and write facilities to a repository.
Definition: repositoryIf.hpp:35
rtctk::componentFramework::FileRepository::SendReadRequest
Response SendReadRequest(const Request &request) const override
Sends a request to read data from the repository.
Definition: fileRepository.cpp:1075
rtctk::componentFramework::DirCreationError
Definition: fileRepository.hpp:43
rtctk::componentFramework::FileRepository::GetDataPointSize
size_t GetDataPointSize(const DataPointPath &path) const override
Fetches the size of the datapoint's data.
Definition: fileRepository.cpp:945
rtctk::componentFramework::FileRepository::~FileRepository
virtual ~FileRepository() noexcept=default
std
Definition: mudpiProcessingError.hpp:119
rtctk::componentFramework::FileFormatError::FileFormatError
FileFormatError(const boost::filesystem::path &filepath)
Definition: fileRepository.cpp:767
rtctk::componentFramework::DataPointPath
This class provides a wraper for DataPoint paths which ensures that they only contain valid character...
Definition: dataPointPath.hpp:34
rtctk::componentFramework::RepositoryIf::Response
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:106
rtctk::componentFramework::FileRepository::DataPointExists
bool DataPointExists(const DataPointPath &path) const override
Checks for the existence of a datapoint in the repository.
Definition: fileRepository.cpp:991
rtctk::componentFramework::InvalidUriInFileError
Definition: fileRepository.hpp:38
rtctk::componentFramework::FileRepository::GetChildren
std::pair< StringList, StringList > GetChildren(const DataPointPath &path) const override
Queries the datapoints and child paths for a given path.
Definition: fileRepository.cpp:1012
rtctk::componentFramework::FileRepository::SendWriteRequest
Response SendWriteRequest(const Request &request) override
Sends a request to write data to the repository.
Definition: fileRepository.cpp:1079