|
1 | 1 | /* |
2 | | - * Copyright (C) 2019-2024 HERE Europe B.V. |
| 2 | + * Copyright (C) 2019-2026 HERE Europe B.V. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
22 | 22 | #include <cstring> |
23 | 23 | #include <map> |
24 | 24 | #include <memory> |
25 | | -#include <mutex> |
26 | | -#include <unordered_map> |
27 | 25 |
|
28 | 26 | #include <olp/core/client/OlpClient.h> |
| 27 | +#include <olp/core/utils/Url.h> |
29 | 28 |
|
30 | 29 | namespace olp { |
31 | 30 | namespace dataservice { |
@@ -55,7 +54,7 @@ BlobApi::DataResponse BlobApi::GetBlob( |
55 | 54 |
|
56 | 55 | // In case we know the size in advance, we should pre-allocated a buffer. |
57 | 56 | const auto expected_size = partition.GetDataSize(); |
58 | | - const auto kPartitionPreallocateLimit = 10 * 1024 * 1024; |
| 57 | + constexpr auto kPartitionPreallocateLimit = 10 * 1024 * 1024; |
59 | 58 | if (expected_size && *expected_size > 0 && |
60 | 59 | *expected_size < kPartitionPreallocateLimit) { |
61 | 60 | buffer.reserve(*expected_size); |
@@ -84,6 +83,51 @@ BlobApi::DataResponse BlobApi::GetBlob( |
84 | 83 | return {std::make_shared<std::vector<unsigned char>>(std::move(buffer)), |
85 | 84 | api_response.GetNetworkStatistics()}; |
86 | 85 | } |
| 86 | + |
| 87 | +BlobApi::DataResponse BlobApi::GetBlobByKey( |
| 88 | + const client::OlpClient& client, const std::string& layer_id, |
| 89 | + const std::string& key, porting::optional<std::string> billing_tag, |
| 90 | + porting::optional<std::string> range, |
| 91 | + const client::CancellationContext& context) { |
| 92 | + std::multimap<std::string, std::string> header_params; |
| 93 | + header_params.emplace("Accept", "application/octet-stream"); |
| 94 | + if (range) { |
| 95 | + header_params.emplace("Range", *range); |
| 96 | + } |
| 97 | + |
| 98 | + std::multimap<std::string, std::string> query_params; |
| 99 | + if (billing_tag) { |
| 100 | + query_params.emplace("billingTag", *billing_tag); |
| 101 | + } |
| 102 | + |
| 103 | + std::string metadata_uri = |
| 104 | + "/layers/" + layer_id + "/keys/" + olp::utils::Url::Encode(key); |
| 105 | + |
| 106 | + std::vector<unsigned char> buffer; |
| 107 | + |
| 108 | + auto data_callback = [&](const std::uint8_t* data, const std::uint64_t offset, |
| 109 | + const std::size_t length) { |
| 110 | + if (!offset) { |
| 111 | + buffer.clear(); |
| 112 | + } |
| 113 | + |
| 114 | + const auto buffer_size = buffer.size(); |
| 115 | + buffer.resize(buffer_size + length); |
| 116 | + std::memcpy(buffer.data() + buffer_size, data, length); |
| 117 | + }; |
| 118 | + |
| 119 | + auto api_response = |
| 120 | + client.CallApiStream(metadata_uri, "GET", query_params, header_params, |
| 121 | + data_callback, nullptr, "", context); |
| 122 | + |
| 123 | + if (api_response.GetStatus() != http::HttpStatusCode::OK) { |
| 124 | + return {client::ApiError(api_response.GetStatus()), |
| 125 | + api_response.GetNetworkStatistics()}; |
| 126 | + } |
| 127 | + |
| 128 | + return {std::make_shared<std::vector<unsigned char>>(std::move(buffer)), |
| 129 | + api_response.GetNetworkStatistics()}; |
| 130 | +} |
87 | 131 | } // namespace read |
88 | 132 | } // namespace dataservice |
89 | 133 | } // namespace olp |
0 commit comments