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 | | |
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 doris { |
31 | | |
32 | | namespace io { |
33 | | |
34 | | class FileSystem; |
35 | | struct IOContext; |
36 | | |
37 | | enum class FileCachePolicy : uint8_t { |
38 | | NO_CACHE, |
39 | | FILE_BLOCK_CACHE, |
40 | | }; |
41 | | |
42 | | inline FileCachePolicy cache_type_from_string(std::string_view type) { |
43 | | if (type == "file_block_cache") { |
44 | | return FileCachePolicy::FILE_BLOCK_CACHE; |
45 | | } else { |
46 | | return FileCachePolicy::NO_CACHE; |
47 | | } |
48 | | } |
49 | | |
50 | | // Only affects remote file readers |
51 | | struct FileReaderOptions { |
52 | | FileCachePolicy cache_type {FileCachePolicy::NO_CACHE}; |
53 | | bool is_doris_table = false; |
54 | | std::string cache_base_path; |
55 | | // Length of the file in bytes, -1 means unset. |
56 | | // If the file length is not set, the file length will be fetched from the file system. |
57 | | int64_t file_size = -1; |
58 | | // Use modification time to determine whether the file is changed |
59 | | int64_t mtime = 0; |
60 | | // Used to query the location of the file cache |
61 | | int64_t tablet_id = -1; |
62 | | // If true, FILE_BLOCK_CACHE readers are created as CacheBlockAwarePrefetchRemoteReader. |
63 | | // Callers can install one file access pattern and let read_at() prefetch file cache blocks. |
64 | | bool enable_cache_block_prefetch = false; |
65 | | |
66 | | static const FileReaderOptions DEFAULT; |
67 | | }; |
68 | | |
69 | | inline const FileReaderOptions FileReaderOptions::DEFAULT; |
70 | | |
71 | | class FileReader : public doris::ProfileCollector { |
72 | | public: |
73 | 877k | FileReader() = default; |
74 | | virtual ~FileReader() = default; |
75 | | |
76 | | FileReader(const FileReader&) = delete; |
77 | | const FileReader& operator=(const FileReader&) = delete; |
78 | | |
79 | | static const std::string VIRTUAL_REMOTE_DATA_DIR; |
80 | | |
81 | | /// If io_ctx is not null, |
82 | | /// the caller must ensure that the IOContext exists during the left cycle of read_at() |
83 | | Status read_at(size_t offset, Slice result, size_t* bytes_read, |
84 | | const IOContext* io_ctx = nullptr); |
85 | | |
86 | | virtual Status close() = 0; |
87 | | |
88 | | virtual const Path& path() const = 0; |
89 | | |
90 | | virtual size_t size() const = 0; |
91 | | |
92 | | virtual bool closed() const = 0; |
93 | | |
94 | 0 | virtual const std::string& get_data_dir_path() { return VIRTUAL_REMOTE_DATA_DIR; } |
95 | | |
96 | | // File modification time (seconds since epoch). Default to 0 meaning unknown. |
97 | | virtual int64_t mtime() const = 0; |
98 | | |
99 | | protected: |
100 | | virtual Status read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
101 | | const IOContext* io_ctx) = 0; |
102 | | }; |
103 | | |
104 | | using FileReaderSPtr = std::shared_ptr<FileReader>; |
105 | | |
106 | | Result<FileReaderSPtr> create_cached_file_reader(FileReaderSPtr raw_reader, |
107 | | const FileReaderOptions& opts); |
108 | | |
109 | | } // namespace io |
110 | | } // namespace doris |