Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
serialbox::BinaryArchive Class Reference

Non-portable binary archive. More...

#include <BinaryArchive.h>

Inheritance diagram for serialbox::BinaryArchive:
serialbox::Archive

Classes

struct  FileOffsetType
 Offset within a file. More...
 

Public Types

using FieldOffsetTable = std::vector< FileOffsetType >
 Table of ids and corresponding offsets whithin in each field (i.e file)
 
using FieldTable = std::unordered_map< std::string, FieldOffsetTable >
 Table of all fields owned by this archive, each field has a corresponding file.
 

Public Member Functions

 BinaryArchive (OpenModeKind mode, const std::string &directory, const std::string &prefix, bool skipMetaData=false)
 Initialize the archive. More...
 
 BinaryArchive (const BinaryArchive &)=delete
 Copy constructor [deleted].
 
BinaryArchiveoperator= (const BinaryArchive &)=delete
 Copy assignment [deleted].
 
virtual ~BinaryArchive ()
 Destructor.
 
void readMetaDataFromJson ()
 Load meta-data from JSON file.
 
void writeMetaDataToJson ()
 Convert meta-data to JSON and serialize to file.
 
void clearFieldTable ()
 Clear fieldTable.
 
FieldTablefieldTable () noexcept
 Get field table.
 
const FieldTablefieldTable () const noexcept
 
void setHash (std::unique_ptr< Hash > hash) noexcept
 Set the hash algorithm.
 
const std::unique_ptr< Hash > & hash () const noexcept
 Get the hash algorithm.
 
Archive implementation
See also
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. More...
 
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. More...
 
virtual void updateMetaData () override
 Update the meta-data on disk.
 
virtual OpenModeKind mode () const override
 Open-policy of the archive.
 
virtual std::string directory () const override
 Directory to write/read files.
 
virtual std::string prefix () const override
 Prefix of all files.
 
virtual std::string name () const override
 Name of the archive.
 
virtual std::string metaDataFile () const override
 Full file path to the meta-data file.
 
virtual std::ostream & toStream (std::ostream &stream) const override
 Convert the archive to stream.
 
virtual void clear () override
 Clear the archive i.e remove all data from disk and reset the internal data-structures.
 
virtual bool isReadingThreadSafe () const override
 Indicate whether it's safe for multiple threads to call Archive::read.
 
virtual bool isWritingThreadSafe () const override
 Indicate whether it's safe for multiple threads to call Archive::write.
 
virtual bool isSlicedReadingSupported () const override
 Indicate whether the archive supports StorageViews with attached slices.
 
- Public Member Functions inherited from serialbox::Archive
virtual ~Archive ()
 Vritual destructor.
 

Static Public Member Functions

static std::unique_ptr< Archivecreate (OpenModeKind mode, const std::string &directory, const std::string &prefix)
 Create a BinaryArchive.
 
static void writeToFile (std::string filename, const StorageView &storageView)
 Directly write field (given by storageView) to file. More...
 
static void readFromFile (std::string filename, StorageView &storageView)
 Directly read field (given by storageView) from file. More...
 

Static Public Attributes

static const std::string Name = "Binary"
 Name of the binary archive.
 
static const int Version = 0
 Revision of the binary archive.
 

Detailed Description

Non-portable binary archive.

Definition at line 32 of file BinaryArchive.h.

Constructor & Destructor Documentation

◆ BinaryArchive()

serialbox::BinaryArchive::BinaryArchive ( OpenModeKind  mode,
const std::string &  directory,
const std::string &  prefix,
bool  skipMetaData = false 
)

Initialize the archive.

Parameters
modePolicy to open files in the archive
directoryDirectory to write/read files. If the archive is opened in ´Read´ mode, the directory is expected to supply an ´ArchiveMetaData-prefix.json´. In case the archive is opened in ´Write´ mode, the directory will be cleansed from all files matching the pattern ´prefix_*.dat´, if the directory is non-existent, it will be created. The ´Append´ mode will try to open ´ArchiveMetaData-prefix.json´ if the directory exists otherwise create it.
prefixPrefix of all files followed by an underscore ´_´ and fieldname
skipMetaDataDo not read meta-data from disk

Definition at line 166 of file BinaryArchive.cpp.

Member Function Documentation

◆ read()

void serialbox::BinaryArchive::read ( StorageView storageView,
const FieldID fieldID,
std::shared_ptr< FieldMetainfoImpl info 
) const
overridevirtual

Read the field identified by fieldID and given by storageView from disk.

Parameters
storageViewAbstract StorageView of the underlying data
fieldIDName and and Id of the field
infoField meta-information (can be a nullptr)

Implements serialbox::Archive.

Definition at line 378 of file BinaryArchive.cpp.

◆ readFromFile()

void serialbox::BinaryArchive::readFromFile ( std::string  filename,
StorageView storageView 
)
static

Directly read field (given by storageView) from file.

This function can be used to implement stateless deserialization methods.

Parameters
filenameFile to read from
storageViewStorageView of the field

Definition at line 417 of file BinaryArchive.cpp.

◆ write()

FieldID serialbox::BinaryArchive::write ( const StorageView storageView,
const std::string &  field,
const std::shared_ptr< FieldMetainfoImpl info 
)
overridevirtual

Write the field given by storageView to disk.

The returned FieldID has to uniquely identify the field i.e it should be possible to read the exact same piece of data when later passing the FieldID to the read method.

Parameters
storageViewAbstract StorageView of the underlying data
fieldName of the field
infoField meta-information (can be a nullptr)
Returns
Unique identidier of the field

Implements serialbox::Archive.

Definition at line 288 of file BinaryArchive.cpp.

◆ writeToFile()

void serialbox::BinaryArchive::writeToFile ( std::string  filename,
const StorageView storageView 
)
static

Directly write field (given by storageView) to file.

Parameters
filenameNewly created file (if file already exists, it's contents will be discarded)
storageViewStorageView of the field

Definition at line 359 of file BinaryArchive.cpp.


The documentation for this class was generated from the following files: