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;
347 template<
class XWriteType >
bool Write(
const XWriteType& obj,
const std::string& shortname =
"")
351 MHO_FileKey key = GenerateObjectFileKey(obj, shortname);
353 fObjectStreamer << key;
356 fObjectStreamer << obj;
368 msg_error(
"file",
"Failed to write object, stream state returned: " << fObjectStreamer.
GetStream().rdstate()
376 msg_error(
"file",
"Failed to write object, file not open for writing." << eom);
390 template<
class XWriteType >
bool Write(
const XWriteType& obj,
const char* shortname)
392 std::string sshortname(shortname);
393 return this->
Write(obj, sshortname);
408 fObjectStreamer >> key;
414 if(key.
fSync != MHO_FileKeySyncWord)
421 fMD5Generator << name;
432 fObjectStreamer >> obj;
439 fObjectStreamer.
GetStream().seekg(skip_size, std::ios_base::cur);
440 msg_warn(
"file",
"Encountered and skipped an unrecognized version of object with class name: "
441 << name <<
"." << eom);
447 msg_error(
"file",
"Encountered object with wrong/corrupt object size");
456 "Failed to read object, object type/key mismatch for object of type: " << name <<
"." << eom);
464 msg_error(
"file",
"Failed to read object, file not open for reading." << eom);
483 template<
class XWriteType >
MHO_FileKey GenerateObjectFileKey(
const XWriteType& obj,
const std::string& shortname)
488 CopyTruncatedString(shortname, key.
fName);
492 fMD5Generator << name;
499 key.
fSize = obj.GetSerializedSize();
504 void CopyTruncatedString(
const std::string& s,
char* arr)
506 size_t len = s.size();
507 if(MHO_FileKeyNameLength <= len)
509 len = MHO_FileKeyNameLength;
511 for(
size_t i = 0; i < len; i++)
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:291
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:248
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
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:403
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:390
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:347
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:293
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:343
virtual uint64_t GetNBytesWritten() const override
Getter for N bytes written.
Definition: MHO_BinaryFileStreamer.hh:359
virtual std::fstream & GetStream() override
Getter for fstream object.
Definition: MHO_BinaryFileStreamer.hh:329
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:54
char fName[MHO_FileKeyNameLength]
Definition: MHO_FileKey.hh:158
MHO_UUID fTypeId
Definition: MHO_FileKey.hh:157
uint32_t fSync
Definition: MHO_FileKey.hh:150
uint64_t fSize
Definition: MHO_FileKey.hh:159
MHO_UUID fObjectId
Definition: MHO_FileKey.hh:156
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:135
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:143
virtual void SkipAhead(size_t n_bytes)
Seeks ahead in file by specified number of bytes and updates object state.
Definition: MHO_FileStreamer.hh:151
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:134
void Finalize()
Finalizes the MD5 hash calculation and stores the result in fDigest.
Definition: MHO_MD5HashGenerator.hh:155
MHO_UUID GetDigestAsUUID()
Getter for digest as uuid.
Definition: MHO_MD5HashGenerator.hh:181
void Initialize()
Recursive function to initialize a game state.
Definition: MHO_MD5HashGenerator.hh:143
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_AdhocFlagging.hh:18
uint32_t MHO_ClassVersion
Definition: MHO_ClassIdentity.hh:22
static std::string ClassName()
Returns the class name as a string.
Definition: MHO_ClassIdentity.hh:51