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