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