blob: a54eaf5f8912330f8a1f974620de1a075fb6e502 [file] [log] [blame]
Kun Yi68c81142018-12-18 11:17:14 -08001#pragma once
2
Kun Yi64dc05c2018-12-19 13:19:03 -08003#include <unistd.h>
4
Kun Yi68c81142018-12-18 11:17:14 -08005#include <cstdint>
Kun Yi64dc05c2018-12-19 13:19:03 -08006#include <memory>
Kun Yi68c81142018-12-18 11:17:14 -08007#include <string>
8#include <vector>
9
10using std::size_t;
11using std::uint16_t;
12using std::uint32_t;
13using std::uint64_t;
14using std::uint8_t;
15
16namespace binstore
17{
18
19/**
20 * @class BinaryStoreInterface is an abstraction for a storage location.
Kun Yi64dc05c2018-12-19 13:19:03 -080021 * Each instance would be uniquely identified by a baseId string.
Kun Yi68c81142018-12-18 11:17:14 -080022 */
23class BinaryStoreInterface
24{
25 public:
26 virtual ~BinaryStoreInterface() = default;
27
Kun Yi64dc05c2018-12-19 13:19:03 -080028 /**
29 * @returns baseId string of the storage.
30 */
Kun Yi68c81142018-12-18 11:17:14 -080031 virtual std::string getBaseBlobId() const = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080032
33 /**
34 * @returns List of all open blob IDs, plus the base.
35 */
Kun Yi68c81142018-12-18 11:17:14 -080036 virtual std::vector<std::string> getBlobIds() const = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080037
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 Yi38146a02018-12-18 21:54:26 -080044 virtual bool openOrCreateBlob(const std::string& blobId,
45 uint16_t flags) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080046
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 Yic0adbc32018-12-18 22:35:29 -080052 virtual bool deleteBlob(const std::string& blobId) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080053
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 Yi68c81142018-12-18 11:17:14 -080061 virtual std::vector<uint8_t> read(uint32_t offset,
62 uint32_t requestedSize) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080063
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 Yi68c81142018-12-18 11:17:14 -080070 virtual bool write(uint32_t offset, const std::vector<uint8_t>& data) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080071
72 /**
73 * TODO
74 */
Kun Yi68c81142018-12-18 11:17:14 -080075 virtual bool commit() = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080076
77 /**
78 * TODO
79 */
Kun Yi68c81142018-12-18 11:17:14 -080080 virtual bool close() = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080081
82 /**
83 * TODO
84 */
Kun Yi68c81142018-12-18 11:17:14 -080085 virtual bool stat() = 0;
86};
87
Kun Yi64dc05c2018-12-19 13:19:03 -080088// TODO: move to protobuf definition
89struct BinaryBlobSingle
90{
91 std::string id;
92 std::vector<uint8_t> data;
93};
94
95struct 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 */
106class 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 Yi68c81142018-12-18 11:17:14 -0800156} // namespace binstore