blob: e0a343a9ed742aa6e3fe437b4c801b3ecf00e275 [file] [log] [blame]
Kun Yi91beea62018-11-26 15:23:14 -08001#include "handler.hpp"
2
Kun Yi68c81142018-12-18 11:17:14 -08003#include <algorithm>
Kun Yid2d8cb62019-01-10 15:06:12 -08004#include <cstdint>
5#include <memory>
6#include <string>
7#include <vector>
8
9using std::size_t;
10using std::uint16_t;
11using std::uint32_t;
12using std::uint64_t;
13using std::uint8_t;
Kun Yi68c81142018-12-18 11:17:14 -080014
Kun Yi91beea62018-11-26 15:23:14 -080015namespace blobs
16{
17
Kun Yi38146a02018-12-18 21:54:26 -080018namespace internal
19{
20
Kun Yi64dc05c2018-12-19 13:19:03 -080021/**
22 * @brief: Get baseId from a blob id string
23 * @param blobId: Input blob id which is expected to only contain alphanumerical
24 * characters and '/'.
25 * @returns: the baseId containing the blobId, stripping all contents from the
26 * last '/'. If no '/' is present, an empty string is returned.
27 */
28static std::string getBaseFromId(const std::string& blobId)
Kun Yi38146a02018-12-18 21:54:26 -080029{
30 return blobId.substr(0, blobId.find_last_of('/') + 1);
31}
32
33} // namespace internal
34
Kun Yi68c81142018-12-18 11:17:14 -080035void BinaryStoreBlobHandler::addNewBinaryStore(
36 std::unique_ptr<binstore::BinaryStoreInterface> store)
37{
38 // TODO: this is a very rough measure to test the mock interface for now.
39 stores_[store->getBaseBlobId()] = std::move(store);
40}
41
Kun Yi91beea62018-11-26 15:23:14 -080042bool BinaryStoreBlobHandler::canHandleBlob(const std::string& path)
43{
Kun Yi64dc05c2018-12-19 13:19:03 -080044 auto base = internal::getBaseFromId(path);
45 if (base.empty() || base == path)
46 {
47 /* Operations on baseId itself or an empty base is not allowed */
48 return false;
49 }
50
Kun Yi68c81142018-12-18 11:17:14 -080051 return std::any_of(stores_.begin(), stores_.end(),
52 [&](const auto& baseStorePair) {
Kun Yi64dc05c2018-12-19 13:19:03 -080053 return base == baseStorePair.second->getBaseBlobId();
Kun Yi68c81142018-12-18 11:17:14 -080054 });
Kun Yi91beea62018-11-26 15:23:14 -080055}
56
57std::vector<std::string> BinaryStoreBlobHandler::getBlobIds()
58{
Kun Yi91beea62018-11-26 15:23:14 -080059 std::vector<std::string> result;
Kun Yi68c81142018-12-18 11:17:14 -080060
61 for (const auto& baseStorePair : stores_)
62 {
63 const auto& ids = baseStorePair.second->getBlobIds();
64 result.insert(result.end(), ids.begin(), ids.end());
65 }
66
Kun Yi91beea62018-11-26 15:23:14 -080067 return result;
68}
69
70bool BinaryStoreBlobHandler::deleteBlob(const std::string& path)
71{
Kun Yic0adbc32018-12-18 22:35:29 -080072 auto it = stores_.find(internal::getBaseFromId(path));
73 if (it == stores_.end())
74 {
75 return false;
76 }
77
78 return it->second->deleteBlob(path);
Kun Yi91beea62018-11-26 15:23:14 -080079}
80
81bool BinaryStoreBlobHandler::stat(const std::string& path,
82 struct BlobMeta* meta)
83{
84 // TODO: implement
85 return false;
86}
87
88bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags,
89 const std::string& path)
90{
Kun Yi68c81142018-12-18 11:17:14 -080091 if (!canHandleBlob(path))
92 {
93 return false;
94 }
95
96 auto found = sessions_.find(session);
97 if (found != sessions_.end())
98 {
99 /* This session is already active */
100 return false;
101 }
102
Kun Yi38146a02018-12-18 21:54:26 -0800103 const auto& base = internal::getBaseFromId(path);
104
105 if (stores_.find(base) == stores_.end())
106 {
107 return false;
108 }
109
110 if (!stores_[base]->openOrCreateBlob(path, flags))
111 {
112 return false;
113 }
114
115 sessions_[session] = stores_[base].get();
116 return true;
Kun Yi91beea62018-11-26 15:23:14 -0800117}
118
119std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session,
120 uint32_t offset,
121 uint32_t requestedSize)
122{
Kun Yic0adbc32018-12-18 22:35:29 -0800123 auto it = sessions_.find(session);
124 if (it == sessions_.end())
125 {
126 return std::vector<uint8_t>();
127 }
128
129 return it->second->read(offset, requestedSize);
Kun Yi91beea62018-11-26 15:23:14 -0800130}
131
132bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset,
133 const std::vector<uint8_t>& data)
134{
Kun Yic0adbc32018-12-18 22:35:29 -0800135 auto it = sessions_.find(session);
136 if (it == sessions_.end())
137 {
138 return false;
139 }
140
141 return it->second->write(offset, data);
Kun Yi91beea62018-11-26 15:23:14 -0800142}
143
144bool BinaryStoreBlobHandler::writeMeta(uint16_t session, uint32_t offset,
145 const std::vector<uint8_t>& data)
146{
147 /* Binary store handler doesn't support write meta */
148 return false;
149}
150
151bool BinaryStoreBlobHandler::commit(uint16_t session,
152 const std::vector<uint8_t>& data)
153{
154 // TODO: implement
155 return false;
156}
157
158bool BinaryStoreBlobHandler::close(uint16_t session)
159{
Kun Yic0adbc32018-12-18 22:35:29 -0800160 auto it = sessions_.find(session);
161 if (it == sessions_.end())
162 {
163 return false;
164 }
165
166 if (!it->second->close())
167 {
168 return false;
169 }
170
171 sessions_.erase(session);
172 return true;
Kun Yi91beea62018-11-26 15:23:14 -0800173}
174
175bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
176{
177 // TODO: implement
178 return false;
179}
180
181bool BinaryStoreBlobHandler::expire(uint16_t session)
182{
183 /* Binary store handler doesn't support expire */
184 return false;
185}
186
187} // namespace blobs