blob: 7020cffb956e170cd6a369e3a1ee5baa22e31af2 [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
Kun Yi0a940b92019-01-07 16:33:11 -080010#include "binaryblob.pb.h"
11
Kun Yi68c81142018-12-18 11:17:14 -080012using std::size_t;
13using std::uint16_t;
14using std::uint32_t;
15using std::uint64_t;
16using std::uint8_t;
17
18namespace binstore
19{
20
21/**
22 * @class BinaryStoreInterface is an abstraction for a storage location.
Kun Yi64dc05c2018-12-19 13:19:03 -080023 * Each instance would be uniquely identified by a baseId string.
Kun Yi68c81142018-12-18 11:17:14 -080024 */
25class BinaryStoreInterface
26{
27 public:
28 virtual ~BinaryStoreInterface() = default;
29
Kun Yi64dc05c2018-12-19 13:19:03 -080030 /**
31 * @returns baseId string of the storage.
32 */
Kun Yi68c81142018-12-18 11:17:14 -080033 virtual std::string getBaseBlobId() const = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080034
35 /**
36 * @returns List of all open blob IDs, plus the base.
37 */
Kun Yi68c81142018-12-18 11:17:14 -080038 virtual std::vector<std::string> getBlobIds() const = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080039
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 Yi38146a02018-12-18 21:54:26 -080046 virtual bool openOrCreateBlob(const std::string& blobId,
47 uint16_t flags) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080048
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 Yic0adbc32018-12-18 22:35:29 -080054 virtual bool deleteBlob(const std::string& blobId) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080055
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 Yi68c81142018-12-18 11:17:14 -080063 virtual std::vector<uint8_t> read(uint32_t offset,
64 uint32_t requestedSize) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080065
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 Yi68c81142018-12-18 11:17:14 -080072 virtual bool write(uint32_t offset, const std::vector<uint8_t>& data) = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080073
74 /**
75 * TODO
76 */
Kun Yi68c81142018-12-18 11:17:14 -080077 virtual bool commit() = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080078
79 /**
80 * TODO
81 */
Kun Yi68c81142018-12-18 11:17:14 -080082 virtual bool close() = 0;
Kun Yi64dc05c2018-12-19 13:19:03 -080083
84 /**
85 * TODO
86 */
Kun Yi68c81142018-12-18 11:17:14 -080087 virtual bool stat() = 0;
88};
89
Kun Yi64dc05c2018-12-19 13:19:03 -080090/**
91 * @class BinaryStore instantiates a concrete implementation of
92 * BinaryStoreInterface. The dependency on file is injected through its
93 * constructor.
94 */
95class BinaryStore : public BinaryStoreInterface
96{
97 public:
98 BinaryStore(const std::string& baseBlobId, int fd, uint32_t offset,
99 uint32_t maxSize) :
100 baseBlobId_(baseBlobId),
101 fd_(fd), offset_(offset), maxSize_(maxSize)
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 Yi0a940b92019-01-07 16:33:11 -0800142 binaryblobproto::BinaryBlobBase blob_;
143 binaryblobproto::BinaryBlob* currentBlob_;
Kun Yi64dc05c2018-12-19 13:19:03 -0800144};
145
Kun Yi68c81142018-12-18 11:17:14 -0800146} // namespace binstore