blob: 5d9dec1f3690913d46a49d176a489293b2f9fc21 [file] [log] [blame]
#ifndef PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_PERSISTENT_STORAGE_BMC_H_
#define PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_PERSISTENT_STORAGE_BMC_H_
#include <cstdint>
#include <string>
#include <vector>
#include "persistent_storage.h"
#include "state_persistence.pb.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
namespace persistent_storage {
// b/359687259 make kRWFSPath and size configurable
constexpr std::uintmax_t kMaxFileSize = 16384; // 16 k
constexpr absl::string_view kRWFSPath = "/var/google/gPowerD/SavedActions/";
constexpr uint32_t kProtoKeepNumber = 10;
constexpr absl::string_view kfileNamePrefix = "savedactions";
struct PersistentStorageManagerBMC
: public safepower_agent::PersistentStorageManager {
public:
~PersistentStorageManagerBMC() override = default;
// Writes a delta to the saved actions to persistent storage.
absl::Status WriteSavedActionsChange(
const safepower_agent_persistence_proto::SavedActions& actions) override;
absl::Status WriteSavedActionsChange(
const safepower_agent_persistence_proto::SavedActions& actions,
absl::string_view dir_path);
// Reads all saved actions from persistent storage.
absl::StatusOr<safepower_agent_persistence_proto::SavedActions>
ReadSavedActions() override;
absl::StatusOr<safepower_agent_persistence_proto::SavedActions>
ReadSavedActions(absl::string_view dir_path);
private:
absl::Status ConvertAppendToMap(
safepower_agent_persistence_proto::SavedActions& map,
safepower_agent_persistence_proto::SavedActionAppendLog& log);
// keep the most recent protos is called when the size of the saved files is
// larger then the kmaxFileSize. In the event of large protos we will always
// have at least kProtoKeepNumber of messages. That garuantees was more
// important then miniziains the file size. From a design perspective it would
// be ideal if kMaxFileSize was reached at 2*kProtoKeepNumber, but that is not
// necessary.
absl::Status keepMostRecentProtos(
safepower_agent_persistence_proto::SavedActions& map,
uint32_t keepNumber = kProtoKeepNumber);
absl::StatusOr<uint64_t> findLargestFileId(
absl::Span<const std::string> files);
absl::StatusOr<std::vector<std::string> >
listFiles(absl::string_view file_path);
absl::Status deleteAllFilesExceptLargest(absl::string_view file_path);
std::string MakeFileName(absl::string_view file_path, uint64_t file_id);
};
} // namespace persistent_storage
#endif // PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_PERSISTENT_STORAGE_BMC_H_