|
| 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::sync::Arc; |
| 19 | + |
| 20 | +use crate::io::FileIO; |
| 21 | +use crate::spec::{ |
| 22 | + FormatVersion, Manifest, ManifestFile, ManifestList, SchemaId, SnapshotRef, TableMetadataRef, |
| 23 | +}; |
| 24 | +use crate::{Error, ErrorKind, Result}; |
| 25 | + |
| 26 | +const DEFAULT_CACHE_SIZE_BYTES: u64 = 2 ^ 15; // 32MB |
| 27 | + |
| 28 | +#[derive(Clone, Debug)] |
| 29 | +pub(crate) enum CachedItem { |
| 30 | + ManifestList(Arc<ManifestList>), |
| 31 | + Manifest(Arc<Manifest>), |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Clone, Debug, Hash, Eq, PartialEq)] |
| 35 | +pub(crate) enum CachedObjectKey { |
| 36 | + ManifestList((String, FormatVersion, SchemaId)), |
| 37 | + Manifest(String), |
| 38 | +} |
| 39 | + |
| 40 | +/// Caches metadata objects deserialized from immutable files |
| 41 | +#[derive(Clone, Debug)] |
| 42 | +pub struct ObjectCache { |
| 43 | + cache: moka::future::Cache<CachedObjectKey, CachedItem>, |
| 44 | + file_io: FileIO, |
| 45 | + cache_disabled: bool, |
| 46 | +} |
| 47 | + |
| 48 | +impl ObjectCache { |
| 49 | + /// Creates a new [`ObjectCache`] |
| 50 | + /// with the default cache size |
| 51 | + pub(crate) fn new(file_io: FileIO) -> Self { |
| 52 | + Self::new_with_cache_size(file_io, DEFAULT_CACHE_SIZE_BYTES) |
| 53 | + } |
| 54 | + |
| 55 | + /// Creates a new [`ObjectCache`] |
| 56 | + /// with a specific cache size |
| 57 | + pub(crate) fn new_with_cache_size(file_io: FileIO, cache_size_bytes: u64) -> Self { |
| 58 | + if cache_size_bytes == 0 { |
| 59 | + Self::with_disabled_cache(file_io) |
| 60 | + } else { |
| 61 | + Self { |
| 62 | + cache: moka::future::Cache::new(cache_size_bytes), |
| 63 | + file_io, |
| 64 | + cache_disabled: false, |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Creates a new [`ObjectCache`] |
| 70 | + /// with caching disabled |
| 71 | + pub(crate) fn with_disabled_cache(file_io: FileIO) -> Self { |
| 72 | + Self { |
| 73 | + cache: moka::future::Cache::new(0), |
| 74 | + file_io, |
| 75 | + cache_disabled: true, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Retrieves an Arc [`Manifest`] from the cache |
| 80 | + /// or retrieves one from FileIO and parses it if not present |
| 81 | + pub(crate) async fn get_manifest(&self, manifest_file: &ManifestFile) -> Result<Arc<Manifest>> { |
| 82 | + if self.cache_disabled { |
| 83 | + return manifest_file |
| 84 | + .load_manifest(&self.file_io) |
| 85 | + .await |
| 86 | + .map(Arc::new); |
| 87 | + } |
| 88 | + |
| 89 | + let key = CachedObjectKey::Manifest(manifest_file.manifest_path.clone()); |
| 90 | + |
| 91 | + let cache_entry = self |
| 92 | + .cache |
| 93 | + .entry_by_ref(&key) |
| 94 | + .or_try_insert_with(self.fetch_and_parse_manifest(manifest_file)) |
| 95 | + .await |
| 96 | + .map_err(|err| Error::new(ErrorKind::Unexpected, err.as_ref().message()))? |
| 97 | + .into_value(); |
| 98 | + |
| 99 | + match cache_entry { |
| 100 | + CachedItem::Manifest(arc_manifest) => Ok(arc_manifest), |
| 101 | + _ => Err(Error::new( |
| 102 | + ErrorKind::Unexpected, |
| 103 | + format!("cached object for key '{:?}' is not a Manifest", key), |
| 104 | + )), |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// Retrieves an Arc [`ManifestList`] from the cache |
| 109 | + /// or retrieves one from FileIO and parses it if not present |
| 110 | + pub(crate) async fn get_manifest_list( |
| 111 | + &self, |
| 112 | + snapshot: &SnapshotRef, |
| 113 | + table_metadata: &TableMetadataRef, |
| 114 | + ) -> Result<Arc<ManifestList>> { |
| 115 | + if self.cache_disabled { |
| 116 | + return snapshot |
| 117 | + .load_manifest_list(&self.file_io, table_metadata) |
| 118 | + .await |
| 119 | + .map(Arc::new); |
| 120 | + } |
| 121 | + |
| 122 | + let key = CachedObjectKey::ManifestList(( |
| 123 | + snapshot.manifest_list().to_string(), |
| 124 | + table_metadata.format_version, |
| 125 | + snapshot.schema_id().unwrap(), |
| 126 | + )); |
| 127 | + let cache_entry = self |
| 128 | + .cache |
| 129 | + .entry_by_ref(&key) |
| 130 | + .or_try_insert_with(self.fetch_and_parse_manifest_list(snapshot, table_metadata)) |
| 131 | + .await |
| 132 | + .map_err(|err| Error::new(ErrorKind::Unexpected, err.as_ref().message()))? |
| 133 | + .into_value(); |
| 134 | + |
| 135 | + match cache_entry { |
| 136 | + CachedItem::ManifestList(arc_manifest_list) => Ok(arc_manifest_list), |
| 137 | + _ => Err(Error::new( |
| 138 | + ErrorKind::Unexpected, |
| 139 | + format!("cached object for path '{:?}' is not a Manifest", key), |
| 140 | + )), |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + async fn fetch_and_parse_manifest(&self, manifest_file: &ManifestFile) -> Result<CachedItem> { |
| 145 | + let manifest = manifest_file.load_manifest(&self.file_io).await?; |
| 146 | + |
| 147 | + Ok(CachedItem::Manifest(Arc::new(manifest))) |
| 148 | + } |
| 149 | + |
| 150 | + async fn fetch_and_parse_manifest_list( |
| 151 | + &self, |
| 152 | + snapshot: &SnapshotRef, |
| 153 | + table_metadata: &TableMetadataRef, |
| 154 | + ) -> Result<CachedItem> { |
| 155 | + let manifest_list = snapshot |
| 156 | + .load_manifest_list(&self.file_io, table_metadata) |
| 157 | + .await?; |
| 158 | + |
| 159 | + Ok(CachedItem::ManifestList(Arc::new(manifest_list))) |
| 160 | + } |
| 161 | +} |
0 commit comments