0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2025-05-23 22:26:07 +00:00

Pass config overide as impl Clone instead of Arc<T>

to make things more composable
This commit is contained in:
ad hoc
2024-08-20 14:19:13 +02:00
parent 592fc3829e
commit 97e6f970f2
4 changed files with 37 additions and 36 deletions
libsql-wal/src/storage

@ -192,7 +192,7 @@ where
&self,
namespace: &NamespaceName,
segment: Self::Segment,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
on_store_callback: OnStoreCallback,
) {
fn into_any<T: Sync + Send + 'static>(t: Arc<T>) -> Arc<dyn Any + Sync + Send> {
@ -215,7 +215,7 @@ where
async fn durable_frame_no(
&self,
namespace: &NamespaceName,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> u64 {
let config = config_override.unwrap_or_else(|| self.backend.default_config());
let meta = self.backend.meta(&config, namespace).await.unwrap();
@ -227,7 +227,7 @@ where
file: impl crate::io::FileExt,
namespace: &NamespaceName,
restore_options: RestoreOptions,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> super::Result<()> {
let config = config_override.unwrap_or_else(|| self.backend.default_config());
self.backend
@ -238,7 +238,7 @@ where
fn durable_frame_no_sync(
&self,
namespace: &NamespaceName,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> u64 {
tokio::runtime::Handle::current()
.block_on(self.durable_frame_no(namespace, config_override))
@ -248,7 +248,7 @@ where
&self,
namespace: &NamespaceName,
frame_no: u64,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> super::Result<super::SegmentKey> {
let config = config_override.unwrap_or_else(|| self.backend.default_config());
let key = self
@ -262,7 +262,7 @@ where
&self,
namespace: &NamespaceName,
key: &super::SegmentKey,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> super::Result<fst::Map<Arc<[u8]>>> {
let config = config_override.unwrap_or_else(|| self.backend.default_config());
let index = self
@ -276,7 +276,7 @@ where
&self,
namespace: &NamespaceName,
key: &super::SegmentKey,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> super::Result<CompactedSegment<impl FileExt>> {
// TODO: make async
let config = config_override.unwrap_or_else(|| self.backend.default_config());

@ -31,7 +31,7 @@ pub struct DbMeta {
pub trait Backend: Send + Sync + 'static {
/// Config type associated with the Storage
type Config: Send + Sync + 'static;
type Config: Clone + Send + Sync + 'static;
/// Store `segment_data` with its associated `meta`
fn store(
@ -69,7 +69,7 @@ pub trait Backend: Send + Sync + 'static {
// impl FileExt variant with all the arguments, with no escape hatch...
async fn fetch_segment_data(
self: Arc<Self>,
config: Arc<Self::Config>,
config: Self::Config,
namespace: NamespaceName,
key: SegmentKey,
) -> Result<impl FileExt>;
@ -99,7 +99,7 @@ pub trait Backend: Send + Sync + 'static {
) -> Result<()>;
/// Returns the default configuration for this storage
fn default_config(&self) -> Arc<Self::Config>;
fn default_config(&self) -> Self::Config;
}
impl<T: Backend> Backend for Arc<T> {
@ -132,7 +132,7 @@ impl<T: Backend> Backend for Arc<T> {
self.as_ref().meta(config, namespace).await
}
fn default_config(&self) -> Arc<Self::Config> {
fn default_config(&self) -> Self::Config {
self.as_ref().default_config()
}
@ -184,7 +184,7 @@ impl<T: Backend> Backend for Arc<T> {
async fn fetch_segment_data(
self: Arc<Self>,
config: Arc<Self::Config>,
config: Self::Config,
namespace: NamespaceName,
key: SegmentKey,
) -> Result<impl FileExt> {

@ -334,7 +334,7 @@ impl<IO> Backend for S3Backend<IO>
where
IO: Io,
{
type Config = S3Config;
type Config = Arc<S3Config>;
async fn store(
&self,
@ -425,7 +425,7 @@ where
})
}
fn default_config(&self) -> Arc<Self::Config> {
fn default_config(&self) -> Self::Config {
self.default_config.clone()
}
@ -489,7 +489,7 @@ where
async fn fetch_segment_data(
self: Arc<Self>,
config: Arc<Self::Config>,
config: Self::Config,
namespace: NamespaceName,
key: SegmentKey,
) -> Result<impl FileExt> {
@ -650,11 +650,11 @@ mod tests {
let dir = tempfile::tempdir().unwrap();
let (aws_config, _s3) = setup(&dir);
let s3_config = S3Config {
let s3_config = Arc::new(S3Config {
bucket: "testbucket".into(),
aws_config: aws_config.clone(),
cluster_id: "123456789".into(),
};
});
let storage = S3Backend::from_sdk_config_with_io(
aws_config,

@ -133,7 +133,7 @@ pub type OnStoreCallback = Box<
pub trait Storage: Send + Sync + 'static {
type Segment: Segment;
type Config;
type Config: Clone + Send;
/// store the passed segment for `namespace`. This function is called in a context where
/// blocking is acceptable.
/// returns a future that resolves when the segment is stored
@ -142,20 +142,20 @@ pub trait Storage: Send + Sync + 'static {
&self,
namespace: &NamespaceName,
seg: Self::Segment,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
on_store: OnStoreCallback,
);
fn durable_frame_no_sync(
&self,
namespace: &NamespaceName,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> u64;
async fn durable_frame_no(
&self,
namespace: &NamespaceName,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> u64;
async fn restore(
@ -163,22 +163,23 @@ pub trait Storage: Send + Sync + 'static {
file: impl FileExt,
namespace: &NamespaceName,
restore_options: RestoreOptions,
config_override: Option<Arc<Self::Config>>,
config_override: Option<Self::Config>,
) -> Result<()>;
async fn find_segment(
&self,
namespace: &NamespaceName,
frame_no: u64,
config_override: Option<Arc<Self::Config>>,
) -> Result<SegmentKey>;
config_override: Option<Self::Config>,
async fn fetch_segment_index(
&self,
namespace: &NamespaceName,
key: &SegmentKey,
config_override: Option<Arc<Self::Config>>,
) -> Result<Map<Arc<[u8]>>>;
config_override: Option<Self::Config>,
config_override: Option<Self::Config>,
async fn fetch_segment_data(
fn shutdown(&self) -> impl Future<Output = ()> + Send {
@ -187,8 +188,8 @@ pub trait Storage: Send + Sync + 'static {
&self,
namespace: &NamespaceName,
key: &SegmentKey,
config_override: Option<Arc<Self::Config>>,
) -> Result<CompactedSegment<impl FileExt>>;
config_override: Option<Self::Config>,
}
/// a placeholder storage that doesn't store segment
@ -211,7 +212,7 @@ impl Storage for NoStorage {
async fn durable_frame_no(
&self,
namespace: &NamespaceName,
config: Option<Arc<Self::Config>>,
config: Option<Self::Config>,
) -> u64 {
self.durable_frame_no_sync(namespace, config)
}
@ -221,7 +222,7 @@ impl Storage for NoStorage {
_file: impl FileExt,
_namespace: &NamespaceName,
_restore_options: RestoreOptions,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> Result<()> {
panic!("can restore from no storage")
}
@ -229,7 +230,7 @@ impl Storage for NoStorage {
fn durable_frame_no_sync(
&self,
_namespace: &NamespaceName,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> u64 {
u64::MAX
}
@ -256,7 +257,7 @@ impl Storage for NoStorage {
&self,
_namespace: &NamespaceName,
_key: &SegmentKey,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> Result<CompactedSegment<impl FileExt>> {
unimplemented!();
#[allow(unreachable_code)]
@ -321,7 +322,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
&self,
namespace: &NamespaceName,
seg: Self::Segment,
_config: Option<Arc<Self::Config>>,
_config: Option<Self::Config>,
on_store: OnStoreCallback,
) {
let mut inner = self.inner.lock();
@ -350,7 +351,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
async fn durable_frame_no(
&self,
namespace: &NamespaceName,
config: Option<Arc<Self::Config>>,
config: Option<Self::Config>,
) -> u64 {
self.durable_frame_no_sync(namespace, config)
}
@ -360,7 +361,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
_file: impl FileExt,
_namespace: &NamespaceName,
_restore_options: RestoreOptions,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> Result<()> {
todo!();
}
@ -368,7 +369,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
fn durable_frame_no_sync(
&self,
namespace: &NamespaceName,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> u64 {
let inner = self.inner.lock();
if inner.store {
@ -385,7 +386,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
&self,
namespace: &NamespaceName,
frame_no: u64,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> Result<SegmentKey> {
let inner = self.inner.lock();
if inner.store {
@ -406,7 +407,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
&self,
namespace: &NamespaceName,
key: &SegmentKey,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> Result<Map<Arc<[u8]>>> {
let inner = self.inner.lock();
if inner.store {
@ -423,7 +424,7 @@ impl<IO: Io> Storage for TestStorage<IO> {
&self,
namespace: &NamespaceName,
key: &SegmentKey,
_config_override: Option<Arc<Self::Config>>,
_config_override: Option<Self::Config>,
) -> Result<CompactedSegment<impl FileExt>> {
let inner = self.inner.lock();
if inner.store {