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