Coverage Report

Created: 2026-03-31 21:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/indexed_column_writer.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <butil/macros.h>
21
#include <gen_cpp/segment_v2.pb.h>
22
#include <stdint.h>
23
24
#include <cstddef>
25
#include <cstdint>
26
#include <memory>
27
#include <string>
28
29
#include "common/status.h"
30
#include "storage/segment/common.h"
31
#include "storage/segment/page_pointer.h"
32
33
namespace doris {
34
35
class BlockCompressionCodec;
36
class KeyCoder;
37
class TypeInfo;
38
39
namespace io {
40
class FileWriter;
41
}
42
43
namespace segment_v2 {
44
45
class IndexPageBuilder;
46
class PageBuilder;
47
48
struct IndexedColumnWriterOptions {
49
    size_t index_page_size = 64 * 1024;
50
    size_t data_page_size = 1024 * 1024;
51
    bool write_ordinal_index = false;
52
    bool write_value_index = false;
53
    EncodingTypePB encoding = DEFAULT_ENCODING;
54
    CompressionTypePB compression = NO_COMPRESSION;
55
    double compression_min_space_saving = 0.1;
56
};
57
58
// IndexedColumn is a column with an optional "ordinal index" and an optional "value index".
59
// - "ordinal index" enables us to seek to a particular rowid within the column
60
// - "value index" enables us to seek to a particular value but requires IndexedColumn to store ordered values
61
//
62
// IndexedColumn can be used as the building blocks for implementing other data structures. For example,
63
// - a bitmap index can be represented by two indexed columns, one for the term dictionary, one for the posting lists.
64
//   the "dictionary" IndexedColumn contains ordered terms and a value index.
65
//   the "posting" IndexedColumn contains bitmap for each term and an ordinal index.
66
// - a bloom filter index can be represented by one indexed column containing bloom filters with an ordinal index
67
//
68
// Currently IndexedColumn has the following restrictions but can be extended to solve in the future
69
// 1. value can't be null
70
// 2. duplicated values are not supported/tested when storing ordered values
71
// TODO test with empty input
72
class IndexedColumnWriter {
73
public:
74
    explicit IndexedColumnWriter(const IndexedColumnWriterOptions& options,
75
                                 const TypeInfo* type_info, io::FileWriter* file_writer);
76
77
    ~IndexedColumnWriter();
78
79
    Status init();
80
81
    // add a single not-null value
82
    Status add(const void* value);
83
84
    Status finish(IndexedColumnMetaPB* meta);
85
86
75
    uint64_t disk_size() const { return _disk_size; }
87
88
2
    uint32_t data_page_num() const { return _num_data_pages + 1; }
89
90
private:
91
    Status _finish_current_data_page(size_t& num_val);
92
93
    Status _flush_index(IndexPageBuilder* index_builder, BTreeMetaPB* meta);
94
95
    IndexedColumnWriterOptions _options;
96
    const TypeInfo* _type_info = nullptr;
97
    io::FileWriter* _file_writer = nullptr;
98
99
    ordinal_t _num_values;
100
    uint32_t _num_data_pages;
101
    uint64_t _disk_size;
102
    // remember the encoded first value key in current page
103
    std::string _first_value_string;
104
    PagePointer _last_data_page;
105
106
    // the following members are initialized in init()
107
    // -----
108
    // builder for data pages
109
    std::unique_ptr<PageBuilder> _data_page_builder;
110
    // builder for index pages of ordinal index, null if write_ordinal_index == false
111
    std::unique_ptr<IndexPageBuilder> _ordinal_index_builder;
112
    // builder for index pages of value index, null if write_value_index == false
113
    std::unique_ptr<IndexPageBuilder> _value_index_builder;
114
    // encoder for value index's key
115
    const KeyCoder* _value_key_coder = nullptr;
116
    BlockCompressionCodec* _compress_codec = nullptr;
117
118
    DISALLOW_COPY_AND_ASSIGN(IndexedColumnWriter);
119
};
120
121
} // namespace segment_v2
122
} // namespace doris