use crate::catalog::types::Row;
use crate::query::error::QueryError;
use crate::storage::encoded_key::EncodedKey;
use crate::storage::keyspace::{KeyspaceSnapshot, TableData};
use parking_lot::Mutex;
use std::sync::Arc;
pub(super) type LazyFetchError = Arc<Mutex<Option<QueryError>>>;
pub(super) struct LazyPkRowSource<'a, I> {
snapshot: &'a KeyspaceSnapshot,
table: &'a TableData,
pks: I,
error: LazyFetchError,
}
impl<'a, I: Iterator<Item = EncodedKey>> LazyPkRowSource<'a, I> {
pub(super) fn new(
snapshot: &'a KeyspaceSnapshot,
table: &'a TableData,
pks: I,
) -> (Self, LazyFetchError) {
let error: LazyFetchError = Arc::new(Mutex::new(None));
(
Self {
snapshot,
table,
pks,
error: Arc::clone(&error),
},
error,
)
}
}
impl<I: Iterator<Item = EncodedKey>> Iterator for LazyPkRowSource<'_, I> {
type Item = Row;
fn next(&mut self) -> Option<Row> {
for pk in self.pks.by_ref() {
match self.snapshot.get_row_in_table(self.table, &pk) {
Ok(Some(row)) => return Some(row.into_owned()),
Ok(None) => continue,
Err(err) => {
*self.error.lock() = Some(err.into());
return None;
}
}
}
None
}
}