be/src/exec/spill/spill_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 <gen_cpp/data.pb.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <string> |
24 | | #include <vector> |
25 | | |
26 | | #include "common/status.h" |
27 | | #include "core/pod_array.h" |
28 | | #include "core/pod_array_fwd.h" |
29 | | #include "io/fs/file_reader_writer_fwd.h" |
30 | | #include "runtime/runtime_profile.h" |
31 | | #include "runtime/workload_management/resource_context.h" |
32 | | |
33 | | namespace doris { |
34 | | #include "common/compile_check_begin.h" |
35 | | class RuntimeState; |
36 | | class Block; |
37 | | |
38 | | /// SpillFileReader reads blocks sequentially across all parts of a SpillFile. |
39 | | /// |
40 | | /// Usage: |
41 | | /// auto reader = spill_file->create_reader(state, profile); |
42 | | /// RETURN_IF_ERROR(reader->open()); |
43 | | /// bool eos = false; |
44 | | /// while (!eos) { RETURN_IF_ERROR(reader->read(&block, &eos)); } |
45 | | /// |
46 | | /// Part boundaries are transparent to the caller. When the current part is |
47 | | /// exhausted, the reader automatically opens the next part. |
48 | | class SpillFileReader { |
49 | | public: |
50 | | SpillFileReader(RuntimeState* state, RuntimeProfile* profile, std::string spill_dir, |
51 | | size_t part_count); |
52 | | |
53 | 185 | ~SpillFileReader() { (void)close(); } |
54 | | |
55 | | /// Open the first part and read its footer metadata. |
56 | | Status open(); |
57 | | |
58 | | /// Read the next block. Automatically advances across part boundaries. |
59 | | /// Sets *eos = true when all parts are exhausted. |
60 | | Status read(Block* block, bool* eos); |
61 | | |
62 | | /// Seek to a global block index within the whole spill file. |
63 | | /// block_index is 0-based across all parts. |
64 | | /// If block_index is out of range, the reader is positioned at EOS. |
65 | | Status seek(size_t block_index); |
66 | | |
67 | | Status close(); |
68 | | |
69 | | private: |
70 | | /// Open a specific part file and read its footer. |
71 | | Status _open_part(size_t part_index); |
72 | | |
73 | | /// Seek implementation with status propagation. |
74 | | Status _seek_to_block(size_t block_index); |
75 | | |
76 | | /// Close the current part's file reader. |
77 | | void _close_current_part(); |
78 | | |
79 | | // ── Configuration ── |
80 | | std::string _spill_dir; |
81 | | size_t _part_count; |
82 | | |
83 | | // ── Current part state ── |
84 | | size_t _current_part_index = 0; |
85 | | bool _is_open = false; |
86 | | bool _part_opened = false; |
87 | | io::FileReaderSPtr _file_reader; |
88 | | size_t _part_block_count = 0; |
89 | | size_t _part_read_block_index = 0; |
90 | | size_t _part_max_sub_block_size = 0; |
91 | | PaddedPODArray<char> _read_buff; |
92 | | std::vector<size_t> _block_start_offsets; |
93 | | |
94 | | PBlock _pb_block; |
95 | | |
96 | | // ── Counters ── |
97 | | RuntimeProfile::Counter* _read_file_timer = nullptr; |
98 | | RuntimeProfile::Counter* _deserialize_timer = nullptr; |
99 | | RuntimeProfile::Counter* _read_block_count = nullptr; |
100 | | RuntimeProfile::Counter* _read_block_data_size = nullptr; |
101 | | RuntimeProfile::Counter* _read_file_size = nullptr; |
102 | | RuntimeProfile::Counter* _read_rows_count = nullptr; |
103 | | RuntimeProfile::Counter* _read_file_count = nullptr; |
104 | | |
105 | | std::shared_ptr<ResourceContext> _resource_ctx = nullptr; |
106 | | }; |
107 | | |
108 | | using SpillFileReaderSPtr = std::shared_ptr<SpillFileReader>; |
109 | | |
110 | | } // namespace doris |
111 | | #include "common/compile_check_end.h" |