binarystore: Move serialized data loading to its own function
Move serialized data loading to a separate function to make the code
cleaner.
Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: I267bc1ec42a0f6b4100aff357b5ed9c9fb1a2716
diff --git a/binarystore.cpp b/binarystore.cpp
index 9c37e47..29e6b9f 100644
--- a/binarystore.cpp
+++ b/binarystore.cpp
@@ -1,5 +1,8 @@
#include "binarystore.hpp"
+#include "sys_file.hpp"
+
+#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
@@ -39,11 +42,46 @@
auto store =
std::make_unique<BinaryStore>(baseBlobId, std::move(file), maxSize);
- store->blob_.set_blob_base_id(store->baseBlobId_);
-
return std::move(store);
}
+bool BinaryStore::loadSerializedData()
+{
+ /* Load blob from sysfile if we know it might not match what we have.
+ * Note it will overwrite existing unsaved data per design. */
+ if (commitState_ == CommitState::Clean)
+ {
+ return true;
+ }
+
+ log<level::NOTICE>("Try loading blob from persistent data",
+ entry("BASE_ID=%s", baseBlobId_.c_str()));
+ try
+ {
+ /* Parse length-prefixed format to protobuf */
+ boost::endian::little_uint64_t size = 0;
+ file_->readToBuf(0, sizeof(size), reinterpret_cast<char*>(&size));
+
+ if (!blob_.ParseFromString(file_->readAsStr(sizeof(uint64_t), size)))
+ {
+ /* Fail to parse the data, which might mean no preexsiting blobs
+ * and is a valid case to handle. Simply init an empty binstore. */
+ log<level::WARNING>(
+ "Fail to parse. There might be no persisted blobs",
+ entry("BASE_ID=%s", baseBlobId_.c_str()));
+ }
+ }
+ catch (const std::exception& e)
+ {
+ /* Read causes unexpected system-level failure */
+ log<level::ERR>("Reading from sysfile failed",
+ entry("ERROR=%s", e.what()));
+ return false;
+ }
+
+ return true;
+}
+
std::string BinaryStore::getBaseBlobId() const
{
return baseBlobId_;
@@ -81,35 +119,10 @@
writable_ = flags & blobs::OpenFlags::write;
- /* Load blob from sysfile if we know it might not match what we have.
- * Note it will overwrite existing unsaved data per design. */
- if (commitState_ != CommitState::Clean)
+ /* If there are uncommitted data, discard them. */
+ if (!this->loadSerializedData())
{
- log<level::NOTICE>("Try loading blob from persistent data",
- entry("BLOB_ID=%s", blobId.c_str()));
- try
- {
- /* Parse length-prefixed format to protobuf */
- boost::endian::little_uint64_t size = 0;
- file_->readToBuf(0, sizeof(size), reinterpret_cast<char*>(&size));
-
- if (!blob_.ParseFromString(
- file_->readAsStr(sizeof(uint64_t), size)))
- {
- /* Fail to parse the data, which might mean no preexsiting data
- * and is a valid case to handle */
- log<level::WARNING>(
- "Fail to parse. There might be no persisted blobs",
- entry("BLOB_ID=%s", blobId.c_str()));
- }
- }
- catch (const std::exception& e)
- {
- /* Read causes unexpected system-level failure */
- log<level::ERR>("Reading from sysfile failed",
- entry("ERROR=%s", e.what()));
- return false;
- }
+ return false;
}
/* Iterate and find if there is an existing blob with this id.
diff --git a/binarystore.hpp b/binarystore.hpp
index 551d22d..f07cf7b 100644
--- a/binarystore.hpp
+++ b/binarystore.hpp
@@ -106,6 +106,7 @@
baseBlobId_(baseBlobId),
file_(std::move(file)), maxSize_(maxSize)
{
+ blob_.set_blob_base_id(baseBlobId_);
}
~BinaryStore() = default;
@@ -146,6 +147,10 @@
CommitError // Error happened during committing
};
+ /* Load the serialized data from sysfile if commit state is dirty.
+ * Returns False if encountered error when loading */
+ bool loadSerializedData();
+
std::string baseBlobId_;
binaryblobproto::BinaryBlobBase blob_;
binaryblobproto::BinaryBlob* currentBlob_ = nullptr;