Struct xitca_postgres::pipeline::Pipeline

source ·
pub struct Pipeline<'a, B = Owned, const SYNC_MODE: bool = true> { /* private fields */ }
Expand description

A pipelined sql query type. It lazily batch queries into local buffer and try to send it with the least amount of syscall when pipeline starts.

§Examples

use xitca_postgres::{AsyncLendingIterator, Client, pipeline::Pipeline};

async fn pipeline(client: &Client) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    // prepare a statement that will be called repeatedly.
    // it can be a collection of statements that will be called in iteration.
    let statement = client.prepare("SELECT * FROM public.users", &[]).await?;

    // create a new pipeline.
    let mut pipe = Pipeline::new();

    // pipeline can encode multiple queries.
    pipe.query(statement.as_ref(), &[])?;
    pipe.query_raw::<[i32; 0]>(statement.as_ref(), [])?;

    // execute the pipeline and on success a streaming response will be returned.
    let mut res = client.pipeline(pipe)?;

    // iterate through the query responses. the response order is the same as the order of
    // queries encoded into pipeline with Pipeline::query_xxx api.
    while let Some(mut item) = res.try_next().await? {
        // every query can contain streaming rows.
        while let Some(row) = item.try_next().await? {
            let _: u32 = row.get("id");
        }
    }

    Ok(())
}

Implementations§

source§

impl Pipeline<'_, Owned, true>

source

pub fn new() -> Self

start a new pipeline.

pipeline is sync by default. which means every query inside is considered separate binding and the pipeline is transparent to database server. the pipeline only happen on socket transport where minimal amount of syscall is needed.

for more relaxed Pipeline Mode see Pipeline::unsync api.

source

pub fn with_capacity(cap: usize) -> Self

start a new pipeline with given capacity. capacity represent how many queries will be contained by a single pipeline. a determined cap can possibly reduce memory reallocation when constructing the pipeline.

source§

impl Pipeline<'_, Owned, false>

source

pub fn unsync() -> Self

start a new un-sync pipeline.

in un-sync mode pipeline treat all queries inside as one single binding and database server can see them as no sync point in between which can result in potential performance gain.

it behaves the same on transportation level as Pipeline::new where minimal amount of socket syscall is needed.

source

pub fn unsync_with_capacity(cap: usize) -> Self

start a new un-sync pipeline with given capacity. capacity represent how many queries will be contained by a single pipeline. a determined cap can possibly reduce memory reallocation when constructing the pipeline.

source§

impl<'a> Pipeline<'_, Borrowed<'a>, true>

source

pub fn from_buf(buf: &'a mut BytesMut) -> Self

start a new borrowed pipeline. pipeline will use borrowed bytes buffer to store encode messages before sending it to database.

pipeline is sync by default. which means every query inside is considered separate binding and the pipeline is transparent to database server. the pipeline only happen on socket transport where minimal amount of syscall is needed.

for more relaxed Pipeline Mode see Pipeline::unsync_from_buf api.

source

pub fn with_capacity_from_buf(cap: usize, buf: &'a mut BytesMut) -> Self

start a new borrowed pipeline with given capacity. capacity represent how many queries will be contained by a single pipeline. a determined cap can possibly reduce memory reallocation when constructing the pipeline.

source§

impl<'a> Pipeline<'_, Borrowed<'a>, false>

source

pub fn unsync_from_buf(buf: &'a mut BytesMut) -> Self

start a new borrowed un-sync pipeline.

in un-sync mode pipeline treat all queries inside as one single binding and database server can see them as no sync point in between which can result in potential performance gain.

it behaves the same on transportation level as Pipeline::from_buf where minimal amount of socket syscall is needed.

source

pub fn unsync_with_capacity_from_buf(cap: usize, buf: &'a mut BytesMut) -> Self

start a new borrowed un-sync pipeline with given capacity. capacity represent how many queries will be contained by a single pipeline. a determined cap can possibly reduce memory reallocation when constructing the pipeline.

source§

impl<'a, B, const SYNC_MODE: bool> Pipeline<'a, B, SYNC_MODE>
where B: DerefMut<Target = BytesMut>,

source

pub fn query( &mut self, stmt: &'a Statement, params: &[&(dyn ToSql + Sync)], ) -> Result<(), Error>

pipelined version of Client::query and Client::execute

source

pub fn query_raw<I>( &mut self, stmt: &'a Statement, params: I, ) -> Result<(), Error>
where I: AsParams,

pipelined version of Client::query_raw and Client::execute_raw

Auto Trait Implementations§

§

impl<'a, B, const SYNC_MODE: bool> Freeze for Pipeline<'a, B, SYNC_MODE>
where B: Freeze,

§

impl<'a, B, const SYNC_MODE: bool> RefUnwindSafe for Pipeline<'a, B, SYNC_MODE>
where B: RefUnwindSafe,

§

impl<'a, B, const SYNC_MODE: bool> Send for Pipeline<'a, B, SYNC_MODE>
where B: Send,

§

impl<'a, B, const SYNC_MODE: bool> Sync for Pipeline<'a, B, SYNC_MODE>
where B: Sync,

§

impl<'a, B, const SYNC_MODE: bool> Unpin for Pipeline<'a, B, SYNC_MODE>
where B: Unpin,

§

impl<'a, B, const SYNC_MODE: bool> UnwindSafe for Pipeline<'a, B, SYNC_MODE>
where B: UnwindSafe,

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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

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