gaodayue commented on a change in pull request #1379: Add rle page URL: https://github.com/apache/incubator-doris/pull/1379#discussion_r297617809
########## File path: be/src/olap/rowset/segment_v2/rle_page.h ########## @@ -0,0 +1,255 @@ +// 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. + +#pragma once + +#include "olap/rowset/segment_v2/page_builder.h" // for PageBuilder +#include "olap/rowset/segment_v2/page_decoder.h" // for PageDecoder +#include "olap/rowset/segment_v2/options.h" // for PageBuilderOptions/PageDecoderOptions +#include "olap/rowset/segment_v2/common.h" // for rowid_t +#include "util/rle_encoding.h" // for RleEncoder/RleDecoder +#include "util/coding.h" // for encode_fixed32_le/decode_fixed32_le + +namespace doris { +namespace segment_v2 { + +enum { + RLE_BLOCK_HEADER_SIZE = 4 +}; + +// RLE builder for generic integer and bool types. What is missing is some way +// to enforce that this can only be instantiated for INT and BOOL types. +// +// The page format is as follows: +// +// 1. Header: (4 bytes total) +// +// <num_elements> [32-bit] +// The number of elements encoded in the page. +// +// NOTE: all on-disk ints are encoded little-endian +// +// 2. Element data +// +// The header is followed by the rle-encoded element data. +// +// This Rle encoding algorithm is only effective for repeated INT type and bool type, +// It is not good for sequence number or random number. BitshufflePage is recommended +// for these case. +// +// TODO(hkp): optimize rle algorithm +template<FieldType Type> +class RlePageBuilder : public PageBuilder { +public: + RlePageBuilder(const PageBuilderOptions& options) : + _options(options), + _count(0), + _finished(false), + _bit_width(0), + _rle_encoder(nullptr) { + switch(Type) { + case OLAP_FIELD_TYPE_BOOL: { + _bit_width = 1; + break; + } + default: { + _bit_width = SIZE_OF_TYPE * 8; + break; + } + } + _rle_encoder = new RleEncoder<CppType>(&_buf, _bit_width); + reset(); + } + + ~RlePageBuilder() { + delete _rle_encoder; + } + + bool is_page_full() override { + return _rle_encoder->len() >= _options.data_page_size; + } + + Status add(const uint8_t* vals, size_t* count) override { + DCHECK(!_finished); + DCHECK_EQ(reinterpret_cast<uintptr_t>(vals) & (alignof(CppType) - 1), 0) + << "Pointer passed to Add() must be naturally-aligned"; + + const CppType* new_vals = reinterpret_cast<const CppType*>(vals); + for (int i = 0; i < *count; ++i) { + _rle_encoder->Put(new_vals[i], 1); + } + + _count += *count; + return Status::OK(); + } + + Status get_dictionary_page(Slice* dictionary_page) override { + return Status::NotSupported("get_dictionary_page not implemented"); Review comment: how about making it the default implementation of PageBuilder? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
