Coverage Report

Created: 2026-03-14 20:54

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
class IndexFileReader {
46
public:
47
    // Modern C++ using std::unordered_map with smart pointers for automatic memory management
48
    using EntriesType = std::unordered_map<std::string, std::unique_ptr<ReaderFileEntry>>;
49
    // Map to hold the file entries for each index ID.
50
    using IndicesEntriesMap =
51
            std::map<std::pair<int64_t, std::string>, std::unique_ptr<EntriesType>>;
52
53
    IndexFileReader(io::FileSystemSPtr fs, std::string index_path_prefix,
54
                    InvertedIndexStorageFormatPB storage_format,
55
                    InvertedIndexFileInfo idx_file_info = InvertedIndexFileInfo())
56
3.01k
            : _fs(std::move(fs)),
57
3.01k
              _index_path_prefix(std::move(index_path_prefix)),
58
3.01k
              _storage_format(storage_format),
59
3.01k
              _idx_file_info(idx_file_info) {}
60
3.01k
    virtual ~IndexFileReader() = default;
61
62
    MOCK_FUNCTION Status init(int32_t read_buffer_size = config::inverted_index_read_buffer_size,
63
                              const io::IOContext* io_ctx = nullptr);
64
    MOCK_FUNCTION Result<std::unique_ptr<DorisCompoundReader, DirectoryDeleter>> open(
65
            const TabletIndex* index_meta, const io::IOContext* io_ctx = nullptr) const;
66
    void debug_file_entries();
67
    std::string get_index_file_cache_key(const TabletIndex* index_meta) const;
68
    std::string get_index_file_path(const TabletIndex* index_meta) const;
69
    Status index_file_exist(const TabletIndex* index_meta, bool* res) const;
70
    Status has_null(const TabletIndex* index_meta, bool* res) const;
71
    Result<InvertedIndexDirectoryMap> get_all_directories();
72
    // open file v2, init _stream
73
0
    int64_t get_inverted_file_size() const { return _stream == nullptr ? 0 : _stream->length(); }
74
0
    const std::string& get_index_path_prefix() const { return _index_path_prefix; }
75
    friend IndexFileWriter;
76
77
protected:
78
    Status _init_from(int32_t read_buffer_size, const io::IOContext* io_ctx);
79
    Result<std::unique_ptr<DorisCompoundReader, DirectoryDeleter>> _open(
80
            int64_t index_id, const std::string& index_suffix,
81
            const io::IOContext* io_ctx = nullptr) const;
82
83
private:
84
    IndicesEntriesMap _indices_entries;
85
    std::unique_ptr<CL_NS(store)::IndexInput> _stream = nullptr;
86
    const io::FileSystemSPtr _fs;
87
    std::string _index_path_prefix;
88
    int32_t _read_buffer_size = -1;
89
    InvertedIndexStorageFormatPB _storage_format;
90
    mutable std::shared_mutex _mutex; // Use mutable for const read operations
91
    bool _inited = false;
92
    InvertedIndexFileInfo _idx_file_info;
93
};
94
95
} // namespace segment_v2
96
} // namespace doris