Skip to content

Commit e8f795e

Browse files
feat: Implement ObjectCache for moka (#1225)
## Which issue does this PR close? Part of #1217 ## What changes are included in this PR? Implement ObjectCache for moka. After this PR, we can start migrating existing `ObjectCache` APIs. ## Are these changes tested? Signed-off-by: Xuanwo <[email protected]> Co-authored-by: Renjie Liu <[email protected]>
1 parent e497fb1 commit e8f795e

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
[package]
19+
name = "iceberg-cache-moka"
20+
21+
edition = { workspace = true }
22+
homepage = { workspace = true }
23+
license = { workspace = true }
24+
repository = { workspace = true }
25+
rust-version = { workspace = true }
26+
version = { workspace = true }
27+
28+
[dependencies]
29+
iceberg = { workspace = true }
30+
moka = { version = "0.12.10", features = ["sync"] }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
~ Licensed to the Apache Software Foundation (ASF) under one
3+
~ or more contributor license agreements. See the NOTICE file
4+
~ distributed with this work for additional information
5+
~ regarding copyright ownership. The ASF licenses this file
6+
~ to you under the Apache License, Version 2.0 (the
7+
~ "License"); you may not use this file except in compliance
8+
~ with the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing,
13+
~ software distributed under the License is distributed on an
14+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
~ KIND, either express or implied. See the License for the
16+
~ specific language governing permissions and limitations
17+
~ under the License.
18+
-->
19+
20+
# Apache Iceberg Rust Cache Moka
21+
22+
This crate provides a [moka](https://github.com/moka-rs/moka) cache implementation for Apache Iceberg Rust. It is used to cache data in memory for faster access.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)