-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdml.rs
More file actions
188 lines (173 loc) · 6.4 KB
/
Copy pathdml.rs
File metadata and controls
188 lines (173 loc) · 6.4 KB
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
use anyhow::Result;
use spacetimedb_lib::{metrics::ExecutionMetrics, AlgebraicValue, ProductValue};
use spacetimedb_physical_plan::{dml::{DeletePlan, InsertPlan, MutationPlan, UpdatePlan}, plan::{ProjectField, ProjectListPlan}};
use spacetimedb_primitives::{ColId, TableId};
use spacetimedb_sats::size_of::SizeOf;
use crate::{pipelined::PipelinedProject, Datastore, DeltaStore, Row};
/// A mutable datastore can read as well as insert and delete rows
pub trait MutDatastore: Datastore + DeltaStore {
fn insert_product_value(&mut self, table_id: TableId, row: &ProductValue) -> Result<bool>;
fn delete_product_value(&mut self, table_id: TableId, row: &ProductValue) -> Result<bool>;
}
/// Executes a physical mutation plan
pub enum MutExecutor {
Insert(InsertExecutor),
Delete(DeleteExecutor),
Update(UpdateExecutor),
}
impl From<MutationPlan> for MutExecutor {
fn from(plan: MutationPlan) -> Self {
match plan {
MutationPlan::Insert(plan) => Self::Insert(plan.into()),
MutationPlan::Delete(plan) => Self::Delete(plan.into()),
MutationPlan::Update(plan) => Self::Update(plan.into()),
}
}
}
impl MutExecutor {
pub fn execute<Tx: MutDatastore>(&self, tx: &mut Tx, metrics: &mut ExecutionMetrics) -> Result<Vec<ProductValue>> {
match self {
Self::Insert(exec) => exec.execute(tx, metrics),
Self::Delete(exec) => exec.execute(tx, metrics),
Self::Update(exec) => exec.execute(tx, metrics),
}
}
}
fn project_returning_row(returning: &ProjectListPlan, row: ProductValue) -> Option<ProductValue> {
match returning {
ProjectListPlan::Name(_) => {
Some(row)
}
ProjectListPlan::List(_, fields) => {
let row = Row::Ref(&row);
Some(ProductValue::from_iter(fields.iter().map(|field| row.project(field))))
}
_ => None
}
}
/// Executes row insertions
pub struct InsertExecutor {
table_id: TableId,
rows: Vec<ProductValue>,
returning: Option<ProjectListPlan>,
}
impl From<InsertPlan> for InsertExecutor {
fn from(plan: InsertPlan) -> Self {
Self {
table_id: plan.table.table_id,
rows: plan.rows,
returning: plan.returning,
}
}
}
impl InsertExecutor {
fn execute<Tx: MutDatastore>(&self, tx: &mut Tx, metrics: &mut ExecutionMetrics) -> Result<Vec<ProductValue>> {
let mut results = vec![];
for row in &self.rows {
if tx.insert_product_value(self.table_id, row)? {
metrics.rows_inserted += 1;
if let Some(returning) = &self.returning {
project_returning_row(returning, row.clone()).map(|res| results.push(res));
}
}
}
// TODO: It would be better to get this metric from the bsatn buffer.
// But we haven't been concerned with optimizing DML up to this point.
metrics.bytes_written += self.rows.iter().map(|row| row.size_of()).sum::<usize>();
Ok(results)
}
}
/// Executes row deletions
pub struct DeleteExecutor {
table_id: TableId,
filter: PipelinedProject,
returning: Option<ProjectListPlan>,
}
impl From<DeletePlan> for DeleteExecutor {
fn from(plan: DeletePlan) -> Self {
Self {
table_id: plan.table.table_id,
filter: plan.filter.into(),
returning: plan.returning,
}
}
}
impl DeleteExecutor {
fn execute<Tx: MutDatastore>(&self, tx: &mut Tx, metrics: &mut ExecutionMetrics) -> Result<Vec<ProductValue>> {
// TODO: Delete by row id instead of product value
let mut deletes = vec![];
self.filter.execute(tx, metrics, &mut |row| {
deletes.push(row.to_product_value());
Ok(())
})?;
// TODO: This metric should be updated inline when we serialize.
// Note, that we don't update bytes written,
// because deletes don't actually write out any bytes.
metrics.bytes_scanned += deletes.iter().map(|row| row.size_of()).sum::<usize>();
let mut results = vec![];
for row in &deletes {
if tx.delete_product_value(self.table_id, row)? {
metrics.rows_deleted += 1;
if let Some(returning) = &self.returning {
project_returning_row(returning, row.clone()).map(|res| results.push(res));
}
}
}
Ok(results)
}
}
/// Executes row updates
pub struct UpdateExecutor {
table_id: TableId,
columns: Vec<(ColId, AlgebraicValue)>,
filter: PipelinedProject,
returning: Option<ProjectListPlan>,
}
impl From<UpdatePlan> for UpdateExecutor {
fn from(plan: UpdatePlan) -> Self {
Self {
columns: plan.columns,
table_id: plan.table.table_id,
filter: plan.filter.into(),
returning: plan.returning,
}
}
}
impl UpdateExecutor {
fn execute<Tx: MutDatastore>(&self, tx: &mut Tx, metrics: &mut ExecutionMetrics) -> Result<Vec<ProductValue>> {
let mut deletes = vec![];
self.filter.execute(tx, metrics, &mut |row| {
deletes.push(row.to_product_value());
Ok(())
})?;
for row in &deletes {
tx.delete_product_value(self.table_id, row)?;
}
// TODO: This metric should be updated inline when we serialize.
metrics.bytes_scanned = deletes.iter().map(|row| row.size_of()).sum::<usize>();
metrics.rows_updated += deletes.len() as u64;
let mut results = vec![];
for row in &deletes {
let row = ProductValue::from_iter(
row
// Update the deleted rows with the new field values
.into_iter()
.cloned()
.enumerate()
.map(|(i, elem)| {
self.columns
.iter()
.find(|(col_id, _)| i == col_id.idx())
.map(|(_, value)| value.clone())
.unwrap_or_else(|| elem)
}),
);
tx.insert_product_value(self.table_id, &row)?;
metrics.bytes_written += row.size_of();
if let Some(returning) = &self.returning {
project_returning_row(returning, row).map(|res| results.push(res));
}
}
Ok(results)
}
}