| 20 | } |
| 21 | |
| 22 | void CompressedWriteBuffer::nextImpl() |
| 23 | { |
| 24 | if (!offset()) |
| 25 | return; |
| 26 | |
| 27 | chassert(offset() <= INT_MAX); |
| 28 | UInt32 decompressed_size = static_cast<UInt32>(offset()); |
| 29 | UInt32 compressed_reserve_size = codec->getCompressedReserveSize(decompressed_size); |
| 30 | |
| 31 | /** During compression we need buffer with capacity >= compressed_reserve_size + CHECKSUM_SIZE. |
| 32 | * |
| 33 | * If output buffer has necessary capacity, we can compress data directly into the output buffer. |
| 34 | * Then we can write checksum at the output buffer begin. |
| 35 | * |
| 36 | * If output buffer does not have necessary capacity. Compress data into a temporary buffer. |
| 37 | * Then we can write checksum and copy the temporary buffer into the output buffer. |
| 38 | */ |
| 39 | if (out.available() >= compressed_reserve_size + sizeof(CityHash_v1_0_2::uint128)) |
| 40 | { |
| 41 | char * out_compressed_ptr = out.position() + sizeof(CityHash_v1_0_2::uint128); |
| 42 | UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, out_compressed_ptr); |
| 43 | |
| 44 | CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(out_compressed_ptr, compressed_size); |
| 45 | |
| 46 | writeBinaryLittleEndian(checksum.low64, out); |
| 47 | writeBinaryLittleEndian(checksum.high64, out); |
| 48 | |
| 49 | out.position() += compressed_size; |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | compressed_buffer.resize(compressed_reserve_size); |
| 54 | UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, compressed_buffer.data()); |
| 55 | |
| 56 | CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(compressed_buffer.data(), compressed_size); |
| 57 | |
| 58 | writeBinaryLittleEndian(checksum.low64, out); |
| 59 | writeBinaryLittleEndian(checksum.high64, out); |
| 60 | |
| 61 | out.write(compressed_buffer.data(), compressed_size); |
| 62 | } |
| 63 | |
| 64 | /// Increase buffer size for next data if adaptive buffer size is used and nextImpl was called because of end of buffer. |
| 65 | if (!available() && use_adaptive_buffer_size && memory.size() < adaptive_buffer_max_size) |
| 66 | { |
| 67 | memory.resize(std::min(memory.size() * 2, adaptive_buffer_max_size)); |
| 68 | BufferBase::set(memory.data(), memory.size(), 0); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void CompressedWriteBuffer::finalizeImpl() |
| 73 | { |
nothing calls this directly
no test coverage detected