Skip to main content

datafusion_execution/cache/
mod.rs

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
18pub mod cache_manager;
19pub mod cache_unit;
20pub mod lru_queue;
21
22mod file_metadata_cache;
23mod list_files_cache;
24
25pub use file_metadata_cache::DefaultFilesMetadataCache;
26pub use list_files_cache::DefaultListFilesCache;
27pub use list_files_cache::ListFilesEntry;
28pub use list_files_cache::TableScopedPath;
29
30/// Base trait for cache implementations with common operations.
31///
32/// This trait provides the fundamental cache operations (`get`, `put`, `remove`, etc.)
33/// that all cache types share. Specific cache traits like [`cache_manager::FileStatisticsCache`],
34/// [`cache_manager::ListFilesCache`], and [`cache_manager::FileMetadataCache`] extend this
35/// trait with their specialized methods.
36///
37/// ## Thread Safety
38///
39/// Implementations must handle their own locking via internal mutability, as methods do not
40/// take mutable references and may be accessed by multiple concurrent queries.
41///
42/// ## Validation Pattern
43///
44/// Validation metadata (e.g., file size, last modified time) should be embedded in the
45/// value type `V`. The typical usage pattern is:
46/// 1. Call `get(key)` to check for cached value
47/// 2. If `Some(cached)`, validate with `cached.is_valid_for(&current_meta)`
48/// 3. If invalid or missing, compute new value and call `put(key, new_value)`
49pub trait CacheAccessor<K, V>: Send + Sync {
50    /// Get a cached entry if it exists.
51    ///
52    /// Returns the cached value without any validation. The caller should
53    /// validate the returned value if freshness matters.
54    fn get(&self, key: &K) -> Option<V>;
55
56    /// Store a value in the cache.
57    ///
58    /// Returns the previous value if one existed.
59    fn put(&self, key: &K, value: V) -> Option<V>;
60
61    /// Remove an entry from the cache, returning the value if it existed.
62    fn remove(&self, k: &K) -> Option<V>;
63
64    /// Check if the cache contains a specific key.
65    fn contains_key(&self, k: &K) -> bool;
66
67    /// Fetch the total number of cache entries.
68    fn len(&self) -> usize;
69
70    /// Check if the cache collection is empty.
71    fn is_empty(&self) -> bool {
72        self.len() == 0
73    }
74
75    /// Remove all entries from the cache.
76    fn clear(&self);
77
78    /// Return the cache name.
79    fn name(&self) -> String;
80}