use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use opendal::Operator;
use opendal::blocking;
use opendal::layers::RetryLayer;
use crate::config::DestinationConfig;
use crate::error::Result;
pub(crate) trait CloudBackend {
const RUNTIME_LABEL: &'static str;
const SCHEME: &'static str;
fn build_operator(config: &DestinationConfig) -> Result<Operator>;
}
pub(crate) struct CloudDestination<B: CloudBackend> {
_runtime: Arc<tokio::runtime::Runtime>,
op: blocking::Operator,
prefix: String,
_backend: PhantomData<fn() -> B>,
}
impl<B: CloudBackend> CloudDestination<B> {
pub fn new(config: &DestinationConfig) -> Result<Self> {
let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| {
anyhow::anyhow!(
"failed to create tokio runtime for {}: {}",
B::RUNTIME_LABEL,
e
)
})?,
);
let _guard = runtime.enter();
let async_op = B::build_operator(config)?.layer(
RetryLayer::new()
.with_max_times(5)
.with_min_delay(Duration::from_millis(200))
.with_max_delay(Duration::from_secs(10))
.with_jitter(),
);
let op = blocking::Operator::new(async_op)?;
let prefix = config.prefix.clone().unwrap_or_default();
Ok(Self {
_runtime: runtime,
op,
prefix,
_backend: PhantomData,
})
}
}
impl<B: CloudBackend> super::Destination for CloudDestination<B> {
fn write(&self, local_path: &Path, remote_key: &str) -> Result<()> {
let key = format!("{}{}", self.prefix, remote_key);
let mut src = std::fs::File::open(local_path)?;
let mut dst = self.op.writer(&key)?.into_std_write();
std::io::copy(&mut src, &mut dst)?;
dst.close()?;
log::info!("uploaded {}://{}", B::SCHEME, key);
Ok(())
}
fn capabilities(&self) -> super::DestinationCapabilities {
super::DestinationCapabilities {
commit_protocol: super::WriteCommitProtocol::FinalizeOnClose,
idempotent_overwrite: true,
retry_safe: true,
partial_write_risk: false,
}
}
fn list_prefix(&self, prefix: &str) -> Result<Vec<super::ObjectMeta>> {
let full = format!("{}{}", self.prefix, prefix);
let listed = if full.is_empty() || full.ends_with('/') {
self.op.list_options(
&full,
opendal::options::ListOptions {
recursive: true,
..Default::default()
},
)?
} else {
self.op.list_options(
&format!("{}/", full),
opendal::options::ListOptions {
recursive: true,
..Default::default()
},
)?
};
let mut out = Vec::with_capacity(listed.len());
for entry in listed {
if entry.metadata().mode() != opendal::EntryMode::FILE {
continue;
}
let abs = entry.path().to_string();
let rel = abs
.strip_prefix(self.prefix.as_str())
.unwrap_or(abs.as_str())
.to_string();
out.push(super::ObjectMeta {
key: rel,
size_bytes: entry.metadata().content_length(),
});
}
Ok(out)
}
fn read(&self, key: &str) -> Result<Vec<u8>> {
let full = format!("{}{}", self.prefix, key);
let buf = self.op.read(&full)?;
Ok(buf.to_vec())
}
fn head(&self, key: &str) -> Result<Option<super::ObjectMeta>> {
let full = format!("{}{}", self.prefix, key);
match self.op.stat(&full) {
Ok(meta) => Ok(Some(super::ObjectMeta {
key: key.to_string(),
size_bytes: meta.content_length(),
})),
Err(e) if e.kind() == opendal::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
fn r#move(&self, from: &str, to: &str) -> Result<()> {
let from_full = format!("{}{}", self.prefix, from);
let to_full = format!("{}{}", self.prefix, to);
self.op.copy(&from_full, &to_full)?;
self.op.delete(&from_full)?;
Ok(())
}
}