Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 3 | #include "sys_file.hpp" |
| 4 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 7 | #include <cstdint> |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 8 | #include <memory> |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 12 | #include "binaryblob.pb.h" |
| 13 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 14 | using std::size_t; |
| 15 | using std::uint16_t; |
| 16 | using std::uint32_t; |
| 17 | using std::uint64_t; |
| 18 | using std::uint8_t; |
| 19 | |
| 20 | namespace binstore |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * @class BinaryStoreInterface is an abstraction for a storage location. |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 25 | * Each instance would be uniquely identified by a baseId string. |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 26 | */ |
| 27 | class BinaryStoreInterface |
| 28 | { |
| 29 | public: |
| 30 | virtual ~BinaryStoreInterface() = default; |
| 31 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @returns baseId string of the storage. |
| 34 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 35 | virtual std::string getBaseBlobId() const = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @returns List of all open blob IDs, plus the base. |
| 39 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 40 | virtual std::vector<std::string> getBlobIds() const = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Opens a blob given its name. If there is no one, create one. |
| 44 | * @param blobId: The blob id to operate on. |
| 45 | * @param flags: Either read flag or r/w flag has to be specified. |
| 46 | * @returns True if open/create successfully. |
| 47 | */ |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 48 | virtual bool openOrCreateBlob(const std::string& blobId, |
| 49 | uint16_t flags) = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Deletes a blob given its name. If there is no one, |
| 53 | * @param blobId: The blob id to operate on. |
| 54 | * @returns True if deleted. |
| 55 | */ |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 56 | virtual bool deleteBlob(const std::string& blobId) = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Reads data from the currently opened blob. |
| 60 | * @param offset: offset into the blob to read |
| 61 | * @param requestedSize: how many bytes to read |
| 62 | * @returns Bytes able to read. Returns empty if nothing can be read or |
| 63 | * if there is no open blob. |
| 64 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 65 | virtual std::vector<uint8_t> read(uint32_t offset, |
| 66 | uint32_t requestedSize) = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Writes data to the currently openend blob. |
| 70 | * @param offset: offset into the blob to write |
| 71 | * @param data: bytes to write |
| 72 | * @returns True if able to write the entire data successfully |
| 73 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 74 | virtual bool write(uint32_t offset, const std::vector<uint8_t>& data) = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 77 | * Commits data to the persistent storage specified during blob init. |
| 78 | * @returns True if able to write data to sysfile successfully |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 79 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 80 | virtual bool commit() = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 83 | * Closes blob, which prevents further modifications. Uncommitted data will |
| 84 | * be lost. |
| 85 | * @returns True if able to close the blob successfully |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 86 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 87 | virtual bool close() = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * TODO |
| 91 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 92 | virtual bool stat() = 0; |
| 93 | }; |
| 94 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 95 | /** |
| 96 | * @class BinaryStore instantiates a concrete implementation of |
| 97 | * BinaryStoreInterface. The dependency on file is injected through its |
| 98 | * constructor. |
| 99 | */ |
| 100 | class BinaryStore : public BinaryStoreInterface |
| 101 | { |
| 102 | public: |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 103 | BinaryStore() = delete; |
| 104 | BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file, |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 105 | uint32_t maxSize) : |
| 106 | baseBlobId_(baseBlobId), |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 107 | file_(std::move(file)), maxSize_(maxSize) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 108 | { |
| 109 | } |
| 110 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 111 | ~BinaryStore() = default; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 112 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 113 | BinaryStore(const BinaryStore&) = delete; |
| 114 | BinaryStore& operator=(const BinaryStore&) = delete; |
| 115 | BinaryStore(BinaryStore&&) = default; |
| 116 | BinaryStore& operator=(BinaryStore&&) = default; |
| 117 | |
| 118 | std::string getBaseBlobId() const override; |
| 119 | std::vector<std::string> getBlobIds() const override; |
| 120 | bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override; |
| 121 | bool deleteBlob(const std::string& blobId) override; |
| 122 | std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override; |
| 123 | bool write(uint32_t offset, const std::vector<uint8_t>& data) override; |
| 124 | bool commit() override; |
| 125 | bool close() override; |
| 126 | bool stat() override; |
| 127 | |
| 128 | /** |
| 129 | * Helper factory method to create a BinaryStore instance |
| 130 | * @param baseBlobId: base id for the created instance |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 131 | * @param sysFile: system file object for storing binary |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 132 | * @param maxSize: max size in bytes that this BinaryStore can expand to. |
| 133 | * Writing data more than allowed size will return failure. |
| 134 | * @returns unique_ptr to constructed BinaryStore. Caller should take |
| 135 | * ownership of the instance. |
| 136 | */ |
| 137 | static std::unique_ptr<BinaryStoreInterface> |
| 138 | createFromConfig(const std::string& baseBlobId, |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 139 | std::unique_ptr<SysFile> file, uint32_t maxSize); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 140 | |
| 141 | private: |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 142 | enum class CommitState |
| 143 | { |
| 144 | Dirty, // In-memory data might not match persisted data |
| 145 | Clean, // In-memory data matches persisted data |
| 146 | CommitError // Error happened during committing |
| 147 | }; |
| 148 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 149 | std::string baseBlobId_; |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 150 | binaryblobproto::BinaryBlobBase blob_; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 151 | binaryblobproto::BinaryBlob* currentBlob_ = nullptr; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 152 | bool writable_ = false; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 153 | std::unique_ptr<SysFile> file_ = nullptr; |
| 154 | uint32_t maxSize_; |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 155 | CommitState commitState_ = CommitState::Dirty; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 158 | } // namespace binstore |