2011-10-16 01:57:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2011-11-18 20:17:27 +00:00
|
|
|
#include <vector>
|
2011-10-16 01:57:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
2011-10-16 01:57:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-30 19:47:47 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_WRITE_AFTER_END_OF_BUFFER;
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:36:41 +00:00
|
|
|
/** Writes data to existing std::vector or similar type. When not enough space, it doubles vector size.
|
|
|
|
*
|
|
|
|
* In destructor, vector is cutted to the size of written data.
|
|
|
|
* You can call to 'finish' to resize earlier.
|
|
|
|
*
|
|
|
|
* The vector should live until this object is destroyed or until the 'finish' method is called.
|
2011-10-16 01:57:10 +00:00
|
|
|
*/
|
2018-11-30 15:36:41 +00:00
|
|
|
template <typename VectorType>
|
2011-10-16 01:57:10 +00:00
|
|
|
class WriteBufferFromVector : public WriteBuffer
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
VectorType & vector;
|
2018-11-30 15:36:41 +00:00
|
|
|
bool is_finished = false;
|
2011-10-16 01:57:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void nextImpl() override
|
|
|
|
{
|
2018-11-30 19:47:47 +00:00
|
|
|
if (is_finished)
|
|
|
|
throw Exception("WriteBufferFromVector is finished", ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t old_size = vector.size();
|
|
|
|
vector.resize(old_size * 2);
|
2019-03-07 20:04:59 +00:00
|
|
|
internal_buffer = Buffer(reinterpret_cast<Position>(vector.data() + old_size), reinterpret_cast<Position>(vector.data() + vector.size()));
|
2017-04-01 07:20:54 +00:00
|
|
|
working_buffer = internal_buffer;
|
|
|
|
}
|
2011-10-16 01:57:10 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferFromVector(VectorType & vector_)
|
2017-11-15 18:24:48 +00:00
|
|
|
: WriteBuffer(reinterpret_cast<Position>(vector_.data()), vector_.size()), vector(vector_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (vector.empty())
|
|
|
|
{
|
2018-11-30 15:36:41 +00:00
|
|
|
static constexpr size_t initial_size = 32;
|
|
|
|
vector.resize(initial_size);
|
2018-09-02 03:00:04 +00:00
|
|
|
set(reinterpret_cast<Position>(vector.data()), vector.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 15:36:41 +00:00
|
|
|
|
|
|
|
void finish()
|
|
|
|
{
|
2018-12-01 02:38:54 +00:00
|
|
|
if (is_finished)
|
|
|
|
return;
|
2018-11-30 15:36:41 +00:00
|
|
|
is_finished = true;
|
|
|
|
vector.resize(
|
|
|
|
((position() - reinterpret_cast<Position>(vector.data()))
|
|
|
|
+ sizeof(typename VectorType::value_type) - 1) /// Align up.
|
|
|
|
/ sizeof(typename VectorType::value_type));
|
2018-11-30 19:47:47 +00:00
|
|
|
|
|
|
|
/// Prevent further writes.
|
|
|
|
set(nullptr, 0);
|
2018-11-30 15:36:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:32:59 +00:00
|
|
|
bool isFinished() const { return is_finished; }
|
|
|
|
|
|
|
|
void restart()
|
|
|
|
{
|
|
|
|
set(reinterpret_cast<Position>(vector.data()), vector.size());
|
|
|
|
is_finished = false;
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:36:41 +00:00
|
|
|
~WriteBufferFromVector() override
|
|
|
|
{
|
|
|
|
if (!is_finished)
|
|
|
|
finish();
|
|
|
|
}
|
2011-10-16 01:57:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|