Skip to content

Commit 5db8565

Browse files
committed
Clean up factor a bit
Signed-off-by: Ryan Levick <[email protected]>
1 parent 2bc860d commit 5db8565

File tree

4 files changed

+21
-40
lines changed

4 files changed

+21
-40
lines changed

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::util::EmptyStoreManager;
21
use anyhow::{Context, Result};
32
use spin_core::{async_trait, wasmtime::component::Resource};
43
use spin_world::v2::key_value;
@@ -40,23 +39,22 @@ pub struct KeyValueDispatch {
4039
}
4140

4241
impl KeyValueDispatch {
43-
pub fn new() -> Self {
44-
Self::new_with_capacity(DEFAULT_STORE_TABLE_CAPACITY)
42+
pub fn new(allowed_stores: HashSet<String>, manager: Arc<dyn StoreManager>) -> Self {
43+
Self::new_with_capacity(allowed_stores, manager, DEFAULT_STORE_TABLE_CAPACITY)
4544
}
4645

47-
pub fn new_with_capacity(capacity: u32) -> Self {
46+
pub fn new_with_capacity(
47+
allowed_stores: HashSet<String>,
48+
manager: Arc<dyn StoreManager>,
49+
capacity: u32,
50+
) -> Self {
4851
Self {
49-
allowed_stores: HashSet::new(),
50-
manager: Arc::new(EmptyStoreManager),
52+
allowed_stores,
53+
manager,
5154
stores: Table::new(capacity),
5255
}
5356
}
5457

55-
pub fn init(&mut self, allowed_stores: HashSet<String>, manager: Arc<dyn StoreManager>) {
56-
self.allowed_stores = allowed_stores;
57-
self.manager = manager;
58-
}
59-
6058
pub fn get_store(&self, store: Resource<key_value::Store>) -> anyhow::Result<&Arc<dyn Store>> {
6159
self.stores.get(store.rep()).context("invalid store")
6260
}
@@ -66,12 +64,6 @@ impl KeyValueDispatch {
6664
}
6765
}
6866

69-
impl Default for KeyValueDispatch {
70-
fn default() -> Self {
71-
Self::new()
72-
}
73-
}
74-
7567
#[async_trait]
7668
impl key_value::Host for KeyValueDispatch {}
7769

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Factor for KeyValueFactor {
5151

5252
let delegating_manager = DelegatingStoreManager::new(store_managers);
5353
let caching_manager = CachingStoreManager::new(delegating_manager);
54-
let store_manager_manager = Arc::new(caching_manager);
54+
let store_manager = Arc::new(caching_manager);
5555

5656
// Build component -> allowed stores map
5757
let mut component_allowed_stores = HashMap::new();
@@ -65,7 +65,7 @@ impl Factor for KeyValueFactor {
6565
for label in &key_value_stores {
6666
// TODO: port nicer errors from KeyValueComponent (via error type?)
6767
ensure!(
68-
store_manager_manager.is_defined(label),
68+
store_manager.is_defined(label),
6969
"unknown key_value_stores label {label:?} for component {component_id:?}"
7070
);
7171
}
@@ -74,7 +74,7 @@ impl Factor for KeyValueFactor {
7474
}
7575

7676
Ok(AppState {
77-
store_manager: store_manager_manager,
77+
store_manager,
7878
component_allowed_stores,
7979
})
8080
}
@@ -150,8 +150,10 @@ impl FactorInstanceBuilder for InstanceBuilder {
150150
store_manager,
151151
allowed_stores,
152152
} = self;
153-
let mut dispatch = KeyValueDispatch::new_with_capacity(u32::MAX);
154-
dispatch.init(allowed_stores, store_manager);
155-
Ok(dispatch)
153+
Ok(KeyValueDispatch::new_with_capacity(
154+
allowed_stores,
155+
store_manager,
156+
u32::MAX,
157+
))
156158
}
157159
}

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,7 @@ use tokio::{
1313
};
1414
use tracing::Instrument;
1515

16-
const DEFAULT_CACHE_SIZE: usize = 256;
17-
18-
pub struct EmptyStoreManager;
19-
20-
#[async_trait]
21-
impl StoreManager for EmptyStoreManager {
22-
async fn get(&self, _name: &str) -> Result<Arc<dyn Store>, Error> {
23-
Err(Error::NoSuchStore)
24-
}
25-
26-
fn is_defined(&self, _store_name: &str) -> bool {
27-
false
28-
}
29-
}
30-
16+
/// A [`StoreManager`] which delegates to other `StoreManager`s based on the store label.
3117
pub struct DelegatingStoreManager {
3218
delegates: HashMap<String, Arc<dyn StoreManager>>,
3319
}
@@ -89,6 +75,8 @@ pub struct CachingStoreManager<T> {
8975
inner: T,
9076
}
9177

78+
const DEFAULT_CACHE_SIZE: usize = 256;
79+
9280
impl<T> CachingStoreManager<T> {
9381
pub fn new(inner: T) -> Self {
9482
Self::new_with_capacity(NonZeroUsize::new(DEFAULT_CACHE_SIZE).unwrap(), inner)

crates/key-value-spin/src/store.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ mod test {
159159

160160
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
161161
async fn all() -> Result<()> {
162-
let mut kv = KeyValueDispatch::new();
163-
kv.init(
162+
let mut kv = KeyValueDispatch::new(
164163
["default", "foo"]
165164
.into_iter()
166165
.map(ToOwned::to_owned)

0 commit comments

Comments
 (0)