-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlib.rs
More file actions
334 lines (285 loc) · 9.32 KB
/
Copy pathlib.rs
File metadata and controls
334 lines (285 loc) · 9.32 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use anyhow::Result;
use core::hash::{Hash, Hasher};
use core::ops::RangeBounds;
use spacetimedb_lib::{identity::AuthCtx, query::Delta, AlgebraicType, Identity};
use spacetimedb_physical_plan::plan::{ParamResolver, ProjectField, TupleField};
use spacetimedb_primitives::{ColList, IndexId, TableId};
use spacetimedb_sats::bsatn::{BufReservedFill, EncodeError, ToBsatn};
use spacetimedb_sats::buffer::BufWriter;
use spacetimedb_sats::product_value::InvalidFieldError;
use spacetimedb_sats::{impl_serialize, AlgebraicValue, ProductValue};
use spacetimedb_sql_parser::ast::Parameter;
use spacetimedb_table::{static_assert_size, table::RowRef};
pub mod dml;
pub mod pipelined;
#[derive(Debug, Clone, Copy)]
pub struct ExecutionParams {
sender: Identity,
}
impl ExecutionParams {
pub fn from_sender(sender: Identity) -> Self {
Self { sender }
}
pub fn from_auth(auth: &AuthCtx) -> Self {
Self::from_sender(auth.caller())
}
}
impl ParamResolver for ExecutionParams {
fn resolve_param(&self, param: Parameter, ty: &AlgebraicType) -> AlgebraicValue {
match param {
Parameter::Sender if ty.is_identity() => self.sender.into(),
Parameter::Sender if ty.is_bytes() => AlgebraicValue::Bytes(self.sender.to_be_byte_array().into()),
Parameter::Sender => panic!("unsupported type for :sender: {ty:?}"),
}
}
}
pub trait Datastore {
/// Iterator type for table scans
type TableIter<'a>: Iterator<Item = RowRef<'a>> + 'a
where
Self: 'a;
/// Iterator type for ranged index scans.
type RangeIndexIter<'a>: Iterator<Item = RowRef<'a>> + 'a
where
Self: 'a;
/// Iterator type for point index scans.
type PointIndexIter<'a>: Iterator<Item = RowRef<'a>> + 'a
where
Self: 'a;
/// Returns the number of rows in this table
fn row_count(&self, table_id: TableId) -> u64;
/// Scans and returns all of the rows in a table
fn table_scan<'a>(&'a self, table_id: TableId) -> Result<Self::TableIter<'a>>;
/// Scans a range of keys from an index returning a [`RowRef`] iterator.
fn index_scan_range<'a>(
&'a self,
table_id: TableId,
index_id: IndexId,
range: &impl RangeBounds<AlgebraicValue>,
) -> Result<Self::RangeIndexIter<'a>>;
/// Scans a key from an index returning a [`RowRef`] iterator.
fn index_scan_point<'a>(
&'a self,
table_id: TableId,
index_id: IndexId,
point: &AlgebraicValue,
) -> Result<Self::PointIndexIter<'a>>;
}
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, Debug)]
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(),
}
}
pub fn project_product(self, cols: &ColList) -> Result<ProductValue, InvalidFieldError> {
match self {
Self::Ptr(ptr) => ptr.project_product(cols),
Self::Ref(val) => val.project_product(cols),
}
}
}
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 (impl BufWriter + BufReservedFill)) -> 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(),
}
}
}
#[derive(Clone, Debug)]
pub enum RelValue<'a> {
Row(Row<'a>),
Projection(ProductValue),
}
impl<'a> From<Row<'a>> for RelValue<'a> {
fn from(value: Row<'a>) -> Self {
Self::Row(value)
}
}
impl From<ProductValue> for RelValue<'_> {
fn from(value: ProductValue) -> Self {
Self::Projection(value)
}
}
impl PartialEq for RelValue<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Row(x), Self::Row(y)) => x == y,
(Self::Projection(x), Self::Projection(y)) => x == y,
(Self::Row(x), Self::Projection(y)) | (Self::Projection(y), Self::Row(x)) => x.to_product_value() == *y,
}
}
}
impl Eq for RelValue<'_> {}
impl Hash for RelValue<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Self::Row(x) => x.hash(state),
Self::Projection(x) => x.hash(state),
}
}
}
impl_serialize!(['a] RelValue<'a>, (self, ser) => match self {
Self::Row(row) => row.serialize(ser),
Self::Projection(row) => row.serialize(ser),
});
impl ToBsatn for RelValue<'_> {
fn static_bsatn_size(&self) -> Option<u16> {
match self {
Self::Row(row) => row.static_bsatn_size(),
Self::Projection(row) => row.static_bsatn_size(),
}
}
fn to_bsatn_extend(&self, buf: &mut (impl BufWriter + BufReservedFill)) -> std::result::Result<(), EncodeError> {
match self {
Self::Row(row) => row.to_bsatn_extend(buf),
Self::Projection(row) => row.to_bsatn_extend(buf),
}
}
fn to_bsatn_vec(&self) -> std::result::Result<Vec<u8>, EncodeError> {
match self {
Self::Row(row) => row.to_bsatn_vec(),
Self::Projection(row) => row.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())
}
}