be/src/io/fs/packed_file_reader.cpp
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 | | #include "io/fs/packed_file_reader.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <utility> |
22 | | |
23 | | #include "common/logging.h" |
24 | | #include "common/status.h" |
25 | | #include "io/fs/file_reader.h" |
26 | | |
27 | | namespace doris::io { |
28 | | |
29 | | PackedFileReader::PackedFileReader(FileReaderSPtr inner_reader, Path path, int64_t offset, |
30 | | int64_t size) |
31 | 1.01k | : _inner_reader(std::move(inner_reader)), |
32 | 1.01k | _path(std::move(path)), |
33 | 1.01k | _packed_file_offset(offset), |
34 | 1.01k | _file_size(size) { |
35 | 1.01k | DCHECK(_inner_reader != nullptr); |
36 | 1.01k | } |
37 | | |
38 | 1.01k | PackedFileReader::~PackedFileReader() { |
39 | 1.01k | if (!_closed) { |
40 | 10 | static_cast<void>(close()); |
41 | 10 | } |
42 | 1.01k | } |
43 | | |
44 | | Status PackedFileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
45 | 650 | const IOContext* io_ctx) { |
46 | 650 | if (_closed) { |
47 | 0 | return Status::InternalError("FileReader is already closed"); |
48 | 0 | } |
49 | | |
50 | | // Calculate the actual offset in packed file |
51 | 650 | size_t actual_offset = _packed_file_offset + offset; |
52 | | |
53 | | // Calculate the maximum bytes we can read |
54 | 650 | size_t max_read = std::min(result.get_size(), static_cast<size_t>(_file_size - offset)); |
55 | | |
56 | | // Adjust result slice to the actual size we can read |
57 | 650 | Slice adjusted_result(result.get_data(), max_read); |
58 | | |
59 | | // Read from packed file at the adjusted offset |
60 | 650 | auto s = _inner_reader->read_at(actual_offset, adjusted_result, bytes_read, io_ctx); |
61 | 650 | if (!s.ok()) { |
62 | 0 | LOG(WARNING) << "failed to read packed file: " << _path.native() << ", offset: " << offset |
63 | 0 | << ", actual offset: " << actual_offset |
64 | 0 | << ", result size: " << adjusted_result.get_size() |
65 | 0 | << ", error: " << s.to_string() |
66 | 0 | << ", packed file offset: " << _packed_file_offset |
67 | 0 | << ", file size: " << _file_size; |
68 | 0 | return s; |
69 | 0 | } |
70 | | |
71 | 650 | return Status::OK(); |
72 | 650 | } |
73 | | |
74 | 1.01k | Status PackedFileReader::close() { |
75 | 1.01k | if (_closed) { |
76 | 1 | return Status::OK(); |
77 | 1 | } |
78 | | |
79 | 1.01k | if (_inner_reader) { |
80 | 1.01k | RETURN_IF_ERROR(_inner_reader->close()); |
81 | 1.01k | } |
82 | | |
83 | 1.01k | _closed = true; |
84 | 1.01k | return Status::OK(); |
85 | 1.01k | } |
86 | | |
87 | | } // namespace doris::io |