Coverage Report

Created: 2026-08-03 13:26

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