761417898 commented on code in PR #542: URL: https://github.com/apache/tsfile/pull/542#discussion_r2206936803
########## cpp/src/encoding/int32_rle_decoder.h: ########## @@ -0,0 +1,246 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * License); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef ENCODING_INT32RLE_DECODER_H +#define ENCODING_INT32RLE_DECODER_H + +#include <vector> + +#include "common/allocator/alloc_base.h" +#include "decoder.h" +#include "encoder.h" +#include "encoding/encode_utils.h" +#include "encoding/int32_packer.h" + +namespace storage { + +class Int32RleDecoder : public Decoder { + private: + uint32_t length_; + uint32_t bit_width_; + int bitpacking_num_; + bool is_length_and_bitwidth_readed_; + int current_count_; + common::ByteStream byte_cache_; + int32_t *current_buffer_; + Int32Packer *packer_; + uint8_t *tmp_buf_; + + public: + Int32RleDecoder() + : length_(0), + bit_width_(0), + bitpacking_num_(0), + is_length_and_bitwidth_readed_(false), + current_count_(0), + byte_cache_(1024, common::MOD_DECODER_OBJ), + current_buffer_(nullptr), + packer_(nullptr), + tmp_buf_(nullptr) {} + ~Int32RleDecoder() override { destroy(); } + + bool has_remaining() override { return has_next_package(); } + int read_boolean(bool &ret_value, common::ByteStream &in) { + int32_t bool_value; + read_int32(bool_value, in); + ret_value = bool_value == 0 ? false : true; + return common::E_OK; + } + int read_int32(int32_t &ret_value, common::ByteStream &in) override { + ret_value = static_cast<int32_t>(read_int(in)); + return common::E_OK; + } + int read_int64(int64_t &ret_value, common::ByteStream &in) override { + return common::E_TYPE_NOT_MATCH; + } + int read_float(float &ret_value, common::ByteStream &in) override { + return common::E_TYPE_NOT_MATCH; + } + int read_double(double &ret_value, common::ByteStream &in) override { + return common::E_TYPE_NOT_MATCH; + } + int read_String(common::String &ret_value, common::PageArena &pa, + common::ByteStream &in) override { + return common::E_TYPE_NOT_MATCH; + } + + void init() { + packer_ = nullptr; + is_length_and_bitwidth_readed_ = false; + length_ = 0; + bit_width_ = 0; + bitpacking_num_ = 0; + current_count_ = 0; + } + + bool has_next(common::ByteStream &buffer) { + if (current_count_ > 0 || buffer.remaining_size() > 0 || + has_next_package()) { + return true; + } + return false; + } + + bool has_next_package() { + return current_count_ > 0 || byte_cache_.remaining_size() > 0; + } + + int32_t read_int(common::ByteStream &buffer) { + if (!is_length_and_bitwidth_readed_) { + // start to reader a new rle+bit-packing pattern + read_length_and_bitwidth(buffer); + } + if (current_count_ == 0) { + uint8_t header; + int ret = common::E_OK; + if (RET_FAIL( + common::SerializationUtil::read_ui8(header, byte_cache_))) { + return ret; + } + call_read_bit_packing_buffer(header); + } + --current_count_; + int32_t result = current_buffer_[bitpacking_num_ - current_count_ - 1]; + if (!has_next_package()) { + is_length_and_bitwidth_readed_ = false; + } + return result; + } + + int call_read_bit_packing_buffer(uint8_t header) { + int bit_packed_group_count = (int)(header >> 1); + // in last bit-packing group, there may be some useless value, + // lastBitPackedNum indicates how many values is useful + uint8_t last_bit_packed_num; + int ret = common::E_OK; + if (RET_FAIL(common::SerializationUtil::read_ui8(last_bit_packed_num, + byte_cache_))) { + return ret; + } + if (bit_packed_group_count > 0) { + current_count_ = + (bit_packed_group_count - 1) * 8 + last_bit_packed_num; + bitpacking_num_ = current_count_; + } else { + printf( + "tsfile-encoding IntRleDecoder: bit_packed_group_count %d, " + "smaller " + "than 1", + bit_packed_group_count); Review Comment: fixed. return error code -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
