Coverage Report

Created: 2026-10-09 22:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/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 <butil/macros.h>
21
#include <stddef.h>
22
23
#include <functional>
24
#include <memory>
25
#include <string>
26
27
#include "common/status.h"
28
#include "io/fs/path.h"
29
#include "util/profile_collector.h"
30
#include "util/slice.h"
31
32
namespace butil {
33
class IOBuf;
34
}
35
36
namespace doris {
37
38
namespace io {
39
40
class FileSystem;
41
struct IOContext;
42
43
enum class FileCachePolicy : uint8_t {
44
    NO_CACHE,
45
    FILE_BLOCK_CACHE,
46
};
47
48
enum class CacheAlignMode : uint8_t {
49
    ALIGN_TO_BLOCK,
50
    UNALIGNED,
51
};
52
53
enum class CacheWriteMode : uint8_t {
54
    DEFAULT,
55
    NO_WRITE,
56
    SYNC_WRITE,
57
    ASYNC_WRITE,
58
};
59
60
inline FileCachePolicy cache_type_from_string(std::string_view type) {
61
    if (type == "file_block_cache") {
62
        return FileCachePolicy::FILE_BLOCK_CACHE;
63
    } else {
64
        return FileCachePolicy::NO_CACHE;
65
    }
66
}
67
68
// Only affects remote file readers
69
struct FileReaderOptions {
70
    FileCachePolicy cache_type {FileCachePolicy::NO_CACHE};
71
    CacheAlignMode align_mode {CacheAlignMode::ALIGN_TO_BLOCK};
72
    CacheWriteMode cache_write_mode {CacheWriteMode::DEFAULT};
73
    bool is_doris_table = false;
74
    std::string cache_base_path;
75
    // Length of the file in bytes, -1 means unset.
76
    // If the file length is not set, the file length will be fetched from the file system.
77
    int64_t file_size = -1;
78
    // Use modification time to determine whether the file is changed
79
    int64_t mtime = 0;
80
    // Used to query the location of the file cache
81
    int64_t tablet_id = -1;
82
    // Storage resource id of the remote file system. Used by peer fill to reconstruct
83
    // the source file system without scanning tablet rowsets on the peer.
84
    std::string storage_resource_id;
85
    // Builds the file cache key of an external file from its path and modification time. The
86
    // creator of the reader sets it when the path alone does not name the file, e.g. FileFactory
87
    // adds the object storage endpoint or the HDFS name node. When it is empty the key is path:mtime.
88
    std::function<std::string(const std::string& path, int64_t mtime)> cache_key_function {};
89
90
    static const FileReaderOptions DEFAULT;
91
};
92
93
inline const FileReaderOptions FileReaderOptions::DEFAULT;
94
95
class FileReader : public doris::ProfileCollector {
96
public:
97
3.78M
    FileReader() = default;
98
    virtual ~FileReader() = default;
99
100
    FileReader(const FileReader&) = delete;
101
    const FileReader& operator=(const FileReader&) = delete;
102
103
    static const std::string VIRTUAL_REMOTE_DATA_DIR;
104
105
    /// If io_ctx is not null,
106
    /// the caller must ensure that the IOContext exists during the left cycle of read_at()
107
    Status read_at(size_t offset, Slice result, size_t* bytes_read,
108
                   const IOContext* io_ctx = nullptr);
109
    /// Read up to bytes_req bytes from offset and append them to out.
110
    /// bytes_read is always set to the actual number of bytes appended on success; reading past
111
    /// EOF is clamped to the file size and returns OK with fewer bytes. out and bytes_read must be
112
    /// non-null. Readers that do not override the IOBuf path return NotSupported.
113
    Status read_at_iobuf(size_t offset, size_t bytes_req, butil::IOBuf* out, size_t* bytes_read,
114
                         const IOContext* io_ctx = nullptr);
115
116
    virtual Status close() = 0;
117
118
    virtual const Path& path() const = 0;
119
120
    virtual size_t size() const = 0;
121
122
    virtual bool closed() const = 0;
123
124
0
    virtual const std::string& get_data_dir_path() { return VIRTUAL_REMOTE_DATA_DIR; }
125
126
    // File modification time (seconds since epoch). Default to 0 meaning unknown.
127
    virtual int64_t mtime() const = 0;
128
129
protected:
130
    virtual Status read_at_impl(size_t offset, Slice result, size_t* bytes_read,
131
                                const IOContext* io_ctx) = 0;
132
    // Default implementation returns NotSupported. Override this in readers that can
133
    // fill iobuf directly.
134
    virtual Status read_at_iobuf_impl(size_t offset, size_t bytes_req, butil::IOBuf* out,
135
                                      size_t* bytes_read, const IOContext* io_ctx);
136
};
137
138
using FileReaderSPtr = std::shared_ptr<FileReader>;
139
140
Result<FileReaderSPtr> create_cached_file_reader(FileReaderSPtr raw_reader,
141
                                                 const FileReaderOptions& opts);
142
143
} // namespace io
144
} // namespace doris