/build/source/nativelink-store/src/store_manager.rs
Line | Count | Source |
1 | | // Copyright 2024 The NativeLink Authors. 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 | 89 | pub fn new() -> Self { |
30 | 89 | Self { |
31 | 89 | stores: RwLock::new(HashMap::new()), |
32 | 89 | } |
33 | 89 | } |
34 | | |
35 | 105 | pub fn add_store(&self, name: &str, store: Store) -> Result<(), Error> { |
36 | 105 | let mut stores = self.stores.write(); |
37 | 105 | 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 | 104 | } |
43 | 104 | stores.insert(name.to_string(), store); |
44 | 104 | Ok(()) |
45 | 105 | } |
46 | | |
47 | 138 | pub fn get_store(&self, name: &str) -> Option<Store> { |
48 | 138 | let stores = self.stores.read(); |
49 | 138 | if let Some(store) = stores.get(name) { |
50 | 138 | return Some(store.clone()); |
51 | 0 | } |
52 | 0 | None |
53 | 138 | } |
54 | | |
55 | 4 | pub async fn run_post_init(&self) -> Result<(), Error> { |
56 | 4 | let stores = { |
57 | 4 | let lock = self.stores.read(); |
58 | 4 | lock.values().cloned().collect::<Vec<_>>() |
59 | | }; |
60 | 9 | for store in stores4 { |
61 | 9 | store.into_inner().post_init().await?0 ; |
62 | | } |
63 | 4 | Ok(()) |
64 | 4 | } |
65 | | } |
66 | | |
67 | | impl RootMetricsComponent for StoreManager {} |