Skip to main content

QueryResultIterator

Struct QueryResultIterator 

Source
pub struct QueryResultIterator {
    pub metadata: QueryMetadata,
    pub total_rows_hint: Option<u64>,
    /* private fields */
}
Expand description

Streaming query result iterator for memory-efficient processing

Instead of materializing all rows into a Vec, this iterator yields rows lazily via a channel, allowing processing of arbitrarily large result sets within the 128MB memory budget.

§Memory Budget

To stay within the 128MB target, callers MUST create a bounded channel with capacity from StreamingConfig::buffer_size. Assuming average row size of 1KB:

  • buffer_size: 1024 = ~1MB in flight
  • chunk_size: 10_000 = ~10MB per chunk
  • Total peak usage: ~11MB (well within 128MB budget)

For rows with large blobs/text, reduce buffer sizes proportionally.

§Contract

  1. The caller MUST create a bounded channel with mpsc::channel(config.buffer_size)
  2. The iterator does NOT own the sender; the caller must spawn a task to send rows
  3. The iterator is consumed once; create a new one for subsequent queries

§Example

let config = StreamingConfig::default();
let (tx, rx) = tokio::sync::mpsc::channel(config.buffer_size);

// Spawn producer
tokio::spawn(async move {
    for row in rows {
        if tx.send(Ok(row)).await.is_err() {
            break; // Consumer dropped
        }
    }
});

// Create iterator from receiver
let mut iterator = QueryResultIterator::new(rx, metadata);

while let Some(row_result) = iterator.next_async().await {
    let row = row_result?;
    writer.write_row(&row)?;
}

Fields§

§metadata: QueryMetadata

Query metadata (columns, etc.)

§total_rows_hint: Option<u64>

Total rows hint (if known from query planning)

Implementations§

Source§

impl QueryResultIterator

Source

pub fn new( receiver: Receiver<Result<QueryRow, Error>>, metadata: QueryMetadata, ) -> Self

Create a new streaming result iterator

Source

pub fn with_total_hint(self, total: u64) -> Self

Create with a known total row count hint

Source

pub async fn next_async(&mut self) -> Option<Result<QueryRow, Error>>

Receive next row (async)

Returns None when all rows have been received.

Source

pub async fn collect_chunk( &mut self, size: usize, ) -> Result<Vec<QueryRow>, Error>

Collect into chunks of specified size

Returns a chunk of rows up to size. May return fewer rows if the stream ends or an error occurs.

§Arguments
  • size - Maximum number of rows to collect. Limited to MAX_CHUNK_SIZE (100,000) to prevent unbounded memory allocation.
§Returns

A vector of rows, which may be smaller than size if the stream ends or an error occurs.

Source

pub fn rows_received(&self) -> u64

Get count of rows received so far

Source

pub fn progress_percent(&self) -> Option<f64>

Get progress as a percentage (if total is known)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.