Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path handling in sqlite and kv #2756

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 47 additions & 35 deletions crates/factor-key-value-spin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,55 @@ use spin_key_value_sqlite::{DatabaseLocation, KeyValueSqlite};
/// A key-value store that uses SQLite as the backend.
pub struct SpinKeyValueStore {
/// The base path or directory for the SQLite database file.
base_path: PathBuf,
base_path: Option<PathBuf>,
}

impl SpinKeyValueStore {
/// Create a new SpinKeyValueStore with the given base path.
pub fn new(base_path: PathBuf) -> Self {
///
/// If `base_path` is `Some`, the database will be stored at the combined
/// `base_path` and the `path` specified in the runtime configuration. Otherwise,
/// only if the `path` in the runtime config is an absolute path will it be used as is.
/// In all other cases, an in-memory database will be used.
pub fn new(base_path: Option<PathBuf>) -> Self {
Self { base_path }
}
}

/// Runtime configuration for the SQLite key-value store.
impl MakeKeyValueStore for SpinKeyValueStore {
const RUNTIME_CONFIG_TYPE: &'static str = "spin";

type RuntimeConfig = SpinKeyValueRuntimeConfig;

type StoreManager = KeyValueSqlite;

fn make_store(
&self,
runtime_config: Self::RuntimeConfig,
) -> anyhow::Result<Self::StoreManager> {
let location = match (&self.base_path, &runtime_config.path) {
// If both the base path and the path are specified, resolve the path against the base path
(Some(base_path), Some(path)) => {
let path = resolve_relative_path(path, base_path);
DatabaseLocation::Path(path)
}
// If the base path is `None` but path is an absolute path, use the absolute path
(None, Some(path)) if path.is_absolute() => DatabaseLocation::Path(path.clone()),
// Otherwise, use an in-memory database
_ => DatabaseLocation::InMemory,
};
if let DatabaseLocation::Path(path) = &location {
// Create the store's parent directory if necessary
if let Some(parent) = path.parent().filter(|p| !p.exists()) {
fs::create_dir_all(parent)
.context("Failed to create key value store's parent directory")?;
}
}
Ok(KeyValueSqlite::new(location))
}
}

/// The serialized runtime configuration for the SQLite key-value store.
#[derive(Deserialize, Serialize)]
pub struct SpinKeyValueRuntimeConfig {
/// The path to the SQLite database file.
Expand All @@ -31,14 +69,13 @@ pub struct SpinKeyValueRuntimeConfig {
impl SpinKeyValueRuntimeConfig {
/// The default filename for the SQLite database.
const DEFAULT_SPIN_STORE_FILENAME: &'static str = "sqlite_key_value.db";
}

/// Create a new runtime configuration with the given directory.
///
/// If the database directory is None, the database is in-memory.
/// If the database directory is Some, the database is stored in a file in the given directory.
pub fn default(default_database_dir: Option<PathBuf>) -> Self {
let path = default_database_dir.map(|dir| dir.join(Self::DEFAULT_SPIN_STORE_FILENAME));
Self { path }
impl Default for SpinKeyValueRuntimeConfig {
fn default() -> Self {
Self {
path: Some(PathBuf::from(Self::DEFAULT_SPIN_STORE_FILENAME)),
}
}
}

Expand All @@ -51,28 +88,3 @@ fn resolve_relative_path(path: &Path, base_dir: &Path) -> PathBuf {
}
base_dir.join(path)
}

impl MakeKeyValueStore for SpinKeyValueStore {
const RUNTIME_CONFIG_TYPE: &'static str = "spin";

type RuntimeConfig = SpinKeyValueRuntimeConfig;

type StoreManager = KeyValueSqlite;

fn make_store(
&self,
runtime_config: Self::RuntimeConfig,
) -> anyhow::Result<Self::StoreManager> {
let location = match runtime_config.path {
Some(path) => {
let path = resolve_relative_path(&path, &self.base_path);
// Create the store's parent directory if necessary
fs::create_dir_all(path.parent().unwrap())
.context("Failed to create key value store")?;
DatabaseLocation::Path(path)
}
None => DatabaseLocation::InMemory,
};
Ok(KeyValueSqlite::new(location))
}
}
2 changes: 2 additions & 0 deletions crates/factor-key-value/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct RuntimeConfig {

impl RuntimeConfig {
/// Adds a store manager for the store with the given label to the runtime configuration.
///
/// If a store manager already exists for the given label, it will be replaced.
pub fn add_store_manager(&mut self, label: String, store_manager: Arc<dyn StoreManager>) {
self.store_managers.insert(label, store_manager);
}
Expand Down
16 changes: 14 additions & 2 deletions crates/factor-key-value/src/runtime_config/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,20 @@ impl RuntimeConfigResolver {
///
/// Users must ensure that the store type for `config` has been registered with
/// the resolver using [`Self::register_store_type`].
pub fn add_default_store(&mut self, label: &'static str, config: StoreConfig) {
self.defaults.insert(label, config);
pub fn add_default_store<T>(
&mut self,
label: &'static str,
config: T::RuntimeConfig,
) -> anyhow::Result<()>
where
T: MakeKeyValueStore,
T::RuntimeConfig: Serialize,
{
self.defaults.insert(
label,
StoreConfig::new(T::RUNTIME_CONFIG_TYPE.to_owned(), config)?,
);
Ok(())
}

/// Registers a store type to the resolver.
Expand Down
Loading
Loading