Skip to main content

ChunkSender

Struct ChunkSender 

Source
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>

Source

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.

Source

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.

Source

pub fn total_rows(&self) -> u64

Returns the total number of rows sent so far.

Source

pub fn chunks_sent(&self) -> usize

Returns the number of chunks sent so far.

Source

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::Other with message "ChunkSender mutex poisoned" if a sender thread panicked while holding the writer lock.
  • Returns Error::Client or Error::Io if sending the COPY trailer or finishing the COPY operation fails.

Trait Implementations§

Source§

impl<'conn> Debug for ChunkSender<'conn>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more