Skip to content

Commit 708b63d

Browse files
committed
Remove custom default label handling for key-value
Signed-off-by: Ryan Levick <[email protected]>
1 parent 90a2adb commit 708b63d

File tree

9 files changed

+288
-304
lines changed

9 files changed

+288
-304
lines changed

crates/factor-key-value/src/lib.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use spin_factors::{
1212
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, PrepareContext, RuntimeFactors,
1313
};
1414
use spin_locked_app::MetadataKey;
15-
use util::DefaultManagerGetter;
1615

1716
/// Metadata key for key-value stores.
1817
pub const KEY_VALUE_STORES_KEY: MetadataKey<Vec<String>> = MetadataKey::new("key_value_stores");
@@ -21,19 +20,15 @@ pub use runtime_config::RuntimeConfig;
2120
pub use util::{CachingStoreManager, DelegatingStoreManager};
2221

2322
/// A factor that provides key-value storage.
23+
#[derive(Default)]
2424
pub struct KeyValueFactor {
25-
default_label_resolver: Arc<dyn DefaultLabelResolver>,
25+
_priv: (),
2626
}
2727

2828
impl KeyValueFactor {
2929
/// Create a new KeyValueFactor.
30-
///
31-
/// The `default_label_resolver` is used to resolve store managers for labels that
32-
/// are not defined in the runtime configuration.
33-
pub fn new(default_label_resolver: impl DefaultLabelResolver + 'static) -> Self {
34-
Self {
35-
default_label_resolver: Arc::new(default_label_resolver),
36-
}
30+
pub fn new() -> Self {
31+
Self { _priv: () }
3732
}
3833
}
3934

@@ -53,11 +48,8 @@ impl Factor for KeyValueFactor {
5348
mut ctx: ConfigureAppContext<T, Self>,
5449
) -> anyhow::Result<Self::AppState> {
5550
let store_managers = ctx.take_runtime_config().unwrap_or_default();
56-
let default_label_resolver = self.default_label_resolver.clone();
57-
let default_fn: DefaultManagerGetter =
58-
Arc::new(move |label| default_label_resolver.default(label));
5951

60-
let delegating_manager = DelegatingStoreManager::new(store_managers, default_fn);
52+
let delegating_manager = DelegatingStoreManager::new(store_managers);
6153
let caching_manager = CachingStoreManager::new(delegating_manager);
6254
let store_manager_manager = Arc::new(caching_manager);
6355

@@ -73,8 +65,7 @@ impl Factor for KeyValueFactor {
7365
for label in &key_value_stores {
7466
// TODO: port nicer errors from KeyValueComponent (via error type?)
7567
ensure!(
76-
store_manager_manager.is_defined(label)
77-
|| self.default_label_resolver.default(label).is_some(),
68+
store_manager_manager.is_defined(label),
7869
"unknown key_value_stores label {label:?} for component {component_id:?}"
7970
);
8071
}
@@ -164,17 +155,3 @@ impl FactorInstanceBuilder for InstanceBuilder {
164155
Ok(dispatch)
165156
}
166157
}
167-
168-
/// Resolves a label to a default [`StoreManager`].
169-
pub trait DefaultLabelResolver: Send + Sync {
170-
/// If there is no runtime configuration for a given store label, return a default store manager.
171-
///
172-
/// If `Option::None` is returned, the store is not allowed.
173-
fn default(&self, label: &str) -> Option<Arc<dyn StoreManager>>;
174-
}
175-
176-
impl<T: DefaultLabelResolver> DefaultLabelResolver for Arc<T> {
177-
fn default(&self, label: &str) -> Option<Arc<dyn StoreManager>> {
178-
self.as_ref().default(label)
179-
}
180-
}

crates/factor-key-value/src/runtime_config/spin.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Runtime configuration implementation used by Spin CLI.
22
3-
use crate::StoreManager;
4-
use crate::{DefaultLabelResolver, RuntimeConfig};
3+
use crate::{RuntimeConfig, StoreManager};
54
use anyhow::Context as _;
65
use serde::de::DeserializeOwned;
76
use serde::{Deserialize, Serialize};
@@ -99,6 +98,8 @@ impl RuntimeConfigResolver {
9998
}
10099

101100
/// Resolves a toml table into a runtime config.
101+
///
102+
/// The default stores are also added to the runtime config.
102103
pub fn resolve_from_toml(
103104
&self,
104105
table: Option<&impl GetTomlValue>,
@@ -115,6 +116,17 @@ impl RuntimeConfigResolver {
115116
})?;
116117
runtime_config.add_store_manager(label.clone(), store_manager);
117118
}
119+
120+
for (&label, config) in &self.defaults {
121+
if !runtime_config.store_managers.contains_key(label) {
122+
let store_manager = self
123+
.store_manager_from_config(config.clone())
124+
.with_context(|| {
125+
format!("could not configure key-value store with label '{label}'")
126+
})?;
127+
runtime_config.add_store_manager(label.to_owned(), store_manager);
128+
}
129+
}
118130
Ok(Some(runtime_config))
119131
}
120132

