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