be/src/io/cache/shard_mem_cache.cpp
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 | | #include "io/cache/shard_mem_cache.h" |
19 | | |
20 | | #include <cstring> |
21 | | #include <memory> |
22 | | #include <mutex> |
23 | | |
24 | | #include "common/logging.h" |
25 | | |
26 | | namespace doris::io { |
27 | | |
28 | 6 | Status ShardMemHashTable::remove(const FileCacheKey& key) { |
29 | | // find and clear the one in _cache_map |
30 | 6 | std::unique_lock<std::shared_mutex> lock(_shard_mt); |
31 | 6 | auto map_key = std::make_pair(key.hash, key.offset); |
32 | 6 | auto iter = _cache_map.find(map_key); |
33 | 6 | if (iter == _cache_map.end()) { |
34 | 0 | LOG_WARNING("key not found in cache map") |
35 | 0 | .tag("hash", key.hash.to_string()) |
36 | 0 | .tag("offset", key.offset); |
37 | 0 | return Status::IOError("key not found in in-memory cache map when remove"); |
38 | 0 | } |
39 | 6 | _cache_map.erase(iter); |
40 | | |
41 | 6 | return Status::OK(); |
42 | 6 | } |
43 | | |
44 | 937 | Status ShardMemHashTable::append(const FileCacheKey& key, const Slice& value) { |
45 | 937 | std::unique_lock<std::shared_mutex> lock(_shard_mt); |
46 | | |
47 | 937 | auto map_key = std::make_pair(key.hash, key.offset); |
48 | 937 | auto iter = _cache_map.find(map_key); |
49 | 937 | if (iter != _cache_map.end()) { |
50 | | // despite the name append, it is indeed a put, so the key should not exist |
51 | 0 | LOG_WARNING("key already exists in in-memory cache map") |
52 | 0 | .tag("hash", key.hash.to_string()) |
53 | 0 | .tag("offset", key.offset); |
54 | 0 | DCHECK(false); |
55 | 0 | return Status::IOError("key already exists in in-memory cache map"); |
56 | 0 | } |
57 | | // TODO(zhengyu): allocate in mempool |
58 | 937 | auto mem_block = |
59 | 937 | MemBlock {std::shared_ptr<char[]>(new char[value.size], std::default_delete<char[]>()), |
60 | 937 | value.size}; |
61 | 937 | DCHECK(mem_block.addr != nullptr); |
62 | 937 | _cache_map[map_key] = mem_block; |
63 | 937 | char* dst = mem_block.addr.get(); |
64 | | // TODO(zhengyu): zero copy! |
65 | 937 | memcpy(dst, value.data, value.size); |
66 | | |
67 | 937 | return Status::OK(); |
68 | 937 | } |
69 | | |
70 | 3 | Status ShardMemHashTable::read(const FileCacheKey& key, size_t value_offset, Slice buffer) { |
71 | 3 | std::shared_lock<std::shared_mutex> lock(_shard_mt); |
72 | 3 | auto map_key = std::make_pair(key.hash, key.offset); |
73 | 3 | auto iter = _cache_map.find(map_key); |
74 | 3 | if (iter == _cache_map.end()) { |
75 | 0 | LOG_WARNING("key not found in cache map") |
76 | 0 | .tag("hash", key.hash.to_string()) |
77 | 0 | .tag("offset", key.offset); |
78 | 0 | return Status::IOError("key not found in in-memory cache map when read"); |
79 | 0 | } |
80 | | |
81 | 3 | auto mem_block = iter->second; |
82 | 3 | DCHECK(mem_block.addr != nullptr); |
83 | 3 | if (value_offset > mem_block.size || buffer.size > mem_block.size - value_offset) { |
84 | 2 | return Status::IOError("out of bound read in in-memory cache map"); |
85 | 2 | } |
86 | 1 | char* src = mem_block.addr.get() + value_offset; |
87 | 1 | char* dst = buffer.data; |
88 | 1 | size_t size = buffer.size; |
89 | 1 | memcpy(dst, src, size); |
90 | 1 | return Status::OK(); |
91 | 3 | } |
92 | | |
93 | 21.5k | Status ShardMemHashTable::clear() { |
94 | 21.5k | std::unique_lock<std::shared_mutex> lock(_shard_mt); |
95 | 21.5k | _cache_map.clear(); |
96 | | |
97 | 21.5k | return Status::OK(); |
98 | 21.5k | } |
99 | | |
100 | | } // namespace doris::io |