use std::io;
use std::pin::Pin;
use futures_util::sink::SinkExt;
use minarrow::{Field, Table, TableV};
use crate::compression::Compression;
use crate::enums::IPCMessageProtocol;
use crate::models::sinks::table_sink::TableSink64;
use crate::traits::transport_writer::IPCTransportWriter;
pub struct QuicTableWriter {
sink: TableSink64<quinn::SendStream>,
}
impl QuicTableWriter {
pub fn new(
send: quinn::SendStream,
schema: Vec<Field>,
compression: Option<Compression>,
) -> io::Result<Self> {
let sink = TableSink64::new(send, schema, IPCMessageProtocol::Stream, compression)?;
Ok(Self { sink })
}
pub async fn write_table_with_metadata(
&mut self,
table: impl Into<TableV> + Send,
metadata: Vec<(String, String)>,
) -> io::Result<()> {
self.sink.encode_frame(&table.into(), Some(metadata.as_slice()))?;
SinkExt::flush(&mut self.sink).await?;
Ok(())
}
}
impl IPCTransportWriter for QuicTableWriter {
fn schema(&self) -> &[Field] {
&self.sink.schema
}
fn register_dictionary(&mut self, dict_id: i64, values: Vec<String>) {
self.sink.codec.register_dictionary(dict_id, values);
}
async fn write_table(&mut self, table: impl Into<TableV> + Send) -> io::Result<()> {
SinkExt::send(&mut self.sink, table.into()).await?;
SinkExt::flush(&mut self.sink).await?;
Ok(())
}
async fn write_all_tables(&mut self, tables: Vec<Table>) -> io::Result<()> {
let mut sink = Pin::new(&mut self.sink);
for table in tables {
SinkExt::send(&mut sink, table.into()).await?;
}
SinkExt::close(&mut sink).await?;
Ok(())
}
async fn finish(&mut self) -> io::Result<()> {
SinkExt::close(&mut self.sink).await
}
}