Sparse Virtual File System  0.4.1
A Sparse Virtual File System.
svfs.h
Go to the documentation of this file.
1 
32 #ifndef CPPSVF_SVFS_H
33 #define CPPSVF_SVFS_H
34 
35 #include <string>
36 #include <unordered_map>
37 
38 #ifdef SVFS_THREAD_SAFE
39 
40 #include <mutex>
41 
42 #endif
43 
44 
45 #include "svf.h"
46 
47 namespace SVFS {
48 
49 #pragma mark - Exceptions
50 
51  namespace Exceptions {
52 
54  class ExceptionSparseVirtualFileSystem : public std::exception {
55  public:
56  explicit ExceptionSparseVirtualFileSystem(const std::string &in_msg) : msg(in_msg) {}
57 
58  [[nodiscard]] const std::string &message() const { return msg; }
59 
60  protected:
61  std::string msg;
62  };
63 
66  public:
67  explicit ExceptionSparseVirtualFileSystemOutOfRange(const std::string &msg)
69  msg) {}
70  };
71 
74  public:
76  msg) {}
77  };
78 
81  public:
83  msg) {}
84  };
85 
86  } // namespace Exceptions {
87 
88 #pragma mark - The SVFS class
89 
95  public:
98  m_config(config) {}
99 
100  // Insert a new SVF
101  void insert(const std::string &id, double mod_time);
102 
103  // Remove a specific SVF.
104  void remove(const std::string &id);
105 
106  // May raise an ExceptionSparseVirtualFileSystemOutOfRange
107  [[nodiscard]] const SparseVirtualFile &at(const std::string &id) const;
108 
109  // May raise an ExceptionSparseVirtualFileSystemOutOfRange
110  [[nodiscard]] SparseVirtualFile &at(const std::string &id);
111 
112  // Has an SVF
113  [[nodiscard]] bool has(const std::string &id) const noexcept { return m_svfs.find(id) != m_svfs.end(); }
114 
115  // Number of SVFs
116  [[nodiscard]] size_t size() const noexcept { return m_svfs.size(); }
117 
118  // Total estimated memory usage.
119  [[nodiscard]] size_t size_of() const noexcept;
120 
121  // Total number of file bytes.
122  [[nodiscard]] size_t num_bytes() const noexcept;
123 
124  // Total number of file blocks.
125  [[nodiscard]] size_t num_blocks() const noexcept;
126 
127  // All the SVF IDs.
128  [[nodiscard]] std::vector<std::string> keys() const noexcept;
129 
131  [[nodiscard]] const tSparseVirtualFileConfig &config() const noexcept { return m_config; }
132 
135 
138 
139  ~SparseVirtualFileSystem() noexcept;
140 
141  protected:
143  std::unordered_map<std::string, SparseVirtualFile> m_svfs;
146 #ifdef SVFS_THREAD_SAFE
149  mutable std::mutex m_mutex;
150 #endif
151  };
152 }
153 
154 #endif //CPPSVF_SVFS_H
Exception specialisation for the SparseVirtualFileSystem.
Definition: svfs.h:54
ExceptionSparseVirtualFileSystem(const std::string &in_msg)
Definition: svfs.h:56
const std::string & message() const
Definition: svfs.h:58
Exception specialisation on insert error.
Definition: svfs.h:73
ExceptionSparseVirtualFileSystemInsert(const std::string &msg)
Definition: svfs.h:75
Exception specialisation for out of range error.
Definition: svfs.h:65
ExceptionSparseVirtualFileSystemOutOfRange(const std::string &msg)
Definition: svfs.h:67
Exception specialisation on remove error.
Definition: svfs.h:80
ExceptionSparseVirtualFileSystemRemove(const std::string &msg)
Definition: svfs.h:82
Implementation of a Sparse Virtual File.
Definition: svf.h:288
A SparseVirtualFileSystem is a key/value store where the key is a file ID as a string and the value i...
Definition: svfs.h:94
size_t size() const noexcept
Definition: svfs.h:116
const SparseVirtualFile & at(const std::string &id) const
Return the const SparseVirtualFile at the given ID.
Definition: svfs.cpp:88
void insert(const std::string &id, double mod_time)
Inserts a new SparseVirtualFile corresponding to the given ID and file modification timestamp.
Definition: svfs.cpp:44
tSparseVirtualFileConfig m_config
The configuration for all SVF values.
Definition: svfs.h:145
SparseVirtualFileSystem(const SparseVirtualFileSystem &rhs)=delete
Eliminate copying.
SparseVirtualFileSystem(const tSparseVirtualFileConfig &config=tSparseVirtualFileConfig())
Constructor takes a tSparseVirtualFileConfig that is passed to every new SparseVirtualFile.
Definition: svfs.h:97
size_t num_blocks() const noexcept
Returns the total number of blocks in the SparseVirtualFileSystem.
Definition: svfs.cpp:139
size_t num_bytes() const noexcept
Returns the total number of readable bytes in the SparseVirtualFileSystem.
Definition: svfs.cpp:127
const tSparseVirtualFileConfig & config() const noexcept
The configuration.
Definition: svfs.h:131
std::unordered_map< std::string, SparseVirtualFile > m_svfs
The key/value store of SVF values.
Definition: svfs.h:143
std::vector< std::string > keys() const noexcept
Return all the SVF IDs (unordered).
Definition: svfs.cpp:151
~SparseVirtualFileSystem() noexcept
Destructor.
Definition: svfs.cpp:161
bool has(const std::string &id) const noexcept
Definition: svfs.h:113
void remove(const std::string &id)
Remove the SparseVirtualFile corresponding to the given ID.
Definition: svfs.cpp:66
size_t size_of() const noexcept
Returns the total in-memory size of the SparseVirtualFileSystem structure in bytes.
Definition: svfs.cpp:115
SparseVirtualFileSystem operator=(const SparseVirtualFileSystem &rhs)=delete
Eliminate copying.
The namespace for all svfsc code.
Definition: svf.cpp:41
struct SVFS::SparseVirtualFileConfig tSparseVirtualFileConfig
Configuration for the Sparse Virtual File.
Configuration for the Sparse Virtual File.
Definition: svf.h:262