Skip to content

Commit ca22eaa

Browse files
authored
feat: Add trait for ObjectCache and ObjectCacheProvider (#1222)
## Which issue does this PR close? Part of #1217 I plan to introduce `ObjectCache` and `ObjectCachePorvide` to replace the existing `ObjectCache`. These cache traits will serve as the entry point for users to configure the cache we need to use in Iceberg. Users can easily implement a cache provider and share the same cache across multiple Iceberg tables. This design also move the entire cache building logic to users side so that iceberg don't need to specify anything hardcode. I also plan to move the existsing moka based cache to our future lite engine so users can still have a good enough default. ## What changes are included in this PR? - Add `ObjectCache` trait for get/set cache enties. - Add `ObjectCacheProvide` trait for users to implement their own cache logic. ## Are these changes tested? No need. --------- Signed-off-by: Xuanwo <[email protected]>
1 parent 74b5f60 commit ca22eaa

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

crates/iceberg/src/cache.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
20+
use std::sync::Arc;
21+
22+
use 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.
32+
pub 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.
47+
pub 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.
55+
pub type ObjectCacheProvider = Arc<dyn ObjectCacheProvide>;
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::*;
60+
61+
struct _TestDynCompatibleForObjectCache(Arc<dyn ObjectCache<String, Arc<Manifest>>>);
62+
struct _TestDynCompatibleForObjectCacheProvider(ObjectCacheProvider);
63+
}

crates/iceberg/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub use catalog::{
7070
pub mod table;
7171

7272
mod avro;
73+
pub mod cache;
7374
pub mod io;
7475
pub mod spec;
7576

0 commit comments

Comments
 (0)