be/src/io/cache/file_cache_storage.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/iobuf.h> |
21 | | |
22 | | #include <mutex> |
23 | | |
24 | | #include "common/status.h" |
25 | | #include "io/cache/file_cache_common.h" |
26 | | #include "util/slice.h" |
27 | | |
28 | | namespace butil { |
29 | | class IOBuf; |
30 | | } |
31 | | |
32 | | namespace doris::io { |
33 | | |
34 | | class BlockFileCache; |
35 | | |
36 | | using FileWriterMapKey = std::pair<UInt128Wrapper, size_t>; |
37 | | |
38 | | enum FileCacheStorageType { DISK = 0, MEMORY = 1 }; |
39 | | |
40 | | struct FileWriterMapKeyHash { |
41 | 1.46M | std::size_t operator()(const FileWriterMapKey& w) const { |
42 | 1.46M | char* v1 = (char*)&w.first.value_; |
43 | 1.46M | char* v2 = (char*)&w.second; |
44 | 1.46M | char buf[24]; |
45 | 1.46M | memcpy(buf, v1, 16); |
46 | 1.46M | memcpy(buf + 16, v2, 8); |
47 | 1.46M | std::string_view str(buf, 24); |
48 | 1.46M | return std::hash<std::string_view> {}(str); |
49 | 1.46M | } |
50 | | }; |
51 | | |
52 | | struct MemBlock { |
53 | | std::shared_ptr<char[]> addr; |
54 | | butil::IOBuf payload; |
55 | | size_t size {0}; |
56 | | bool use_iobuf {false}; |
57 | | }; |
58 | | |
59 | | // The interface is for organizing datas in disk |
60 | | class FileCacheStorage { |
61 | | public: |
62 | 277 | FileCacheStorage() = default; |
63 | 277 | virtual ~FileCacheStorage() = default; |
64 | | // init the manager, read the blocks meta into memory |
65 | | virtual Status init(BlockFileCache* _mgr) = 0; |
66 | | // append datas into block |
67 | | virtual Status append(const FileCacheKey& key, const Slice& value) = 0; |
68 | | virtual Status appendv(const FileCacheKey& key, const Slice* values, size_t value_cnt) = 0; |
69 | | virtual Status append_iobuf(const FileCacheKey& key, const butil::IOBuf& value) = 0; |
70 | | // finalize the block |
71 | | virtual Status finalize(const FileCacheKey& key, const size_t size) = 0; |
72 | | // read the block |
73 | | virtual Status read(const FileCacheKey& key, size_t value_offset, Slice result) = 0; |
74 | | // read the block and append bytes into iobuf |
75 | | virtual Status read_to_iobuf(const FileCacheKey& key, size_t value_offset, size_t bytes_req, |
76 | | butil::IOBuf* out, size_t* bytes_read) = 0; |
77 | | // remove the block |
78 | | virtual Status remove(const FileCacheKey& key) = 0; |
79 | | // change the block meta |
80 | | virtual Status change_key_meta_type(const FileCacheKey& key, const FileCacheType type, |
81 | | const size_t size) = 0; |
82 | | // use when lazy load cache |
83 | | virtual void load_blocks_directly_unlocked(BlockFileCache* _mgr, const FileCacheKey& key, |
84 | 0 | std::lock_guard<std::mutex>& cache_lock) {} |
85 | | // force clear all current data in the cache |
86 | | virtual Status clear(std::string& msg) = 0; |
87 | | virtual FileCacheStorageType get_type() = 0; |
88 | | // get local cached file |
89 | | virtual std::string get_local_file(const FileCacheKey& key) = 0; |
90 | | virtual Status get_file_cache_infos(std::vector<FileCacheInfo>& infos, |
91 | 1 | std::lock_guard<std::mutex>& cache_lock) const { |
92 | 1 | return Status::OK(); |
93 | 1 | }; |
94 | | }; |
95 | | |
96 | | } // namespace doris::io |