@@ -134,15 +146,6 @@ impl RuntimeConfigResolver {
134146
}
135147
}
136148

137-
impl DefaultLabelResolver for RuntimeConfigResolver {
138-
fn default(&self, label: &str) -> Option<Arc<dyn StoreManager>> {
139-
let config = self.defaults.get(label)?;
140-
// TODO(rylev): The unwrap here is not ideal. We should return a Result instead.
141-
// Piping that through `DefaultLabelResolver` is a bit awkward, though.
142-
Some(self.store_manager_from_config(config.clone()).unwrap())
143-
}
144-
}
145-
146149
#[derive(Deserialize, Clone)]
147150
pub struct StoreConfig {
148151
#[serde(rename = "type")]

crates/factor-key-value/src/util.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,14 @@ impl StoreManager for EmptyStoreManager {
2828
}
2929
}
3030

31-
/// A function that takes a store label and returns the default store manager, if one exists.
32-
pub type DefaultManagerGetter = Arc<dyn Fn(&str) -> Option<Arc<dyn StoreManager>> + Send + Sync>;
33-
3431
pub struct DelegatingStoreManager {
3532
delegates: HashMap<String, Arc<dyn StoreManager>>,
36-
default_manager: DefaultManagerGetter,
3733
}
3834

3935
impl DelegatingStoreManager {
40-
pub fn new(
41-
delegates: impl IntoIterator<Item = (String, Arc<dyn StoreManager>)>,
42-
default_manager: DefaultManagerGetter,
43-
) -> Self {
36+
pub fn new(delegates: impl IntoIterator<Item = (String, Arc<dyn StoreManager>)>) -> Self {
4437
let delegates = delegates.into_iter().collect();
45-
Self {
46-
delegates,
47-
default_manager,
48-
}
38+
Self { delegates }
4939
}
5040
}
5141

@@ -54,12 +44,7 @@ impl StoreManager for DelegatingStoreManager {
5444
async fn get(&self, name: &str) -> Result<Arc<dyn Store>, Error> {
5545
match self.delegates.get(name) {
5646
Some(store) => store.get(name).await,
57-
None => {
58-
(self.default_manager)(name)
59-
.ok_or(Error::NoSuchStore)?
60-
.get(name)
61-
.await
62-
}
47+
None => Err(Error::NoSuchStore),
6348
}
6449
}
6550

@@ -71,7 +56,7 @@ impl StoreManager for DelegatingStoreManager {
7156
if let Some(store) = self.delegates.get(store_name) {
7257
return store.summary(store_name);
7358
}
74-
(self.default_manager)(store_name)?.summary(store_name)
59+
None
7560
}
7661
}
7762

0 commit comments

Comments
 (0)