@@ -3,7 +3,7 @@ use std::{
33 path:: { Path , PathBuf } ,
44} ;
55
6- use anyhow:: Context ;
6+ use anyhow:: { bail , Context } ;
77use serde:: { Deserialize , Serialize } ;
88use spin_factor_key_value:: runtime_config:: spin:: MakeKeyValueStore ;
99use spin_key_value_sqlite:: { DatabaseLocation , KeyValueSqlite } ;
@@ -25,26 +25,6 @@ impl SpinKeyValueStore {
2525 }
2626}
2727
28- /// Runtime configuration for the SQLite key-value store.
29- #[ derive( Deserialize , Serialize ) ]
30- pub struct SpinKeyValueRuntimeConfig {
31- /// The path to the SQLite database file.
32- path : Option < PathBuf > ,
33- }
34-
35- impl SpinKeyValueRuntimeConfig {
36- /// The default filename for the SQLite database.
37- const DEFAULT_SPIN_STORE_FILENAME : & ' static str = "sqlite_key_value.db" ;
38- }
39-
40- impl Default for SpinKeyValueRuntimeConfig {
41- fn default ( ) -> Self {
42- Self {
43- path : Some ( PathBuf :: from ( Self :: DEFAULT_SPIN_STORE_FILENAME ) ) ,
44- }
45- }
46- }
47-
4828impl MakeKeyValueStore for SpinKeyValueStore {
4929 const RUNTIME_CONFIG_TYPE : & ' static str = "spin" ;
5030
@@ -56,24 +36,50 @@ impl MakeKeyValueStore for SpinKeyValueStore {
5636 & self ,
5737 runtime_config : Self :: RuntimeConfig ,
5838 ) -> anyhow:: Result < Self :: StoreManager > {
59- // The base path and the subpath must both be set otherwise, we default to in-memory.
60- let location =
61- if let ( Some ( base_path) , Some ( path) ) = ( & self . base_path , & runtime_config . path ) {
39+ let location = match ( & self . base_path , & runtime_config . path ) {
40+ // If both the base path and the path are specified, resolve the path against the base path
41+ ( Some ( base_path) , Some ( path) ) => {
6242 let path = resolve_relative_path ( path, base_path) ;
63- // Create the store's parent directory if necessary
64- let parent = path. parent ( ) . unwrap ( ) ;
65- if !parent. exists ( ) {
66- fs:: create_dir_all ( parent)
67- . context ( "Failed to create key value store's parent directory" ) ?;
68- }
6943 DatabaseLocation :: Path ( path)
70- } else {
71- DatabaseLocation :: InMemory
72- } ;
44+ }
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+ }
54+ // Otherwise, use an in-memory database
55+ ( None | Some ( _) , None ) => DatabaseLocation :: InMemory ,
56+ } ;
57+ if let DatabaseLocation :: Path ( path) = & location {
58+ // Create the store's parent directory if necessary
59+ 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" ) ?;
62+ }
63+ }
7364 Ok ( KeyValueSqlite :: new ( location) )
7465 }
7566}
7667
68+ /// The serialized runtime configuration for the SQLite key-value store.
69+ #[ derive( Deserialize , Serialize ) ]
70+ pub struct SpinKeyValueRuntimeConfig {
71+ /// The path to the SQLite database file.
72+ path : Option < PathBuf > ,
73+ }
74+
75+ impl SpinKeyValueRuntimeConfig {
76+ /// Create a new SpinKeyValueRuntimeConfig with the given parent directory
77+ /// where the key-value store will live.
78+ pub fn new ( path : Option < PathBuf > ) -> Self {
79+ Self { path }
80+ }
81+ }
82+
7783/// Resolve a relative path against a base dir.
7884///
7985/// If the path is absolute, it is returned as is. Otherwise, it is resolved against the base dir.
0 commit comments