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 | |
| 11 | /* Strip the basename till the last '/' */ |
| 12 | std::string getBaseFromId(const std::string& blobId) |
| 13 | { |
| 14 | return blobId.substr(0, blobId.find_last_of('/') + 1); |
| 15 | } |
| 16 | |
| 17 | } // namespace internal |
| 18 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 19 | void 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 26 | bool BinaryStoreBlobHandler::canHandleBlob(const std::string& path) |
| 27 | { |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 28 | return std::any_of(stores_.begin(), stores_.end(), |
| 29 | [&](const auto& baseStorePair) { |
| 30 | return baseStorePair.second->canHandleBlob(path); |
| 31 | }); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | std::vector<std::string> BinaryStoreBlobHandler::getBlobIds() |
| 35 | { |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 36 | std::vector<std::string> result; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 37 | |
| 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 44 | return result; |
| 45 | } |
| 46 | |
| 47 | bool BinaryStoreBlobHandler::deleteBlob(const std::string& path) |
| 48 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame^] | 49 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool BinaryStoreBlobHandler::stat(const std::string& path, |
| 59 | struct BlobMeta* meta) |
| 60 | { |
| 61 | // TODO: implement |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | bool BinaryStoreBlobHandler::open(uint16_t session, uint16_t flags, |
| 66 | const std::string& path) |
| 67 | { |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 68 | 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 Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 80 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | std::vector<uint8_t> BinaryStoreBlobHandler::read(uint16_t session, |
| 97 | uint32_t offset, |
| 98 | uint32_t requestedSize) |
| 99 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame^] | 100 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool BinaryStoreBlobHandler::write(uint16_t session, uint32_t offset, |
| 110 | const std::vector<uint8_t>& data) |
| 111 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame^] | 112 | auto it = sessions_.find(session); |
| 113 | if (it == sessions_.end()) |
| 114 | { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | return it->second->write(offset, data); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool 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 | |
| 128 | bool BinaryStoreBlobHandler::commit(uint16_t session, |
| 129 | const std::vector<uint8_t>& data) |
| 130 | { |
| 131 | // TODO: implement |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | bool BinaryStoreBlobHandler::close(uint16_t session) |
| 136 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame^] | 137 | 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 Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | bool BinaryStoreBlobHandler::stat(uint16_t session, struct BlobMeta* meta) |
| 153 | { |
| 154 | // TODO: implement |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool BinaryStoreBlobHandler::expire(uint16_t session) |
| 159 | { |
| 160 | /* Binary store handler doesn't support expire */ |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | } // namespace blobs |