-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlib.rs
More file actions
263 lines (229 loc) · 7.24 KB
/
lib.rs
File metadata and controls
263 lines (229 loc) · 7.24 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use std::{
hash::{Hash, Hasher},
ops::RangeBounds,
};
use anyhow::{anyhow, Result};
use iter::PlanIter;
use spacetimedb_lib::{
bsatn::{EncodeError, ToBsatn},
query::Delta,
sats::impl_serialize,
AlgebraicValue, ProductValue,
};
use spacetimedb_physical_plan::plan::{ProjectField, ProjectPlan, TupleField};
use spacetimedb_primitives::{IndexId, TableId};
use spacetimedb_table::{
blob_store::BlobStore,
static_assert_size,
table::{IndexScanPointIter, IndexScanRangeIter, RowRef, Table, TableScanIter},
};
pub mod dml;
pub mod iter;
pub mod pipelined;
/// The datastore interface required for building an executor
pub trait Datastore {
fn table(&self, table_id: TableId) -> Option<&Table>;
fn blob_store(&self) -> &dyn BlobStore;
fn table_or_err(&self, table_id: TableId) -> Result<&Table> {
self.table(table_id)
.ok_or_else(|| anyhow!("TableId `{table_id}` does not exist"))
}
fn table_scan(&self, table_id: TableId) -> Result<TableScanIter> {
self.table(table_id)
.map(|table| table.scan_rows(self.blob_store()))
.ok_or_else(|| anyhow!("TableId `{table_id}` does not exist"))
}
fn index_scan_point(
&self,
table_id: TableId,
index_id: IndexId,
key: &AlgebraicValue,
) -> Result<IndexScanPointIter> {
self.table(table_id)
.ok_or_else(|| anyhow!("TableId `{table_id}` does not exist"))
.and_then(|table| {
table
.get_index_by_id_with_table(self.blob_store(), index_id)
.map(|i| i.seek_point(key))
.ok_or_else(|| anyhow!("IndexId `{index_id}` does not exist"))
})
}
fn index_scan_range(
&self,
table_id: TableId,
index_id: IndexId,
range: &impl RangeBounds<AlgebraicValue>,
) -> Result<IndexScanRangeIter> {
self.table(table_id)
.ok_or_else(|| anyhow!("TableId `{table_id}` does not exist"))
.and_then(|table| {
table
.get_index_by_id_with_table(self.blob_store(), index_id)
.map(|i| i.seek_range(range))
.ok_or_else(|| anyhow!("IndexId `{index_id}` does not exist"))
})
}
}
pub trait DeltaStore {
fn num_inserts(&self, table_id: TableId) -> usize;
fn num_deletes(&self, table_id: TableId) -> usize;
fn has_inserts(&self, table_id: TableId) -> bool {
self.num_inserts(table_id) != 0
}
fn has_deletes(&self, table_id: TableId) -> bool {
self.num_deletes(table_id) != 0
}
fn inserts_for_table(&self, table_id: TableId) -> Option<std::slice::Iter<'_, ProductValue>>;
fn deletes_for_table(&self, table_id: TableId) -> Option<std::slice::Iter<'_, ProductValue>>;
fn index_scan_range_for_delta(
&self,
table_id: TableId,
index_id: IndexId,
delta: Delta,
range: impl RangeBounds<AlgebraicValue>,
) -> impl Iterator<Item = Row>;
fn index_scan_point_for_delta(
&self,
table_id: TableId,
index_id: IndexId,
delta: Delta,
point: &AlgebraicValue,
) -> impl Iterator<Item = Row>;
fn delta_scan(&self, table_id: TableId, inserts: bool) -> DeltaScanIter {
match inserts {
true => DeltaScanIter {
iter: self.inserts_for_table(table_id),
},
false => DeltaScanIter {
iter: self.deletes_for_table(table_id),
},
}
}
}
#[derive(Clone)]
pub enum Row<'a> {
Ptr(RowRef<'a>),
Ref(&'a ProductValue),
}
impl PartialEq for Row<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Ptr(x), Self::Ptr(y)) => x == y,
(Self::Ref(x), Self::Ref(y)) => x == y,
(Self::Ptr(x), Self::Ref(y)) => x == *y,
(Self::Ref(x), Self::Ptr(y)) => y == *x,
}
}
}
impl Eq for Row<'_> {}
impl Hash for Row<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Self::Ptr(x) => x.hash(state),
Self::Ref(x) => x.hash(state),
}
}
}
impl Row<'_> {
pub fn to_product_value(&self) -> ProductValue {
match self {
Self::Ptr(ptr) => ptr.to_product_value(),
Self::Ref(val) => (*val).clone(),
}
}
}
impl_serialize!(['a] Row<'a>, (self, ser) => match self {
Self::Ptr(row) => row.serialize(ser),
Self::Ref(row) => row.serialize(ser),
});
impl ToBsatn for Row<'_> {
fn static_bsatn_size(&self) -> Option<u16> {
match self {
Self::Ptr(ptr) => ptr.static_bsatn_size(),
Self::Ref(val) => val.static_bsatn_size(),
}
}
fn to_bsatn_extend(&self, buf: &mut Vec<u8>) -> std::result::Result<(), EncodeError> {
match self {
Self::Ptr(ptr) => ptr.to_bsatn_extend(buf),
Self::Ref(val) => val.to_bsatn_extend(buf),
}
}
fn to_bsatn_vec(&self) -> std::result::Result<Vec<u8>, EncodeError> {
match self {
Self::Ptr(ptr) => ptr.to_bsatn_vec(),
Self::Ref(val) => val.to_bsatn_vec(),
}
}
}
impl ProjectField for Row<'_> {
fn project(&self, field: &TupleField) -> AlgebraicValue {
match self {
Self::Ptr(ptr) => ptr.project(field),
Self::Ref(val) => val.project(field),
}
}
}
/// Each query operator returns a tuple of [RowRef]s
#[derive(Clone)]
pub enum Tuple<'a> {
/// A pointer to a row in a base table
Row(Row<'a>),
/// A temporary returned by a join operator
Join(Vec<Row<'a>>),
}
static_assert_size!(Tuple, 40);
impl ProjectField for Tuple<'_> {
fn project(&self, field: &TupleField) -> AlgebraicValue {
match self {
Self::Row(row) => row.project(field),
Self::Join(ptrs) => field
.label_pos
.and_then(|i| ptrs.get(i))
.map(|ptr| ptr.project(field))
.unwrap(),
}
}
}
impl<'a> Tuple<'a> {
/// Select the tuple element at position `i`
fn select(self, i: usize) -> Option<Row<'a>> {
match self {
Self::Row(_) => None,
Self::Join(mut ptrs) => Some(ptrs.swap_remove(i)),
}
}
/// Append a [Row] to a tuple
fn append(self, ptr: Row<'a>) -> Self {
match self {
Self::Row(row) => Self::Join(vec![row, ptr]),
Self::Join(mut rows) => {
rows.push(ptr);
Self::Join(rows)
}
}
}
fn join(self, with: Self) -> Self {
match with {
Self::Row(ptr) => self.append(ptr),
Self::Join(ptrs) => ptrs.into_iter().fold(self, |tup, ptr| tup.append(ptr)),
}
}
}
pub struct DeltaScanIter<'a> {
iter: Option<std::slice::Iter<'a, ProductValue>>,
}
impl<'a> Iterator for DeltaScanIter<'a> {
type Item = &'a ProductValue;
fn next(&mut self) -> Option<Self::Item> {
self.iter.as_mut().and_then(|iter| iter.next())
}
}
/// Execute a query plan.
/// The actual execution is driven by `f`.
pub fn execute_plan<T, R>(plan: &ProjectPlan, tx: &T, f: impl Fn(PlanIter) -> R) -> Result<R>
where
T: Datastore + DeltaStore,
{
PlanIter::build(plan, tx).map(f)
}