@@ -3,7 +3,7 @@ use std::{
3
3
path:: { Path , PathBuf } ,
4
4
} ;
5
5
6
- use anyhow:: Context ;
6
+ use anyhow:: { bail , Context } ;
7
7
use serde:: { Deserialize , Serialize } ;
8
8
use spin_factor_key_value:: runtime_config:: spin:: MakeKeyValueStore ;
9
9
use spin_key_value_sqlite:: { DatabaseLocation , KeyValueSqlite } ;
@@ -25,26 +25,6 @@ impl SpinKeyValueStore {
25
25
}
26
26
}
27
27
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
-
48
28
impl MakeKeyValueStore for SpinKeyValueStore {
49
29
const RUNTIME_CONFIG_TYPE : & ' static str = "spin" ;
50
30
@@ -56,24 +36,50 @@ impl MakeKeyValueStore for SpinKeyValueStore {
56
36
& self ,
57
37
runtime_config : Self :: RuntimeConfig ,
58
38
) -> 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) ) => {
62
42
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
- }
69
43
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
+ }
73
64
Ok ( KeyValueSqlite :: new ( location) )
74
65
}
75
66
}
76
67
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
+
77
83
/// Resolve a relative path against a base dir.
78
84
///
79
85
/// If the path is absolute, it is returned as is. Otherwise, it is resolved against the base dir.
0 commit comments