blob: 7bbac6b03b925c35f2639d297cb1094d117821b4 [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{
49 // TODO: implement
50 return false;
51}
52
53bool BinaryStoreBlobHandler::stat(const std::string& path,
54 struct BlobMeta* meta)
55{
56 // TODO: implement
57 return false;
58}
59
60bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags,
61 const std::string& path)
62{
Kun Yi68c81142018-12-18 11:17:14 -080063 if (!canHandleBlob(path))
64 {
65 return false;
66 }
67
68 auto found = sessions_.find(session);
69 if (found != sessions_.end())
70 {
71 /* This session is already active */
72 return false;
73 }
74
Kun Yi38146a02018-12-18 21:54:26 -080075 const auto& base = internal::getBaseFromId(path);
76
77 if (stores_.find(base) == stores_.end())
78 {
79 return false;
80 }
81
82 if (!stores_[base]->openOrCreateBlob(path, flags))
83 {
84 return false;
85 }
86
87 sessions_[session] = stores_[base].get();
88 return true;
Kun Yi91beea62018-11-26 15:23:14 -080089}
90
91std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session,
92 uint32_t offset,
93 uint32_t requestedSize)
94{
95 // TODO: implement
96 std::vector<uint8_t> result;
97 return result;
98}
99
100bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset,
101 const std::vector<uint8_t>& data)
102{
103 // TODO: implement
104 return false;
105}
106
107bool BinaryStoreBlobHandler::writeMeta(uint16_t session, uint32_t offset,
108 const std::vector<uint8_t>& data)
109{
110 /* Binary store handler doesn't support write meta */
111 return false;
112}
113
114bool BinaryStoreBlobHandler::commit(uint16_t session,
115 const std::vector<uint8_t>& data)
116{
117 // TODO: implement
118 return false;
119}
120
121bool BinaryStoreBlobHandler::close(uint16_t session)
122{
123 // TODO: implement
124 return false;
125}
126
127bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
128{
129 // TODO: implement
130 return false;
131}
132
133bool BinaryStoreBlobHandler::expire(uint16_t session)
134{
135 /* Binary store handler doesn't support expire */
136 return false;
137}
138
139} // namespace blobs