use std::future::{Future, IntoFuture};
use std::pin::Pin;
use std::task::{Context, Poll};
use pigeonhole_format::Durability;
use crate::cell::RowBuf;
use crate::write::{Submitted, SubmittedCheck, TicketState};
use crate::{
Cell, CommitInfo, CommitTicket, Condition, Error, Pigeonhole, Result, Row, RowMutation,
Transaction, WriteBatch,
};
use pigeonhole_engine::ScanCursor;
#[derive(Debug)]
#[must_use = "a commit future dropped while it waits for room writes nothing (D210); await it"]
pub struct CommitFuture {
state: State,
}
#[derive(Debug)]
enum State {
Pending(Submitted),
Ready(Option<Result<CommitInfo>>),
}
impl CommitFuture {
fn new(submitted: Result<Submitted>) -> Self {
Self {
state: match submitted {
Ok(s) => State::Pending(s),
Err(e) => State::Ready(Some(Err(e))),
},
}
}
}
impl Future for CommitFuture {
type Output = Result<CommitInfo>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match &mut self.state {
State::Pending(s) => match Pin::new(&mut s.pending).poll(cx) {
Poll::Ready(r) => {
let out = s.outcome(r);
self.state = State::Ready(None);
Poll::Ready(out)
}
Poll::Pending => Poll::Pending,
},
State::Ready(r) => Poll::Ready(r.take().unwrap_or_else(|| {
Err(Error::new(
crate::ErrorCode::InvalidArgument,
"commit future polled after it resolved",
))
})),
}
}
}
impl IntoFuture for CommitTicket {
type Output = Result<CommitInfo>;
type IntoFuture = CommitFuture;
fn into_future(self) -> CommitFuture {
CommitFuture {
state: match self.into_state() {
TicketState::Pending(s) => State::Pending(s),
TicketState::Done(r) => State::Ready(Some(r)),
},
}
}
}
impl RowMutation<'_> {
pub fn commit_async(self) -> CommitFuture {
CommitFuture::new(self.submit_async())
}
}
impl RowMutation<'_> {
pub fn commit_if_async(self, condition: &Condition) -> CheckFuture {
CheckFuture {
state: match self.submit_if_async(condition) {
Ok(s) => CheckState::Pending(s),
Err(e) => CheckState::Ready(Some(Err(e))),
},
}
}
}
impl WriteBatch {
pub fn commit_async(self) -> CommitFuture {
self.async_commit(None)
}
pub fn commit_with_async(self, durability: Durability) -> CommitFuture {
self.async_commit(Some(durability))
}
fn async_commit(self, durability: Option<Durability>) -> CommitFuture {
CommitFuture::new(self.submit_async(durability))
}
}
impl Transaction {
pub fn commit_async(self) -> CommitFuture {
self.async_commit(None)
}
pub fn commit_with_async(self, durability: Durability) -> CommitFuture {
self.async_commit(Some(durability))
}
fn async_commit(self, durability: Option<Durability>) -> CommitFuture {
CommitFuture::new(self.submit_async(durability))
}
}
#[derive(Debug)]
#[must_use = "a commit future dropped while it waits for room writes nothing (D210); await it"]
pub struct CheckFuture {
state: CheckState,
}
#[derive(Debug)]
enum CheckState {
Pending(SubmittedCheck),
Ready(Option<Result<Option<CommitInfo>>>),
}
impl Future for CheckFuture {
type Output = Result<Option<CommitInfo>>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match &mut self.state {
CheckState::Pending(s) => match Pin::new(&mut s.pending).poll(cx) {
Poll::Ready(r) => {
let out = s.outcome(r);
self.state = CheckState::Ready(None);
Poll::Ready(out)
}
Poll::Pending => Poll::Pending,
},
CheckState::Ready(r) => Poll::Ready(r.take().unwrap_or_else(|| {
Err(Error::new(
crate::ErrorCode::InvalidArgument,
"commit future polled after it resolved",
))
})),
}
}
}
#[derive(Debug)]
#[must_use = "dropping a flush or compaction future does not stop it, but its result is lost"]
pub struct MaintenanceFuture {
inner: std::result::Result<pigeonhole_engine::PendingMaintenance, Option<Error>>,
}
impl Future for MaintenanceFuture {
type Output = Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match &mut self.inner {
Ok(m) => Pin::new(m).poll(cx).map(|r| r.map_err(Error::from)),
Err(e) => Poll::Ready(Err(e.take().unwrap_or_else(polled_after_done))),
}
}
}
impl Pigeonhole {
pub fn flush_async(&self) -> MaintenanceFuture {
MaintenanceFuture {
inner: self.db.engine.submit_flush().map_err(|e| Some(e.into())),
}
}
pub fn compact_async(&self) -> MaintenanceFuture {
MaintenanceFuture {
inner: self
.db
.engine
.submit_compact(None)
.map_err(|e| Some(e.into())),
}
}
}
#[derive(Debug)]
#[must_use = "a read does nothing unless polled"]
pub struct GetFuture {
inner: std::result::Result<pigeonhole_engine::GetFuture, Option<Error>>,
}
impl GetFuture {
pub(crate) fn new(started: Result<pigeonhole_engine::GetFuture>) -> Self {
Self {
inner: started.map_err(Some),
}
}
}
impl Future for GetFuture {
type Output = Result<Option<Cell>>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match &mut self.inner {
Ok(f) => Pin::new(f)
.poll(cx)
.map(|r| r.map(|c| c.map(Cell::from_data)).map_err(Error::from)),
Err(e) => Poll::Ready(Err(e.take().unwrap_or_else(polled_after_done))),
}
}
}
#[derive(Debug)]
#[must_use = "a read does nothing unless polled"]
pub struct RowFuture {
inner: std::result::Result<pigeonhole_engine::RowFuture<RowBuf>, Option<Error>>,
key: Vec<u8>,
}
impl RowFuture {
pub(crate) fn new(started: Result<pigeonhole_engine::RowFuture<RowBuf>>, key: Vec<u8>) -> Self {
Self {
inner: started.map_err(Some),
key,
}
}
}
impl Future for RowFuture {
type Output = Result<Option<Row>>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
match &mut this.inner {
Ok(f) => Pin::new(f).poll(cx).map(|r| match r {
Ok(Some(mut buf)) => {
buf.key = std::mem::take(&mut this.key);
Ok(Some(Row::new(buf)))
}
Ok(None) => Ok(None),
Err(e) => Err(e.into()),
}),
Err(e) => Poll::Ready(Err(e.take().unwrap_or_else(polled_after_done))),
}
}
}
fn polled_after_done() -> Error {
Error::new(
crate::ErrorCode::InvalidArgument,
"a read future polled after it resolved",
)
}
#[must_use = "a stream does nothing unless polled"]
pub struct RowStream<'t> {
inner: std::result::Result<(ScanCursor, RowBuf), Option<Error>>,
done: bool,
_table: std::marker::PhantomData<&'t crate::Table>,
}
impl std::fmt::Debug for RowStream<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RowStream")
.field("done", &self.done)
.finish()
}
}
impl RowStream<'_> {
pub(crate) fn new(started: Result<(ScanCursor, RowBuf)>, limit_zero: bool) -> Self {
Self {
inner: started.map_err(Some),
done: limit_zero,
_table: std::marker::PhantomData,
}
}
}
impl futures_core::Stream for RowStream<'_> {
type Item = Result<Row>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = &mut *self;
if this.done {
return Poll::Ready(None);
}
let (cursor, buf) = match &mut this.inner {
Ok(parts) => parts,
Err(e) => {
this.done = true;
return Poll::Ready(e.take().map(Err));
}
};
let item = match cursor.poll_next_row(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Ok(false)) => None,
Poll::Ready(Ok(true)) => match cursor.counted(|c| crate::read::fill_row(c, buf)) {
Ok(()) => {
let next = buf.empty_like();
Some(Ok(Row::new(std::mem::replace(buf, next))))
}
Err(e) => Some(Err(e)),
},
Poll::Ready(Err(e)) => Some(Err(e.into())),
};
if !matches!(item, Some(Ok(_))) {
this.done = true;
}
Poll::Ready(item)
}
}