-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcondition_cache.h
More file actions
170 lines (141 loc) · 5.46 KB
/
condition_cache.h
File metadata and controls
170 lines (141 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once
#include <butil/macros.h>
#include <glog/logging.h>
#include <stddef.h>
#include <stdint.h>
#include <atomic>
#include <memory>
#include <roaring/roaring.hh>
#include <string>
#include "common/config.h"
#include "common/status.h"
#include "cpp/lru_cache.h"
#include "io/fs/file_system.h"
#include "io/fs/path.h"
#include "runtime/exec_env.h"
#include "runtime/memory/lru_cache_policy.h"
#include "runtime/memory/mem_tracker.h"
#include "util/slice.h"
#include "util/time.h"
namespace doris::segment_v2 {
class ConditionCacheHandle;
class ConditionCache : public LRUCachePolicy {
public:
using LRUCachePolicy::insert;
// The cache key or segment lru cache
struct CacheKey {
CacheKey(RowsetId rowset_id_, int64_t segment_id_, uint64_t digest_)
: rowset_id(rowset_id_), segment_id(segment_id_), digest(digest_) {}
RowsetId rowset_id;
int64_t segment_id;
uint64_t digest;
// Encode to a flat binary which can be used as LRUCache's key
[[nodiscard]] std::string encode() const {
char buf[16];
memcpy(buf, &segment_id, 8);
memcpy(buf + 8, &digest, 8);
return rowset_id.to_string() + std::string(buf, 16);
}
};
class CacheValue : public LRUCacheValueBase {
public:
std::shared_ptr<std::vector<bool>> filter_result;
};
// Cache key for external tables (Hive ORC/Parquet)
struct ExternalCacheKey {
ExternalCacheKey() = default;
ExternalCacheKey(const std::string& path_, int64_t modification_time_, int64_t file_size_,
uint64_t digest_, int64_t start_offset_, int64_t size_)
: path(path_),
modification_time(modification_time_),
file_size(file_size_),
digest(digest_),
start_offset(start_offset_),
size(size_) {}
std::string path;
int64_t modification_time = 0;
int64_t file_size = 0;
uint64_t digest = 0;
int64_t start_offset = 0;
int64_t size = 0;
[[nodiscard]] std::string encode() const {
std::string key = path;
char buf[40];
memcpy(buf, &modification_time, 8);
memcpy(buf + 8, &file_size, 8);
memcpy(buf + 16, &digest, 8);
memcpy(buf + 24, &start_offset, 8);
memcpy(buf + 32, &size, 8);
key.append(buf, 40);
return key;
}
};
// Create global instance of this class
static ConditionCache* create_global_cache(size_t capacity, uint32_t num_shards = 16) {
auto* res = new ConditionCache(capacity, num_shards);
return res;
}
// Return global instance.
// Client should call create_global_cache before.
static ConditionCache* instance() { return ExecEnv::GetInstance()->get_condition_cache(); }
ConditionCache() = delete;
ConditionCache(size_t capacity, uint32_t num_shards)
: LRUCachePolicy(CachePolicy::CacheType::CONDITION_CACHE, capacity, LRUCacheType::SIZE,
config::inverted_index_cache_stale_sweep_time_sec, num_shards,
/*element_count_capacity*/ 0, /*enable_prune*/ true,
/*is_lru_k*/ true) {}
template <typename KeyType>
bool lookup(const KeyType& key, ConditionCacheHandle* handle);
template <typename KeyType>
void insert(const KeyType& key, std::shared_ptr<std::vector<bool>> filter_result);
};
class ConditionCacheHandle {
public:
ConditionCacheHandle() = default;
ConditionCacheHandle(LRUCachePolicy* cache, Cache::Handle* handle)
: _cache(cache), _handle(handle) {}
~ConditionCacheHandle() {
if (_handle != nullptr) {
_cache->release(_handle);
}
}
ConditionCacheHandle(ConditionCacheHandle&& other) noexcept {
// we can use std::exchange if we switch c++14 on
std::swap(_cache, other._cache);
std::swap(_handle, other._handle);
}
ConditionCacheHandle& operator=(ConditionCacheHandle&& other) noexcept {
std::swap(_cache, other._cache);
std::swap(_handle, other._handle);
return *this;
}
LRUCachePolicy* cache() const { return _cache; }
std::shared_ptr<std::vector<bool>> get_filter_result() const {
if (!_cache) {
return nullptr;
}
return ((ConditionCache::CacheValue*)_cache->value(_handle))->filter_result;
}
private:
LRUCachePolicy* _cache = nullptr;
Cache::Handle* _handle = nullptr;
// Don't allow copy and assign
DISALLOW_COPY_AND_ASSIGN(ConditionCacheHandle);
};
} // namespace doris::segment_v2