Coverage Report

Created: 2026-03-16 16:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
157k
        : _inner_reader(std::move(inner_reader)),
32
157k
          _path(std::move(path)),
33
157k
          _packed_file_offset(offset),
34
157k
          _file_size(size) {
35
157k
    DCHECK(_inner_reader != nullptr);
36
157k
}
37
38
125k
PackedFileReader::~PackedFileReader() {
39
125k
    if (!_closed) {
40
10
        static_cast<void>(close());
41
10
    }
42
125k
}
43
44
Status PackedFileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read,
45
39.2k
                                      const IOContext* io_ctx) {
46
39.2k
    if (_closed) {
47
0
        return Status::InternalError("FileReader is already closed");
48
0
    }
49
50
    // Calculate the actual offset in packed file
51
39.2k
    size_t actual_offset = _packed_file_offset + offset;
52
53
    // Calculate the maximum bytes we can read
54
39.2k
    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
39.2k
    Slice adjusted_result(result.get_data(), max_read);
58
59
    // Read from packed file at the adjusted offset
60
39.2k
    auto s = _inner_reader->read_at(actual_offset, adjusted_result, bytes_read, io_ctx);
61
39.2k
    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
39.2k
    return Status::OK();
72
39.2k
}
73
74
129k
Status PackedFileReader::close() {
75
129k
    if (_closed) {
76
2.68k
        return Status::OK();
77
2.68k
    }
78
79
126k
    if (_inner_reader) {
80
126k
        RETURN_IF_ERROR(_inner_reader->close());
81
126k
    }
82
83
126k
    _closed = true;
84
126k
    return Status::OK();
85
126k
}
86
87
} // namespace doris::io