iceberg_cache_moka/
lib.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
18use std::hash::Hash;
19use std::sync::Arc;
20
21use iceberg::cache::{ObjectCache, ObjectCacheProvide};
22use iceberg::spec::{Manifest, ManifestList};
23
24const DEFAULT_CACHE_SIZE_BYTES: u64 = 32 * 1024 * 1024; // 32MiB
25
26struct MokaObjectCache<K, V>(moka::sync::Cache<K, V>);
27
28impl<K, V> ObjectCache<K, V> for MokaObjectCache<K, V>
29where
30    K: Hash + Eq + Send + Sync + 'static,
31    V: Clone + Send + Sync + 'static,
32{
33    fn get(&self, key: &K) -> Option<V> {
34        self.0.get(key)
35    }
36
37    fn set(&self, key: K, value: V) {
38        self.0.insert(key, value);
39    }
40}
41
42/// A cache provider that uses Moka for caching objects.
43pub struct MokaObjectCacheProvider {
44    manifest_cache: MokaObjectCache<String, Arc<Manifest>>,
45    manifest_list_cache: MokaObjectCache<String, Arc<ManifestList>>,
46}
47
48impl Default for MokaObjectCacheProvider {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl MokaObjectCacheProvider {
55    /// Creates a new `MokaObjectCacheProvider` with default cache sizes.
56    pub fn new() -> Self {
57        let manifest_cache = MokaObjectCache(moka::sync::Cache::new(DEFAULT_CACHE_SIZE_BYTES));
58        let manifest_list_cache = MokaObjectCache(moka::sync::Cache::new(DEFAULT_CACHE_SIZE_BYTES));
59
60        Self {
61            manifest_cache,
62            manifest_list_cache,
63        }
64    }
65
66    /// Set the cache for manifests.
67    pub fn with_manifest_cache(mut self, cache: moka::sync::Cache<String, Arc<Manifest>>) -> Self {
68        self.manifest_cache = MokaObjectCache(cache);
69        self
70    }
71
72    /// Set the cache for manifest lists.
73    pub fn with_manifest_list_cache(
74        mut self,
75        cache: moka::sync::Cache<String, Arc<ManifestList>>,
76    ) -> Self {
77        self.manifest_list_cache = MokaObjectCache(cache);
78        self
79    }
80}
81
82impl ObjectCacheProvide for MokaObjectCacheProvider {
83    fn manifest_cache(&self) -> &dyn ObjectCache<String, Arc<Manifest>> {
84        &self.manifest_cache
85    }
86
87    fn manifest_list_cache(&self) -> &dyn ObjectCache<String, Arc<ManifestList>> {
88        &self.manifest_list_cache
89    }
90}