Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
BinaryArchive.h
Go to the documentation of this file.
1 //===-- serialbox/core/archive/BinaryArchive.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_ARCHIVE_BINARYARCHIVE_H
16 #define SERIALBOX_CORE_ARCHIVE_BINARYARCHIVE_H
17 
20 #include "serialbox/core/Json.h"
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 namespace serialbox {
28 
32 class BinaryArchive : public Archive {
33 public:
35  static const std::string Name;
36 
38  static const int Version;
39 
41  struct FileOffsetType {
42  std::streamoff offset;
43  std::string checksum;
44  };
45 
47  using FieldOffsetTable = std::vector<FileOffsetType>;
48 
50  using FieldTable = std::unordered_map<std::string, FieldOffsetTable>;
51 
53  BinaryArchive();
54 
67  BinaryArchive(OpenModeKind mode, const std::string& directory, const std::string& prefix,
68  bool skipMetaData = false);
69 
71  BinaryArchive(const BinaryArchive&) = delete;
72 
74  BinaryArchive& operator=(const BinaryArchive&) = delete;
75 
77  virtual ~BinaryArchive();
78 
80  void readMetaDataFromJson();
81 
83  void writeMetaDataToJson();
84 
88  virtual FieldID write(const StorageView& storageView, const std::string& fieldID,
89  const std::shared_ptr<FieldMetainfoImpl> info) override;
90 
91  virtual void read(StorageView& storageView, const FieldID& fieldID,
92  std::shared_ptr<FieldMetainfoImpl> info) const override;
93 
94  virtual void updateMetaData() override;
95 
96  virtual OpenModeKind mode() const override { return mode_; }
97 
98  virtual std::string directory() const override { return directory_.string(); }
99 
100  virtual std::string prefix() const override { return prefix_; }
101 
102  virtual std::string name() const override { return BinaryArchive::Name; }
103 
104  virtual std::string metaDataFile() const override { return metaDatafile_.string(); }
105 
106  virtual std::ostream& toStream(std::ostream& stream) const override;
107 
108  virtual void clear() override;
109 
110  virtual bool isReadingThreadSafe() const override { return true; }
111 
112  virtual bool isWritingThreadSafe() const override { return false; }
113 
114  virtual bool isSlicedReadingSupported() const override { return true; }
115 
117 
119  void clearFieldTable();
120 
122  static std::unique_ptr<Archive> create(OpenModeKind mode, const std::string& directory,
123  const std::string& prefix);
124 
126  FieldTable& fieldTable() noexcept { return fieldTable_; }
127  const FieldTable& fieldTable() const noexcept { return fieldTable_; }
128 
134  static void writeToFile(std::string filename, const StorageView& storageView);
135 
142  static void readFromFile(std::string filename, StorageView& storageView);
143 
145  void setHash(std::unique_ptr<Hash> hash) noexcept { hash_ = std::move(hash); }
146 
148  const std::unique_ptr<Hash>& hash() const noexcept { return hash_; }
149 
150 private:
151  OpenModeKind mode_;
152  filesystem::path directory_;
153  std::string prefix_;
154 
155  filesystem::path metaDatafile_;
156  std::unique_ptr<Hash> hash_;
157  json::json json_;
158  FieldTable fieldTable_;
159 };
160 
161 } // namespace serialbox
162 
163 #endif
virtual std::string metaDataFile() const override
Full file path to the meta-data file.
virtual void clear() override
Clear the archive i.e remove all data from disk and reset the internal data-structures.
std::streamoff offset
Binary offset within the file.
Definition: BinaryArchive.h:42
static void writeToFile(std::string filename, const StorageView &storageView)
Directly write field (given by storageView) to file.
virtual std::ostream & toStream(std::ostream &stream) const override
Convert the archive to stream.
static const std::string Name
Name of the binary archive.
Definition: BinaryArchive.h:35
static void readFromFile(std::string filename, StorageView &storageView)
Directly read field (given by storageView) from file.
static const int Version
Revision of the binary archive.
Definition: BinaryArchive.h:38
virtual std::string directory() const override
Directory to write/read files.
Definition: BinaryArchive.h:98
std::vector< FileOffsetType > FieldOffsetTable
Table of ids and corresponding offsets whithin in each field (i.e file)
Definition: BinaryArchive.h:47
virtual bool isSlicedReadingSupported() const override
Indicate whether the archive supports StorageViews with attached slices.
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
FieldTable & fieldTable() noexcept
Get field table.
void setHash(std::unique_ptr< Hash > hash) noexcept
Set the hash algorithm.
BinaryArchive & operator=(const BinaryArchive &)=delete
Copy assignment [deleted].
Namespace of the serialbox library.
Definition: Archive.h:25
std::string checksum
Checksum of the field.
Definition: BinaryArchive.h:43
virtual void read(StorageView &storageView, const FieldID &fieldID, std::shared_ptr< FieldMetainfoImpl > info) const override
Read the field identified by fieldID and given by storageView from disk.
Uniquely identifiy a field.
Definition: FieldID.h:27
virtual std::string prefix() const override
Prefix of all files.
virtual void updateMetaData() override
Update the meta-data on disk.
virtual OpenModeKind mode() const override
Open-policy of the archive.
Definition: BinaryArchive.h:96
void readMetaDataFromJson()
Load meta-data from JSON file.
void writeMetaDataToJson()
Convert meta-data to JSON and serialize to file.
virtual bool isReadingThreadSafe() const override
Indicate whether it&#39;s safe for multiple threads to call Archive::read.
const std::unique_ptr< Hash > & hash() const noexcept
Get the hash algorithm.
void clearFieldTable()
Clear fieldTable.
std::unordered_map< std::string, FieldOffsetTable > FieldTable
Table of all fields owned by this archive, each field has a corresponding file.
Definition: BinaryArchive.h:50
Non-portable binary archive.
Definition: BinaryArchive.h:32
OpenModeKind
Policy for opening files in the Serializer and Archive.
Definition: Type.h:40
static std::unique_ptr< Archive > create(OpenModeKind mode, const std::string &directory, const std::string &prefix)
Create a BinaryArchive.
virtual std::string name() const override
Name of the archive.
virtual FieldID write(const StorageView &storageView, const std::string &fieldID, const std::shared_ptr< FieldMetainfoImpl > info) override
Write the field given by storageView to disk.
virtual bool isWritingThreadSafe() const override
Indicate whether it&#39;s safe for multiple threads to call Archive::write.
Abstract interface for Archives.
Definition: Archive.h:30
virtual ~BinaryArchive()
Destructor.