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> |
| 4 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 5 | namespace blobs |
| 6 | { |
| 7 | |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 8 | namespace internal |
| 9 | { |
| 10 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 11 | /** |
| 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 | */ |
| 18 | static std::string getBaseFromId(const std::string& blobId) |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 19 | { |
| 20 | return blobId.substr(0, blobId.find_last_of('/') + 1); |
| 21 | } |
| 22 | |
| 23 | } // namespace internal |
| 24 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 25 | void 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 32 | bool BinaryStoreBlobHandler::canHandleBlob(const std::string& path) |
| 33 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 34 | 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 Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 41 | return std::any_of(stores_.begin(), stores_.end(), |
| 42 | [&](const auto& baseStorePair) { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 43 | return base == baseStorePair.second->getBaseBlobId(); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 44 | }); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | std::vector<std::string> BinaryStoreBlobHandler::getBlobIds() |
| 48 | { |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 49 | std::vector<std::string> result; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 50 | |
| 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 57 | return result; |
| 58 | } |
| 59 | |
| 60 | bool BinaryStoreBlobHandler::deleteBlob(const std::string& path) |
| 61 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 62 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool BinaryStoreBlobHandler::stat(const std::string& path, |
| 72 | struct BlobMeta* meta) |
| 73 | { |
| 74 | // TODO: implement |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags, |
| 79 | const std::string& path) |
| 80 | { |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 81 | 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 Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 93 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session, |
| 110 | uint32_t offset, |
| 111 | uint32_t requestedSize) |
| 112 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 113 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset, |
| 123 | const std::vector<uint8_t>& data) |
| 124 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 125 | auto it = sessions_.find(session); |
| 126 | if (it == sessions_.end()) |
| 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return it->second->write(offset, data); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | bool 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 | |
| 141 | bool BinaryStoreBlobHandler::commit(uint16_t session, |
| 142 | const std::vector<uint8_t>& data) |
| 143 | { |
| 144 | // TODO: implement |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool BinaryStoreBlobHandler::close(uint16_t session) |
| 149 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 150 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta) |
| 166 | { |
| 167 | // TODO: implement |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | bool BinaryStoreBlobHandler::expire(uint16_t session) |
| 172 | { |
| 173 | /* Binary store handler doesn't support expire */ |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | } // namespace blobs |