Skip to content

Commit 97e6f97

Browse files
committed
Pass config overide as impl Clone instead of Arc<T>
to make things more composable
1 parent 592fc38 commit 97e6f97

4 files changed

Lines changed: 37 additions & 36 deletions

File tree

libsql-wal/src/storage/async_storage.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ where
192192
&self,
193193
namespace: &NamespaceName,
194194
segment: Self::Segment,
195-
config_override: Option<Arc<Self::Config>>,
195+
config_override: Option<Self::Config>,
196196
on_store_callback: OnStoreCallback,
197197
) {
198198
fn into_any<T: Sync + Send + 'static>(t: Arc<T>) -> Arc<dyn Any + Sync + Send> {
@@ -215,7 +215,7 @@ where
215215
async fn durable_frame_no(
216216
&self,
217217
namespace: &NamespaceName,
218-
config_override: Option<Arc<Self::Config>>,
218+
config_override: Option<Self::Config>,
219219
) -> u64 {
220220
let config = config_override.unwrap_or_else(|| self.backend.default_config());
221221
let meta = self.backend.meta(&config, namespace).await.unwrap();
@@ -227,7 +227,7 @@ where
227227
file: impl crate::io::FileExt,
228228
namespace: &NamespaceName,
229229
restore_options: RestoreOptions,
230-
config_override: Option<Arc<Self::Config>>,
230+
config_override: Option<Self::Config>,
231231
) -> super::Result<()> {
232232
let config = config_override.unwrap_or_else(|| self.backend.default_config());
233233
self.backend
@@ -238,7 +238,7 @@ where
238238
fn durable_frame_no_sync(
239239
&self,
240240
namespace: &NamespaceName,
241-
config_override: Option<Arc<Self::Config>>,
241+
config_override: Option<Self::Config>,
242242
) -> u64 {
243243
tokio::runtime::Handle::current()
244244
.block_on(self.durable_frame_no(namespace, config_override))
@@ -248,7 +248,7 @@ where
248248
&self,
249249
namespace: &NamespaceName,
250250
frame_no: u64,
251-
config_override: Option<Arc<Self::Config>>,
251+
config_override: Option<Self::Config>,
252252
) -> super::Result<super::SegmentKey> {
253253
let config = config_override.unwrap_or_else(|| self.backend.default_config());
254254
let key = self
@@ -262,7 +262,7 @@ where
262262
&self,
263263
namespace: &NamespaceName,
264264
key: &super::SegmentKey,
265-
config_override: Option<Arc<Self::Config>>,
265+
config_override: Option<Self::Config>,
266266
) -> super::Result<fst::Map<Arc<[u8]>>> {
267267
let config = config_override.unwrap_or_else(|| self.backend.default_config());
268268
let index = self
@@ -276,7 +276,7 @@ where
276276
&self,
277277
namespace: &NamespaceName,
278278
key: &super::SegmentKey,
279-
config_override: Option<Arc<Self::Config>>,
279+
config_override: Option<Self::Config>,
280280
) -> super::Result<CompactedSegment<impl FileExt>> {
281281
// TODO: make async
282282
let config = config_override.unwrap_or_else(|| self.backend.default_config());

libsql-wal/src/storage/backend/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct DbMeta {
3131

3232
pub trait Backend: Send + Sync + 'static {
3333
/// Config type associated with the Storage
34-
type Config: Send + Sync + 'static;
34+
type Config: Clone + Send + Sync + 'static;
3535

3636
/// Store `segment_data` with its associated `meta`
3737
fn store(
@@ -69,7 +69,7 @@ pub trait Backend: Send + Sync + 'static {
6969
// impl FileExt variant with all the arguments, with no escape hatch...
7070
async fn fetch_segment_data(
7171
self: Arc<Self>,
72-
config: Arc<Self::Config>,
72+
config: Self::Config,
7373
namespace: NamespaceName,
7474
key: SegmentKey,
7575
) -> Result<impl FileExt>;
@@ -99,7 +99,7 @@ pub trait Backend: Send + Sync + 'static {
9999
) -> Result<()>;
100100

101101
/// Returns the default configuration for this storage
102-
fn default_config(&self) -> Arc<Self::Config>;
102+
fn default_config(&self) -> Self::Config;
103103
}
104104

105105
impl<T: Backend> Backend for Arc<T> {
@@ -132,7 +132,7 @@ impl<T: Backend> Backend for Arc<T> {
132132
self.as_ref().meta(config, namespace).await
133133
}
134134

135-
fn default_config(&self) -> Arc<Self::Config> {
135+
fn default_config(&self) -> Self::Config {
136136
self.as_ref().default_config()
137137
}
138138

@@ -184,7 +184,7 @@ impl<T: Backend> Backend for Arc<T> {
184184

185185
async fn fetch_segment_data(
186186
self: Arc<Self>,
187-
config: Arc<Self::Config>,
187+
config: Self::Config,
188188
namespace: NamespaceName,
189189
key: SegmentKey,
190190
) -> Result<impl FileExt> {

libsql-wal/src/storage/backend/s3.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<IO> Backend for S3Backend<IO>
334334
where
335335
IO: Io,
336336
{
337-
type Config = S3Config;
337+
type Config = Arc<S3Config>;
338338

339339
async fn store(
340340
&self,
@@ -425,7 +425,7 @@ where
425425
})
426426
}
427427

428-
fn default_config(&self) -> Arc<Self::Config> {
428+
fn default_config(&self) -> Self::Config {
429429
self.default_config.clone()
430430
}
431431

@@ -489,7 +489,7 @@ where
489489

490490
async fn fetch_segment_data(
491491
self: Arc<Self>,
492-
config: Arc<Self::Config>,
492+
config: Self::Config,
493493
namespace: NamespaceName,
494494
key: SegmentKey,
495495
) -> Result<impl FileExt> {
@@ -650,11 +650,11 @@ mod tests {
650650
let dir = tempfile::tempdir().unwrap();
651651
let (aws_config, _s3) = setup(&dir);
652652

653-
let s3_config = S3Config {
653+
let s3_config = Arc::new(S3Config {
654654
bucket: "testbucket".into(),
655655
aws_config: aws_config.clone(),
656656
cluster_id: "123456789".into(),
657-
};
657+
});
658658

659659
let storage = S3Backend::from_sdk_config_with_io(
660660
aws_config,

libsql-wal/src/storage/mod.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub type OnStoreCallback = Box<
133133

134134
pub trait Storage: Send + Sync + 'static {
135135
type Segment: Segment;
136-
type Config;
136+
type Config: Clone + Send;
137137
/// store the passed segment for `namespace`. This function is called in a context where
138138
/// blocking is acceptable.
139139
/// returns a future that resolves when the segment is stored
@@ -142,43 +142,44 @@ pub trait Storage: Send + Sync + 'static {
142142
&self,
143143
namespace: &NamespaceName,
144144
seg: Self::Segment,
145-
config_override: Option<Arc<Self::Config>>,
145+
config_override: Option<Self::Config>,
146146
on_store: OnStoreCallback,
147147
);
148148

149149
fn durable_frame_no_sync(
150150
&self,
151151
namespace: &NamespaceName,
152-
config_override: Option<Arc<Self::Config>>,
152+
config_override: Option<Self::Config>,
153153
) -> u64;
154154

155155
async fn durable_frame_no(
156156
&self,
157157
namespace: &NamespaceName,
158-
config_override: Option<Arc<Self::Config>>,
158+
config_override: Option<Self::Config>,
159159
) -> u64;
160160

161161
async fn restore(
162162
&self,
163163
file: impl FileExt,
164164
namespace: &NamespaceName,
165165
restore_options: RestoreOptions,
166-
config_override: Option<Arc<Self::Config>>,
166+
config_override: Option<Self::Config>,
167167
) -> Result<()>;
168168

169169
async fn find_segment(
170170
&self,
171171
namespace: &NamespaceName,
172172
frame_no: u64,
173-
config_override: Option<Arc<Self::Config>>,
174173
) -> Result<SegmentKey>;
174+
config_override: Option<Self::Config>,
175175

176176
async fn fetch_segment_index(
177177
&self,
178178
namespace: &NamespaceName,
179179
key: &SegmentKey,
180-
config_override: Option<Arc<Self::Config>>,
181180
) -> Result<Map<Arc<[u8]>>>;
181+
config_override: Option<Self::Config>,
182+
config_override: Option<Self::Config>,
182183

183184
async fn fetch_segment_data(
184185
fn shutdown(&self) -> impl Future<Output = ()> + Send {
@@ -187,8 +188,8 @@ pub trait Storage: Send + Sync + 'static {
187188
&self,
188189
namespace: &NamespaceName,
189190
key: &SegmentKey,
190-
config_override: Option<Arc<Self::Config>>,
191191
) -> Result<CompactedSegment<impl FileExt>>;
192+
config_override: Option<Self::Config>,
192193
}
193194

194195
/// a placeholder storage that doesn't store segment
@@ -211,7 +212,7 @@ impl Storage for NoStorage {
211212
async fn durable_frame_no(
212213
&self,
213214
namespace: &NamespaceName,
214-
config: Option<Arc<Self::Config>>,
215+
config: Option<Self::Config>,
215216
) -> u64 {
216217
self.durable_frame_no_sync(namespace, config)
217218
}
@@ -221,15 +222,15 @@ impl Storage for NoStorage {
221222
_file: impl FileExt,
222223
_namespace: &NamespaceName,
223224
_restore_options: RestoreOptions,
224-
_config_override: Option<Arc<Self::Config>>,
225+
_config_override: Option<Self::Config>,
225226
) -> Result<()> {
226227
panic!("can restore from no storage")
227228
}
228229

229230
fn durable_frame_no_sync(
230231
&self,
231232
_namespace: &NamespaceName,
232-
_config_override: Option<Arc<Self::Config>>,
233+
_config_override: Option<Self::Config>,
233234
) -> u64 {
234235
u64::MAX
235236
}
@@ -256,7 +257,7 @@ impl Storage for NoStorage {
256257
&self,
257258
_namespace: &NamespaceName,
258259
_key: &SegmentKey,
259-
_config_override: Option<Arc<Self::Config>>,
260+
_config_override: Option<Self::Config>,
260261
) -> Result<CompactedSegment<impl FileExt>> {
261262
unimplemented!();
262263
#[allow(unreachable_code)]
@@ -321,7 +322,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
321322
&self,
322323
namespace: &NamespaceName,
323324
seg: Self::Segment,
324-
_config: Option<Arc<Self::Config>>,
325+
_config: Option<Self::Config>,
325326
on_store: OnStoreCallback,
326327
) {
327328
let mut inner = self.inner.lock();
@@ -350,7 +351,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
350351
async fn durable_frame_no(
351352
&self,
352353
namespace: &NamespaceName,
353-
config: Option<Arc<Self::Config>>,
354+
config: Option<Self::Config>,
354355
) -> u64 {
355356
self.durable_frame_no_sync(namespace, config)
356357
}
@@ -360,15 +361,15 @@ impl<IO: Io> Storage for TestStorage<IO> {
360361
_file: impl FileExt,
361362
_namespace: &NamespaceName,
362363
_restore_options: RestoreOptions,
363-
_config_override: Option<Arc<Self::Config>>,
364+
_config_override: Option<Self::Config>,
364365
) -> Result<()> {
365366
todo!();
366367
}
367368

368369
fn durable_frame_no_sync(
369370
&self,
370371
namespace: &NamespaceName,
371-
_config_override: Option<Arc<Self::Config>>,
372+
_config_override: Option<Self::Config>,
372373
) -> u64 {
373374
let inner = self.inner.lock();
374375
if inner.store {
@@ -385,7 +386,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
385386
&self,
386387
namespace: &NamespaceName,
387388
frame_no: u64,
388-
_config_override: Option<Arc<Self::Config>>,
389+
_config_override: Option<Self::Config>,
389390
) -> Result<SegmentKey> {
390391
let inner = self.inner.lock();
391392
if inner.store {
@@ -406,7 +407,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
406407
&self,
407408
namespace: &NamespaceName,
408409
key: &SegmentKey,
409-
_config_override: Option<Arc<Self::Config>>,
410+
_config_override: Option<Self::Config>,
410411
) -> Result<Map<Arc<[u8]>>> {
411412
let inner = self.inner.lock();
412413
if inner.store {
@@ -423,7 +424,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
423424
&self,
424425
namespace: &NamespaceName,
425426
key: &SegmentKey,
426-
_config_override: Option<Arc<Self::Config>>,
427+
_config_override: Option<Self::Config>,
427428
) -> Result<CompactedSegment<impl FileExt>> {
428429
let inner = self.inner.lock();
429430
if inner.store {

0 commit comments

Comments
 (0)