summaryrefslogtreecommitdiffstats
path: root/src/filestore/util.rs
blob: ebfbbd797623552190609e05155842d2a19fade6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

//! Module containing utilities for the filestore implementation
//!

use std::collections::HashSet;

use anyhow::Result;
use indicatif::ProgressBar;

use crate::filestore::path::ArtifactPath;
use crate::filestore::path::StoreRoot;

/// The actual filestore implementation
///
/// Because the "staging" filestore and the "release" filestore function the same underneath, we
/// provide this type as the implementation.
///
/// It can then be wrapped into the actual interface of this module with specialized functionality.
#[derive(getset::Getters)]
pub struct FileStoreImpl {
    #[getset(get = "pub")]
    root_path: StoreRoot,
    store: HashSet<ArtifactPath>,
}

impl FileStoreImpl {
    /// Loads the passed path recursively
    pub fn load(root_path: StoreRoot, progress: &ProgressBar) -> Result<Self> {
        let store = root_path
            .find_artifacts_recursive()
            .inspect(|path| {
                log::trace!("Found artifact path: {:?}", path);
                progress.tick();
            })
            .collect::<Result<HashSet<ArtifactPath>>>()?;

        Ok(FileStoreImpl { root_path, store })
    }

    pub fn get(&self, artifact_path: &ArtifactPath) -> Option<&ArtifactPath> {
        self.store.get(artifact_path)
    }

    pub(in crate::filestore) fn load_from_path<'a>(
        &mut self,
        artifact_path: &'a ArtifactPath,
    ) -> &'a ArtifactPath {
        if !self.store.contains(artifact_path) {
            self.store.insert(artifact_path.clone());
        }
        artifact_path
    }
}