2021-09-29 14:44:53 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
#include <Common/config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_AZURE_BLOB_STORAGE
|
|
|
|
|
|
|
|
#include <IO/WriteBufferFromBlobStorage.h>
|
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
|
2021-09-29 14:44:53 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-10-08 14:34:40 +00:00
|
|
|
// TODO: abstract this function from DiskS3.cpp, from where it was copy-pasted
|
|
|
|
String getRandomName(char first = 'a', char last = 'z', size_t len = 64)
|
|
|
|
{
|
|
|
|
std::uniform_int_distribution<int> distribution(first, last);
|
|
|
|
String res(len, ' ');
|
|
|
|
for (auto & c : res)
|
|
|
|
c = distribution(thread_local_rng);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
WriteBufferFromBlobStorage::WriteBufferFromBlobStorage(
|
2021-10-19 09:30:15 +00:00
|
|
|
std::shared_ptr<Azure::Storage::Blobs::BlobContainerClient> blob_container_client_,
|
2021-10-01 08:32:28 +00:00
|
|
|
const String & blob_path_,
|
2021-10-08 14:34:40 +00:00
|
|
|
UInt64 max_single_part_upload_size_,
|
2021-10-01 08:32:28 +00:00
|
|
|
size_t buf_size_) :
|
2021-10-01 13:41:10 +00:00
|
|
|
BufferWithOwnMemory<WriteBuffer>(buf_size_, nullptr, 0),
|
2021-10-01 08:32:28 +00:00
|
|
|
blob_container_client(blob_container_client_),
|
2021-10-08 14:34:40 +00:00
|
|
|
max_single_part_upload_size(max_single_part_upload_size_),
|
2021-10-06 15:12:24 +00:00
|
|
|
blob_path(blob_path_) {}
|
2021-10-01 13:41:10 +00:00
|
|
|
|
2021-10-01 08:32:28 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
void WriteBufferFromBlobStorage::nextImpl()
|
|
|
|
{
|
2021-10-01 13:41:10 +00:00
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2021-10-05 12:00:59 +00:00
|
|
|
auto pos = working_buffer.begin();
|
|
|
|
auto len = offset();
|
2021-10-19 09:30:15 +00:00
|
|
|
auto block_blob_client = blob_container_client->GetBlockBlobClient(blob_path);
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
size_t read = 0;
|
|
|
|
while (read < len)
|
2021-10-08 14:34:40 +00:00
|
|
|
{
|
2021-10-21 10:14:41 +00:00
|
|
|
auto part_len = std::min(len - read, max_single_part_upload_size);
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
auto block_id = getRandomName();
|
|
|
|
block_ids.push_back(block_id);
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
Azure::Core::IO::MemoryBodyStream tmp_buffer(reinterpret_cast<uint8_t *>(pos + read), part_len);
|
|
|
|
block_blob_client.StageBlock(block_ids.back(), tmp_buffer);
|
2021-10-01 13:41:10 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
read += part_len;
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
void WriteBufferFromBlobStorage::finalize()
|
|
|
|
{
|
|
|
|
if (finalized)
|
|
|
|
return;
|
2021-10-08 14:34:40 +00:00
|
|
|
|
2021-10-21 10:14:41 +00:00
|
|
|
auto block_blob_client = blob_container_client->GetBlockBlobClient(blob_path);
|
|
|
|
block_blob_client.CommitBlockList(block_ids);
|
|
|
|
finalized = true;
|
2021-09-29 14:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|