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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// SPDX-License-Identifier: BUSL-1.1
//! `ArrayOp::Slice` and `ArrayOp::Project` handlers.
//!
//! Both are read-only fan-outs over the engine's tile scan. Slice
//! prunes by per-dim coord ranges and (optionally) projects an attribute
//! subset; Project is a pure attribute projection over every cell.
//!
//! Decoded slice payloads ride as zerompk bytes — matching the
//! contract documented on `ArrayOp::Slice::slice_msgpack`.
use nodedb_array::query::project::{Projection, project_sparse};
use nodedb_array::query::slice::{Slice, slice_sparse, tile_overlaps_slice};
use nodedb_array::segment::{MbrQueryPredicate, TilePayload};
use nodedb_array::tile::sparse_tile::SparseTile;
use nodedb_array::types::ArrayId;
use nodedb_types::{ArrayCell, SurrogateBitmap, SystemTimeScope, Value};
/// Slice parameters bundled to avoid exceeding the 7-argument limit.
pub(in crate::data::executor) struct SliceParams<'a> {
pub array_id: &'a ArrayId,
pub slice_msgpack: &'a [u8],
pub attr_projection: &'a [u32],
pub limit: u32,
pub cell_filter: Option<&'a SurrogateBitmap>,
/// Optional Hilbert-prefix range `[lo, hi]` for shard-level partitioning.
/// When set, only tiles whose Hilbert prefix falls within this range are
/// included. Used by the distributed shard handler to prevent duplicate
/// rows when all vShards share a single Data Plane.
pub hilbert_range: Option<(u64, u64)>,
/// Bitemporal system-time scope.
///
/// - `Current`: live read (effective cutoff `i64::MAX`).
/// - `AsOf(t)`: point-in-time snapshot at `t`.
/// - `AllVersions`: audit-log — every live cell-version, sorted ascending
/// by `system_from_ms`, limit bounds total versions.
pub system_time: SystemTimeScope,
/// Bitemporal valid-time point. `None` = no valid-time filter.
pub valid_at_ms: Option<i64>,
}
use crate::bridge::envelope::{ErrorCode, Response};
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::task::ExecutionTask;
use super::convert::{cell_value_to_value, coord_value_to_value, sparse_tile_to_array_cells};
use super::encode::encode_value_rows;
impl CoreLoop {
pub(in crate::data::executor) fn dispatch_array_slice(
&mut self,
task: &ExecutionTask,
p: SliceParams<'_>,
) -> Response {
let SliceParams {
array_id,
slice_msgpack,
attr_projection,
limit,
cell_filter,
hilbert_range,
system_time,
valid_at_ms,
} = p;
if let Err(resp) = self.ensure_array_open(task, array_id) {
return resp;
}
let slice: Slice = match zerompk::from_msgpack(slice_msgpack) {
Ok(s) => s,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array slice decode: {e}"),
},
);
}
};
let schema = match self.array_engine.store(array_id) {
Ok(store) => store.schema().clone(),
Err(e) => {
return self.response_error(
task,
ErrorCode::Unsupported {
detail: format!("array '{}' not open: {e}", array_id.name),
},
);
}
};
let proj = if attr_projection.is_empty() {
None
} else {
Some(Projection::new(
attr_projection.iter().map(|&i| i as usize).collect(),
))
};
let cap = limit as usize;
match system_time {
SystemTimeScope::AllVersions => {
// Audit-log path: emit one row per live cell-version.
let store = match self.array_engine.store(array_id) {
Ok(s) => s,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array '{}' not open: {e}", array_id.name),
},
);
}
};
let mut versions = match store.scan_tiles_all_versions(valid_at_ms) {
Ok(v) => v,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array audit-log scan: {e}"),
},
);
}
};
// Apply Hilbert-range and slice coord filters.
versions.retain(|(hp, coord, _sys_ms, _payload)| {
if let Some((lo, hi)) = hilbert_range
&& (*hp < lo || *hp > hi)
{
return false;
}
// Check coord against slice predicate dim-by-dim.
slice_coord_matches(coord, &slice)
});
// Sort ascending by system_from_ms, ties by coord lexicographic order.
versions.sort_by(|(_, coord_a, sys_a, _), (_, coord_b, sys_b, _)| {
sys_a.cmp(sys_b).then_with(|| {
coord_a
.partial_cmp(coord_b)
.unwrap_or(std::cmp::Ordering::Equal)
})
});
// Collect rows.
let mut rows: Vec<Value> = Vec::new();
'av_outer: for (_, coord, sys_ms, payload) in versions {
// Apply cell_filter via surrogate.
if let Some(f) = cell_filter
&& !f.contains(payload.surrogate)
{
continue;
}
// Build coords from CoordValue.
let coords: Vec<Value> = coord.iter().map(coord_value_to_value).collect();
// Build attrs from payload, applying projection.
let all_attrs: Vec<Value> =
payload.attrs.iter().map(cell_value_to_value).collect();
let attrs = if let Some(p) = proj.as_ref() {
p.attr_indices
.iter()
.map(|&i| all_attrs.get(i).cloned().unwrap_or(Value::Null))
.collect()
} else {
all_attrs
};
rows.push(Value::ArrayCell(ArrayCell {
coords,
attrs,
system_time: Some(sys_ms),
}));
if cap > 0 && rows.len() >= cap {
break 'av_outer;
}
}
encode_slice_rows(self, task, rows, false)
}
SystemTimeScope::Current | SystemTimeScope::AsOf(_) => {
// Point-in-time / live path — use existing Ceiling resolver.
let cutoff = match system_time {
SystemTimeScope::AsOf(t) => t,
SystemTimeScope::Current => i64::MAX,
SystemTimeScope::AllVersions => unreachable!(),
};
let store = match self.array_engine.store(array_id) {
Ok(s) => s,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array '{}' not open: {e}", array_id.name),
},
);
}
};
let (resolved_tiles, truncated_before_horizon) =
match store.scan_tiles_at(cutoff, valid_at_ms) {
Ok(r) => r,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array bitemporal scan: {e}"),
},
);
}
};
let mut rows: Vec<Value> = Vec::new();
'outer: for (hp, sparse) in resolved_tiles {
if let Some((lo, hi)) = hilbert_range
&& (hp < lo || hp > hi)
{
continue;
}
if !tile_overlaps_slice(&sparse.mbr.dim_mins, &sparse.mbr.dim_maxs, &slice) {
continue;
}
let filtered = match slice_sparse(&schema, &sparse, &slice) {
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array slice filter: {e}"),
},
);
}
};
let final_tile = match proj.as_ref() {
Some(p) => match project_sparse(&filtered, p) {
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array slice project: {e}"),
},
);
}
},
None => filtered,
};
for (row_idx, cell) in sparse_tile_to_array_cells(&schema, &final_tile)
.into_iter()
.enumerate()
{
if let Some(f) = cell_filter {
let sur = final_tile
.surrogates
.get(row_idx)
.copied()
.unwrap_or(nodedb_types::Surrogate::ZERO);
if !f.contains(sur) {
continue;
}
}
rows.push(Value::ArrayCell(cell));
if cap > 0 && rows.len() >= cap {
break 'outer;
}
}
}
encode_slice_rows(self, task, rows, truncated_before_horizon)
}
}
}
}
/// Check whether a coordinate tuple matches a slice predicate.
///
/// Returns `true` if for every constrained dimension the coord value falls
/// within `[lo, hi]`. Unconstrained dims (None) are always included.
fn slice_coord_matches(
coord: &[nodedb_array::types::coord::value::CoordValue],
slice: &Slice,
) -> bool {
for (i, dr_opt) in slice.dim_ranges.iter().enumerate() {
let Some(dr) = dr_opt else { continue };
let Some(cv) = coord.get(i) else { return false };
// Compare coord value against [lo, hi] domain bounds.
if !coord_in_dim_range(cv, dr) {
return false;
}
}
true
}
fn coord_in_dim_range(
cv: &nodedb_array::types::coord::value::CoordValue,
dr: &nodedb_array::query::slice::DimRange,
) -> bool {
// Convert DomainBound to CoordValue for comparison.
let lo_cv = domain_bound_to_coord_value(&dr.lo);
let hi_cv = domain_bound_to_coord_value(&dr.hi);
let (Some(lo), Some(hi)) = (lo_cv, hi_cv) else {
return true;
};
cv >= &lo && cv <= &hi
}
fn domain_bound_to_coord_value(
b: &nodedb_array::types::domain::DomainBound,
) -> Option<nodedb_array::types::coord::value::CoordValue> {
use nodedb_array::types::coord::value::CoordValue;
use nodedb_array::types::domain::DomainBound;
match b {
DomainBound::Int64(v) => Some(CoordValue::Int64(*v)),
DomainBound::Float64(v) => Some(CoordValue::Float64(*v)),
DomainBound::TimestampMs(v) => Some(CoordValue::TimestampMs(*v)),
DomainBound::String(v) => Some(CoordValue::String(v.clone())),
}
}
/// Encode the slice result rows into an `ArraySliceResponse` and return a `Response`.
fn encode_slice_rows(
core: &mut crate::data::executor::core_loop::CoreLoop,
task: &crate::data::executor::task::ExecutionTask,
rows: Vec<Value>,
truncated_before_horizon: bool,
) -> crate::bridge::envelope::Response {
let rows_msgpack = {
let mut buf: Vec<u8> = Vec::with_capacity(rows.len() * 64);
let n = rows.len();
if n < 16 {
buf.push(0x90 | n as u8);
} else if n <= u16::MAX as usize {
buf.push(0xDC);
buf.extend_from_slice(&(n as u16).to_be_bytes());
} else {
buf.push(0xDD);
buf.extend_from_slice(&(n as u32).to_be_bytes());
}
for row in &rows {
match nodedb_types::value_to_msgpack(row) {
Ok(b) => buf.extend_from_slice(&b),
Err(e) => {
return core.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("array response encode: {e}"),
},
);
}
}
}
buf
};
let resp = crate::data::executor::response_codec::ArraySliceResponse {
rows_msgpack,
truncated_before_horizon,
};
match zerompk::to_msgpack_vec(&resp) {
Ok(bytes) => core.response_with_payload(task, bytes),
Err(e) => core.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("array slice response encode: {e}"),
},
),
}
}
impl CoreLoop {
pub(in crate::data::executor) fn dispatch_array_project(
&mut self,
task: &ExecutionTask,
array_id: &ArrayId,
attr_indices: &[u32],
) -> Response {
if let Err(resp) = self.ensure_array_open(task, array_id) {
return resp;
}
let schema = match self.array_engine.store(array_id) {
Ok(store) => store.schema().clone(),
Err(e) => {
return self.response_error(
task,
ErrorCode::Unsupported {
detail: format!("array '{}' not open: {e}", array_id.name),
},
);
}
};
let tiles = match self
.array_engine
.scan_tiles(array_id, &MbrQueryPredicate::default())
{
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array project scan: {e}"),
},
);
}
};
let proj = Projection::new(attr_indices.iter().map(|&i| i as usize).collect());
let mut rows: Vec<Value> = Vec::new();
for tile in tiles {
let sparse: SparseTile = match tile {
TilePayload::Sparse(s) => s,
TilePayload::Dense(_) => {
return self.response_error(
task,
ErrorCode::Unsupported {
detail: "dense tile payload in project".to_string(),
},
);
}
};
let projected = match project_sparse(&sparse, &proj) {
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("array project: {e}"),
},
);
}
};
for cell in sparse_tile_to_array_cells(&schema, &projected) {
rows.push(Value::ArrayCell(cell));
}
}
encode_value_rows(self, task, &rows)
}
}