1 #ifndef MHO_BinaryFileInterface_HH__
2 #define MHO_BinaryFileInterface_HH__
64 bool OpenToWrite(
const std::string& obj_filename,
const std::string& index_filename =
"")
69 if(index_filename !=
"")
87 bool OpenToAppend(
const std::string& obj_filename,
const std::string& index_filename =
"")
92 if(index_filename !=
"")
128 fObjectStreamer.
GetStream().seekg(offset_bytes, std::ios_base::cur);
146 msg_warn(
"file",
"Cannot extract index file keys with active stream. Close open file first." << eom);
164 fKeyStreamer.
Close();
169 msg_error(
"file",
"Failed to read object keys, file not open for reading." << eom);
170 fKeyStreamer.
Close();
191 msg_warn(
"file",
"Cannot extract file keys with active stream. Close open file first." << eom);
209 if(key.
fSync != MHO_FileKeySyncWord)
222 "Failed to read object key, sync word " << key.
fSync <<
" not recognized." << eom);
231 fKeyStreamer.
Close();
236 msg_error(
"file",
"Failed to read key, file not open for reading." << eom);
237 fKeyStreamer.
Close();
253 std::vector< std::size_t >& byte_offsets)
256 std::size_t byte_count = 0;
261 msg_warn(
"file",
"Cannot extract file keys with active stream. Close open file first." << eom);
279 if(key.
fSync != MHO_FileKeySyncWord)
285 byte_offsets.push_back(byte_count);
287 byte_count += MHO_FileKeySize + key.
fSize;
294 "Failed to read object key, sync word " << key.
fSync <<
" not recognized." << eom);
303 fKeyStreamer.
Close();
308 msg_error(
"file",
"Failed to read key, file not open for reading." << eom);
309 fKeyStreamer.
Close();
321 fObjectStreamer.
Close();
324 msg_error(
"file",
"Failed to close file." << eom);
329 fKeyStreamer.
Close();
332 msg_error(
"file",
"Failed to close key/index file." << eom);
335 fCollectKeys =
false;
346 template<
class XWriteType >
bool Write(
const XWriteType& obj,
const std::string& shortname =
"")
350 MHO_FileKey key = GenerateObjectFileKey(obj, shortname);
352 fObjectStreamer << key;
355 fObjectStreamer << obj;
367 msg_error(
"file",
"Failed to write object, stream state returned: " << fObjectStreamer.
GetStream().rdstate()
375 msg_error(
"file",
"Failed to write object, file not open for writing." << eom);
388 template<
class XWriteType >
bool Write(
const XWriteType& obj,
const char* shortname)
390 std::string sshortname(shortname);
391 return this->
Write(obj, sshortname);
406 fObjectStreamer >> key;
412 if(key.
fSync != MHO_FileKeySyncWord)
419 fMD5Generator << name;
430 fObjectStreamer >> obj;
437 fObjectStreamer.
GetStream().seekg(skip_size, std::ios_base::cur);
438 msg_warn(
"file",
"Encountered and skipped an unrecognized version of object with class name: "
439 << name <<
"." << eom);
445 msg_error(
"file",
"Encountered object with wrong/corrupt object size");
454 "Failed to read object, object type/key mismatch for object of type: " << name <<
"." << eom);
462 msg_error(
"file",
"Failed to read object, file not open for reading." << eom);
481 template<
class XWriteType >
MHO_FileKey GenerateObjectFileKey(
const XWriteType& obj,
const std::string& shortname)
486 CopyTruncatedString(shortname, key.
fName);
490 fMD5Generator << name;
497 key.
fSize = obj.GetSerializedSize();
502 void CopyTruncatedString(
const std::string& s,
char* arr)
504 size_t len = s.size();
505 if(MHO_FileKeyNameLength <= len)
507 len = MHO_FileKeyNameLength;
509 for(
size_t i = 0; i < len; i++)
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:297
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:254
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
Class MHO_BinaryFileInterface.
Definition: MHO_BinaryFileInterface.hh:27
bool Read(XReadType &obj, MHO_FileKey &obj_key)
Reads the object (XReadType) specified by the object file key from the file.
Definition: MHO_BinaryFileInterface.hh:401
bool OpenToWrite(const std::string &obj_filename, const std::string &index_filename="")
Opens a file for writing and optionally opens an index file if provided.
Definition: MHO_BinaryFileInterface.hh:64
bool OpenToRead(const std::string &filename)
Opens a file for reading and checks if it's open.
Definition: MHO_BinaryFileInterface.hh:109
virtual ~MHO_BinaryFileInterface()
Definition: MHO_BinaryFileInterface.hh:31
bool ExtractFileObjectKeysAndOffsets(const std::string &filename, std::vector< MHO_FileKey > &keys, std::vector< std::size_t > &byte_offsets)
pulls out the object keys and the bytes offsets to the key entry of each object from the start of the...
Definition: MHO_BinaryFileInterface.hh:252
bool OpenToAppend(const std::string &obj_filename, const std::string &index_filename="")
Opens a file for appending objects and optionally streams keys to an index file.
Definition: MHO_BinaryFileInterface.hh:87
void Close()
Closes file and key/index streamers if open.
Definition: MHO_BinaryFileInterface.hh:319
bool Write(const XWriteType &obj, const char *shortname)
Writes an object (must inherit from MHO_Serializable) to a file (with optional shortname string - ove...
Definition: MHO_BinaryFileInterface.hh:388
bool Write(const XWriteType &obj, const std::string &shortname="")
Writes an object (must inherit from MHO_Serializable) to a file (with optional shortname string)....
Definition: MHO_BinaryFileInterface.hh:346
bool ExtractFileObjectKeys(const std::string &filename, std::vector< MHO_FileKey > &keys)
reads an object file and extracts the object file keys while skipping over the object data (cannot be...
Definition: MHO_BinaryFileInterface.hh:184
bool OpenToReadAtOffset(const std::string &filename, uint64_t offset_bytes)
Opens a file for reading at a specified byte offset.
Definition: MHO_BinaryFileInterface.hh:124
bool IsOpenForWrite()
Checks if both object and key streamers are open for writing when fCollectKeys is true,...
Definition: MHO_BinaryFileInterface.hh:38
MHO_BinaryFileInterface()
Definition: MHO_BinaryFileInterface.hh:29
bool IsOpenForRead()
Checks if the object streamer is open for reading.
Definition: MHO_BinaryFileInterface.hh:55
bool ExtractIndexFileObjectKeys(const std::string &index_filename, std::vector< MHO_FileKey > &keys)
reads an index file and extracts the object file keys (cannot be actively streaming to another open f...
Definition: MHO_BinaryFileInterface.hh:139
Class MHO_BinaryFileStreamer declare the concrete class which does the work for file streams.
Definition: MHO_BinaryFileStreamer.hh:298
virtual void OpenToWrite() override
Opens a file for binary writing and sets appropriate state.
Definition: MHO_BinaryFileStreamer.cc:39
virtual void OpenToAppend() override
Opens a binary file for appending and sets appropriate file state.
Definition: MHO_BinaryFileStreamer.cc:23
virtual void OpenToRead() override
Opens a file for binary reading and sets appropriate state.
Definition: MHO_BinaryFileStreamer.cc:7
virtual void ResetByteCount() override
Resets byte count to zero.
Definition: MHO_BinaryFileStreamer.hh:348
virtual uint64_t GetNBytesWritten() const override
Getter for N bytes written.
Definition: MHO_BinaryFileStreamer.hh:364
virtual std::fstream & GetStream() override
Getter for fstream object.
Definition: MHO_BinaryFileStreamer.hh:334
virtual void Close() override
Closes the open file if it exists and sets the file state to closed.
Definition: MHO_BinaryFileStreamer.cc:55
Class MHO_FileKey the version-0 size of the file key is (512 bits / 64 bytes), and all of the version...
Definition: MHO_FileKey.hh:57
char fName[MHO_FileKeyNameLength]
Definition: MHO_FileKey.hh:162
MHO_UUID fTypeId
Definition: MHO_FileKey.hh:161
uint32_t fSync
Definition: MHO_FileKey.hh:154
uint64_t fSize
Definition: MHO_FileKey.hh:163
MHO_UUID fObjectId
Definition: MHO_FileKey.hh:160
virtual bool IsOpenForRead()
Checks if a file is open for reading.
Definition: MHO_FileStreamer.hh:99
virtual void ResetObjectState()
Resets the object state to unset.
Definition: MHO_FileStreamer.hh:136
virtual bool IsClosed()
Checks if the file is closed.
Definition: MHO_FileStreamer.hh:114
void SetFilename(const std::string filename)
Setter for filename.
Definition: MHO_FileStreamer.hh:41
virtual bool IsObjectUnknown()
Checks if object state is unknown.
Definition: MHO_FileStreamer.hh:144
virtual void SkipAhead(size_t n_bytes)
Seeks ahead in file by specified number of bytes and updates object state.
Definition: MHO_FileStreamer.hh:152
virtual bool IsOpenForWrite()
Checks if file is open for writing.
Definition: MHO_FileStreamer.hh:84
Class MHO_MD5HashGenerator declares the concrete class which does the work for file streams.
Definition: MHO_MD5HashGenerator.hh:135
void Finalize()
Finalizes the MD5 hash calculation and stores the result in fDigest.
Definition: MHO_MD5HashGenerator.hh:156
MHO_UUID GetDigestAsUUID()
Getter for digest as uuid.
Definition: MHO_MD5HashGenerator.hh:182
void Initialize()
Recursive function to initialize a game state.
Definition: MHO_MD5HashGenerator.hh:144
Definition: MHO_UUIDGenerator.hh:23
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
std::string as_string() const
Definition: MHO_UUID.hh:125
Definition: MHO_ChannelLabeler.hh:17
uint32_t MHO_ClassVersion
Definition: MHO_ClassIdentity.hh:22
static std::string ClassName()
Returns the class name as a string.
Definition: MHO_ClassIdentity.hh:36