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 | /** |
| 77 | * TODO |
| 78 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 79 | virtual bool commit() = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * TODO |
| 83 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 84 | virtual bool close() = 0; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * TODO |
| 88 | */ |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 89 | virtual bool stat() = 0; |
| 90 | }; |
| 91 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 92 | /** |
| 93 | * @class BinaryStore instantiates a concrete implementation of |
| 94 | * BinaryStoreInterface. The dependency on file is injected through its |
| 95 | * constructor. |
| 96 | */ |
| 97 | class BinaryStore : public BinaryStoreInterface |
| 98 | { |
| 99 | public: |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 100 | BinaryStore() = delete; |
| 101 | BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file, |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 102 | uint32_t maxSize) : |
| 103 | baseBlobId_(baseBlobId), |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 104 | file_(std::move(file)), maxSize_(maxSize) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 105 | { |
| 106 | } |
| 107 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 108 | ~BinaryStore() = default; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 109 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 110 | BinaryStore(const BinaryStore&) = delete; |
| 111 | BinaryStore& operator=(const BinaryStore&) = delete; |
| 112 | BinaryStore(BinaryStore&&) = default; |
| 113 | BinaryStore& operator=(BinaryStore&&) = default; |
| 114 | |
| 115 | std::string getBaseBlobId() const override; |
| 116 | std::vector<std::string> getBlobIds() const override; |
| 117 | bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override; |
| 118 | bool deleteBlob(const std::string& blobId) override; |
| 119 | std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override; |
| 120 | bool write(uint32_t offset, const std::vector<uint8_t>& data) override; |
| 121 | bool commit() override; |
| 122 | bool close() override; |
| 123 | bool stat() override; |
| 124 | |
| 125 | /** |
| 126 | * Helper factory method to create a BinaryStore instance |
| 127 | * @param baseBlobId: base id for the created instance |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 128 | * @param sysFile: system file object for storing binary |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 129 | * @param maxSize: max size in bytes that this BinaryStore can expand to. |
| 130 | * Writing data more than allowed size will return failure. |
| 131 | * @returns unique_ptr to constructed BinaryStore. Caller should take |
| 132 | * ownership of the instance. |
| 133 | */ |
| 134 | static std::unique_ptr<BinaryStoreInterface> |
| 135 | createFromConfig(const std::string& baseBlobId, |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 136 | std::unique_ptr<SysFile> file, uint32_t maxSize); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | std::string baseBlobId_; |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 140 | binaryblobproto::BinaryBlobBase blob_; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 141 | binaryblobproto::BinaryBlob* currentBlob_ = nullptr; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 142 | bool writable_ = false; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 143 | std::unique_ptr<SysFile> file_ = nullptr; |
| 144 | uint32_t maxSize_; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 147 | } // namespace binstore |