Skip to content

Commit 8129386

Browse files
committed
Align store path behavior with previous behavior
Signed-off-by: Ryan Levick <[email protected]>
1 parent c6230bf commit 8129386

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
path::{Path, PathBuf},
44
};
55

6-
use anyhow::{bail, Context};
6+
use anyhow::Context as _;
77
use serde::{Deserialize, Serialize};
88
use spin_factor_key_value::runtime_config::spin::MakeKeyValueStore;
99
use spin_key_value_sqlite::{DatabaseLocation, KeyValueSqlite};
@@ -42,23 +42,20 @@ impl MakeKeyValueStore for SpinKeyValueStore {
4242
let path = resolve_relative_path(path, base_path);
4343
DatabaseLocation::Path(path)
4444
}
45-
// If the base path is `None` but path is an absolute path, use the absolute path
46-
(None, Some(path)) if path.is_absolute() => DatabaseLocation::Path(path.clone()),
47-
// If the base path is `None` but path is a relative path, error out
48-
(None, Some(path)) => {
49-
bail!(
50-
"key-value store path '{}' is relative, but no base path is set",
51-
path.display()
52-
)
53-
}
45+
// If the base path is `None` but use the path without resolving relative to the base path.
46+
(None, Some(path)) => DatabaseLocation::Path(path.clone()),
5447
// Otherwise, use an in-memory database
5548
(None | Some(_), None) => DatabaseLocation::InMemory,
5649
};
5750
if let DatabaseLocation::Path(path) = &location {
5851
// Create the store's parent directory if necessary
5952
if let Some(parent) = path.parent().filter(|p| !p.exists()) {
60-
fs::create_dir_all(parent)
61-
.context("Failed to create key value store's parent directory")?;
53+
fs::create_dir_all(parent).with_context(|| {
54+
format!(
55+
"failed to create key value store's parent directory: '{}",
56+
parent.display()
57+
)
58+
})?;
6259
}
6360
}
6461
Ok(KeyValueSqlite::new(location))

crates/key-value-sqlite/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rusqlite = { version = "0.29.0", features = ["bundled"] }
1111
spin-core = { path = "../core" }
1212
spin-key-value = { path = "../key-value" }
1313
spin-world = { path = "../world" }
14-
tokio = "1"
14+
tokio = { version = "1", features = ["rt-multi-thread"] }
1515
tracing = { workspace = true }
1616

1717
[lints]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ pub struct KeyValueSqlite {
2222
}
2323

2424
impl KeyValueSqlite {
25+
/// Create a new `KeyValueSqlite` store manager.
26+
///
27+
/// If location is `DatabaseLocation::InMemory`, the database will be created in memory.
28+
/// If it's `DatabaseLocation::Path`, the database will be created at the specified path.
29+
/// Relative paths will be resolved against the current working directory.
2530
pub fn new(location: DatabaseLocation) -> Self {
2631
Self {
2732
location,

crates/key-value/src/util.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ impl StoreManager for DelegatingStoreManager {
6868
}
6969

7070
fn summary(&self, store_name: &str) -> Option<String> {
71+
if let Some(store) = self.delegates.get(store_name) {
72+
return store.summary(store_name);
73+
}
7174
(self.default_manager)(store_name)?.summary(store_name)
7275
}
7376
}

crates/runtime-config/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ where
120120
provided_log_dir: UserProvidedPath,
121121
use_gpu: bool,
122122
) -> anyhow::Result<Self> {
123+
let runtime_config_dir = runtime_config_path
124+
.and_then(Path::parent)
125+
.map(ToOwned::to_owned);
123126
let toml_resolver =
124127
TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir);
125-
let tls_resolver = runtime_config_path.map(SpinTlsRuntimeConfig::new);
126-
let key_value_config_resolver = key_value_config_resolver(toml_resolver.state_dir()?);
128+
let tls_resolver = runtime_config_dir.clone().map(SpinTlsRuntimeConfig::new);
129+
let key_value_config_resolver =
130+
key_value_config_resolver(runtime_config_dir, toml_resolver.state_dir()?);
127131
let sqlite_config_resolver = sqlite_config_resolver(toml_resolver.state_dir()?)
128132
.context("failed to resolve sqlite runtime config")?;
129133

@@ -396,9 +400,11 @@ const DEFAULT_KEY_VALUE_STORE_LABEL: &str = "default";
396400
/// The key-value runtime configuration resolver.
397401
///
398402
/// Takes a base path that all local key-value stores which are configured with
399-
/// relative paths will be relative to.
403+
/// relative paths will be relative to. It also takes a default store base path
404+
/// which will be used as the directory for the default store.
400405
pub fn key_value_config_resolver(
401406
local_store_base_path: Option<PathBuf>,
407+
default_store_base_path: Option<PathBuf>,
402408
) -> key_value::RuntimeConfigResolver {
403409
let mut key_value = key_value::RuntimeConfigResolver::new();
404410

@@ -417,11 +423,12 @@ pub fn key_value_config_resolver(
417423
.unwrap();
418424

419425
// Add handling of "default" store.
426+
let default_store_path = default_store_base_path.map(|p| p.join(DEFAULT_SPIN_STORE_FILENAME));
420427
// Unwraps are safe because the store is known to be serializable as toml.
421428
key_value
422429
.add_default_store::<SpinKeyValueStore>(
423430
DEFAULT_KEY_VALUE_STORE_LABEL,
424-
SpinKeyValueRuntimeConfig::new(Some(PathBuf::from(DEFAULT_SPIN_STORE_FILENAME))),
431+
SpinKeyValueRuntimeConfig::new(default_store_path),
425432
)
426433
.unwrap();
427434

0 commit comments

Comments
 (0)