use async_trait::async_trait;
use bytes::Bytes;
use datafusion_common::Result;
use futures::Stream;
use std::io::Write;
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;
#[async_trait]
pub trait SpillFile: Send + Sync {
fn path(&self) -> Option<&Path> {
None
}
fn size(&self) -> Option<u64>;
fn read_stream(&self) -> Result<Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>>;
fn open_writer(&self) -> Result<Box<dyn SpillWriter>>;
async fn open_async_writer(&self) -> Result<Box<dyn AsyncSpillWriter>> {
Ok(Box::new(BlockingSpillWriterAdapter {
inner: self.open_writer()?,
}))
}
}
pub trait SpillWriter: Write + Send {
fn finish(&mut self) -> Result<()>;
}
#[async_trait]
pub trait AsyncSpillWriter: Send {
async fn write_all(&mut self, data: Bytes) -> Result<()>;
async fn flush(&mut self) -> Result<()> {
Ok(())
}
async fn finish(&mut self) -> Result<()>;
async fn abort(&mut self) -> Result<()> {
Ok(())
}
}
struct BlockingSpillWriterAdapter {
inner: Box<dyn SpillWriter>,
}
#[async_trait]
impl AsyncSpillWriter for BlockingSpillWriterAdapter {
async fn write_all(&mut self, data: Bytes) -> Result<()> {
self.inner.write_all(&data)?;
Ok(())
}
async fn flush(&mut self) -> Result<()> {
self.inner.flush()?;
Ok(())
}
async fn finish(&mut self) -> Result<()> {
self.inner.flush()?;
self.inner.finish()
}
}
pub trait TempFileFactory: Send + Sync {
fn create_temp_file(&self, description: &str) -> Result<Arc<dyn SpillFile>>;
}