Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 15f0f94 | 2020-07-09 09:38:18 -0700 | [diff] [blame^] | 3 | #include "binarystore_interface.hpp" |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 4 | #include "sys_file.hpp" |
| 5 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 8 | #include <blobs-ipmid/blobs.hpp> |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 9 | #include <cstdint> |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 10 | #include <memory> |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 14 | #include "binaryblob.pb.h" |
| 15 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 16 | using std::size_t; |
| 17 | using std::uint16_t; |
| 18 | using std::uint32_t; |
| 19 | using std::uint64_t; |
| 20 | using std::uint8_t; |
| 21 | |
| 22 | namespace binstore |
| 23 | { |
| 24 | |
| 25 | /** |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 26 | * @class BinaryStore instantiates a concrete implementation of |
| 27 | * BinaryStoreInterface. The dependency on file is injected through its |
| 28 | * constructor. |
| 29 | */ |
| 30 | class BinaryStore : public BinaryStoreInterface |
| 31 | { |
| 32 | public: |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 33 | /* |CommitState| differs slightly with |StateFlags| in blob.hpp, |
| 34 | * and thus is defined in the OEM space (bit 8 - 15). User can call stat() |
| 35 | * to query the |CommitState| of the blob path. */ |
| 36 | enum CommitState |
| 37 | { |
| 38 | Dirty = (1 << 8), // In-memory data might not match persisted data |
| 39 | Clean = (1 << 9), // In-memory data matches persisted data |
| 40 | Uninitialized = (1 << 10), // Cannot find persisted data |
| 41 | CommitError = (1 << 11) // Error happened during committing |
| 42 | }; |
| 43 | |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 44 | BinaryStore() = delete; |
| 45 | BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file, |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 46 | uint32_t maxSize) : |
| 47 | baseBlobId_(baseBlobId), |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 48 | file_(std::move(file)), maxSize_(maxSize) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 49 | { |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 50 | blob_.set_blob_base_id(baseBlobId_); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 53 | ~BinaryStore() = default; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 54 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 55 | BinaryStore(const BinaryStore&) = delete; |
| 56 | BinaryStore& operator=(const BinaryStore&) = delete; |
| 57 | BinaryStore(BinaryStore&&) = default; |
| 58 | BinaryStore& operator=(BinaryStore&&) = default; |
| 59 | |
| 60 | std::string getBaseBlobId() const override; |
| 61 | std::vector<std::string> getBlobIds() const override; |
| 62 | bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override; |
| 63 | bool deleteBlob(const std::string& blobId) override; |
| 64 | std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override; |
| 65 | bool write(uint32_t offset, const std::vector<uint8_t>& data) override; |
| 66 | bool commit() override; |
| 67 | bool close() override; |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 68 | bool stat(blobs::BlobMeta* meta) override; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Helper factory method to create a BinaryStore instance |
| 72 | * @param baseBlobId: base id for the created instance |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 73 | * @param sysFile: system file object for storing binary |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 74 | * @param maxSize: max size in bytes that this BinaryStore can expand to. |
| 75 | * Writing data more than allowed size will return failure. |
| 76 | * @returns unique_ptr to constructed BinaryStore. Caller should take |
| 77 | * ownership of the instance. |
| 78 | */ |
| 79 | static std::unique_ptr<BinaryStoreInterface> |
| 80 | createFromConfig(const std::string& baseBlobId, |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 81 | std::unique_ptr<SysFile> file, uint32_t maxSize); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 82 | |
| 83 | private: |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 84 | /* Load the serialized data from sysfile if commit state is dirty. |
| 85 | * Returns False if encountered error when loading */ |
| 86 | bool loadSerializedData(); |
| 87 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 88 | std::string baseBlobId_; |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 89 | binaryblobproto::BinaryBlobBase blob_; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 90 | binaryblobproto::BinaryBlob* currentBlob_ = nullptr; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 91 | bool writable_ = false; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 92 | std::unique_ptr<SysFile> file_ = nullptr; |
| 93 | uint32_t maxSize_; |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 94 | CommitState commitState_ = CommitState::Dirty; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 97 | } // namespace binstore |