Skip to content

Commit 5698805

Browse files
authored
refactor(query): remove named cache (#16245)
* refactor(query): remove named cache * refactor(query): remove named cache * refactor(query): remove named cache * refactor(query): remove named cache
1 parent eaae4e0 commit 5698805

File tree

16 files changed

+258
-341
lines changed

16 files changed

+258
-341
lines changed

src/common/cache/src/cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ use crate::Meter;
2323
pub trait Cache<K: Eq + Hash, V, M: Meter<K, V>> {
2424
/// Returns a reference to the value corresponding to the given key in the cache, if
2525
/// any.
26-
fn get<'a, Q>(&'a mut self, k: &Q) -> Option<&'a V>
26+
fn get<Q>(&mut self, k: &Q) -> Option<&V>
2727
where
2828
K: Borrow<Q>,
2929
Q: Hash + Eq + ?Sized;
3030

3131
/// Returns a reference to the value corresponding to the key in the cache or `None` if it is
3232
/// not present in the cache. Unlike `get`, `peek` does not update the Cache state so the key's
3333
/// position will be unchanged.
34-
fn peek<'a, Q>(&'a self, k: &Q) -> Option<&'a V>
34+
fn peek<Q>(&self, k: &Q) -> Option<&V>
3535
where
3636
K: Borrow<Q>,
3737
Q: Hash + Eq + ?Sized;

src/common/cache/src/cache/lru.rs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,6 @@ impl<K: Eq + Hash, V, M: CountableMeter<K, V>> Cache<K, V, M> for LruCache<K, V,
211211
self.map.front()
212212
}
213213

214-
/// Checks if the map contains the given key.
215-
///
216-
/// # Examples
217-
///
218-
/// ```rust,ignore
219-
/// use databend_common_cache::{Cache, LruCache};
220-
///
221-
/// let mut cache = LruCache::new(1);
222-
///
223-
/// cache.put(1, "a");
224-
/// assert!(cache.contains(&1));
225-
/// ```
226-
fn contains<Q>(&self, key: &Q) -> bool
227-
where
228-
K: Borrow<Q>,
229-
Q: Hash + Eq + ?Sized,
230-
{
231-
self.map.contains_key(key)
232-
}
233-
234214
/// Inserts a key-value pair into the cache. If the key already existed, the old value is
235215
/// returned.
236216
///
@@ -314,6 +294,50 @@ impl<K: Eq + Hash, V, M: CountableMeter<K, V>> Cache<K, V, M> for LruCache<K, V,
314294
})
315295
}
316296

297+
/// Checks if the map contains the given key.
298+
///
299+
/// # Examples
300+
///
301+
/// ```rust,ignore
302+
/// use databend_common_cache::{Cache, LruCache};
303+
///
304+
/// let mut cache = LruCache::new(1);
305+
///
306+
/// cache.put(1, "a");
307+
/// assert!(cache.contains(&1));
308+
/// ```
309+
fn contains<Q>(&self, key: &Q) -> bool
310+
where
311+
K: Borrow<Q>,
312+
Q: Hash + Eq + ?Sized,
313+
{
314+
self.map.contains_key(key)
315+
}
316+
317+
/// Returns the number of key-value pairs in the cache.
318+
fn len(&self) -> usize {
319+
self.map.len()
320+
}
321+
322+
/// Returns `true` if the cache contains no key-value pairs.
323+
fn is_empty(&self) -> bool {
324+
self.map.is_empty()
325+
}
326+
327+
/// Returns the maximum size of the key-value pairs the cache can hold, as measured by the
328+
/// `Meter` used by the cache.
329+
///
330+
/// # Examples
331+
///
332+
/// ```rust,ignore
333+
/// use databend_common_cache::{Cache, LruCache};
334+
/// let mut cache: LruCache<i32, &str> = LruCache::new(2);
335+
/// assert_eq!(cache.capacity(), 2);
336+
/// ```
337+
fn capacity(&self) -> u64 {
338+
self.max_capacity
339+
}
340+
317341
/// Sets the size of the key-value pairs the cache can hold, as measured by the `Meter` used by
318342
/// the cache.
319343
///
@@ -355,11 +379,6 @@ impl<K: Eq + Hash, V, M: CountableMeter<K, V>> Cache<K, V, M> for LruCache<K, V,
355379
self.max_capacity = capacity;
356380
}
357381

