Coverage Report

Created: 2026-03-29 02:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/index_file_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 <CLucene.h> // IWYU pragma: keep
21
#include <CLucene/store/IndexInput.h>
22
#include <gen_cpp/olap_common.pb.h>
23
#include <gen_cpp/olap_file.pb.h>
24
25
#include <string>
26
#include <utility>
27
28
#include "common/be_mock_util.h"
29
#include "io/fs/file_system.h"
30
#include "io/fs/file_writer.h"
31
#include "io/fs/local_file_system.h"
32
#include "storage/index/index_storage_format.h"
33
#include "storage/index/inverted/inverted_index_common.h"
34
#include "storage/index/inverted/inverted_index_compound_reader.h"
35
#include "storage/index/inverted/inverted_index_searcher.h"
36
37
namespace doris {
38
class TabletIndex;
39
40
namespace segment_v2 {
41
class DorisFSDirectory;
42
43
using InvertedIndexDirectoryMap =
44
        std::map<std::pair<int64_t, std::string>, std::shared_ptr<lucene::store::Directory>>;
45
46
class IndexFileWriter;
47
using IndexFileWriterPtr = std::unique_ptr<IndexFileWriter>;
48
49
class IndexFileWriter {
50
public:
51
    IndexFileWriter(io::FileSystemSPtr fs, std::string index_path_prefix, std::string rowset_id,
52
                    int64_t seg_id, InvertedIndexStorageFormatPB storage_format,
53
                    io::FileWriterPtr file_writer = nullptr, bool can_use_ram_dir = true,
54
                    int64_t tablet_id = -1);
55
452
    virtual ~IndexFileWriter() = default;
56
57
    MOCK_FUNCTION Result<std::shared_ptr<DorisFSDirectory>> open(const TabletIndex* index_meta);
58
    Status delete_index(const TabletIndex* index_meta);
59
    Status initialize(InvertedIndexDirectoryMap& indices_dirs);
60
    Status add_into_searcher_cache();
61
    // Begin the close process. This mainly triggers the asynchronous close operation of
62
    // _idx_v2_writer by calling close(true), which starts the close process but returns
63
    // immediately without waiting for completion.
64
    Status begin_close();
65
    // Finish the close process. This waits for the close operation to complete by calling
66
    // _idx_v2_writer->close(false), which blocks until the close is fully done.
67
    Status finish_close();
68
217
    const InvertedIndexFileInfo* get_index_file_info() const {
69
217
        DCHECK(_closed) << debug_string();
70
217
        return &_file_info;
71
217
    }
72
292
    int64_t get_index_file_total_size() const {
73
292
        DCHECK(_closed) << debug_string();
74
292
        return _total_file_size;
75
292
    }
76
0
    const io::FileSystemSPtr& get_fs() const { return _fs; }
77
7.14k
    InvertedIndexStorageFormatPB get_storage_format() const { return _storage_format; }
78
228
    void set_file_writer_opts(const io::FileWriterOptions& opts) { _opts = opts; }
79
    std::vector<std::string> get_index_file_names() const;
80
    std::string debug_string() const;
81
82
    // Get internal file writer (for merge file index collection)
83
0
    io::FileWriter* get_file_writer() const { return _idx_v2_writer.get(); }
84
85
private:
86
    Status _insert_directory_into_map(int64_t index_id, const std::string& index_suffix,
87
                                      std::shared_ptr<DorisFSDirectory> dir);
88
    virtual Result<std::unique_ptr<IndexSearcherBuilder>> _construct_index_searcher_builder(
89
            const DorisCompoundReader* dir);
90
91
    // Member variables...
92
    InvertedIndexDirectoryMap _indices_dirs;
93
    const io::FileSystemSPtr _fs;
94
    std::string _index_path_prefix;
95
    std::string _rowset_id;
96
    int64_t _seg_id;
97
    InvertedIndexStorageFormatPB _storage_format;
98
    std::string _tmp_dir;
99
    const std::shared_ptr<io::LocalFileSystem>& _local_fs;
100
101
    // write to disk or stream
102
    io::FileWriterPtr _idx_v2_writer = nullptr;
103
    io::FileWriterOptions _opts;
104
105
    // v1: all file size
106
    // v2: file size
107
    int64_t _total_file_size = 0;
108
    InvertedIndexFileInfo _file_info;
109
110
    // only once
111
    bool _closed = false;
112
    bool _can_use_ram_dir = true;
113
114
    IndexStorageFormatPtr _index_storage_format;
115
    int64_t _tablet_id = -1;
116
117
    friend class IndexStorageFormatV1;
118
    friend class IndexStorageFormatV2;
119
    friend class IndexFileWriterTest;
120
};
121
122
} // namespace segment_v2
123
} // namespace doris