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 | | |
63 | | static const FileReaderOptions DEFAULT; |
64 | | }; |
65 | | |
66 | | inline const FileReaderOptions FileReaderOptions::DEFAULT; |
67 | | |
68 | | class FileReader : public doris::ProfileCollector { |
69 | | public: |
70 | 477k | FileReader() = default; |
71 | | virtual ~FileReader() = default; |
72 | | |
73 | | FileReader(const FileReader&) = delete; |
74 | | const FileReader& operator=(const FileReader&) = delete; |
75 | | |
76 | | static const std::string VIRTUAL_REMOTE_DATA_DIR; |
77 | | |
78 | | /// If io_ctx is not null, |
79 | | /// the caller must ensure that the IOContext exists during the left cycle of read_at() |
80 | | Status read_at(size_t offset, Slice result, size_t* bytes_read, |
81 | | const IOContext* io_ctx = nullptr); |
82 | | |
83 | | virtual Status close() = 0; |
84 | | |
85 | | virtual const Path& path() const = 0; |
86 | | |
87 | | virtual size_t size() const = 0; |
88 | | |
89 | | virtual bool closed() const = 0; |
90 | | |
91 | 0 | virtual const std::string& get_data_dir_path() { return VIRTUAL_REMOTE_DATA_DIR; } |
92 | | |
93 | | // File modification time (seconds since epoch). Default to 0 meaning unknown. |
94 | | virtual int64_t mtime() const = 0; |
95 | | |
96 | | protected: |
97 | | virtual Status read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
98 | | const IOContext* io_ctx) = 0; |
99 | | }; |
100 | | |
101 | | using FileReaderSPtr = std::shared_ptr<FileReader>; |
102 | | |
103 | | Result<FileReaderSPtr> create_cached_file_reader(FileReaderSPtr raw_reader, |
104 | | const FileReaderOptions& opts); |
105 | | |
106 | | } // namespace io |
107 | | } // namespace doris |