Skip to main content

fireweed_dsl/
executor.rs

1//! The executor interface (API-008 §Executors): the five functions every
2//! backend provides, for any tenant and space.
3//!
4//! A consumer depends on this crate and holds an `Arc<dyn Executor>`. Which
5//! backend it gets is the host's choice at startup:
6//! `fireweed_dsl_postgres::PgExecutor` for PostgreSQL, or
7//! `fireweed_dsl_local::Local` for Turso MVCC with the log shipped to S3. Both pass the same conformance suite, so a program gives the same
8//! result on either.
9
10use std::future::Future;
11use std::pin::Pin;
12
13use crate::{Applied, Program, Response, Result, Schema};
14
15/// A boxed future that can move between threads.
16pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
17
18/// What one sweep pass changed.
19#[derive(Clone, Debug, Default, PartialEq, Eq)]
20pub struct Swept {
21    /// Leased items whose leases expired, written back as pending or failed.
22    pub expired_leases: u64,
23    /// Finished items, expired table rows and request records deleted.
24    pub deleted: u64,
25}
26
27/// A Fireweed backend. Calls may run concurrently from many tasks; each
28/// call is its own transaction (a batch is one transaction for all its
29/// programs). The implementations need a Tokio runtime.
30pub trait Executor: Send + Sync {
31    /// Creates a space, or changes its schema. `expected` is the version
32    /// the caller expects to replace: `None` for a new space.
33    fn apply_schema<'a>(
34        &'a self,
35        tenant: &'a str,
36        space: &'a str,
37        schema: &'a Schema,
38        expected: Option<i64>,
39    ) -> BoxFuture<'a, Result<Applied>>;
40
41    /// The stored schema of a space, if it exists.
42    fn get_schema<'a>(
43        &'a self,
44        tenant: &'a str,
45        space: &'a str,
46    ) -> BoxFuture<'a, Result<Option<Schema>>>;
47
48    /// Runs a program as one transaction.
49    fn execute<'a>(
50        &'a self,
51        tenant: &'a str,
52        space: &'a str,
53        program: &'a Program,
54    ) -> BoxFuture<'a, Result<Response>>;
55
56    /// Runs independent programs in one space with one commit; each gets
57    /// its own result, in order.
58    fn execute_batch<'a>(
59        &'a self,
60        tenant: &'a str,
61        space: &'a str,
62        programs: &'a [Program],
63    ) -> BoxFuture<'a, Result<Vec<Result<Response>>>>;
64
65    /// Writes back expired leases and deletes what retention expired, at
66    /// most `batch` rows per object.
67    fn sweep<'a>(
68        &'a self,
69        tenant: &'a str,
70        space: &'a str,
71        batch: u32,
72    ) -> BoxFuture<'a, Result<Swept>>;
73}
74
75impl<E: Executor + ?Sized> Executor for std::sync::Arc<E> {
76    fn apply_schema<'a>(
77        &'a self,
78        tenant: &'a str,
79        space: &'a str,
80        schema: &'a Schema,
81        expected: Option<i64>,
82    ) -> BoxFuture<'a, Result<Applied>> {
83        (**self).apply_schema(tenant, space, schema, expected)
84    }
85    fn get_schema<'a>(
86        &'a self,
87        tenant: &'a str,
88        space: &'a str,
89    ) -> BoxFuture<'a, Result<Option<Schema>>> {
90        (**self).get_schema(tenant, space)
91    }
92    fn execute<'a>(
93        &'a self,
94        tenant: &'a str,
95        space: &'a str,
96        program: &'a Program,
97    ) -> BoxFuture<'a, Result<Response>> {
98        (**self).execute(tenant, space, program)
99    }
100    fn execute_batch<'a>(
101        &'a self,
102        tenant: &'a str,
103        space: &'a str,
104        programs: &'a [Program],
105    ) -> BoxFuture<'a, Result<Vec<Result<Response>>>> {
106        (**self).execute_batch(tenant, space, programs)
107    }
108    fn sweep<'a>(
109        &'a self,
110        tenant: &'a str,
111        space: &'a str,
112        batch: u32,
113    ) -> BoxFuture<'a, Result<Swept>> {
114        (**self).sweep(tenant, space, batch)
115    }
116}