pub struct ChunkSender<'conn> { /* private fields */ }Expand description
A thread-safe sender for InsertChunks.
ChunkSender manages the COPY protocol connection and ensures that only one
chunk is sent at a time. Multiple threads can call send_chunk() concurrently;
the mutex ensures serialized access.
§Example
use hyperdb_api::{Catalog, Connection, CreateMode, ChunkSender, InsertChunk, TableDefinition, SqlType, Result};
use std::sync::mpsc;
use std::thread;
fn main() -> Result<()> {
let conn = Connection::connect("localhost:7483", "test.hyper", CreateMode::CreateIfNotExists)?;
let table_def = TableDefinition::new("products")
.add_required_column("id", SqlType::int())
.add_nullable_column("name", SqlType::text());
Catalog::new(&conn).create_table(&table_def)?;
let sender = ChunkSender::new(&conn, &table_def)?;
let (tx, rx) = mpsc::channel::<InsertChunk>();
// Worker thread
let table_def_clone = table_def.clone();
let handle = thread::spawn(move || {
let mut chunk = InsertChunk::from_table_definition(&table_def_clone);
for i in 0..1000i32 {
chunk.add_i32(i).unwrap();
chunk.add_str(&format!("Product {}", i)).unwrap();
chunk.end_row().unwrap();
}
tx.send(chunk).unwrap();
});
// Receive and send chunks
while let Ok(chunk) = rx.recv() {
sender.send_chunk(chunk)?;
}
handle.join().unwrap();
let rows = sender.finish()?;
println!("Inserted {} rows", rows);
Ok(())
}Implementations§
Source§impl<'conn> ChunkSender<'conn>
impl<'conn> ChunkSender<'conn>
Sourcepub fn new(
connection: &'conn Connection,
table_def: &TableDefinition,
) -> Result<Self>
pub fn new( connection: &'conn Connection, table_def: &TableDefinition, ) -> Result<Self>
Creates a new chunk sender for the given table.
§Errors
Returns Error::InvalidTableDefinition if table_def has zero
columns. The COPY session itself is opened lazily on the first
send_chunk, so transport errors surface there.
Sourcepub fn send_chunk(&self, chunk: InsertChunk) -> Result<()>
pub fn send_chunk(&self, chunk: InsertChunk) -> Result<()>
Sends a chunk to Hyper.
This method is thread-safe - multiple threads can call it concurrently, but only one chunk will be sent at a time.
Each InsertChunk includes a HyperBinary header (19 bytes). This method
automatically handles headers: the first chunk’s header is sent, and
headers in subsequent chunks are stripped (HyperBinary expects only one
header per COPY stream).
§Errors
Returns an error if the chunk is empty or if sending fails.
Sourcepub fn total_rows(&self) -> u64
pub fn total_rows(&self) -> u64
Returns the total number of rows sent so far.
Sourcepub fn chunks_sent(&self) -> usize
pub fn chunks_sent(&self) -> usize
Returns the number of chunks sent so far.
Sourcepub fn finish(self) -> Result<u64>
pub fn finish(self) -> Result<u64>
Finishes the COPY operation and returns the total row count.
This method consumes the sender. After calling this, the COPY operation is complete and all data has been committed.
§Errors
- Returns
Error::Otherwith message"ChunkSender mutex poisoned"if a sender thread panicked while holding the writer lock. - Returns
Error::ClientorError::Ioif sending the COPY trailer or finishing the COPY operation fails.
Trait Implementations§
Auto Trait Implementations§
impl<'conn> !Freeze for ChunkSender<'conn>
impl<'conn> !RefUnwindSafe for ChunkSender<'conn>
impl<'conn> !Send for ChunkSender<'conn>
impl<'conn> !Sync for ChunkSender<'conn>
impl<'conn> Unpin for ChunkSender<'conn>
impl<'conn> UnsafeUnpin for ChunkSender<'conn>
impl<'conn> !UnwindSafe for ChunkSender<'conn>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request