fireweed-dsl 0.36.0

Fireweed's data-plane DSL (API-008): schema and program documents, and the checker that resolves a program against a schema.
Documentation
//! The executor interface (API-008 §Executors): the five functions every
//! backend provides, for any tenant and space.
//!
//! A consumer depends on this crate and holds an `Arc<dyn Executor>`. Which
//! backend it gets is the host's choice at startup:
//! `fireweed_dsl_postgres::PgExecutor` for PostgreSQL, or
//! `fireweed_dsl_local::Local` for SQLite or Turso MVCC with the log shipped
//! to S3. Both pass the same conformance suite, so a program gives the same
//! result on either.

use std::future::Future;
use std::pin::Pin;

use crate::{Applied, Program, Response, Result, Schema};

/// A boxed future that can move between threads.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// What one sweep pass changed.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Swept {
    /// Leased items whose leases expired, written back as pending or failed.
    pub expired_leases: u64,
    /// Finished items, expired table rows and request records deleted.
    pub deleted: u64,
}

/// A Fireweed backend. Calls may run concurrently from many tasks; each
/// call is its own transaction (a batch is one transaction for all its
/// programs). The implementations need a Tokio runtime.
pub trait Executor: Send + Sync {
    /// Creates a space, or changes its schema. `expected` is the version
    /// the caller expects to replace: `None` for a new space.
    fn apply_schema<'a>(
        &'a self,
        tenant: &'a str,
        space: &'a str,
        schema: &'a Schema,
        expected: Option<i64>,
    ) -> BoxFuture<'a, Result<Applied>>;

    /// The stored schema of a space, if it exists.
    fn get_schema<'a>(
        &'a self,
        tenant: &'a str,
        space: &'a str,
    ) -> BoxFuture<'a, Result<Option<Schema>>>;

    /// Runs a program as one transaction.
    fn execute<'a>(
        &'a self,
        tenant: &'a str,
        space: &'a str,
        program: &'a Program,
    ) -> BoxFuture<'a, Result<Response>>;

    /// Runs independent programs in one space with one commit; each gets
    /// its own result, in order.
    fn execute_batch<'a>(
        &'a self,
        tenant: &'a str,
        space: &'a str,
        programs: &'a [Program],
    ) -> BoxFuture<'a, Result<Vec<Result<Response>>>>;

    /// Writes back expired leases and deletes what retention expired, at
    /// most `batch` rows per object.
    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)
    }
}