358-
/// Returns the number of key-value pairs in the cache.
359-
fn len(&self) -> usize {
360-
self.map.len()
361-
}
362-
363382
/// Returns the size of all the key-value pairs in the cache, as measured by the `Meter` used
364383
/// by the cache.
365384
fn size(&self) -> u64 {
@@ -368,25 +387,6 @@ impl<K: Eq + Hash, V, M: CountableMeter<K, V>> Cache<K, V, M> for LruCache<K, V,
368387
.unwrap_or_else(|| self.map.len() as u64)
369388
}
370389

371-
/// Returns the maximum size of the key-value pairs the cache can hold, as measured by the
372-
/// `Meter` used by the cache.
373-
///
374-
/// # Examples
375-
///
376-
/// ```rust,ignore
377-
/// use databend_common_cache::{Cache, LruCache};
378-
/// let mut cache: LruCache<i32, &str> = LruCache::new(2);
379-
/// assert_eq!(cache.capacity(), 2);
380-
/// ```
381-
fn capacity(&self) -> u64 {
382-
self.max_capacity
383-
}
384-
385-
/// Returns `true` if the cache contains no key-value pairs.
386-
fn is_empty(&self) -> bool {
387-
self.map.is_empty()
388-
}
389-
390390
/// Removes all key-value pairs from the cache.
391391
fn clear(&mut self) {
392392
self.map.clear();

src/query/service/tests/it/storages/fuse/bloom_index_meta_size.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::sync::Arc;
1818

1919
use chrono::Utc;
2020
use databend_common_base::base::tokio;
21-
use databend_common_cache::Cache;
21+
use databend_common_cache::LruCache;
2222
use databend_common_expression::types::Int32Type;
2323
use databend_common_expression::types::NumberDataType;
2424
use databend_common_expression::types::NumberScalar;
@@ -34,8 +34,9 @@ use databend_common_storages_fuse::statistics::gen_columns_statistics;
3434
use databend_common_storages_fuse::statistics::STATS_STRING_PREFIX_LEN;
3535
use databend_common_storages_fuse::FuseStorageFormat;
3636
use databend_query::test_kits::*;
37-
use databend_storages_common_cache::InMemoryCacheBuilder;
37+
use databend_storages_common_cache::CacheAccessor;
3838
use databend_storages_common_cache::InMemoryItemCacheHolder;
39+
use databend_storages_common_cache::Unit;
3940
use databend_storages_common_table_meta::meta::BlockMeta;
4041
use databend_storages_common_table_meta::meta::ColumnMeta;
4142
use databend_storages_common_table_meta::meta::ColumnStatistics;
@@ -176,9 +177,12 @@ async fn test_segment_info_size() -> databend_common_exception::Result<()> {
176177
scenario, pid, base_memory_usage
177178
);
178179

179-
let cache = InMemoryCacheBuilder::new_item_cache::<SegmentInfo>(cache_number as u64);
180+
let cache = InMemoryItemCacheHolder::create(
181+
String::from(""),
182+
Unit::Count,
183+
LruCache::new(cache_number as u64),
184+
);
180185
{
181-
let mut c = cache.write();
182186
for _ in 0..cache_number {
183187
let uuid = Uuid::new_v4();
184188
let block_metas = segment_info
@@ -188,7 +192,7 @@ async fn test_segment_info_size() -> databend_common_exception::Result<()> {
188192
.collect::<Vec<_>>();
189193
let statistics = segment_info.summary.clone();
190194
let segment_info = SegmentInfo::new(block_metas, statistics);
191-
(*c).put(format!("{}", uuid.simple()), Arc::new(segment_info));
195+
cache.put(format!("{}", uuid.simple()), Arc::new(segment_info));
192196
}
193197
}
194198
show_memory_usage("SegmentInfoCache", base_memory_usage, cache_number);
@@ -220,17 +224,20 @@ async fn test_segment_raw_bytes_size() -> databend_common_exception::Result<()>
220224
scenario, pid, base_memory_usage
221225
);
222226

223-
let cache = InMemoryCacheBuilder::new_item_cache::<Vec<u8>>(cache_number as u64);
224-
{
225-
let mut c = cache.write();
226-
for _ in 0..cache_number {
227-
let uuid = Uuid::new_v4();
228-
(*c).put(
229-
format!("{}", uuid.simple()),
230-
Arc::new(segment_info_bytes.clone()),
231-
);
232-
}
227+
let cache = InMemoryItemCacheHolder::create(
228+
String::from(""),
229+
Unit::Count,
230+
LruCache::new(cache_number as u64),
231+
);
232+
233+
for _ in 0..cache_number {
234+
let uuid = Uuid::new_v4();
235+
cache.put(
236+
format!("{}", uuid.simple()),
237+
Arc::new(segment_info_bytes.clone()),
238+
);
233239
}
240+
234241
show_memory_usage(
235242
"SegmentInfoCache(raw bytes Vec<u8>)",
236243
base_memory_usage,
@@ -265,13 +272,15 @@ async fn test_segment_raw_repr_bytes_size() -> databend_common_exception::Result
265272
scenario, pid, base_memory_usage
266273
);
267274

268-
let cache = InMemoryCacheBuilder::new_item_cache::<CompactSegmentInfo>(cache_number as u64);
269-
{
270-
let mut c = cache.write();
271-
for _ in 0..cache_number {
272-
let uuid = Uuid::new_v4();
273-
(*c).put(format!("{}", uuid.simple()), Arc::new(segment_raw.clone()));
274-
}
275+
let cache = InMemoryItemCacheHolder::create(
276+
String::from(""),
277+
Unit::Count,
278+
LruCache::new(cache_number as u64),
279+
);
280+
281+
for _ in 0..cache_number {
282+
let uuid = Uuid::new_v4();
283+
cache.put(format!("{}", uuid.simple()), Arc::new(segment_raw.clone()));
275284
}
276285
show_memory_usage(
277286
"SegmentInfoCache (compact repr)",
@@ -363,10 +372,9 @@ fn build_test_segment_info(
363372
#[allow(dead_code)]
364373
fn populate_cache<T>(cache: &InMemoryItemCacheHolder<T>, item: T, num_cache: usize)
365374
where T: Clone {
366-
let mut c = cache.write();
367375
for _ in 0..num_cache {
368376
let uuid = Uuid::new_v4();
369-
(*c).put(
377+
cache.put(
370378
format!("{}", uuid.simple()),
371379
std::sync::Arc::new(item.clone()),
372380
);

src/query/storages/common/cache/src/cache.rs

Lines changed: 9 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
use std::fmt::Display;
1616
use std::fmt::Formatter;
17-
use std::hash::Hash;
1817
use std::sync::Arc;
1918

2019
use databend_common_cache::Count;
2120
use databend_common_cache::CountableMeter;
22-
use databend_common_metrics::cache::*;
2321

2422
#[derive(Copy, Clone, Debug)]
2523
pub enum Unit {
@@ -37,133 +35,22 @@ impl Display for Unit {
3735
}
3836

3937
// The cache accessor, crate users usually working on this interface while manipulating caches
40-
pub trait CacheAccessor<K, V, M = Count>
41-
where
42-
K: Eq + Hash,
43-
M: CountableMeter<K, Arc<V>>,
44-
{
45-
fn get<Q: AsRef<str>>(&self, k: Q) -> Option<Arc<V>>;
46-
fn put(&self, key: K, value: Arc<V>);
38+
pub trait CacheAccessor {
39+
type V;
40+
type M: CountableMeter<String, Arc<Self::V>> = Count;
41+
42+
fn get<Q: AsRef<str>>(&self, k: Q) -> Option<Arc<Self::V>>;
43+
fn get_sized<Q: AsRef<str>>(&self, k: Q, len: u64) -> Option<Arc<Self::V>>;
44+
45+
fn put(&self, key: String, value: Arc<Self::V>);
4746
fn evict(&self, k: &str) -> bool;
4847
fn contains_key(&self, k: &str) -> bool;
4948
fn size(&self) -> u64;
5049
fn capacity(&self) -> u64;
51-
fn set_capacity(&self, capacity: u64);
5250
fn len(&self) -> usize;
5351
fn is_empty(&self) -> bool {
5452
self.len() == 0
5553
}
56-
}
57-
58-
/// Helper trait to convert a Cache into NamedCache
59-
pub trait Named
60-
where Self: Sized
61-
{
62-
fn name_with(self, name: impl Into<String>, unit: Unit) -> NamedCache<Self> {
63-
NamedCache {
64-
name: name.into(),
65-
cache: self,
66-
unit,
67-
}
68-
}
69-
}
70-
71-
impl<T> Named for T where T: Sized + Clone {}
72-
73-
/// A named cache that with embedded metrics logging
74-
#[derive(Clone)]
75-
pub struct NamedCache<C> {
76-
name: String,
77-
unit: Unit,
78-
cache: C,
79-
}
80-
81-
impl<C> NamedCache<C> {
82-
#[inline]
83-
pub fn name(&self) -> &str {
84-
&self.name
85-
}
86-
#[inline]
87-
pub fn unit(&self) -> Unit {
88-
self.unit
89-
}
90-
}
91-
92-
pub trait CacheAccessorExt<K, V, M> {
93-
fn get_with_len<Q: AsRef<str>>(&self, k: Q, len: u64) -> Option<Arc<V>>;
94-
}
95-
96-
impl<K, V, M, C> CacheAccessorExt<K, V, M> for NamedCache<C>
97-
where
98-
C: CacheAccessor<K, V, M>,
99-
K: Eq + Hash,
100-
M: CountableMeter<K, Arc<V>>,
101-
{
102-
fn get_with_len<Q: AsRef<str>>(&self, k: Q, len: u64) -> Option<Arc<V>> {
103-
let r = self.get(k);
104-
if r.is_none() {
105-
metrics_inc_cache_miss_count(len, &self.name);
106-
}
107-
r
108-
}
109-
}
11054

111-
impl<K, V, M, C> CacheAccessorExt<K, V, M> for Option<NamedCache<C>>
112-
where
113-
C: CacheAccessor<K, V, M>,
114-
K: Eq + Hash,
115-
M: CountableMeter<K, Arc<V>>,
116-
{
117-
fn get_with_len<Q: AsRef<str>>(&self, k: Q, len: u64) -> Option<Arc<V>> {
118-
self.as_ref().and_then(|cache| cache.get_with_len(k, len))
119-
}
120-
}
121-
122-
impl<K, V, M, C> CacheAccessor<K, V, M> for NamedCache<C>
123-
where
124-
C: CacheAccessor<K, V, M>,
125-
K: Eq + Hash,
126-
M: CountableMeter<K, Arc<V>>,
127-
{
128-
fn get<Q: AsRef<str>>(&self, k: Q) -> Option<Arc<V>> {
129-
metrics_inc_cache_access_count(1, &self.name);
130-
match self.cache.get(k) {
131-
None => {
132-
metrics_inc_cache_miss_count(1, &self.name);
133-
None
134-
}
135-
v @ Some(_) => {
136-
metrics_inc_cache_hit_count(1, &self.name);
137-
v
138-
}
139-
}
140-
}
141-
142-
fn put(&self, key: K, value: Arc<V>) {
143-
self.cache.put(key, value)
144-
}
145-
146-
fn evict(&self, k: &str) -> bool {
147-
self.cache.evict(k)
148-
}
149-
150-
fn size(&self) -> u64 {
151-
self.cache.size()
152-
}
153-
154-
fn capacity(&self) -> u64 {
155-
self.cache.capacity()
156-
}
157-
158-
fn set_capacity(&self, capacity: u64) {
159-
self.cache.set_capacity(capacity)
160-
}
161-
162-
fn len(&self) -> usize {
163-
self.cache.len()
164-
}
165-
166-
fn contains_key(&self, k: &str) -> bool {
167-
self.cache.contains_key(k)
168-
}
55+
fn name(&self) -> &str;
16956
}

0 commit comments

Comments
 (0)