Sparse Virtual File System  0.4.0
A Sparse Virtual File System.
svfs.cpp
Go to the documentation of this file.
1 
32 #include <sstream>
33 #include <utility>
34 
35 #include "svfs.h"
36 
37 namespace SVFS {
38 
44  void SparseVirtualFileSystem::insert(const std::string &id, double mod_time) {
45 #ifdef SVFS_THREAD_SAFE
46  std::lock_guard<std::mutex> mutex(m_mutex);
47 #endif
48  auto result = m_svfs.emplace(std::piecewise_construct,
49  std::forward_as_tuple(id),
50  std::forward_as_tuple(id, mod_time, m_config));
51  if (! result.second) {
52  // Error, insertion failed
53  std::ostringstream os;
54  os << "SparseVirtualFileSystem::insert():";
55  os << " can not insert \"" << id << "\"";
57  }
58  }
59 
66  void SparseVirtualFileSystem::remove(const std::string &id) {
67 #ifdef SVFS_THREAD_SAFE
68  std::lock_guard<std::mutex> mutex(m_mutex);
69 #endif
70  auto iter = m_svfs.find(id);
71  if (iter == m_svfs.end()) {
72  std::ostringstream os;
73  os << "SparseVirtualFileSystem::remove():";
74  os << " id \"" << id << "\" not found.";
76  } else {
77  m_svfs.erase(iter);
78  }
79  }
80 
88  const SparseVirtualFile &SparseVirtualFileSystem::at(const std::string &id) const {
89  try {
90  return m_svfs.at(id);
91  } catch (std::out_of_range &err) {
93  }
94  }
95 
104  try {
105  return m_svfs.at(id);
106  } catch (std::out_of_range &err) {
108  }
109  }
110 
115  size_t SparseVirtualFileSystem::size_of() const noexcept {
116  size_t ret = sizeof(SparseVirtualFileSystem);
117  for (auto &iter: m_svfs) {
118  ret += iter.second.size_of();
119  }
120  return ret;
121  }
122 
127  size_t SparseVirtualFileSystem::num_bytes() const noexcept {
128  size_t ret = 0;
129  for (auto &iter: m_svfs) {
130  ret += iter.second.num_bytes();
131  }
132  return ret;
133  }
134 
139  size_t SparseVirtualFileSystem::num_blocks() const noexcept {
140  size_t ret = 0;
141  for (auto &iter: m_svfs) {
142  ret += iter.second.num_blocks();
143  }
144  return ret;
145  }
146 
151  std::vector<std::string> SparseVirtualFileSystem::keys() const noexcept {
152  std::vector<std::string> ret;
153  ret.reserve(m_svfs.size());
154  for(const auto &iter: m_svfs) {
155  ret.push_back(iter.first);
156  }
157  return ret;
158  }
159 
162  for (auto &iter: m_svfs) {
163  iter.second.clear();
164  }
165  m_svfs.clear();
166  }
167 
168 }
Exception specialisation on insert error.
Definition: svfs.h:73
Exception specialisation for out of range error.
Definition: svfs.h:65
Exception specialisation on remove error.
Definition: svfs.h:80
Implementation of a Sparse Virtual File.
Definition: svf.h:288
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 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
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
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
The namespace for all svfsc code.
Definition: svf.cpp:41