761417898 commented on code in PR #192: URL: https://github.com/apache/tsfile/pull/192#discussion_r1729796656
########## cpp/src/reader/aligned_chunk_reader.cc: ########## @@ -0,0 +1,623 @@ +/* + * 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. + */ + +#include "aligned_chunk_reader.h" + +#include "compress/compressor_factory.h" +#include "encoding/decoder_factory.h" + +using namespace common; +namespace storage { + +int AlignedChunkReader::init(ReadFile *read_file, String m_name, + TSDataType data_type, Filter *time_filter) { + read_file_ = read_file; + measurement_name_.shallow_copy_from(m_name); + time_decoder_ = DecoderFactory::alloc_time_decoder(); + value_decoder_ = nullptr; + compressor_ = nullptr; + time_filter_ = time_filter; + time_uncompressed_buf_ = nullptr; + value_uncompressed_buf_ = nullptr; + if (IS_NULL(time_decoder_)) { + return E_OOM; + } + return E_OK; +} + +void AlignedChunkReader::reset() { + time_chunk_meta_ = nullptr; + value_chunk_meta_ = nullptr; + time_chunk_header_.reset(); + value_chunk_header_.reset(); + cur_time_page_header_.reset(); + cur_value_page_header_.reset(); + + char *file_data_buf = time_in_stream_.get_wrapped_buf(); + if (file_data_buf != nullptr) { + mem_free(file_data_buf); + } + time_in_stream_.reset(); + file_data_buf = value_in_stream_.get_wrapped_buf(); + if (file_data_buf != nullptr) { + mem_free(file_data_buf); + } + value_in_stream_.reset(); + file_data_time_buf_size_ = 0; + file_data_value_buf_size_ = 0; + time_chunk_visit_offset_ = 0; + value_chunk_visit_offset_ = 0; +} + +void AlignedChunkReader::destroy() { + if (time_decoder_ != nullptr) { + time_decoder_->~Decoder(); + DecoderFactory::free(time_decoder_); + time_decoder_ = nullptr; + } + if (value_decoder_ != nullptr) { + value_decoder_->~Decoder(); + DecoderFactory::free(value_decoder_); + value_decoder_ = nullptr; + } + if (compressor_ != nullptr) { + compressor_->~Compressor(); + CompressorFactory::free(compressor_); + compressor_ = nullptr; + } + char *buf = time_in_stream_.get_wrapped_buf(); + if (buf != nullptr) { + mem_free(buf); + time_in_stream_.clear_wrapped_buf(); + } + cur_time_page_header_.reset(); + buf = value_in_stream_.get_wrapped_buf(); + if (buf != nullptr) { + mem_free(buf); + value_in_stream_.clear_wrapped_buf(); + } + cur_value_page_header_.reset(); +} + +int AlignedChunkReader::load_by_aligned_meta(ChunkMeta *time_chunk_meta, + ChunkMeta *value_chunk_meta) { + int ret = E_OK; + time_chunk_meta_ = time_chunk_meta; + value_chunk_meta_ = value_chunk_meta; +#if DEBUG_SE + std::cout << "AlignedChunkReader::load_by_meta, meta=" << *time_chunk_meta + << ", " << *value_chunk_meta << std::endl; +#endif + /* ================ deserialize time_chunk_header ================*/ + // at least, we can reader the chunk header and the first page header. + // TODO configurable + file_data_time_buf_size_ = 1024; + file_data_value_buf_size_ = 1024; + int32_t ret_read_len = 0; + char *time_file_data_buf = + (char *)mem_alloc(file_data_time_buf_size_, MOD_CHUNK_READER); + if (IS_NULL(time_file_data_buf)) { + return E_OOM; + } + ret = read_file_->read(time_chunk_meta_->offset_of_chunk_header_, + time_file_data_buf, file_data_time_buf_size_, + ret_read_len); + if (IS_SUCC(ret) && ret_read_len < ChunkHeader::MIN_SERIALIZED_SIZE) { + ret = E_TSFILE_CORRUPTED; + LOGE("file corrupted, ret=" << ret << ", offset=" + << time_chunk_meta_->offset_of_chunk_header_ + << "read_len=" << ret_read_len); + } + if (IS_SUCC(ret)) { + time_in_stream_.wrap_from(time_file_data_buf, ret_read_len); + if (RET_FAIL(time_chunk_header_.deserialize_from(time_in_stream_))) { + } else { + time_chunk_visit_offset_ = time_in_stream_.read_pos(); + } + } + /* ================ deserialize value_chunk_header ================*/ + ret_read_len = 0; + char *value_file_data_buf = + (char *)mem_alloc(file_data_value_buf_size_, MOD_CHUNK_READER); + if (IS_NULL(value_file_data_buf)) { + return E_OOM; + } + ret = read_file_->read(value_chunk_meta_->offset_of_chunk_header_, + value_file_data_buf, file_data_value_buf_size_, + ret_read_len); + if (IS_SUCC(ret) && ret_read_len < ChunkHeader::MIN_SERIALIZED_SIZE) { + ret = E_TSFILE_CORRUPTED; + LOGE("file corrupted, ret=" + << ret << ", offset=" << value_chunk_meta_->offset_of_chunk_header_ + << "read_len=" << ret_read_len); + } + if (IS_SUCC(ret)) { + value_in_stream_.wrap_from(value_file_data_buf, ret_read_len); + if (RET_FAIL(value_chunk_header_.deserialize_from(value_in_stream_))) { + } else if (RET_FAIL(alloc_compressor_and_value_decoder( + value_chunk_header_.encoding_type_, + value_chunk_header_.data_type_, + value_chunk_header_.compression_type_))) { + } else { + value_chunk_visit_offset_ = value_in_stream_.read_pos(); +#if DEBUG_SE + std::cout << "AlignedChunkReader::load_by_meta, time_chunk_header=" + << time_chunk_header_ + << ", value_chunk_header=" << value_chunk_header_ + << std::endl; +#endif + } + } + return ret; +} + +int AlignedChunkReader::alloc_compressor_and_value_decoder( + TSEncoding encoding, TSDataType data_type, CompressionType compression) { + if (value_decoder_ != nullptr) { + value_decoder_->reset(); + } else { + value_decoder_ = + DecoderFactory::alloc_value_decoder(encoding, data_type); + if (IS_NULL(value_decoder_)) { + return E_OOM; + } + } + + if (compressor_ != nullptr) { + compressor_->reset(false); + } else { + compressor_ = CompressorFactory::alloc_compressor(compression); + if (compressor_ == nullptr) { + return E_OOM; + } + } + return E_OK; +} + +int AlignedChunkReader::get_next_page(TsBlock *ret_tsblock, + Filter *oneshoot_filter) { + int ret = E_OK; + Filter *filter = + (oneshoot_filter != nullptr ? oneshoot_filter : time_filter_); + + if (prev_time_page_not_finish() && prev_value_page_not_finish()) { + ret = decode_time_value_buf_into_tsblock(ret_tsblock, oneshoot_filter); + return ret; + } + if (!prev_time_page_not_finish()) { + while (IS_SUCC(ret)) { + if (RET_FAIL(get_cur_page_header( + time_chunk_meta_, time_in_stream_, cur_time_page_header_, + time_chunk_visit_offset_, time_chunk_header_))) { + } else if (cur_page_statisify_filter(filter)) { Review Comment: Modify the code to check if both the time page and the value page satisfy the filters. If they do not satisfy the filters, both the time page and the value page will be skipped. -- 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]
