odbc_api/cursor/concurrent_block_cursor.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
use std::{
mem::swap,
sync::mpsc::{sync_channel, Receiver, SyncSender},
thread::{self, JoinHandle},
};
use crate::{BlockCursor, Cursor, Error};
use super::RowSetBuffer;
/// 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
///
/// ```no_run
/// 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
/// }
///
/// # Ok::<_, odbc_api::Error>(())
/// ```
pub struct ConcurrentBlockCursor<C, B> {
/// In order to avoid reallocating buffers over and over again, we use this channel to send the
/// buffers back to the fetch thread after we copied their contents into arrow arrays.
send_buffer: SyncSender<B>,
/// Receives filled batches from the fetch thread. Once the source is empty or if an error
/// occurs its associated sender is dropped, and receiving batches will return an error (which
/// we expect during normal operation and cleanup, and is not forwarded to the user).
receive_batch: Receiver<B>,
/// We join with the fetch thread if we stop receiving batches (i.e. receive_batch.recv()
/// returns an error) or `into_cursor` is called. `None` if the thread has already been joined.
/// In this case either an error has been reported to the user, or the cursor is stored in
/// `cursor`.
fetch_thread: Option<JoinHandle<Result<C, Error>>>,
/// Only `Some`, if the cursor has been consumed succesfully and `fetch_thread` has been joined.
/// Can only be `Some` if `fetch_thread` is `None`. If both `fetch_thread` and `cursor` are
/// `None`, it is implied that `fetch_thread` returned an error joining.
cursor: Option<C>,
}
impl<C, B> ConcurrentBlockCursor<C, B>
where
C: Cursor + Send + 'static,
B: RowSetBuffer + Send + 'static,
{
/// 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.
pub fn from_block_cursor(block_cursor: BlockCursor<C, B>) -> Self {
let (send_buffer, receive_buffer) = sync_channel(1);
let (send_batch, receive_batch) = sync_channel(1);
let fetch_thread = thread::spawn(move || {
let mut block_cursor = block_cursor;
loop {
match block_cursor.fetch_with_truncation_check(true) {
Ok(Some(_batch)) => (),
Ok(None) => {
break block_cursor
.unbind()
.map(|(undbound_cursor, _buffer)| undbound_cursor);
}
Err(odbc_error) => {
drop(send_batch);
break Err(odbc_error);
}
}
// There has been another row group fetched by the cursor. We unbind the buffers so
// we can pass ownership of it to the application and bind a new buffer to the
// cursor in order to start fetching the next batch.
let (cursor, buffer) = block_cursor.unbind()?;
if send_batch.send(buffer).is_err() {
// Should the main thread stop receiving buffers, this thread should
// also stop fetching batches.
break Ok(cursor);
}
// Wait for the application thread to give us a buffer to fill.
match receive_buffer.recv() {
Err(_) => {
// Application thread dropped sender and does not want more buffers to be
// filled. Let's stop this thread and return the cursor
break Ok(cursor);
}
Ok(next_buffer) => {
block_cursor = cursor.bind_buffer(next_buffer).unwrap();
}
}
}
});
Self {
send_buffer,
receive_batch,
fetch_thread: Some(fetch_thread),
cursor: None,
}
}
/// Join fetch thread and yield the cursor back.
pub fn into_cursor(self) -> Result<C, Error> {
drop(self.receive_batch);
// Dropping the send buffer is necessary to avoid deadlocks, in case there would not be any
// buffer in the channel waiting for the fetch thread. Since we consume the cursor here, it
// is also impossible for the application to send another buffer.
drop(self.send_buffer);
if let Some(cursor) = self.cursor {
Ok(cursor)
} else {
self.fetch_thread.unwrap().join().unwrap()
}
}
}
impl<C, B> ConcurrentBlockCursor<C, B> {
/// 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.
pub fn fetch(&mut self) -> Result<Option<B>, Error> {
match self.receive_batch.recv() {
// We successfully fetched a batch from the database.
Ok(batch) => Ok(Some(batch)),
// Fetch thread stopped sending batches. Either because we consumed the result set
// completly or we hit an error.
Err(_receive_error) => {
if let Some(join_handle) = self.fetch_thread.take() {
// If there has been an error returning the batch, or unbinding the buffer `?`
// will raise it.
self.cursor = Some(join_handle.join().unwrap()?);
// We ran out of batches in the result set. End the stream.
Ok(None)
} else {
// This only happen if this method is called after it returned either `false` or
// `Err` once. Let us treat this scenario like a result set which is consumed
// completly.
Ok(None)
}
}
}
}
/// 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`.
pub fn fill(&mut self, buffer: B) {
let _ = self.send_buffer.send(buffer);
}
/// 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.
pub fn fetch_into(&mut self, buffer: &mut B) -> Result<bool, Error> {
if let Some(mut batch) = self.fetch()? {
swap(buffer, &mut batch);
self.fill(batch);
Ok(true)
} else {
Ok(false)
}
}
}