|
| 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 | +use std::hash::Hash; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +use iceberg::cache::{ObjectCache, ObjectCacheProvide}; |
| 22 | +use iceberg::spec::{Manifest, ManifestList}; |
| 23 | + |
| 24 | +const DEFAULT_CACHE_SIZE_BYTES: u64 = 32 * 1024 * 1024; // 32MiB |
| 25 | + |
| 26 | +struct MokaObjectCache<K, V>(moka::sync::Cache<K, V>); |
| 27 | + |
| 28 | +impl<K, V> ObjectCache<K, V> for MokaObjectCache<K, V> |
| 29 | +where |
| 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. |
| 43 | +pub struct MokaObjectCacheProvider { |
| 44 | + manifest_cache: MokaObjectCache<String, Arc<Manifest>>, |
| 45 | + manifest_list_cache: MokaObjectCache<String, Arc<ManifestList>>, |
| 46 | +} |
| 47 | + |
| 48 | +impl Default for MokaObjectCacheProvider { |
| 49 | + fn default() -> Self { |
| 50 | + Self::new() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl 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 | + |
| 82 | +impl 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 | +} |
0 commit comments