ClickHouse/src/IO/WriteBufferFromVector.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
3.0 KiB
C++
Raw Normal View History

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
#include <IO/WriteBuffer.h>
2011-10-16 01:57:10 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_WRITE_AFTER_END_OF_BUFFER;
}
struct AppendModeTag {};
/** Writes data to existing std::vector or similar type. When not enough space, it doubles vector size.
*
* In destructor, vector is cut to the size of written data.
2020-07-19 02:55:08 +00:00
* You can call 'finalize' to resize earlier.
*
2021-11-10 22:58:56 +00:00
* The vector should live until this object is destroyed or until the 'finalizeImpl()' method is called.
2011-10-16 01:57:10 +00:00
*/
template <typename VectorType>
2011-10-16 01:57:10 +00:00
class WriteBufferFromVector : public WriteBuffer
{
public:
2021-02-26 15:29:26 +00:00
explicit WriteBufferFromVector(VectorType & vector_)
2017-11-15 18:24:48 +00:00
: WriteBuffer(reinterpret_cast<Position>(vector_.data()), vector_.size()), vector(vector_)
2011-10-16 01:57:10 +00:00
{
if (vector.empty())
{
vector.resize(initial_size);
set(reinterpret_cast<Position>(vector.data()), vector.size());
2011-10-16 01:57:10 +00:00
}
}
2019-05-18 09:43:43 +00:00
/// Append to vector instead of rewrite.
2019-05-13 23:44:55 +00:00
WriteBufferFromVector(VectorType & vector_, AppendModeTag)
: WriteBuffer(nullptr, 0), vector(vector_)
{
size_t old_size = vector.size();
size_t size = (old_size < initial_size) ? initial_size
: ((old_size < vector.capacity()) ? vector.capacity()
: vector.capacity() * size_multiplier);
vector.resize(size);
set(reinterpret_cast<Position>(vector.data() + old_size), (size - old_size) * sizeof(typename VectorType::value_type));
2019-05-13 23:44:55 +00:00
}
2021-11-10 22:58:56 +00:00
bool isFinished() const { return finalized; }
void restart()
{
if (vector.empty())
vector.resize(initial_size);
set(reinterpret_cast<Position>(vector.data()), vector.size());
finalized = false;
}
~WriteBufferFromVector() override
{
2021-11-11 17:27:23 +00:00
finalize();
2021-11-10 22:58:56 +00:00
}
private:
void finalizeImpl() override
2021-11-10 22:58:56 +00:00
{
vector.resize(
((position() - reinterpret_cast<Position>(vector.data())) /// NOLINT
+ sizeof(typename VectorType::value_type) - 1) /// Align up.
/ sizeof(typename VectorType::value_type));
/// Prevent further writes.
set(nullptr, 0);
}
2021-11-10 22:58:56 +00:00
void nextImpl() override
{
2021-11-10 22:58:56 +00:00
if (finalized)
throw Exception("WriteBufferFromVector is finalized", ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER);
2021-11-10 22:58:56 +00:00
size_t old_size = vector.size();
/// 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>(vector.data());
vector.resize(old_size * size_multiplier);
internal_buffer = Buffer(reinterpret_cast<Position>(vector.data() + pos_offset), reinterpret_cast<Position>(vector.data() + vector.size()));
working_buffer = internal_buffer;
}
2021-11-10 22:58:56 +00:00
VectorType & vector;
static constexpr size_t initial_size = 32;
static constexpr size_t size_multiplier = 2;
2011-10-16 01:57:10 +00:00
};
}