当前位置:网站首页>ClickHouse Buffer
ClickHouse Buffer
2022-06-24 03:17:00 【jasong】
/** Base class for ReadBuffer and WriteBuffer.
* Contains common types, variables, and functions.
*
* ReadBuffer and WriteBuffer are similar to istream and ostream, respectively.
* They have to be used, because using iostreams it is impossible to effectively implement some operations.
* For example, using istream, you can not quickly read string values from a tab-separated file,
* so that after reading, the position remains immediately after the read value.
* (The only option is to call the std::istream::get() function on each byte, but this slows down due to several virtual calls.)
*
* Read/WriteBuffers provide direct access to the internal buffer, so the necessary operations are implemented more efficiently.
* Only one virtual function nextImpl() is used, which is rarely called:
* - in the case of ReadBuffer - fill in the buffer with new data from the source;
* - in the case of WriteBuffer - write data from the buffer into the receiver.
*
* Read/WriteBuffer can own or not own an own piece of memory.
* In the second case, you can effectively read from an already existing piece of memory / std::string without copying it.
*/
class BufferBase
{
public:
/** Cursor in the buffer. The position of write or read. */
using Position = char *;
/** A reference to the range of memory. */
struct Buffer
{
Buffer(Position begin_pos_, Position end_pos_) : begin_pos(begin_pos_), end_pos(end_pos_) {}
inline Position begin() const { return begin_pos; }
inline Position end() const { return end_pos; }
inline size_t size() const { return size_t(end_pos - begin_pos); }
inline void resize(size_t size) { end_pos = begin_pos + size; }
inline bool empty() const { return size() == 0; }
inline void swap(Buffer & other)
{
std::swap(begin_pos, other.begin_pos);
std::swap(end_pos, other.end_pos);
}
private:
Position begin_pos;
Position end_pos; /// 1 byte after the end of the buffer
};
/** The constructor takes a range of memory to use for the buffer.
* offset - the starting point of the cursor. ReadBuffer must set it to the end of the range, and WriteBuffer - to the beginning.
*/
BufferBase(Position ptr, size_t size, size_t offset)
: pos(ptr + offset), working_buffer(ptr, ptr + size), internal_buffer(ptr, ptr + size) {}
void set(Position ptr, size_t size, size_t offset)
{
internal_buffer = Buffer(ptr, ptr + size);
working_buffer = Buffer(ptr, ptr + size);
pos = ptr + offset;
}
/// get buffer
inline Buffer & internalBuffer() { return internal_buffer; }
/// get the part of the buffer from which you can read / write data
inline Buffer & buffer() { return working_buffer; }
/// get (for reading and modifying) the position in the buffer
inline Position & position() { return pos; }
/// offset in bytes of the cursor from the beginning of the buffer
inline size_t offset() const { return size_t(pos - working_buffer.begin()); }
/// How many bytes are available for read/write
inline size_t available() const { return size_t(working_buffer.end() - pos); }
inline void swap(BufferBase & other)
{
internal_buffer.swap(other.internal_buffer);
working_buffer.swap(other.working_buffer);
std::swap(pos, other.pos);
}
/** How many bytes have been read/written, counting those that are still in the buffer. */
size_t count() const { return bytes + offset(); }
/** Check that there is more bytes in buffer after cursor. */
bool ALWAYS_INLINE hasPendingData() const { return available() > 0; }
bool isPadded() const { return padded; }
protected:
/// Read/write position.
Position pos;
/** How many bytes have been read/written, not counting those that are now in the buffer.
* (counting those that were already used and "removed" from the buffer)
*/
size_t bytes = 0;
/** A piece of memory that you can use.
* For example, if internal_buffer is 1MB, and from a file for reading it was loaded into the buffer
* only 10 bytes, then working_buffer will be 10 bytes in size
* (working_buffer.end() will point to the position immediately after the 10 bytes that can be read).
*/
Buffer working_buffer;
/// A reference to a piece of memory for the buffer.
Buffer internal_buffer;
/// Indicator of 15 bytes pad_right
bool padded{false};
};
边栏推荐
- Instructions for performance pressure test tool
- Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)
- Why should I change my PC to a cloud desktop server? What are the characteristics of this server?
- UI automation based on Selenium
- 2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report
- What is the role of the distributed configuration center? What are the advantages of a distributed configuration center?
- Is the cloud game edge computing server highly required? What problems will occur during the use of cloud game edge computing server?
- What are the security guarantees for cloud desktop servers? What are the cloud desktop server platforms?
- Why can't the fortress machine open the port? There is a problem with the use of the fortress machine port
- How to check the progress of trademark registration? Where can I find it?
猜你喜欢
![[51nod] 2106 an odd number times](/img/af/59b441420aa4f12fd50f5062a83fae.jpg)
[51nod] 2106 an odd number times

2022-2028 global cell-based seafood industry research and trend analysis report

2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis

2022-2028 global medical coating materials industry research and trend analysis report

2022-2028 global high tibial osteotomy plate industry research and trend analysis report
![[51nod] 3395 n-bit gray code](/img/b5/2c072a11601de82cb92ade94672ecd.jpg)
[51nod] 3395 n-bit gray code

2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report

Simple and beautiful weather code
![[51nod] 2653 section XOR](/img/2d/cb4bf4e14939ce432cac6d35b6a41b.jpg)
[51nod] 2653 section XOR
随机推荐
How to check the progress of trademark registration? Where can I find it?
Supply chain system platform: two management areas
How to design a hybrid system
Grp: how to automatically add requestid in GRP service?
Why can't the fortress machine log in? What are the ways to solve the problem
Tencent Mu Lei: real scene 3D linking industrial Internet and consumer Internet
No monitoring information seen in kibana
How is intelligent character recognition realized? Is the rate of intelligent character recognition high?
RI Geng series: write a simple shell script, but it seems to have technical content
An example of SPM manual binding execution plan
What is etcd and its application scenarios
Liaoyuan social cloud primary actual combat camp test environment script
How to register a trademark? Is the process troublesome?
2022-2028 global anti counterfeiting label industry research and trend analysis report
Does the user need a code signing certificate? What is the use of a code signing certificate
Is it necessary to buy EIP? Price analysis of EIP
Concise and practical time code
What is distributed configuration center Nacos? What are the functions of distributed configuration center Nacos?
Innovation or hype? Is low code a real artifact or a fake tuyere?
The server size of the cloud desktop. The cloud desktop faces the server configuration requirements