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