Skip to content

Commit fa209f4

Browse files
authored
chore: bring back function set_cache_capacity (#17196)
* chore:bring back func `set_cache_capacity` * test cases * revert config file * tweak test case * fix: use usize as bytes capacity * tweak lock scope * rename vars/methods of cache mgr
1 parent dceaa71 commit fa209f4

File tree

11 files changed

+363
-69
lines changed

11 files changed

+363
-69
lines changed

src/common/cache/src/cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ pub trait Cache<K: Eq + Hash + MemSized, V: MemSized> {
7272

7373
fn items_capacity(&self) -> u64;
7474

75+
fn set_bytes_capacity(&mut self, capacity: usize);
76+
77+
fn set_items_capacity(&mut self, capacity: usize);
78+
7579
/// Returns the bytes size of all the key-value pairs in the cache.
7680
fn bytes_size(&self) -> u64;
7781

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ impl<K: Eq + Hash + MemSized, V: MemSized> Cache<K, V> for LruCache<K, V> {
301301
self.max_items as u64
302302
}
303303

304+
fn set_bytes_capacity(&mut self, max_bytes: usize) {
305+
while self.bytes > max_bytes || self.map.len() > self.max_items {
306+
self.pop_by_policy();
307+
}
308+
self.max_bytes = max_bytes;
309+
}
310+
311+
fn set_items_capacity(&mut self, max_items: usize) {
312+
while self.bytes > self.max_bytes || self.map.len() > max_items {
313+
self.pop_by_policy();
314+
}
315+
self.max_items = max_items;
316+
}
317+
304318
/// Returns the bytes size of all the key-value pairs in the cache.
305319
fn bytes_size(&self) -> u64 {
306320
self.bytes as u64

src/query/service/src/interpreters/access/privilege_access.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ enum ObjectId {
6060
}
6161

6262
// table functions that need `Super` privilege
63-
const SYSTEM_TABLE_FUNCTIONS: [&str; 1] = ["fuse_amend"];
63+
const SYSTEM_TABLE_FUNCTIONS: [&str; 2] = ["fuse_amend", "set_cache_capacity"];
6464

6565
impl PrivilegeAccess {
6666
pub fn create(ctx: Arc<QueryContext>) -> Box<dyn AccessChecker> {

src/query/service/src/table_functions/table_function_factory.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use databend_common_storages_fuse::table_functions::FuseEncodingFunc;
2727
use databend_common_storages_fuse::table_functions::FuseStatisticsFunc;
2828
use databend_common_storages_fuse::table_functions::FuseTimeTravelSizeFunc;
2929
use databend_common_storages_fuse::table_functions::FuseVacuumTemporaryTable;
30+
use databend_common_storages_fuse::table_functions::SetCacheCapacity;
3031
use databend_common_storages_fuse::table_functions::TableFunctionTemplate;
3132
use databend_common_storages_stream::stream_status_table_func::StreamStatusTable;
3233
use databend_storages_common_table_meta::table_id_ranges::SYS_TBL_FUC_ID_END;
@@ -138,6 +139,14 @@ impl TableFunctionFactory {
138139
),
139140
);
140141

142+
creators.insert(
143+
"set_cache_capacity".to_string(),
144+
(
145+
next_id(),
146+
Arc::new(TableFunctionTemplate::<SetCacheCapacity>::create),
147+
),
148+
);
149+
141150
creators.insert(
142151
"fuse_segment".to_string(),
143152
(

0 commit comments

Comments
 (0)