-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core-clp)!: Migrate archive metadata file format to MessagePack. #700
Changes from 12 commits
ecde0d3
a6974c7
d3a52d4
72452c1
aafee6e
f19a7ed
620ce22
fd7a451
fa67ad6
13d5fdf
1b1b290
202c4ee
a885dc8
fc00b4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,12 @@ | ||||||||||||||||||||||||||
#include "ArchiveMetadata.hpp" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
#include <sys/stat.h> | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
#include <fmt/core.h> | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
#include "../Array.hpp" | ||||||||||||||||||||||||||
#include "../FileReader.hpp" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
namespace clp::streaming_archive { | ||||||||||||||||||||||||||
ArchiveMetadata::ArchiveMetadata( | ||||||||||||||||||||||||||
archive_format_version_t archive_format_version, | ||||||||||||||||||||||||||
|
@@ -22,15 +29,27 @@ ArchiveMetadata::ArchiveMetadata( | |||||||||||||||||||||||||
+ sizeof(m_end_timestamp) + sizeof(m_compressed_size); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
ArchiveMetadata::ArchiveMetadata(FileReader& file_reader) { | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_archive_format_version, false); | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_creator_id_len, false); | ||||||||||||||||||||||||||
file_reader.read_string(m_creator_id_len, m_creator_id, false); | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_creation_idx, false); | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_uncompressed_size, false); | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_compressed_size, false); | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_begin_timestamp, false); | ||||||||||||||||||||||||||
file_reader.read_numeric_value(m_end_timestamp, false); | ||||||||||||||||||||||||||
auto ArchiveMetadata::create_from_file(std::string_view file_path) -> ArchiveMetadata { | ||||||||||||||||||||||||||
FileReader file_reader{std::string(file_path)}; | ||||||||||||||||||||||||||
struct stat file_stat{}; | ||||||||||||||||||||||||||
if (auto const clp_rc = file_reader.try_fstat(file_stat); | ||||||||||||||||||||||||||
clp::ErrorCode::ErrorCode_Success != clp_rc) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
throw OperationFailed(clp_rc, __FILENAME__, __LINE__); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
clp::Array<char> buf{static_cast<size_t>(file_stat.st_size)}; | ||||||||||||||||||||||||||
if (auto const clp_rc = file_reader.try_read_exact_length(buf.data(), buf.size()); | ||||||||||||||||||||||||||
clp::ErrorCode::ErrorCode_Success != clp_rc) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
throw OperationFailed(clp_rc, __FILENAME__, __LINE__); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
ArchiveMetadata metadata; | ||||||||||||||||||||||||||
msgpack::object_handle const obj_handle = msgpack::unpack(buf.data(), buf.size()); | ||||||||||||||||||||||||||
msgpack::object const obj = obj_handle.get(); | ||||||||||||||||||||||||||
obj.convert(metadata); | ||||||||||||||||||||||||||
return metadata; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
void ArchiveMetadata::expand_time_range(epochtime_t begin_timestamp, epochtime_t end_timestamp) { | ||||||||||||||||||||||||||
|
@@ -43,13 +62,9 @@ void ArchiveMetadata::expand_time_range(epochtime_t begin_timestamp, epochtime_t | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
void ArchiveMetadata::write_to_file(FileWriter& file_writer) const { | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_archive_format_version); | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_creator_id_len); | ||||||||||||||||||||||||||
file_writer.write_string(m_creator_id); | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_creation_idx); | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_uncompressed_size + m_dynamic_uncompressed_size); | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_compressed_size + m_dynamic_uncompressed_size); | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_begin_timestamp); | ||||||||||||||||||||||||||
file_writer.write_numeric_value(m_end_timestamp); | ||||||||||||||||||||||||||
std::ostringstream buf; | ||||||||||||||||||||||||||
msgpack::pack(buf, *this); | ||||||||||||||||||||||||||
auto const& string_buf = buf.str(); | ||||||||||||||||||||||||||
file_writer.write(string_buf.data(), string_buf.size()); | ||||||||||||||||||||||||||
Comment on lines
+53
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for the write operation. The write operation should be checked for errors to ensure the data is written successfully. Apply this diff to add error handling: void ArchiveMetadata::write_to_file(FileWriter& file_writer) const {
std::ostringstream buf;
msgpack::pack(buf, *this);
auto const& string_buf = buf.str();
- file_writer.write(string_buf.data(), string_buf.size());
+ if (auto const clp_rc = file_writer.try_write(string_buf.data(), string_buf.size());
+ clp::ErrorCode::ErrorCode_Success != clp_rc)
+ {
+ throw OperationFailed(clp_rc, __FILENAME__, __LINE__);
+ }
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} // namespace clp::streaming_archive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add file size validation to prevent excessive memory allocation.
Consider adding a size limit check before allocating memory for the buffer.
📝 Committable suggestion