Coverage Report

Created: 2026-11-13 23:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/build/source/nativelink-store/src/store_manager.rs
Line
Count
Source
1
// Copyright 2024 Trace Machina, Inc. All rights reserved.
2
//
3
// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (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
//    See LICENSE file for details
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
15
use std::collections::HashMap;
16
17
use nativelink_error::{Code, Error, make_err};
18
use nativelink_metric::{MetricsComponent, RootMetricsComponent};
19
use nativelink_util::store_trait::Store;
20
use parking_lot::RwLock;
21
22
#[derive(Debug, Default, MetricsComponent)]
23
pub struct StoreManager {
24
    #[metric]
25
    stores: RwLock<HashMap<String, Store>>,
26
}
27
28
impl StoreManager {
29
369
    pub fn new() -> Self {
30
369
        Self {
31
369
            stores: RwLock::new(HashMap::new()),
32
369
        }
33
369
    }
34
35
434
    pub fn add_store(&self, name: &str, store: Store) -> Result<(), Error> {
36
434
        let mut stores = self.stores.write();
37
434
        if stores.contains_key(name) {
38
1
            return Err(make_err!(
39
1
                Code::AlreadyExists,
40
1
                "Store name '{name}' already exists in the store manager"
41
1
            ));
42
433
        }
43
433
        stores.insert(name.to_string(), store);
44
433
        Ok(())
45
434
    }
46
47
705
    pub fn get_store(&self, name: &str) -> Option<Store> {
48
705
        let stores = self.stores.read();
49
705
        if let Some(
store704
) = stores.get(name) {
50
704
            return Some(store.clone());
51
1
        }
52
1
        None
53
705
    }
54
55
5
    pub async fn run_post_init(&self) -> Result<(), Error> {
56
5
        let stores = {
57
5
            let lock = self.stores.read();
58
5
            lock.values().cloned().collect::<Vec<_>>()
59
        };
60
9
        for store in 
stores5
{
61
9
            store.into_inner().post_init().await
?0
;
62
        }
63
5
        Ok(())
64
5
    }
65
}
66
67
impl RootMetricsComponent for StoreManager {}