@@ -14,19 +14,15 @@ use spin_world::v2::sqlite as v2;
1414
1515pub use runtime_config:: RuntimeConfig ;
1616
17+ #[ derive( Default ) ]
1718pub struct SqliteFactor {
18- default_label_resolver : Arc < dyn DefaultLabelResolver > ,
19+ _priv : ( ) ,
1920}
2021
2122impl SqliteFactor {
2223 /// Create a new `SqliteFactor`
23- ///
24- /// Takes a `default_label_resolver` for how to handle when a database label doesn't
25- /// have a corresponding runtime configuration.
26- pub fn new ( default_label_resolver : impl DefaultLabelResolver + ' static ) -> Self {
27- Self {
28- default_label_resolver : Arc :: new ( default_label_resolver) ,
29- }
24+ pub fn new ( ) -> Self {
25+ Self { _priv : ( ) }
3026 }
3127}
3228
@@ -50,8 +46,8 @@ impl Factor for SqliteFactor {
5046 ) -> anyhow:: Result < Self :: AppState > {
5147 let connection_creators = ctx
5248 . take_runtime_config ( )
53- . map ( |r| r . connection_creators )
54- . unwrap_or_default ( ) ;
49+ . unwrap_or_default ( )
50+ . connection_creators ;
5551
5652 let allowed_databases = ctx
5753 . app ( )
@@ -69,19 +65,12 @@ impl Factor for SqliteFactor {
6965 ) )
7066 } )
7167 . collect :: < anyhow:: Result < HashMap < _ , _ > > > ( ) ?;
72- let resolver = self . default_label_resolver . clone ( ) ;
73- let get_connection_creator: host:: ConnectionCreatorGetter = Arc :: new ( move |label| {
74- connection_creators
75- . get ( label)
76- . cloned ( )
77- . or_else ( || resolver. default ( label) )
78- } ) ;
7968
8069 ensure_allowed_databases_are_configured ( & allowed_databases, |label| {
81- get_connection_creator ( label ) . is_some ( )
70+ connection_creators . contains_key ( label )
8271 } ) ?;
8372
84- Ok ( AppState :: new ( allowed_databases, get_connection_creator ) )
73+ Ok ( AppState :: new ( allowed_databases, connection_creators ) )
8574 }
8675
8776 fn prepare < T : spin_factors:: RuntimeFactors > (
@@ -94,10 +83,9 @@ impl Factor for SqliteFactor {
9483 . get ( ctx. app_component ( ) . id ( ) )
9584 . cloned ( )
9685 . unwrap_or_default ( ) ;
97- let get_connection_creator = ctx. app_state ( ) . get_connection_creator . clone ( ) ;
9886 Ok ( InstanceState :: new (
9987 allowed_databases,
100- get_connection_creator ,
88+ ctx . app_state ( ) . connection_creators . clone ( ) ,
10189 ) )
10290 }
10391}
@@ -138,31 +126,23 @@ fn ensure_allowed_databases_are_configured(
138126/// Metadata key for a list of allowed databases for a component.
139127pub const ALLOWED_DATABASES_KEY : MetadataKey < Vec < String > > = MetadataKey :: new ( "databases" ) ;
140128
141- /// Resolves a label to a default connection creator.
142- pub trait DefaultLabelResolver : Send + Sync {
143- /// If there is no runtime configuration for a given database label, return a default connection creator.
144- ///
145- /// If `Option::None` is returned, the database is not allowed.
146- fn default ( & self , label : & str ) -> Option < Arc < dyn ConnectionCreator > > ;
147- }
148-
149129#[ derive( Clone ) ]
150130pub struct AppState {
151131 /// A map from component id to a set of allowed database labels.
152132 allowed_databases : HashMap < String , Arc < HashSet < String > > > ,
153- /// A function for mapping from database name to a connection creator.
154- get_connection_creator : host :: ConnectionCreatorGetter ,
133+ /// A mapping from database label to a connection creator.
134+ connection_creators : HashMap < String , Arc < dyn ConnectionCreator > > ,
155135}
156136
157137impl AppState {
158138 /// Create a new `AppState`
159139 pub fn new (
160140 allowed_databases : HashMap < String , Arc < HashSet < String > > > ,
161- get_connection_creator : host :: ConnectionCreatorGetter ,
141+ connection_creators : HashMap < String , Arc < dyn ConnectionCreator > > ,
162142 ) -> Self {
163143 Self {
164144 allowed_databases,
165- get_connection_creator ,
145+ connection_creators ,
166146 }
167147 }
168148
@@ -173,7 +153,9 @@ impl AppState {
173153 & self ,
174154 label : & str ,
175155 ) -> Option < Result < Box < dyn Connection > , v2:: Error > > {
176- let connection = ( self . get_connection_creator ) ( label) ?
156+ let connection = self
157+ . connection_creators
158+ . get ( label) ?
177159 . create_connection ( label)
178160 . await ;
179161 Some ( connection)
0 commit comments