Struct 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::{iter::AsyncLendingIterator, pipeline::Pipeline, Client, Execute, ExecuteMut, Statement};

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 = Statement::named("SELECT * FROM public.users", &[]).execute(client).await?;

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

    // bind value param to statement and query with the pipeline.
    // pipeline can encode multiple queries locally before send it to database.
    statement.bind([] as [i32; 0]).query_mut(&mut pipe)?;
    statement.bind([] as [i32; 0]).query_mut(&mut pipe)?;
    statement.bind([] as [i32; 0]).query_mut(&mut pipe)?;

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

    // 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.

Trait Implementations§

Source§

impl<'p, C, B, const SYNC_MODE: bool> Execute<'_, C> for Pipeline<'p, B, SYNC_MODE>
where C: Query, B: DerefMut<Target = BytesMut>,

Source§

type ExecuteOutput = Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'p>>

outcome of execute. used for single time database response: number of rows affected by execution for example.
Source§

type QueryOutput = Result<PipelineStream<'p>, Error>

outcome of query. used for repeated database response: database rows for example Read more
Source§

fn execute(self, cli: &C) -> Self::ExecuteOutput

define how a statement is executed with single time response
Source§

fn query(self, cli: &C) -> Self::QueryOutput

define how a statement is queried with repeated response
Source§

impl<'p, C, B, const SYNC_MODE: bool> ExecuteBlocking<'_, C> for Pipeline<'p, B, SYNC_MODE>
where C: Query, B: DerefMut<Target = BytesMut>,

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<'a, B, E, const SYNC_MODE: bool> ExecuteMut<'_, Pipeline<'a, B, SYNC_MODE>> for E
where B: DerefMut<Target = BytesMut>, E: Encode<Output = &'a [Column]>,

Source§

type ExecuteMutOutput = <E as ExecuteMut<'_, Pipeline<'a, B, SYNC_MODE>>>::QueryMutOutput

Source§

type QueryMutOutput = Result<(), Error>

Source§

fn execute_mut( self, pipe: &mut Pipeline<'a, B, SYNC_MODE>, ) -> <E as ExecuteMut<'_, Pipeline<'a, B, SYNC_MODE>>>::ExecuteMutOutput

Source§

fn query_mut( self, pipe: &mut Pipeline<'a, B, SYNC_MODE>, ) -> <E as ExecuteMut<'_, Pipeline<'a, B, SYNC_MODE>>>::QueryMutOutput

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