use std::fmt;
use std::future::Future;
use std::ops::ControlFlow;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::task::{Context, Poll};
use futures::stream::FusedStream;
pub use futures::stream::{Stream, StreamExt, TryStreamExt};
use crate::collection::Collection;
use crate::column::IntoExpr;
use crate::error::{OrmError, Result};
use crate::exec::{Access, Executor, Target};
use crate::hooks::{Hooks, ModelEvent};
use crate::ir::{ColumnRef, Direction, Expr, Operator, Order, QueryIr, Where, WhereClause};
use crate::model::Model;
use crate::query::{Builder, HasQuery, TableBuilder, key_expr};
use crate::raw::Raw;
use crate::record::Record;
use crate::row::Row;
use crate::scope::group_scope_wheres;
use crate::value::Value;
pub struct RowStream<'a, T> {
producer: Option<Pin<Box<dyn Future<Output = ()> + Send + 'a>>>,
slot: Arc<Mutex<Option<T>>>,
}
pub(crate) struct Yielder<T> {
slot: Arc<Mutex<Option<T>>>,
}
impl<T> Yielder<T> {
pub(crate) fn send(&mut self, item: T) -> impl Future<Output = ()> + '_ {
*lock(&self.slot) = Some(item);
let mut handed_over = false;
std::future::poll_fn(move |_| {
if handed_over {
Poll::Ready(())
} else {
handed_over = true;
Poll::Pending
}
})
}
}
impl<'a, T: Send + 'a> RowStream<'a, T> {
pub(crate) fn generate<F, Fut>(produce: F) -> Self
where
F: FnOnce(Yielder<T>) -> Fut,
Fut: Future<Output = ()> + Send + 'a,
{
let slot = Arc::new(Mutex::new(None));
let yielder = Yielder { slot: slot.clone() };
RowStream {
producer: Some(Box::pin(produce(yielder))),
slot,
}
}
pub(crate) fn try_generate<F, Fut>(produce: F) -> RowStream<'a, Result<T>>
where
F: FnOnce(Yielder<Result<T>>) -> Fut + Send + 'a,
Fut: Future<Output = (Yielder<Result<T>>, Result<()>)> + Send + 'a,
{
RowStream::generate(move |yielder| async move {
let (mut yielder, outcome) = produce(yielder).await;
if let Err(error) = outcome {
yielder.send(Err(error)).await;
}
})
}
}
impl<T> Stream for RowStream<'_, T> {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> {
let this = &mut *self;
let Some(producer) = this.producer.as_mut() else {
return Poll::Ready(None);
};
match producer.as_mut().poll(cx) {
Poll::Ready(()) => {
this.producer = None;
Poll::Ready(lock(&this.slot).take())
}
Poll::Pending => match lock(&this.slot).take() {
Some(item) => Poll::Ready(Some(item)),
None => Poll::Pending,
},
}
}
}
impl<T> FusedStream for RowStream<'_, T> {
fn is_terminated(&self) -> bool {
self.producer.is_none()
}
}
impl<T> fmt::Debug for RowStream<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RowStream")
.field("finished", &self.producer.is_none())
.finish_non_exhaustive()
}
}
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
mutex.lock().unwrap_or_else(PoisonError::into_inner)
}
static ACTIVE: AtomicUsize = AtomicUsize::new(0);
static STREAMING: Mutex<Vec<usize>> = Mutex::new(Vec::new());
pub(crate) struct StreamingGuard(usize);
impl StreamingGuard {
pub(crate) fn new(transaction: usize) -> Self {
lock(&STREAMING).push(transaction);
ACTIVE.fetch_add(1, Ordering::SeqCst);
StreamingGuard(transaction)
}
}
impl Drop for StreamingGuard {
fn drop(&mut self) {
let mut streaming = lock(&STREAMING);
if let Some(at) = streaming.iter().position(|key| *key == self.0) {
streaming.swap_remove(at);
}
ACTIVE.fetch_sub(1, Ordering::SeqCst);
}
}
pub(crate) fn ensure_free(transaction: Option<usize>) -> Result<()> {
let Some(transaction) = transaction else {
return Ok(());
};
if ACTIVE.load(Ordering::SeqCst) == 0 || !lock(&STREAMING).contains(&transaction) {
return Ok(());
}
Err(OrmError::invalid(
"this transaction's connection is busy streaming a cursor; finish or drop the cursor first (lazy() and chunk_by_id() read in pages and allow writes in between)",
))
}
pub(crate) trait Paged: Clone + Send + Sync + Sized + 'static {
type Item: Send + 'static;
fn query_mut(&mut self) -> &mut QueryIr;
fn route(&self) -> crate::Route<'_>;
fn fetch_page<'t>(
self,
target: &'t Target<'_>,
) -> impl Future<Output = Result<Vec<Self::Item>>> + Send + 't;
fn column_value(item: &Self::Item, column: &str) -> Result<Option<Value>>;
}
impl<M: Model> Paged for Builder<M> {
type Item = Record<M>;
fn query_mut(&mut self) -> &mut QueryIr {
&mut self.ir
}
fn route(&self) -> crate::Route<'_> {
Builder::route(self)
}
async fn fetch_page(self, target: &Target<'_>) -> Result<Vec<Record<M>>> {
Ok(self.fetch(target).await?.into_vec())
}
fn column_value(record: &Record<M>, column: &str) -> Result<Option<Value>> {
if M::meta().has_column(column) {
return record.model().value(column);
}
Ok(record
.extras()
.iter()
.find(|(name, _)| name == column)
.map(|(_, value)| value.clone()))
}
}
impl Paged for TableBuilder {
type Item = Row;
fn query_mut(&mut self) -> &mut QueryIr {
HasQuery::query_mut(self)
}
fn route(&self) -> crate::Route<'_> {
TableBuilder::route(self)
}
async fn fetch_page(self, target: &Target<'_>) -> Result<Vec<Row>> {
target.fetch(&self.into_ir()).await
}
fn column_value(row: &Row, column: &str) -> Result<Option<Value>> {
if row.has_column(column) {
row.value(column).map(Some)
} else {
Ok(None)
}
}
}
struct Keyset {
column: ColumnRef,
alias: String,
descending: bool,
last: Option<Value>,
is_key: bool,
}
struct Walk<Q: Paged> {
query: Q,
size: u64,
skip: u64,
remaining: Option<u64>,
page: u64,
keyset: Option<Keyset>,
done: bool,
operation: &'static str,
}
fn chunk_size(size: u64) -> Result<u64> {
if size == 0 {
Err(OrmError::invalid("the chunk size must be at least 1"))
} else {
Ok(size)
}
}
impl<Q: Paged> Walk<Q> {
fn by_offset(mut query: Q, size: u64, operation: &'static str) -> Result<Self> {
let size = chunk_size(size)?;
let ir = query.query_mut();
let skip = ir.offset.unwrap_or(0);
let remaining = ir.limit;
Ok(Walk {
query,
size,
skip,
remaining,
page: 0,
keyset: None,
done: false,
operation,
})
}
fn by_column(
mut query: Q,
size: u64,
column: ColumnRef,
alias: String,
descending: bool,
is_key: bool,
operation: &'static str,
) -> Result<Self> {
let size = chunk_size(size)?;
let ir = query.query_mut();
let skip = ir.offset.unwrap_or(0);
let remaining = ir.limit;
Ok(Walk {
query,
size,
skip,
remaining,
page: 0,
keyset: Some(Keyset {
column,
alias,
descending,
last: None,
is_key,
}),
done: false,
operation,
})
}
fn next_query(&self, limit: u64) -> Result<Q> {
let mut query = self.query.clone();
let ir = query.query_mut();
match &self.keyset {
None => {
let offset = self
.page
.checked_mul(self.size)
.and_then(|start| start.checked_add(self.skip))
.ok_or_else(|| OrmError::invalid("the chunk offset overflows"))?;
ir.offset = (offset > 0).then_some(offset);
ir.limit = Some(limit);
}
Some(keyset) => {
ir.orders = vec![Order::By {
expr: Expr::Column(keyset.column.clone()),
direction: if keyset.descending {
Direction::Desc
} else {
Direction::Asc
},
nulls: None,
}];
ir.offset = (self.page == 0 && self.skip > 0).then_some(self.skip);
ir.limit = Some(limit);
let condition = match &keyset.last {
None if keyset.is_key => None,
None => Some(Where::Null {
expr: Expr::Column(keyset.column.clone()),
not: true,
}),
Some(last) => Some(Where::Compare {
left: Expr::Column(keyset.column.clone()),
op: if keyset.descending {
Operator::Lt
} else {
Operator::Gt
},
right: Expr::Value(last.clone()),
}),
};
if let Some(condition) = condition {
let before = ir.wheres.len();
ir.wheres.push(WhereClause::and(condition));
group_scope_wheres(ir, before);
}
}
}
Ok(query)
}
async fn next_page<E: Executor + ?Sized>(&mut self, exec: &E) -> Result<Option<Vec<Q::Item>>> {
if self.done {
return Ok(None);
}
let limit = self.remaining.map_or(self.size, |left| left.min(self.size));
if limit == 0 {
self.done = true;
return Ok(None);
}
let mut query = self.next_query(limit)?;
let access = crate::query::read_access(query.query_mut());
let items = {
let target = exec.target_routed(access, query.route()).await?;
query.fetch_page(&target).await?
};
let count = items.len() as u64;
if let Some(left) = &mut self.remaining {
*left = left.saturating_sub(count);
}
if count < self.size {
self.done = true;
}
let Some(last) = items.last() else {
self.done = true;
return Ok(None);
};
if let Some(keyset) = &mut self.keyset {
let value = Q::column_value(last, &keyset.alias)?.ok_or_else(|| {
OrmError::invalid(format!(
"{} was stopped: the result has no `{}` column to continue from (select it, or name the column it is selected as)",
self.operation, keyset.alias
))
})?;
if crate::value::is_null(&value) {
return Err(OrmError::invalid(format!(
"{} was stopped: the `{}` column of the last row is NULL, so the next page cannot start after it",
self.operation, keyset.alias
)));
}
keyset.last = Some(value);
}
self.page += 1;
Ok(Some(items))
}
async fn chunks<E, F, Fut>(mut self, exec: &E, mut f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Vec<Q::Item>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
while let Some(page) = self.next_page(exec).await? {
if f(page).await?.is_break() {
return Ok(false);
}
}
Ok(true)
}
async fn items<E, F, Fut>(mut self, exec: &E, mut f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Q::Item) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
while let Some(page) = self.next_page(exec).await? {
for item in page {
if f(item).await?.is_break() {
return Ok(false);
}
}
}
Ok(true)
}
fn stream<'e, E: Executor + ?Sized>(
walk: Result<Self>,
exec: &'e E,
) -> RowStream<'e, Result<Q::Item>> {
RowStream::try_generate(move |mut out| async move {
let outcome = async {
let mut walk = walk?;
while let Some(page) = walk.next_page(exec).await? {
for item in page {
out.send(Ok(item)).await;
}
}
Ok(())
}
.await;
(out, outcome)
})
}
}
fn keyset_column(column: impl IntoExpr) -> Result<ColumnRef> {
match column.into_expr() {
Expr::Column(column) if column.path.is_empty() => Ok(column),
other => Err(OrmError::invalid(format!(
"chunking by a column needs a plain column, not {other:?}"
))),
}
}
impl<M: Model> Builder<M> {
fn for_offset_pages(self) -> Self {
let mut query = self.apply_global_scopes();
if query.ir.orders.is_empty() {
query.ir.orders.push(Order::asc(key_expr::<M>()));
}
query
}
fn key_walk(
self,
size: u64,
column: Option<(ColumnRef, String)>,
descending: bool,
operation: &'static str,
) -> Result<Walk<Self>> {
let meta = M::meta();
let (column, alias) = column.unwrap_or_else(|| {
(
ColumnRef::new(Some(meta.table()), meta.key_column()),
meta.key_column().to_owned(),
)
});
let is_key = column.name == meta.key_column()
&& column
.table
.as_deref()
.is_none_or(|table| table == meta.table());
Walk::by_column(
self.apply_global_scopes(),
size,
column,
alias,
descending,
is_key,
operation,
)
}
pub async fn chunk<E, F, Fut>(self, exec: &E, size: u64, mut f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Collection<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
Walk::by_offset(self.for_offset_pages(), size, "chunk")?
.chunks(exec, |page| f(Collection::from(page)))
.await
}
pub async fn chunk_map<E, T, F>(self, exec: &E, size: u64, mut f: F) -> Result<Vec<T>>
where
E: Executor + ?Sized,
F: FnMut(Record<M>) -> T,
{
let mut mapped = Vec::new();
let mut walk = Walk::by_offset(self.for_offset_pages(), size, "chunk_map")?;
while let Some(page) = walk.next_page(exec).await? {
mapped.extend(page.into_iter().map(&mut f));
}
Ok(mapped)
}
pub async fn each<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Record<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
Walk::by_offset(self.for_offset_pages(), size, "each")?
.items(exec, f)
.await
}
pub async fn chunk_by_id<E, F, Fut>(self, exec: &E, size: u64, mut f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Collection<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
self.key_walk(size, None, false, "chunk_by_id")?
.chunks(exec, |page| f(Collection::from(page)))
.await
}
pub async fn chunk_by_id_desc<E, F, Fut>(self, exec: &E, size: u64, mut f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Collection<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
self.key_walk(size, None, true, "chunk_by_id_desc")?
.chunks(exec, |page| f(Collection::from(page)))
.await
}
pub async fn chunk_by_column<E, F, Fut>(
self,
exec: &E,
size: u64,
column: impl IntoExpr,
alias: impl Into<String>,
mut f: F,
) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Collection<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
let column = keyset_column(column)?;
self.key_walk(size, Some((column, alias.into())), false, "chunk_by_id")?
.chunks(exec, |page| f(Collection::from(page)))
.await
}
pub async fn chunk_by_column_desc<E, F, Fut>(
self,
exec: &E,
size: u64,
column: impl IntoExpr,
alias: impl Into<String>,
mut f: F,
) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Collection<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
let column = keyset_column(column)?;
self.key_walk(size, Some((column, alias.into())), true, "chunk_by_id_desc")?
.chunks(exec, |page| f(Collection::from(page)))
.await
}
pub async fn each_by_id<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Record<M>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
self.key_walk(size, None, false, "each_by_id")?
.items(exec, f)
.await
}
pub fn lazy<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
) -> RowStream<'e, Result<Record<M>>> {
Walk::stream(Walk::by_offset(self.for_offset_pages(), size, "lazy"), exec)
}
pub fn lazy_by_id<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
) -> RowStream<'e, Result<Record<M>>> {
Walk::stream(self.key_walk(size, None, false, "lazy_by_id"), exec)
}
pub fn lazy_by_id_desc<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
) -> RowStream<'e, Result<Record<M>>> {
Walk::stream(self.key_walk(size, None, true, "lazy_by_id_desc"), exec)
}
pub fn lazy_by_column<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
column: impl IntoExpr,
alias: impl Into<String>,
) -> RowStream<'e, Result<Record<M>>> {
let walk = keyset_column(column).and_then(|column| {
self.key_walk(size, Some((column, alias.into())), false, "lazy_by_id")
});
Walk::stream(walk, exec)
}
pub fn lazy_by_column_desc<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
column: impl IntoExpr,
alias: impl Into<String>,
) -> RowStream<'e, Result<Record<M>>> {
let walk = keyset_column(column).and_then(|column| {
self.key_walk(size, Some((column, alias.into())), true, "lazy_by_id_desc")
});
Walk::stream(walk, exec)
}
pub fn cursor<'e, E: Executor + ?Sized>(self, exec: &'e E) -> RowStream<'e, Result<Record<M>>> {
RowStream::try_generate(move |mut out| async move {
let outcome = async {
let target = exec
.target_routed(crate::query::read_access(&self.ir), self.route())
.await?;
let ir = crate::workspace::with_target(&target, || self.into_ir());
let mut rows = target.stream(&ir).await?;
let ctx = target.context();
let hooks = Hooks::<M>::resolve(&ctx).await?;
while let Some(row) = rows.next().await {
let mut record = Record::<M>::from_row(&row?)?;
if !hooks.is_empty() {
hooks
.after(ModelEvent::Retrieved, &ctx, &mut record)
.await?;
}
out.send(Ok(record)).await;
}
Ok(())
}
.await;
(out, outcome)
})
}
}
impl TableBuilder {
fn for_offset_pages(mut self, operation: &str) -> Result<Self> {
let ir = HasQuery::query_mut(&mut self);
if ir.orders.is_empty() {
return Err(OrmError::invalid(format!(
"{operation} needs an order on a table query (add order_by(..) so pages neither overlap nor skip rows)"
)));
}
Ok(self)
}
fn key_walk(
self,
size: u64,
column: ColumnRef,
alias: String,
descending: bool,
operation: &'static str,
) -> Result<Walk<Self>> {
Walk::by_column(self, size, column, alias, descending, false, operation)
}
pub async fn chunk<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Vec<Row>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
Walk::by_offset(self.for_offset_pages("chunk")?, size, "chunk")?
.chunks(exec, f)
.await
}
pub async fn chunk_map<E, T, F>(self, exec: &E, size: u64, mut f: F) -> Result<Vec<T>>
where
E: Executor + ?Sized,
F: FnMut(Row) -> T,
{
let mut mapped = Vec::new();
let mut walk = Walk::by_offset(self.for_offset_pages("chunk_map")?, size, "chunk_map")?;
while let Some(page) = walk.next_page(exec).await? {
mapped.extend(page.into_iter().map(&mut f));
}
Ok(mapped)
}
pub async fn each<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Row) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
Walk::by_offset(self.for_offset_pages("each")?, size, "each")?
.items(exec, f)
.await
}
pub async fn chunk_by_id<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Vec<Row>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
let column = id_column(&self);
self.key_walk(size, column, "id".to_owned(), false, "chunk_by_id")?
.chunks(exec, f)
.await
}
pub async fn chunk_by_id_desc<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Vec<Row>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
let column = id_column(&self);
self.key_walk(size, column, "id".to_owned(), true, "chunk_by_id_desc")?
.chunks(exec, f)
.await
}
pub async fn chunk_by_column<E, F, Fut>(
self,
exec: &E,
size: u64,
column: impl IntoExpr,
alias: impl Into<String>,
f: F,
) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Vec<Row>) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
let column = keyset_column(column)?;
self.key_walk(size, column, alias.into(), false, "chunk_by_id")?
.chunks(exec, f)
.await
}
pub async fn each_by_id<E, F, Fut>(self, exec: &E, size: u64, f: F) -> Result<bool>
where
E: Executor + ?Sized,
F: FnMut(Row) -> Fut,
Fut: Future<Output = Result<ControlFlow<()>>>,
{
let column = id_column(&self);
self.key_walk(size, column, "id".to_owned(), false, "each_by_id")?
.items(exec, f)
.await
}
pub fn lazy<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
) -> RowStream<'e, Result<Row>> {
let walk = self
.for_offset_pages("lazy")
.and_then(|query| Walk::by_offset(query, size, "lazy"));
Walk::stream(walk, exec)
}
pub fn lazy_by_id<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
) -> RowStream<'e, Result<Row>> {
let column = id_column(&self);
Walk::stream(
self.key_walk(size, column, "id".to_owned(), false, "lazy_by_id"),
exec,
)
}
pub fn lazy_by_id_desc<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
) -> RowStream<'e, Result<Row>> {
let column = id_column(&self);
Walk::stream(
self.key_walk(size, column, "id".to_owned(), true, "lazy_by_id_desc"),
exec,
)
}
pub fn lazy_by_column<'e, E: Executor + ?Sized>(
self,
exec: &'e E,
size: u64,
column: impl IntoExpr,
alias: impl Into<String>,
) -> RowStream<'e, Result<Row>> {
let walk = keyset_column(column)
.and_then(|column| self.key_walk(size, column, alias.into(), false, "lazy_by_id"));
Walk::stream(walk, exec)
}
pub fn cursor<'e, E: Executor + ?Sized>(self, exec: &'e E) -> RowStream<'e, Result<Row>> {
RowStream::try_generate(move |mut out| async move {
let outcome = async {
let target = exec
.target_routed(crate::query::read_access(self.ir()), self.route())
.await?;
let ir = self.into_ir();
let mut rows = target.stream(&ir).await?;
while let Some(row) = rows.next().await {
out.send(Ok(row?)).await;
}
Ok(())
}
.await;
(out, outcome)
})
}
}
fn id_column(query: &TableBuilder) -> ColumnRef {
ColumnRef::new(Some(query.ir().table.qualifier().to_owned()), "id")
}
pub fn cursor<E: Executor + ?Sized>(exec: &E, query: Raw) -> RowStream<'_, Result<Row>> {
RowStream::try_generate(move |mut out| async move {
let outcome = async {
let target = exec.target(Access::Read).await?;
let sql = crate::raw::sql_target(&target)?;
let statement = query.to_statement(sql.dialect())?;
let mut rows = sql.stream(&statement).await?;
while let Some(row) = rows.next().await {
out.send(Ok(row?)).await;
}
Ok(())
}
.await;
(out, outcome)
})
}
#[cfg(test)]
mod tests {
use futures::StreamExt;
use super::*;
#[tokio::test]
async fn generators_hand_over_one_item_at_a_time() {
let produced = Arc::new(AtomicUsize::new(0));
let counter = produced.clone();
let mut stream = RowStream::generate(move |mut out| async move {
for i in 0..3 {
counter.fetch_add(1, Ordering::SeqCst);
out.send(i).await;
}
});
assert_eq!(
produced.load(Ordering::SeqCst),
0,
"nothing runs before a poll"
);
assert_eq!(stream.next().await, Some(0));
assert_eq!(produced.load(Ordering::SeqCst), 1, "no read-ahead");
assert_eq!(stream.next().await, Some(1));
assert_eq!(stream.next().await, Some(2));
assert!(!stream.is_terminated());
assert_eq!(stream.next().await, None);
assert!(stream.is_terminated());
assert_eq!(stream.next().await, None);
}
#[tokio::test]
async fn failures_end_the_stream() {
let mut stream = RowStream::<i32>::try_generate(|mut out| async move {
out.send(Ok(1)).await;
(out, Err(OrmError::invalid("boom")))
});
assert_eq!(stream.next().await.unwrap().unwrap(), 1);
assert_eq!(
stream.next().await.unwrap().unwrap_err().to_string(),
"invalid query: boom"
);
assert!(stream.next().await.is_none());
}
#[test]
fn busy_transactions_are_refused_until_the_guard_drops() {
assert!(ensure_free(Some(41)).is_ok());
let guard = StreamingGuard::new(41);
assert!(ensure_free(Some(41)).is_err());
assert!(ensure_free(Some(42)).is_ok());
assert!(ensure_free(None).is_ok());
drop(guard);
assert!(ensure_free(Some(41)).is_ok());
}
}