iceberg/
cache.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
18//! Cache management for Iceberg.
19
20use std::sync::Arc;
21
22use crate::spec::{Manifest, ManifestList};
23
24/// A trait for caching in-memory objects of given type.
25///
26/// # Notes
27///
28/// ObjectCache will store deeply nested objects, such as `Manifest`,
29/// which contains `Schema`. Please ensure that the cache stores the
30/// object in memory as-is, without attempting to serialize it, as
31/// serialization could be extremely expensive.
32pub trait ObjectCache<K, V>: Send + Sync {
33    /// Gets an object from the cache by its key.
34    fn get(&self, key: &K) -> Option<V>;
35    /// Sets an object in the cache with the given key and value.
36    fn set(&self, key: K, value: V);
37}
38
39/// A trait for caching different in-memory objects used by iceberg.
40///
41/// # Notes
42///
43/// ObjectCache will store deeply nested objects, such as `Manifest`,
44/// which contains `Schema`. Please ensure that the cache stores the
45/// object in memory as-is, without attempting to serialize it, as
46/// serialization could be extremely expensive.
47pub trait ObjectCacheProvide: Send + Sync {
48    /// Gets a cache for manifests.
49    fn manifest_cache(&self) -> &dyn ObjectCache<String, Arc<Manifest>>;
50    /// Gets a cache for manifest lists.
51    fn manifest_list_cache(&self) -> &dyn ObjectCache<String, Arc<ManifestList>>;
52}
53
54/// CacheProvider is a type alias for a thread-safe reference-counted pointer to a CacheProvide trait object.
55pub type ObjectCacheProvider = Arc<dyn ObjectCacheProvide>;
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    struct _TestDynCompatibleForObjectCache(Arc<dyn ObjectCache<String, Arc<Manifest>>>);
62    struct _TestDynCompatibleForObjectCacheProvider(ObjectCacheProvider);
63}