BlakeOrth commented on code in PR #18855: URL: https://github.com/apache/datafusion/pull/18855#discussion_r2582916461
########## datafusion/execution/src/cache/list_files_cache.rs: ########## @@ -0,0 +1,731 @@ +// 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. + +use std::{ + sync::{Arc, Mutex}, + time::Duration, +}; + +use datafusion_common::instant::Instant; +use object_store::{path::Path, ObjectMeta}; + +use crate::cache::{cache_manager::ListFilesCache, lru_queue::LruQueue, CacheAccessor}; + +/// Default implementation of [`ListFilesCache`] +/// +/// Caches file metadata for file listing operations. +/// +/// # Internal details +/// +/// The `memory_limit` parameter controls the maximum size of the cache, which uses a Least +/// Recently Used eviction algorithm. When adding a new entry, if the total number of entries in +/// the cache exceeds `memory_limit`, the least recently used entries are evicted until the total +/// size is lower than the `memory_limit`. +/// +/// # `Extra` Handling +/// +/// Users should use the [`Self::get`] and [`Self::put`] methods. The +/// [`Self::get_with_extra`] and [`Self::put_with_extra`] methods simply call +/// `get` and `put`, respectively. +#[derive(Default)] +pub struct DefaultListFilesCache { + state: Mutex<DefaultListFilesCacheState>, +} + +impl DefaultListFilesCache { + /// Creates a new instance of [`DefaultListFilesCache`]. + /// + /// # Arguments + /// * `memory_limit` - The maximum size of the cache, in bytes. + /// * `ttl` - The TTL (time-to-live) of entries in the cache. + pub fn new(memory_limit: usize, ttl: Option<Duration>) -> Self { + Self { + state: Mutex::new(DefaultListFilesCacheState::new(memory_limit, ttl)), + } + } + + /// Returns the cache's memory limit in bytes. + pub fn cache_limit(&self) -> usize { + self.state.lock().unwrap().memory_limit + } + + /// Updates the cache with a new memory limit in bytes. + pub fn update_cache_limit(&self, limit: usize) { + let mut state = self.state.lock().unwrap(); + state.memory_limit = limit; + state.evict_entries(); + } + + /// Returns the TTL (time-to-live) applied to cache entries. + pub fn cache_ttl(&self) -> Option<Duration> { + self.state.lock().unwrap().ttl + } +} + +struct ListFilesEntry { + metas: Arc<Vec<ObjectMeta>>, + size_bytes: usize, + expires: Option<Instant>, +} + +impl ListFilesEntry { + fn try_new(metas: Arc<Vec<ObjectMeta>>, ttl: Option<Duration>) -> Option<Self> { + let size_bytes = (metas.capacity() * size_of::<ObjectMeta>()) + + metas.iter().map(meta_heap_bytes).reduce(|acc, b| acc + b)?; Review Comment: Thank you for double checking this! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
