mito2/manifest/storage/
utils.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use object_store::{Entry, ObjectStore};
16use snafu::ResultExt;
17use store_api::ManifestVersion;
18
19use crate::cache::manifest_cache::ManifestCache;
20use crate::error::{OpenDalSnafu, Result};
21
22/// Gets a manifest file from cache.
23/// Returns the file data if found in cache, None otherwise.
24pub(crate) async fn get_from_cache(cache: Option<&ManifestCache>, key: &str) -> Option<Vec<u8>> {
25    let cache = cache?;
26    cache.get_file(key).await
27}
28
29/// Puts a manifest file into cache.
30pub(crate) async fn put_to_cache(cache: Option<&ManifestCache>, key: String, data: &[u8]) {
31    let Some(cache) = cache else {
32        return;
33    };
34    cache.put_file(key, data.to_vec()).await
35}
36
37/// Removes a manifest file from cache.
38pub(crate) async fn remove_from_cache(cache: Option<&ManifestCache>, key: &str) {
39    let Some(cache) = cache else {
40        return;
41    };
42    cache.remove(key).await
43}
44
45/// Writes data to object store and puts it into cache.
46pub(crate) async fn write_and_put_cache(
47    object_store: &ObjectStore,
48    cache: Option<&ManifestCache>,
49    path: &str,
50    data: Vec<u8>,
51) -> Result<()> {
52    // Clone data for cache before writing, only if cache is enabled.
53    let cache_data = if cache.is_some() {
54        Some(data.clone())
55    } else {
56        None
57    };
58
59    // Write to object store
60    object_store.write(path, data).await.context(OpenDalSnafu)?;
61
62    // Put to cache if we cloned the data
63    if let Some(data) = cache_data {
64        put_to_cache(cache, path.to_string(), &data).await;
65    }
66
67    Ok(())
68}
69
70/// Sorts the manifest files.
71pub(crate) fn sort_manifests(entries: &mut [(ManifestVersion, Entry)]) {
72    entries.sort_unstable_by_key(|(version, _)| *version);
73}