1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Definition for a `Program` to run code.
//!
//! It carries an [EnvDb] with the functions, idents, types.
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::relation::{MemTable, RelIter, Relation, Table};
use std::collections::HashMap;

use crate::env::EnvDb;
use crate::errors::ErrorVm;
use crate::eval::{build_query, IterRows};
use crate::expr::{Code, CrudCode, FunctionId};
use crate::functions::FunDef;
use crate::operator::*;
use crate::ops::logic;
use crate::ops::math;
use crate::rel_ops::RelOps;

/// A trait to allow split the execution of `programs` to allow executing
/// `queries` that take in account each `program` state/enviroment.
///
/// In concrete, it allows to run queries that run on the `SpaceTimeDb` engine.
///
/// It could also permite run queries backed by different engines, like in `MySql`.
pub trait ProgramVm {
    /// Load the in-built functions that define the operators of the VM,
    /// like `+`, `and`, `==`, etc.
    fn load_ops(env: &mut EnvDb)
    where
        Self: Sized,
    {
        let mut ops: HashMap<Op, FunctionId> = HashMap::new();

        ops.insert(OpCmp::Eq.into(), env.functions.add(OpCmp::Eq, Box::new(logic::eq)));
        ops.insert(
            OpCmp::NotEq.into(),
            env.functions.add(OpCmp::NotEq, Box::new(logic::not_eq)),
        );
        ops.insert(OpCmp::Lt.into(), env.functions.add(OpCmp::Lt, Box::new(logic::less)));
        ops.insert(
            OpCmp::LtEq.into(),
            env.functions.add(OpCmp::LtEq, Box::new(logic::less_than)),
        );
        ops.insert(OpCmp::Gt.into(), env.functions.add(OpCmp::Gt, Box::new(logic::greater)));
        ops.insert(
            OpCmp::GtEq.into(),
            env.functions.add(OpCmp::GtEq, Box::new(logic::greater_than)),
        );
        ops.insert(
            OpUnary::Not.into(),
            env.functions.add(OpUnary::Not, Box::new(logic::not)),
        );
        ops.insert(
            OpLogic::And.into(),
            env.functions.add(OpLogic::And, Box::new(logic::and)),
        );
        ops.insert(OpLogic::Or.into(), env.functions.add(OpLogic::Or, Box::new(logic::or)));

        ops.insert(OpMath::Add.into(), env.functions.add(OpMath::Add, Box::new(math::add)));
        ops.insert(
            OpMath::Minus.into(),
            env.functions.add(OpMath::Minus, Box::new(math::minus)),
        );
        ops.insert(OpMath::Mul.into(), env.functions.add(OpMath::Mul, Box::new(math::mul)));
        ops.insert(OpMath::Div.into(), env.functions.add(OpMath::Div, Box::new(math::div)));

        env.functions.ops = ops
    }

    fn env(&self) -> &EnvDb;
    fn env_mut(&mut self) -> &mut EnvDb;
    fn ctx(&self) -> &dyn ProgramVm;
    fn auth(&self) -> &AuthCtx;
    /// Add a `function` that is defined natively by [Code]
    fn add_lambda(&mut self, f: FunDef, body: Code) {
        if let Some(s) = self.env_mut().child.last_mut() {
            s.lambdas.add(f, body)
        } else {
            self.env_mut().lambdas.add(f, body)
        }
    }

    fn update_lambda(&mut self, f: FunDef, body: Code) {
        if let Some(s) = self.env_mut().child.last_mut() {
            s.lambdas.update(f, body)
        } else {
            self.env_mut().lambdas.update(f, body)
        }
    }

    /// Add a `ident` into the environment, similar to `let x = expr`
    fn add_ident(&mut self, name: &str, v: Code) {
        if let Some(s) = self.env_mut().child.last_mut() {
            s.idents.add(name, v)
        } else {
            self.env_mut().idents.add(name, v)
        }
    }

    /// Locates the `ident` in the environment
    fn find_ident(&self, key: &str) -> Option<&Code> {
        for s in self.env().child.iter().rev() {
            let ident = s.idents.get_by_name(key);
            if ident.is_some() {
                return ident;
            }
        }
        self.env().idents.get_by_name(key)
    }

    /// Allows to execute the query with the state carried by the implementation of this
    /// trait
    fn eval_query(&mut self, query: CrudCode) -> Result<Code, ErrorVm>;

    fn as_program_ref(&self) -> ProgramRef<'_>;
}

pub struct ProgramStore<P> {
    pub p: P,
    pub code: Code,
}

impl<P> ProgramStore<P> {
    pub fn new(p: P, code: Code) -> Self {
        Self { p, code }
    }
}

pub struct ProgramRef<'a> {
    pub env: &'a EnvDb,
    pub stats: &'a HashMap<String, u64>,
    pub ctx: &'a dyn ProgramVm,
}

/// A default program that run in-memory without a database
pub struct Program {
    pub(crate) env: EnvDb,
    pub(crate) stats: HashMap<String, u64>,
    pub(crate) auth: AuthCtx,
}

impl Program {
    pub fn new(auth: AuthCtx) -> Self {
        let mut env = EnvDb::new();
        Self::load_ops(&mut env);
        Self {
            env,
            stats: Default::default(),
            auth,
        }
    }
}

impl ProgramVm for Program {
    fn env(&self) -> &EnvDb {
        &self.env
    }

    fn env_mut(&mut self) -> &mut EnvDb {
        &mut self.env
    }

    fn ctx(&self) -> &dyn ProgramVm {
        self as &dyn ProgramVm
    }

    fn auth(&self) -> &AuthCtx {
        &self.auth
    }

    #[tracing::instrument(skip_all)]
    fn eval_query(&mut self, query: CrudCode) -> Result<Code, ErrorVm> {
        match query {
            CrudCode::Query(query) => {
                let head = query.head().clone();
                let row_count = query.row_count();
                let table_access = query.table.table_access();
                let result = match query.table {
                    Table::MemTable(x) => Box::new(RelIter::new(head, row_count, x)) as Box<IterRows<'_>>,
                    Table::DbTable(_) => {
                        panic!("DB not set")
                    }
                };

                let result = build_query(result, query.query)?;

                let head = result.head().clone();
                let rows: Vec<_> = result.collect_vec()?;

                Ok(Code::Table(MemTable::new(head, table_access, rows)))
            }
            CrudCode::Insert { .. } => {
                todo!()
            }
            CrudCode::Update { .. } => {
                todo!()
            }
            CrudCode::Delete { .. } => {
                todo!()
            }
            CrudCode::CreateTable { .. } => {
                todo!()
            }
            CrudCode::Drop { .. } => {
                todo!()
            }
        }
    }

    fn as_program_ref(&self) -> ProgramRef<'_> {
        ProgramRef {
            env: self.env(),
            stats: &self.stats,
            ctx: self.ctx(),
        }
    }
}