diff --git a/src/Disks/S3/DiskS3.cpp b/src/Disks/S3/DiskS3.cpp index 42c022a3714..3d91d5fbb78 100644 --- a/src/Disks/S3/DiskS3.cpp +++ b/src/Disks/S3/DiskS3.cpp @@ -249,7 +249,7 @@ public: if (whence == SEEK_CUR) { /// If position within current working buffer - shift pos. - if (working_buffer.size() && size_t(getPosition() + offset_) < absolute_position) + if (!working_buffer.empty() && size_t(getPosition() + offset_) < absolute_position) { pos += offset_; return getPosition(); @@ -262,7 +262,7 @@ public: else if (whence == SEEK_SET) { /// If position within current working buffer - shift pos. - if (working_buffer.size() && size_t(offset_) >= absolute_position - working_buffer.size() + if (!working_buffer.empty() && size_t(offset_) >= absolute_position - working_buffer.size() && size_t(offset_) < absolute_position) { pos = working_buffer.end() - (absolute_position - offset_); diff --git a/src/IO/BrotliReadBuffer.cpp b/src/IO/BrotliReadBuffer.cpp index 70d3a76e629..41991ad0516 100644 --- a/src/IO/BrotliReadBuffer.cpp +++ b/src/IO/BrotliReadBuffer.cpp @@ -77,7 +77,7 @@ bool BrotliReadBuffer::nextImpl() if (in->eof()) { eof = true; - return working_buffer.size() != 0; + return !working_buffer.empty(); } else { diff --git a/src/IO/BufferBase.h b/src/IO/BufferBase.h index c22dcbecf7b..198441d8bc1 100644 --- a/src/IO/BufferBase.h +++ b/src/IO/BufferBase.h @@ -40,6 +40,7 @@ public: 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) { diff --git a/src/IO/ConcatReadBuffer.h b/src/IO/ConcatReadBuffer.h index 1df99429e93..c416b0fd892 100644 --- a/src/IO/ConcatReadBuffer.h +++ b/src/IO/ConcatReadBuffer.h @@ -25,11 +25,16 @@ protected: return false; /// First reading - if (working_buffer.size() == 0 && (*current)->hasPendingData()) + if (working_buffer.empty()) { - working_buffer = Buffer((*current)->position(), (*current)->buffer().end()); - return true; + if ((*current)->hasPendingData()) + { + working_buffer = Buffer((*current)->position(), (*current)->buffer().end()); + return true; + } } + else + (*current)->position() = position(); if (!(*current)->next()) { @@ -51,14 +56,12 @@ protected: } public: - ConcatReadBuffer(const ReadBuffers & buffers_) : ReadBuffer(nullptr, 0), buffers(buffers_), current(buffers.begin()) {} - - ConcatReadBuffer(ReadBuffer & buf1, ReadBuffer & buf2) : ReadBuffer(nullptr, 0) + explicit ConcatReadBuffer(const ReadBuffers & buffers_) : ReadBuffer(nullptr, 0), buffers(buffers_), current(buffers.begin()) { - buffers.push_back(&buf1); - buffers.push_back(&buf2); - current = buffers.begin(); + assert(!buffers.empty()); } + + ConcatReadBuffer(ReadBuffer & buf1, ReadBuffer & buf2) : ConcatReadBuffer({&buf1, &buf2}) {} }; } diff --git a/src/IO/HashingReadBuffer.h b/src/IO/HashingReadBuffer.h index 9fcd6dc6b41..08b6de69dcb 100644 --- a/src/IO/HashingReadBuffer.h +++ b/src/IO/HashingReadBuffer.h @@ -1,10 +1,11 @@ #pragma once -#include #include +#include namespace DB { + /* * Calculates the hash from the read data. When reading, the data is read from the nested ReadBuffer. * Small pieces are copied into its own memory. @@ -12,14 +13,14 @@ namespace DB class HashingReadBuffer : public IHashingBuffer { public: - HashingReadBuffer(ReadBuffer & in_, size_t block_size_ = DBMS_DEFAULT_HASHING_BLOCK_SIZE) : - IHashingBuffer(block_size_), in(in_) + explicit HashingReadBuffer(ReadBuffer & in_, size_t block_size_ = DBMS_DEFAULT_HASHING_BLOCK_SIZE) + : IHashingBuffer(block_size_), in(in_) { working_buffer = in.buffer(); pos = in.position(); /// calculate hash from the data already read - if (working_buffer.size()) + if (!working_buffer.empty()) { calculateHash(pos, working_buffer.end() - pos); } @@ -39,7 +40,7 @@ private: return res; } -private: ReadBuffer & in; }; + } diff --git a/src/IO/LZMAInflatingReadBuffer.cpp b/src/IO/LZMAInflatingReadBuffer.cpp index e30e8df5f9d..6a0a7e5ee31 100644 --- a/src/IO/LZMAInflatingReadBuffer.cpp +++ b/src/IO/LZMAInflatingReadBuffer.cpp @@ -66,7 +66,7 @@ bool LZMAInflatingReadBuffer::nextImpl() if (in->eof()) { eof = true; - return working_buffer.size() != 0; + return !working_buffer.empty(); } else { diff --git a/src/IO/LimitReadBuffer.cpp b/src/IO/LimitReadBuffer.cpp index f36facfdd99..b0d734c9ca0 100644 --- a/src/IO/LimitReadBuffer.cpp +++ b/src/IO/LimitReadBuffer.cpp @@ -50,7 +50,7 @@ LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exc LimitReadBuffer::~LimitReadBuffer() { /// Update underlying buffer's position in case when limit wasn't reached. - if (working_buffer.size() != 0) + if (!working_buffer.empty()) in.position() = position(); } diff --git a/src/IO/MemoryReadWriteBuffer.cpp b/src/IO/MemoryReadWriteBuffer.cpp index 0b0d9704de6..69bcd52a8d2 100644 --- a/src/IO/MemoryReadWriteBuffer.cpp +++ b/src/IO/MemoryReadWriteBuffer.cpp @@ -61,7 +61,7 @@ private: position() = nullptr; } - return buffer().size() != 0; + return !buffer().empty(); } using Container = std::forward_list; diff --git a/src/IO/ReadBuffer.h b/src/IO/ReadBuffer.h index 68ebf154597..e871205aef3 100644 --- a/src/IO/ReadBuffer.h +++ b/src/IO/ReadBuffer.h @@ -55,6 +55,7 @@ public: */ bool next() { + assert(!hasPendingData()); assert(position() <= working_buffer.end()); bytes += offset(); @@ -77,7 +78,7 @@ public: next(); } - virtual ~ReadBuffer() {} + virtual ~ReadBuffer() = default; /** Unlike std::istream, it returns true if all data was read @@ -197,7 +198,7 @@ private: */ virtual bool nextImpl() { return false; } - [[noreturn]] void throwReadAfterEOF() + [[noreturn]] static void throwReadAfterEOF() { throw Exception("Attempt to read after eof", ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF); } diff --git a/src/IO/ReadWriteBufferFromHTTP.h b/src/IO/ReadWriteBufferFromHTTP.h index de10f268dc3..9cd37bd00f8 100644 --- a/src/IO/ReadWriteBufferFromHTTP.h +++ b/src/IO/ReadWriteBufferFromHTTP.h @@ -76,9 +76,7 @@ public: } } - virtual ~UpdatableSessionBase() - { - } + virtual ~UpdatableSessionBase() = default; }; @@ -205,6 +203,8 @@ namespace detail { if (next_callback) next_callback(count()); + if (!working_buffer.empty()) + impl->position() = position(); if (!impl->next()) return false; internal_buffer = impl->buffer(); diff --git a/src/IO/WriteBuffer.h b/src/IO/WriteBuffer.h index 6abcc1c8eed..d425f813d7b 100644 --- a/src/IO/WriteBuffer.h +++ b/src/IO/WriteBuffer.h @@ -61,7 +61,7 @@ public: /** it is desirable in the derived classes to place the next() call in the destructor, * so that the last data is written */ - virtual ~WriteBuffer() {} + virtual ~WriteBuffer() = default; inline void nextIfAtEnd() { @@ -75,7 +75,7 @@ public: size_t bytes_copied = 0; /// Produces endless loop - assert(working_buffer.size() > 0); + assert(!working_buffer.empty()); while (bytes_copied < n) { diff --git a/src/IO/ZlibInflatingReadBuffer.cpp b/src/IO/ZlibInflatingReadBuffer.cpp index 0b23bef1b10..bea83c74e21 100644 --- a/src/IO/ZlibInflatingReadBuffer.cpp +++ b/src/IO/ZlibInflatingReadBuffer.cpp @@ -70,7 +70,7 @@ bool ZlibInflatingReadBuffer::nextImpl() if (in->eof()) { eof = true; - return working_buffer.size() != 0; + return !working_buffer.empty(); } else { diff --git a/src/IO/ZstdInflatingReadBuffer.cpp b/src/IO/ZstdInflatingReadBuffer.cpp index 94a0b56fc6d..b441a6a7210 100644 --- a/src/IO/ZstdInflatingReadBuffer.cpp +++ b/src/IO/ZstdInflatingReadBuffer.cpp @@ -54,7 +54,7 @@ bool ZstdInflatingReadBuffer::nextImpl() if (in->eof()) { eof = true; - return working_buffer.size() != 0; + return !working_buffer.empty(); } return true; diff --git a/src/Server/HTTPHandler.cpp b/src/Server/HTTPHandler.cpp index e161b5752ae..5e0d1f0ac66 100644 --- a/src/Server/HTTPHandler.cpp +++ b/src/Server/HTTPHandler.cpp @@ -219,8 +219,11 @@ void HTTPHandler::pushDelayedResults(Output & used_output) } } - ConcatReadBuffer concat_read_buffer(read_buffers_raw_ptr); - copyData(concat_read_buffer, *used_output.out_maybe_compressed); + if (!read_buffers_raw_ptr.empty()) + { + ConcatReadBuffer concat_read_buffer(read_buffers_raw_ptr); + copyData(concat_read_buffer, *used_output.out_maybe_compressed); + } }