Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
SerializerImpl.h
Go to the documentation of this file.
1 //===-- serialbox/core/SerializerImpl.h ---------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #ifndef SERIALBOX_CORE_SERIALIZERIMPL_H
16 #define SERIALBOX_CORE_SERIALIZERIMPL_H
17 
20 #include "serialbox/core/Json.h"
25 #include <iosfwd>
26 
27 namespace serialbox {
28 
31 
37 public:
50  static int serializationStatus() noexcept { return enabled_; }
51 
61  static void enableSerialization() noexcept { enabled_ = 1; }
62 
72  static void disableSerialization() noexcept { enabled_ = -1; }
73 
75  SerializerImpl(const SerializerImpl&) = delete;
76 
78  SerializerImpl(SerializerImpl&&) = default;
79 
81  SerializerImpl& operator=(const SerializerImpl&) = delete;
82 
85 
98  SerializerImpl(OpenModeKind mode, const std::string& directory, const std::string& prefix,
99  const std::string& archiveName);
100 
102  OpenModeKind mode() const noexcept { return mode_; }
103 
105  const filesystem::path& directory() const noexcept { return directory_; }
106 
108  std::string prefix() const noexcept { return prefix_; }
109 
111  std::string archiveName() const noexcept { return archive_->name(); }
112 
114  const filesystem::path& metaDataFile() const noexcept { return metaDataFile_; }
115 
119  void clear() noexcept;
120 
121  //===----------------------------------------------------------------------------------------===//
122  // Global Meta-Information
123  //===----------------------------------------------------------------------------------------===//
124 
131  template <class StringType, class ValueType>
132  void addGlobalMetainfo(StringType&& key, ValueType&& value) {
133  if(!globalMetainfo_->insert(std::forward<StringType>(key), std::forward<ValueType>(value)))
134  throw Exception("cannot add element with key '%s' to globalMetainfo: element already exists",
135  key);
136  }
137 
144  template <class T, class StringType>
145  T getGlobalMetainfoAs(StringType&& key) const {
146  try {
147  return globalMetainfo_->at(key).template as<T>();
148  } catch(Exception& e) {
149  throw Exception("cannot get element with key '%s' from globalMetainfo: %s", key, e.what());
150  }
151  }
152 
154  MetainfoMapImpl& globalMetainfo() noexcept { return *globalMetainfo_; }
155  const MetainfoMapImpl& globalMetainfo() const noexcept { return *globalMetainfo_; }
156 
158  std::shared_ptr<MetainfoMapImpl>& globalMetainfoPtr() noexcept { return globalMetainfo_; }
159  const std::shared_ptr<MetainfoMapImpl>& globalMetainfoPtr() const noexcept {
160  return globalMetainfo_;
161  }
162 
163  //===----------------------------------------------------------------------------------------===//
164  // Register and Query Fields
165  //===----------------------------------------------------------------------------------------===//
166 
173  template <class StringType, typename... Args>
174  void registerField(StringType&& name, Args&&... args) {
175  if(!fieldMap_->insert(std::forward<StringType>(name), std::forward<Args>(args)...))
176  throw Exception("cannot register field '%s': field already exists", name);
177  }
178 
183  template <class StringType>
184  bool hasField(StringType&& name) const noexcept {
185  return fieldMap_->hasField(std::forward<StringType>(name));
186  }
187 
196  template <class StringType, class KeyType, class ValueType>
197  bool addFieldMetainfoImpl(StringType&& name, KeyType&& key, ValueType&& value) {
198  return fieldMap_->getMetainfoOf(name).insert(std::forward<KeyType>(key),
199  std::forward<ValueType>(value));
200  }
201 
207  template <class StringType>
208  const FieldMetainfoImpl& getFieldMetainfoImplOf(StringType&& name) const {
209  return fieldMap_->getFieldMetainfoImplOf(std::forward<StringType>(name));
210  }
211 
214  std::vector<std::string> fieldnames() const;
215 
217  FieldMap& fieldMap() noexcept { return *fieldMap_; }
218  const FieldMap& fieldMap() const noexcept { return *fieldMap_; }
219 
221  std::shared_ptr<FieldMap>& fieldMapPtr() noexcept { return fieldMap_; }
222  const std::shared_ptr<FieldMap>& fieldMapPtr() const noexcept { return fieldMap_; }
223 
224  //===----------------------------------------------------------------------------------------===//
225  // Register and Query Savepoints
226  //===----------------------------------------------------------------------------------------===//
227 
232  template <typename... Args>
233  bool registerSavepoint(Args&&... args) noexcept {
234  return (savepointVector_->insert(SavepointImpl(std::forward<Args>(args)...)) != -1);
235  }
236 
239  bool addFieldToSavepoint(const SavepointImpl& savepoint, const FieldID& fieldID) noexcept {
240  return savepointVector_->addField(savepoint, fieldID);
241  }
242 
246  FieldID getFieldIDAtSavepoint(const SavepointImpl& savepoint, const std::string& field) const {
247  return savepointVector_->getFieldID(savepoint, field);
248  }
249 
252  return savepointVector_->savepoints();
253  }
255  return savepointVector_->savepoints();
256  }
257 
259  const SavepointVector& savepointVector() const noexcept { return *savepointVector_; }
260  SavepointVector& savepointVector() noexcept { return *savepointVector_; }
261 
263  const std::shared_ptr<SavepointVector>& savepointVectorPtr() const noexcept {
264  return savepointVector_;
265  }
266  std::shared_ptr<SavepointVector>& savepointVectorPtr() noexcept { return savepointVector_; }
267 
268  //===----------------------------------------------------------------------------------------===//
269  // Writing
270  //===----------------------------------------------------------------------------------------===//
271 
299  void write(const std::string& name, const SavepointImpl& savepoint,
300  const StorageView& storageView);
301 
302  //===----------------------------------------------------------------------------------------===//
303  // Reading
304  //===----------------------------------------------------------------------------------------===//
305 
326  void read(const std::string& name, const SavepointImpl& savepoint, StorageView& storageView, bool alsoPrevious = false);
327 
340  void readSliced(const std::string& name, const SavepointImpl& savepoint, StorageView& storageView,
341  Slice slice);
342 
363  void readAsync(const std::string& name, const SavepointImpl& savepoint, StorageView& storageView);
364 
366  void waitForAll();
367 
368  //===----------------------------------------------------------------------------------------===//
369  // JSON Serialization
370  //===----------------------------------------------------------------------------------------===//
371 
377  void updateMetaData();
378 
380  json::json toJSON() const;
381 
383  std::string toString() const;
384 
386  friend std::ostream& operator<<(std::ostream& stream, const SerializerImpl& s);
387 
388 protected:
394 
400  void constructArchive(const std::string& archiveName);
401 
406  std::shared_ptr<FieldMetainfoImpl> checkStorageView(const std::string& name,
407  const StorageView& storageView) const;
408 
422  bool upgradeMetaData();
423 
425  void readAsyncImpl(const std::string name, const SavepointImpl savepoint,
426  StorageView storageView);
427 
428 protected:
429  OpenModeKind mode_;
430  filesystem::path directory_;
431  filesystem::path metaDataFile_;
432  std::string prefix_;
433 
434  std::shared_ptr<SavepointVector> savepointVector_;
435  std::shared_ptr<FieldMap> fieldMap_;
436  std::shared_ptr<MetainfoMapImpl> globalMetainfo_;
437 
438  std::unique_ptr<Archive> archive_;
439 
440  // This variable can take three values:
441  //
442  // 0: the variable is not yet initialized -> the serialization is enabled if the environment
443  // variable SERIALBOX_SERIALIZATION_DISABLE is not set to a positive value. The first
444  // serializer which is initialized has to set this value either to +1 or to -1 according to
445  // the environment.
446  // +1: the serialization is enabled, independently of the environment
447  // -1: the serialization is disabled, independently of the environment
448  //
449  // The value is initialized to 0
450  static int enabled_;
451 };
452 
454 
455 } // namespace serialbox
456 
457 #endif
const std::shared_ptr< SavepointVector > & savepointVectorPtr() const noexcept
Get pointer to SavepointVector.
void readAsync(const std::string &name, const SavepointImpl &savepoint, StorageView &storageView)
Asynchronously deserialize field name (given as storageView) at savepoint from disk using std::async...
void registerField(StringType &&name, Args &&... args)
Register a new field within the Serializer.
std::vector< std::shared_ptr< SavepointImpl > > savepoint_vector_type
Vector of savepoints.
std::shared_ptr< MetainfoMapImpl > & globalMetainfoPtr() noexcept
Get pointer to the global meta information.
const SavepointVector & savepointVector() const noexcept
Get refrence to SavepointVector.
OpenModeKind mode() const noexcept
Access the mode of the serializer.
void constructArchive(const std::string &archiveName)
Construct Archive from JSON.
Meta-information of a data field.
void updateMetaData()
Convert meta-data to JSON and serialize to MetaData-prefix.json and ArchiveMetaData-prefix.json.
static int serializationStatus() noexcept
Get the status of serialization.
void readAsyncImpl(const std::string name, const SavepointImpl savepoint, StorageView storageView)
Implementation of SerializerImpl::readAsync.
void addGlobalMetainfo(StringType &&key, ValueType &&value)
Add a new key-value pair to the global meta-information of the Serializer.
friend std::ostream & operator<<(std::ostream &stream, const SerializerImpl &s)
Convert to stream.
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
const SavepointVector::savepoint_vector_type & savepoints() const noexcept
Get refrence to savepoint vector.
FieldID getFieldIDAtSavepoint(const SavepointImpl &savepoint, const std::string &field) const
Get the FielID of field field at savepoint savepoint
MetainfoMapImpl & globalMetainfo() noexcept
Get a refrence to the global meta information.
std::shared_ptr< FieldMap > & fieldMapPtr() noexcept
Get pointer to the field map.
Namespace of the serialbox library.
Definition: Archive.h:25
static void disableSerialization() noexcept
Disable serialization.
std::string toString() const
Convert to string.
std::shared_ptr< FieldMetainfoImpl > checkStorageView(const std::string &name, const StorageView &storageView) const
Check if storageView is consistent with the field name
bool upgradeMetaData()
Check if the current directory contains meta-information of an older version of serialbox and upgrade...
Shared implementation of the Serializer.
Uniquely identifiy a field.
Definition: FieldID.h:27
const filesystem::path & metaDataFile() const noexcept
Access the path to the meta-data file.
bool addFieldToSavepoint(const SavepointImpl &savepoint, const FieldID &fieldID) noexcept
Add a field to the savepoint.
The SavepointVector manages the registered savepoints and their mapping to the stored fields...
std::vector< std::string > fieldnames() const
Get a vector of all registered fields.
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
void constructMetaDataFromJson()
Construct meta-data from JSON.
SerializerImpl & operator=(const SerializerImpl &)=delete
Copy assignment [deleted].
const filesystem::path & directory() const noexcept
Access the directory in which the Serializer and Archive are opened.
void write(const std::string &name, const SavepointImpl &savepoint, const StorageView &storageView)
Serialize field name (given as storageView) at savepoint to disk.
T getGlobalMetainfoAs(StringType &&key) const
Query globalMetainfo map for key key and retrieve value as type T
OpenModeKind
Policy for opening files in the Serializer and Archive.
Definition: Type.h:40
static void enableSerialization() noexcept
Enable serialization.
std::string prefix() const noexcept
Access prefix of all filenames.
serialbox::Slice slice
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:43
json::json toJSON() const
Convert all members of the Serializer to JSON.
void readSliced(const std::string &name, const SavepointImpl &savepoint, StorageView &storageView, Slice slice)
Deserialize sliced field name (given as storageView and slice) at savepoint from disk.
std::string archiveName() const noexcept
Name of the archive in use.
bool registerSavepoint(Args &&... args) noexcept
Register a savepoint.
const FieldMetainfoImpl & getFieldMetainfoImplOf(StringType &&name) const
Query FieldMap for field with name and return a refrence to FieldMetainfoImpl.
bool hasField(StringType &&name) const noexcept
Check if field name has been registred within the Serializer.
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:53
Exception class which stores a human-readable error description.
Definition: Exception.h:30
void clear() noexcept
Drop all field and savepoint meta-data.
SerializerImpl(const SerializerImpl &)=delete
Copy constructor [deleted].
Hash-map to query the meta-information of the registered fields.
Definition: FieldMap.h:28
Shared implementation of the Savepoint.
Definition: SavepointImpl.h:38
FieldMap & fieldMap() noexcept
Get refrence to the field map.
void read(const std::string &name, const SavepointImpl &savepoint, StorageView &storageView, bool alsoPrevious=false)
Deserialize field name (given as storageView) at savepoint from disk.
void waitForAll()
Wait for all pending asynchronous read operations and reset the internal queue.
bool addFieldMetainfoImpl(StringType &&name, KeyType &&key, ValueType &&value)
Add key = value or key = {value1, ..., valueN} meta-information to field name