Coverage Report

Created: 2026-04-01 22:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/index_file_reader.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_file.pb.h>
23
24
#include <map>
25
#include <memory>
26
#include <mutex>
27
#include <shared_mutex>
28
#include <string>
29
#include <unordered_map>
30
#include <utility>
31
#include <vector>
32
33
#include "common/be_mock_util.h"
34
#include "common/config.h"
35
#include "io/fs/file_system.h"
36
#include "storage/index/index_file_writer.h"
37
#include "storage/index/inverted/inverted_index_desc.h"
38
39
namespace doris {
40
class TabletIndex;
41
namespace segment_v2 {
42
class ReaderFileEntry;
43
class DorisCompoundReader;
44
45
// A singleton class responsible for reading index files, managing file entries, and providing interfaces to access index data.
46
// The singleton object is at segment level, and it is shared by all threads that read the same segment (even across different queries).
47
// It is created when the first index reader is initialized, and destroyed when the segment is closed.
48
class IndexFileReader {
49
public:
50
    // Modern C++ using std::unordered_map with smart pointers for automatic memory management
51
    using EntriesType = std::unordered_map<std::string, std::unique_ptr<ReaderFileEntry>>;
52
    // Map to hold the file entries for each index ID.
53
    using IndicesEntriesMap =
54
            std::map<std::pair<int64_t, std::string>, std::unique_ptr<EntriesType>>;
55
56
    IndexFileReader(io::FileSystemSPtr fs, std::string index_path_prefix,
57
                    InvertedIndexStorageFormatPB storage_format,
58
                    InvertedIndexFileInfo idx_file_info = InvertedIndexFileInfo(),
59
                    int64_t tablet_id = -1)
60
3.02k
            : _fs(std::move(fs)),
61
3.02k
              _index_path_prefix(std::move(index_path_prefix)),
62
3.02k
              _storage_format(storage_format),
63
3.02k
              _idx_file_info(idx_file_info),
64
3.02k
              _tablet_id(tablet_id) {}
65
3.02k
    virtual ~IndexFileReader() = default;
66
67
    MOCK_FUNCTION Status init(int32_t read_buffer_size = config::inverted_index_read_buffer_size,
68
                              const io::IOContext* io_ctx = nullptr);
69
    MOCK_FUNCTION Result<std::unique_ptr<DorisCompoundReader, DirectoryDeleter>> open(
70
            const TabletIndex* index_meta, const io::IOContext* io_ctx = nullptr) const;
71
    void debug_file_entries();
72
    std::string get_index_file_cache_key(const TabletIndex* index_meta) const;
73
    std::string get_index_file_path(const TabletIndex* index_meta) const;
74
    Status index_file_exist(const TabletIndex* index_meta, bool* res) const;
75
    Status has_null(const TabletIndex* index_meta, bool* res) const;
76
    Result<InvertedIndexDirectoryMap> get_all_directories();
77
    // open file v2, init _stream
78
0
    int64_t get_inverted_file_size() const { return _stream == nullptr ? 0 : _stream->length(); }
79
0
    const std::string& get_index_path_prefix() const { return _index_path_prefix; }
80
    friend IndexFileWriter;
81
82
protected:
83
    Status _init_from(int32_t read_buffer_size, const io::IOContext* io_ctx);
84
    Result<std::unique_ptr<DorisCompoundReader, DirectoryDeleter>> _open(
85
            int64_t index_id, const std::string& index_suffix,
86
            const io::IOContext* io_ctx = nullptr) const;
87
88
private:
89
    IndicesEntriesMap _indices_entries;
90
    std::unique_ptr<CL_NS(store)::IndexInput> _stream = nullptr;
91
    const io::FileSystemSPtr _fs;
92
    std::string _index_path_prefix;
93
    int32_t _read_buffer_size = -1;
94
    InvertedIndexStorageFormatPB _storage_format;
95
    mutable std::shared_mutex _mutex; // Use mutable for const read operations
96
    bool _inited = false;
97
    InvertedIndexFileInfo _idx_file_info;
98
    int64_t _tablet_id = -1;
99
};
100
101
} // namespace segment_v2
102
} // namespace doris