blob: ae21ab1d04933fe99e38c0f1a638cd12bef9662e [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
11/* Strip the basename till the last '/' */
12std::string getBaseFromId(const std::string& blobId)
13{
14 return blobId.substr(0, blobId.find_last_of('/') + 1);
15}
16
17} // namespace internal
18
Kun Yi68c81142018-12-18 11:17:14 -080019void BinaryStoreBlobHandler::addNewBinaryStore(
20 std::unique_ptr<binstore::BinaryStoreInterface> store)
21{
22 // TODO: this is a very rough measure to test the mock interface for now.
23 stores_[store->getBaseBlobId()] = std::move(store);
24}
25
Kun Yi91beea62018-11-26 15:23:14 -080026bool BinaryStoreBlobHandler::canHandleBlob(const std::string& path)
27{
Kun Yi68c81142018-12-18 11:17:14 -080028 return std::any_of(stores_.begin(), stores_.end(),
29 [&](const auto& baseStorePair) {
30 return baseStorePair.second->canHandleBlob(path);
31 });
Kun Yi91beea62018-11-26 15:23:14 -080032}
33
34std::vector<std::string> BinaryStoreBlobHandler::getBlobIds()
35{
Kun Yi91beea62018-11-26 15:23:14 -080036 std::vector<std::string> result;
Kun Yi68c81142018-12-18 11:17:14 -080037
38 for (const auto& baseStorePair : stores_)
39 {
40 const auto& ids = baseStorePair.second->getBlobIds();
41 result.insert(result.end(), ids.begin(), ids.end());
42 }
43
Kun Yi91beea62018-11-26 15:23:14 -080044 return result;
45}
46
47bool BinaryStoreBlobHandler::deleteBlob(const std::string& path)
48{
Kun Yic0adbc32018-12-18 22:35:29 -080049 auto it = stores_.find(internal::getBaseFromId(path));
50 if (it == stores_.end())
51 {
52 return false;
53 }
54
55 return it->second->deleteBlob(path);
Kun Yi91beea62018-11-26 15:23:14 -080056}
57
58bool BinaryStoreBlobHandler::stat(const std::string& path,
59 struct BlobMeta* meta)
60{
61 // TODO: implement
62 return false;
63}
64
65bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags,
66 const std::string& path)
67{
Kun Yi68c81142018-12-18 11:17:14 -080068 if (!canHandleBlob(path))
69 {
70 return false;
71 }
72
73 auto found = sessions_.find(session);
74 if (found != sessions_.end())
75 {
76 /* This session is already active */
77 return false;
78 }
79
Kun Yi38146a02018-12-18 21:54:26 -080080 const auto& base = internal::getBaseFromId(path);
81
82 if (stores_.find(base) == stores_.end())
83 {
84 return false;
85 }
86
87 if (!stores_[base]->openOrCreateBlob(path, flags))
88 {
89 return false;
90 }
91
92 sessions_[session] = stores_[base].get();
93 return true;
Kun Yi91beea62018-11-26 15:23:14 -080094}
95
96std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session,
97 uint32_t offset,
98 uint32_t requestedSize)
99{
Kun Yic0adbc32018-12-18 22:35:29 -0800100 auto it = sessions_.find(session);
101 if (it == sessions_.end())
102 {
103 return std::vector<uint8_t>();
104 }
105
106 return it->second->read(offset, requestedSize);
Kun Yi91beea62018-11-26 15:23:14 -0800107}
108
109bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset,
110 const std::vector<uint8_t>& data)
111{
Kun Yic0adbc32018-12-18 22:35:29 -0800112 auto it = sessions_.find(session);
113 if (it == sessions_.end())
114 {
115 return false;
116 }
117
118 return it->second->write(offset, data);
Kun Yi91beea62018-11-26 15:23:14 -0800119}
120
121bool BinaryStoreBlobHandler::writeMeta(uint16_t session, uint32_t offset,
122 const std::vector<uint8_t>& data)
123{
124 /* Binary store handler doesn't support write meta */
125 return false;
126}
127
128bool BinaryStoreBlobHandler::commit(uint16_t session,
129 const std::vector<uint8_t>& data)
130{
131 // TODO: implement
132 return false;
133}
134
135bool BinaryStoreBlobHandler::close(uint16_t session)
136{
Kun Yic0adbc32018-12-18 22:35:29 -0800137 auto it = sessions_.find(session);
138 if (it == sessions_.end())
139 {
140 return false;
141 }
142
143 if (!it->second->close())
144 {
145 return false;
146 }
147
148 sessions_.erase(session);
149 return true;
Kun Yi91beea62018-11-26 15:23:14 -0800150}
151
152bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
153{
154 // TODO: implement
155 return false;
156}
157
158bool BinaryStoreBlobHandler::expire(uint16_t session)
159{
160 /* Binary store handler doesn't support expire */
161 return false;
162}
163
164} // namespace blobs