use std::future::Future;
use std::pin::Pin;
use crate::{Applied, Program, Response, Result, Schema};
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Swept {
pub expired_leases: u64,
pub deleted: u64,
}
pub trait Executor: Send + Sync {
fn apply_schema<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
schema: &'a Schema,
expected: Option<i64>,
) -> BoxFuture<'a, Result<Applied>>;
fn get_schema<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
) -> BoxFuture<'a, Result<Option<Schema>>>;
fn execute<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
program: &'a Program,
) -> BoxFuture<'a, Result<Response>>;
fn execute_batch<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
programs: &'a [Program],
) -> BoxFuture<'a, Result<Vec<Result<Response>>>>;
fn sweep<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
batch: u32,
) -> BoxFuture<'a, Result<Swept>>;
}
impl<E: Executor + ?Sized> Executor for std::sync::Arc<E> {
fn apply_schema<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
schema: &'a Schema,
expected: Option<i64>,
) -> BoxFuture<'a, Result<Applied>> {
(**self).apply_schema(tenant, space, schema, expected)
}
fn get_schema<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
) -> BoxFuture<'a, Result<Option<Schema>>> {
(**self).get_schema(tenant, space)
}
fn execute<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
program: &'a Program,
) -> BoxFuture<'a, Result<Response>> {
(**self).execute(tenant, space, program)
}
fn execute_batch<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
programs: &'a [Program],
) -> BoxFuture<'a, Result<Vec<Result<Response>>>> {
(**self).execute_batch(tenant, space, programs)
}
fn sweep<'a>(
&'a self,
tenant: &'a str,
space: &'a str,
batch: u32,
) -> BoxFuture<'a, Result<Swept>> {
(**self).sweep(tenant, space, batch)
}
}