Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 1 | #include "handler.hpp" |
| 2 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 3 | #include <algorithm> |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 4 | #include <cstdint> |
| 5 | #include <memory> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | using std::size_t; |
| 10 | using std::uint16_t; |
| 11 | using std::uint32_t; |
| 12 | using std::uint64_t; |
| 13 | using std::uint8_t; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 14 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 15 | namespace blobs |
| 16 | { |
| 17 | |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 18 | namespace internal |
| 19 | { |
| 20 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 21 | /** |
| 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 | */ |
| 28 | static std::string getBaseFromId(const std::string& blobId) |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 29 | { |
| 30 | return blobId.substr(0, blobId.find_last_of('/') + 1); |
| 31 | } |
| 32 | |
| 33 | } // namespace internal |
| 34 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 35 | void 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 42 | bool BinaryStoreBlobHandler::canHandleBlob(const std::string& path) |
| 43 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 44 | 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 Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 51 | return std::any_of(stores_.begin(), stores_.end(), |
| 52 | [&](const auto& baseStorePair) { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 53 | return base == baseStorePair.second->getBaseBlobId(); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 54 | }); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | std::vector<std::string> BinaryStoreBlobHandler::getBlobIds() |
| 58 | { |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 59 | std::vector<std::string> result; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 60 | |
| 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 67 | return result; |
| 68 | } |
| 69 | |
| 70 | bool BinaryStoreBlobHandler::deleteBlob(const std::string& path) |
| 71 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 72 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool BinaryStoreBlobHandler::stat(const std::string& path, |
| 82 | struct BlobMeta* meta) |
| 83 | { |
| 84 | // TODO: implement |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags, |
| 89 | const std::string& path) |
| 90 | { |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 91 | 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 Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 103 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session, |
| 120 | uint32_t offset, |
| 121 | uint32_t requestedSize) |
| 122 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 123 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset, |
| 133 | const std::vector<uint8_t>& data) |
| 134 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 135 | auto it = sessions_.find(session); |
| 136 | if (it == sessions_.end()) |
| 137 | { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | return it->second->write(offset, data); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool 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 | |
| 151 | bool BinaryStoreBlobHandler::commit(uint16_t session, |
| 152 | const std::vector<uint8_t>& data) |
| 153 | { |
| 154 | // TODO: implement |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool BinaryStoreBlobHandler::close(uint16_t session) |
| 159 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 160 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta) |
| 176 | { |
| 177 | // TODO: implement |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool BinaryStoreBlobHandler::expire(uint16_t session) |
| 182 | { |
| 183 | /* Binary store handler doesn't support expire */ |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | } // namespace blobs |