odbc_api

Struct ConcurrentBlockCursor

Source
pub struct ConcurrentBlockCursor<C, B> { /* private fields */ }
Expand description

A wrapper around block cursors which fetches data in a dedicated system thread. Intended to fetch data batch by batch while the application processes the batch last fetched. Works best with a double buffer strategy using two fetch buffers.

§Example

use odbc_api::{
    Environment, buffers::{ColumnarAnyBuffer, BufferDesc}, Cursor, ConcurrentBlockCursor
};
use std::sync::OnceLock;

// We want to use the ODBC environment from another system thread without scope => Therefore it
// needs to be static.
static ENV: OnceLock<Environment> = OnceLock::new();
let env = Environment::new()?;

let conn = ENV.get_or_init(|| env).connect_with_connection_string(
    "Driver={ODBC Driver 18 for SQL Server};Server=localhost;UID=SA;PWD=My@Test@Password1;",
    Default::default())?;

// We must use into_cursor to create a statement handle with static lifetime, which also owns
// the connection. This way we can send it to another thread safely.
let cursor = conn.into_cursor("SELECT * FROM very_big_table", ())?.unwrap();

// Batch size and buffer description. Here we assume there is only one integer column
let buffer_a = ColumnarAnyBuffer::from_descs(1000, [BufferDesc::I32 { nullable: false }]);
let mut buffer_b = ColumnarAnyBuffer::from_descs(1000, [BufferDesc::I32 { nullable: false }]);
// And now we have a sendable block cursor with static lifetime
let block_cursor = cursor.bind_buffer(buffer_a)?;

let mut cbc = ConcurrentBlockCursor::from_block_cursor(block_cursor);
while cbc.fetch_into(&mut buffer_b)? {
    // Proccess batch in buffer b asynchronously to fetching it
}

Implementations§

Source§

impl<C, B> ConcurrentBlockCursor<C, B>
where C: Cursor + Send + 'static, B: RowSetBuffer + Send + 'static,

Source

pub fn from_block_cursor(block_cursor: BlockCursor<C, B>) -> Self

Construct a new concurrent block cursor.

§Parameters
  • block_cursor: Taking a BlockCursor instead of a Cursor allows for better resource stealing if constructing starting from a sequential Cursor, as we do not need to undbind and bind the cursor.
Source

pub fn into_cursor(self) -> Result<C, Error>

Join fetch thread and yield the cursor back.

Source§

impl<C, B> ConcurrentBlockCursor<C, B>

Source

pub fn fetch(&mut self) -> Result<Option<B>, Error>

Receive the current batch and take ownership of its buffer. None if the cursor is already consumed, or had an error previously. This method blocks until a new batch available. In order for new batches available new buffers must be send to the thread in order for it to fill them. So calling fetch repeatedly without calling Self::fill in between may deadlock.

Source

pub fn fill(&mut self, buffer: B)

Send a buffer to the thread fetching in order for it to be filled and to be retrieved later using either fetch, or fetch_into.

Source

pub fn fetch_into(&mut self, buffer: &mut B) -> Result<bool, Error>

Fetches values from the ODBC datasource into buffer. Values are streamed batch by batch in order to avoid reallocation of the buffers used for tranistion. This call blocks until a new batch is ready. This method combines both Self::fetch and Self::fill.

§Parameters
  • buffer: A columnar any buffer which can bind to the cursor wrapped by this instance. After the method call the reference will not point to the same instance which had been passed into the function call, but to the one which was bound to the cursor in order to fetch the last batch. The buffer passed into this method, is then used to fetch the next batch. As such this method is ideal to implement concurrent fetching using two buffers. One which is written to, and one that is read, which flip their roles between batches. Also called double buffering.
§Return
  • true: Fetched a batch from the data source. The contents of that batch are now in buffer.
  • false: No batch could be fetched. The result set is consumed completly.

Auto Trait Implementations§

§

impl<C, B> Freeze for ConcurrentBlockCursor<C, B>
where C: Freeze,

§

impl<C, B> !RefUnwindSafe for ConcurrentBlockCursor<C, B>

§

impl<C, B> Send for ConcurrentBlockCursor<C, B>
where B: Send, C: Send,

§

impl<C, B> !Sync for ConcurrentBlockCursor<C, B>

§

impl<C, B> Unpin for ConcurrentBlockCursor<C, B>
where C: Unpin,

§

impl<C, B> !UnwindSafe for ConcurrentBlockCursor<C, B>

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