2021-01-19 09:40:25 +00:00
|
|
|
#include <Coordination/WriteBufferFromNuraftBuffer.h>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-01-19 09:40:25 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_WRITE_AFTER_END_OF_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBufferFromNuraftBuffer::nextImpl()
|
|
|
|
{
|
2021-11-10 22:58:56 +00:00
|
|
|
if (finalized)
|
2021-01-19 09:40:25 +00:00
|
|
|
throw Exception("WriteBufferFromNuraftBuffer is finished", ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER);
|
|
|
|
|
|
|
|
/// pos may not be equal to vector.data() + old_size, because WriteBuffer::next() can be used to flush data
|
|
|
|
size_t pos_offset = pos - reinterpret_cast<Position>(buffer->data_begin());
|
2021-01-25 10:19:02 +00:00
|
|
|
size_t old_size = buffer->size();
|
|
|
|
if (pos_offset == old_size)
|
|
|
|
{
|
|
|
|
nuraft::ptr<nuraft::buffer> new_buffer = nuraft::buffer::alloc(old_size * size_multiplier);
|
|
|
|
memcpy(new_buffer->data_begin(), buffer->data_begin(), buffer->size());
|
|
|
|
buffer = new_buffer;
|
|
|
|
}
|
2021-01-19 09:40:25 +00:00
|
|
|
internal_buffer = Buffer(reinterpret_cast<Position>(buffer->data_begin() + pos_offset), reinterpret_cast<Position>(buffer->data_begin() + buffer->size()));
|
|
|
|
working_buffer = internal_buffer;
|
2021-01-25 10:19:02 +00:00
|
|
|
|
2021-01-19 09:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteBufferFromNuraftBuffer::WriteBufferFromNuraftBuffer()
|
|
|
|
: WriteBuffer(nullptr, 0)
|
|
|
|
{
|
|
|
|
buffer = nuraft::buffer::alloc(initial_size);
|
|
|
|
set(reinterpret_cast<Position>(buffer->data_begin()), buffer->size());
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:58:56 +00:00
|
|
|
void WriteBufferFromNuraftBuffer::finalizeImpl()
|
2021-01-19 09:40:25 +00:00
|
|
|
{
|
2021-01-25 10:19:02 +00:00
|
|
|
size_t real_size = pos - reinterpret_cast<Position>(buffer->data_begin());
|
2021-01-19 09:40:25 +00:00
|
|
|
nuraft::ptr<nuraft::buffer> new_buffer = nuraft::buffer::alloc(real_size);
|
|
|
|
memcpy(new_buffer->data_begin(), buffer->data_begin(), real_size);
|
|
|
|
buffer = new_buffer;
|
|
|
|
|
|
|
|
/// Prevent further writes.
|
|
|
|
set(nullptr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nuraft::ptr<nuraft::buffer> WriteBufferFromNuraftBuffer::getBuffer()
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:25:30 +00:00
|
|
|
WriteBufferFromNuraftBuffer::~WriteBufferFromNuraftBuffer()
|
2021-01-19 09:40:25 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|