blob: 80790edcf22f3b0df73697defe6779fbefa0509a [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>
4
Kun Yi91beea62018-11-26 15:23:14 -08005namespace blobs
6{
7
Kun Yi38146a02018-12-18 21:54:26 -08008namespace internal
9{
10
Kun Yi64dc05c2018-12-19 13:19:03 -080011/**
12 * @brief: Get baseId from a blob id string
13 * @param blobId: Input blob id which is expected to only contain alphanumerical
14 * characters and '/'.
15 * @returns: the baseId containing the blobId, stripping all contents from the
16 * last '/'. If no '/' is present, an empty string is returned.
17 */
18static std::string getBaseFromId(const std::string& blobId)
Kun Yi38146a02018-12-18 21:54:26 -080019{
20 return blobId.substr(0, blobId.find_last_of('/') + 1);
21}
22
23} // namespace internal
24
Kun Yi68c81142018-12-18 11:17:14 -080025void BinaryStoreBlobHandler::addNewBinaryStore(
26 std::unique_ptr<binstore::BinaryStoreInterface> store)
27{
28 // TODO: this is a very rough measure to test the mock interface for now.
29 stores_[store->getBaseBlobId()] = std::move(store);
30}
31
Kun Yi91beea62018-11-26 15:23:14 -080032bool BinaryStoreBlobHandler::canHandleBlob(const std::string& path)
33{
Kun Yi64dc05c2018-12-19 13:19:03 -080034 auto base = internal::getBaseFromId(path);
35 if (base.empty() || base == path)
36 {
37 /* Operations on baseId itself or an empty base is not allowed */
38 return false;
39 }
40
Kun Yi68c81142018-12-18 11:17:14 -080041 return std::any_of(stores_.begin(), stores_.end(),
42 [&](const auto& baseStorePair) {
Kun Yi64dc05c2018-12-19 13:19:03 -080043 return base == baseStorePair.second->getBaseBlobId();
Kun Yi68c81142018-12-18 11:17:14 -080044 });
Kun Yi91beea62018-11-26 15:23:14 -080045}
46
47std::vector<std::string> BinaryStoreBlobHandler::getBlobIds()
48{
Kun Yi91beea62018-11-26 15:23:14 -080049 std::vector<std::string> result;
Kun Yi68c81142018-12-18 11:17:14 -080050
51 for (const auto& baseStorePair : stores_)
52 {
53 const auto& ids = baseStorePair.second->getBlobIds();
54 result.insert(result.end(), ids.begin(), ids.end());
55 }
56
Kun Yi91beea62018-11-26 15:23:14 -080057 return result;
58}
59
60bool BinaryStoreBlobHandler::deleteBlob(const std::string& path)
61{
Kun Yic0adbc32018-12-18 22:35:29 -080062 auto it = stores_.find(internal::getBaseFromId(path));
63 if (it == stores_.end())
64 {
65 return false;
66 }
67
68 return it->second->deleteBlob(path);
Kun Yi91beea62018-11-26 15:23:14 -080069}
70
71bool BinaryStoreBlobHandler::stat(const std::string& path,
72 struct BlobMeta* meta)
73{
74 // TODO: implement
75 return false;
76}
77
78bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags,
79 const std::string& path)
80{
Kun Yi68c81142018-12-18 11:17:14 -080081 if (!canHandleBlob(path))
82 {
83 return false;
84 }
85
86 auto found = sessions_.find(session);
87 if (found != sessions_.end())
88 {
89 /* This session is already active */
90 return false;
91 }
92
Kun Yi38146a02018-12-18 21:54:26 -080093 const auto& base = internal::getBaseFromId(path);
94
95 if (stores_.find(base) == stores_.end())
96 {
97 return false;
98 }
99
100 if (!stores_[base]->openOrCreateBlob(path, flags))
101 {
102 return false;
103 }
104
105 sessions_[session] = stores_[base].get();
106 return true;
Kun Yi91beea62018-11-26 15:23:14 -0800107}
108
109std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session,
110 uint32_t offset,
111 uint32_t requestedSize)
112{
Kun Yic0adbc32018-12-18 22:35:29 -0800113 auto it = sessions_.find(session);
114 if (it == sessions_.end())
115 {
116 return std::vector<uint8_t>();
117 }
118
119 return it->second->read(offset, requestedSize);
Kun Yi91beea62018-11-26 15:23:14 -0800120}
121
122bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset,
123 const std::vector<uint8_t>& data)
124{
Kun Yic0adbc32018-12-18 22:35:29 -0800125 auto it = sessions_.find(session);
126 if (it == sessions_.end())
127 {
128 return false;
129 }
130
131 return it->second->write(offset, data);
Kun Yi91beea62018-11-26 15:23:14 -0800132}
133
134bool BinaryStoreBlobHandler::writeMeta(uint16_t session, uint32_t offset,
135 const std::vector<uint8_t>& data)
136{
137 /* Binary store handler doesn't support write meta */
138 return false;
139}
140
141bool BinaryStoreBlobHandler::commit(uint16_t session,
142 const std::vector<uint8_t>& data)
143{
144 // TODO: implement
145 return false;
146}
147
148bool BinaryStoreBlobHandler::close(uint16_t session)
149{
Kun Yic0adbc32018-12-18 22:35:29 -0800150 auto it = sessions_.find(session);
151 if (it == sessions_.end())
152 {
153 return false;
154 }
155
156 if (!it->second->close())
157 {
158 return false;
159 }
160
161 sessions_.erase(session);
162 return true;
Kun Yi91beea62018-11-26 15:23:14 -0800163}
164
165bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
166{
167 // TODO: implement
168 return false;
169}
170
171bool BinaryStoreBlobHandler::expire(uint16_t session)
172{
173 /* Binary store handler doesn't support expire */
174 return false;
175}
176
177} // namespace blobs