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