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
// SPDX-License-Identifier: BUSL-1.1
//! Records the per-core last-write-LSN version for every key a committed
//! transaction batch wrote.
//!
//! This is the transaction-batch funnel for the version index: it runs once,
//! after the batch has committed, over the buffered sub-plans — covering both
//! the single-shard fast-path commit and every Calvin apply (both delegate to
//! `execute_transaction_batch`). One WAL LSN (the batch's single
//! `append_transaction` LSN, threaded onto the task) applies to every key in
//! the batch.
use crate::bridge::envelope::PhysicalPlan;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::core_loop::write_index::KeyRepr;
use crate::data::executor::task::ExecutionTask;
use crate::types::{Lsn, TenantId};
use nodedb_physical::physical_plan::{
ColumnarOp, CrdtOp, DocumentOp, GraphOp, KvOp, SpatialOp, TextOp, TimeseriesOp, VectorOp,
};
use nodedb_types::Surrogate;
impl CoreLoop {
/// Record the version of every key written by a committed transaction batch.
///
/// No-op when the task carries no WAL LSN (the version is not advanced with
/// a wrong value). Per-key engines record `KeyRepr`; engines whose per-key
/// identity is internal (columnar / timeseries / spatial / FTS) record only
/// the collection floor.
pub(in crate::data::executor) fn record_batch_write_versions(
&mut self,
task: &ExecutionTask,
tid: u64,
plans: &[PhysicalPlan],
) {
let Some(lsn) = task.wal_lsn() else {
return;
};
let db = task.request.database_id;
let tenant = TenantId::new(tid);
for plan in plans {
self.record_plan_write_version(db, tenant, plan, lsn);
}
}
fn record_plan_write_version(
&mut self,
db: crate::types::DatabaseId,
tenant: TenantId,
plan: &PhysicalPlan,
lsn: Lsn,
) {
match plan {
PhysicalPlan::Document(op) => self.record_document_version(db, tenant, op, lsn),
PhysicalPlan::Vector(op) => self.record_vector_version(db, tenant, op, lsn),
PhysicalPlan::Graph(op) => self.record_graph_version(db, tenant, op, lsn),
PhysicalPlan::Kv(op) => self.record_kv_version(db, tenant, op, lsn),
// Collection-floor engines: per-key identity is engine-internal.
PhysicalPlan::Columnar(op) => {
let coll = match op {
ColumnarOp::Insert { collection, .. }
| ColumnarOp::Update { collection, .. }
| ColumnarOp::Delete { collection, .. } => Some(collection.as_str()),
_ => None,
};
if let Some(c) = coll {
self.note_write_lsn(db, tenant, c, None, lsn);
}
}
PhysicalPlan::Timeseries(TimeseriesOp::Ingest { collection, .. }) => {
self.note_write_lsn(db, tenant, collection, None, lsn);
}
PhysicalPlan::Spatial(op) => {
let coll = match op {
SpatialOp::Insert { collection, .. } | SpatialOp::Delete { collection, .. } => {
Some(collection.as_str())
}
_ => None,
};
if let Some(c) = coll {
self.note_write_lsn(db, tenant, c, None, lsn);
}
}
PhysicalPlan::Text(op) => {
let coll = match op {
TextOp::FtsIndexDoc { collection, .. }
| TextOp::FtsDeleteDoc { collection, .. } => Some(collection.as_str()),
_ => None,
};
if let Some(c) = coll {
self.note_write_lsn(db, tenant, c, None, lsn);
}
}
PhysicalPlan::Crdt(CrdtOp::Apply { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::DocUpsert { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::DocDelete { collection, .. }) => {
self.note_write_lsn(db, tenant, collection, None, lsn);
}
// No per-key/collection version recorded for reads, control ops, or
// engines not (yet) part of the funnel (array is keyed by tile).
PhysicalPlan::Timeseries(_)
| PhysicalPlan::Crdt(_)
| PhysicalPlan::Query(_)
| PhysicalPlan::Meta(_)
| PhysicalPlan::Array(_)
| PhysicalPlan::ClusterArray(_) => {}
}
}
fn record_document_version(
&mut self,
db: crate::types::DatabaseId,
tenant: TenantId,
op: &DocumentOp,
lsn: Lsn,
) {
let (collection, surrogate) = match op {
DocumentOp::PointPut {
collection,
surrogate,
..
}
| DocumentOp::PointInsert {
collection,
surrogate,
..
}
| DocumentOp::PointDelete {
collection,
surrogate,
..
} => (collection.as_str(), *surrogate),
_ => return,
};
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::Surrogate(surrogate.as_u32())),
lsn,
);
}
/// Records the version of a vector-engine write within a committed
/// transaction batch.
///
/// Mirrors the fork resolution the live write handlers apply (see
/// `handlers/vector*.rs`): surrogate-carrying ops record `KeyRepr::Surrogate`
/// per surrogate (a superset of the collection floor, since vector SEARCH
/// reads always record `ReadKey::Predicate`, the collection floor); sparse
/// ops (keyed by `doc_id: String`, no cross-engine surrogate) and
/// `Delete` (whose `vector_id` is the internal HNSW node id, NOT the
/// surrogate — recording it as `KeyRepr::Surrogate` would be a wrong
/// identity) record the collection floor only. Read/config/query ops
/// write nothing and are named explicitly below so the match stays
/// exhaustive.
fn record_vector_version(
&mut self,
db: crate::types::DatabaseId,
tenant: TenantId,
op: &VectorOp,
lsn: Lsn,
) {
match op {
VectorOp::Insert {
collection,
surrogate,
..
}
| VectorOp::DirectUpsert {
collection,
surrogate,
..
}
| VectorOp::DeleteBySurrogate {
collection,
surrogate,
..
} => {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::Surrogate(surrogate.as_u32())),
lsn,
);
}
VectorOp::MultiVectorInsert {
collection,
document_surrogate,
..
}
| VectorOp::MultiVectorDelete {
collection,
document_surrogate,
..
} => {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::Surrogate(document_surrogate.as_u32())),
lsn,
);
}
VectorOp::BatchInsert {
collection,
surrogates,
..
} => {
let mut any_surrogate_recorded = false;
for s in surrogates {
if *s != Surrogate::ZERO {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::Surrogate(s.as_u32())),
lsn,
);
any_surrogate_recorded = true;
}
}
if !any_surrogate_recorded {
self.note_write_lsn(db, tenant, collection, None, lsn);
}
}
// Sparse (doc_id-keyed, no surrogate) and `Delete` (vector_id is
// the internal HNSW node id, not the surrogate): collection floor
// only.
VectorOp::SparseInsert { collection, .. }
| VectorOp::SparseDelete { collection, .. }
| VectorOp::Delete { collection, .. } => {
self.note_write_lsn(db, tenant, collection, None, lsn);
}
// Read / config / query ops: nothing written, no version to record.
VectorOp::Search { .. }
| VectorOp::MultiSearch { .. }
| VectorOp::SetParams { .. }
| VectorOp::QueryStats { .. }
| VectorOp::Seal { .. }
| VectorOp::CompactIndex { .. }
| VectorOp::Rebuild { .. }
| VectorOp::SparseSearch { .. }
| VectorOp::MultiVectorScoreSearch { .. } => {}
}
}
fn record_graph_version(
&mut self,
db: crate::types::DatabaseId,
tenant: TenantId,
op: &GraphOp,
lsn: Lsn,
) {
match op {
GraphOp::EdgePut {
collection,
src_id,
label,
dst_id,
..
}
| GraphOp::EdgeDelete {
collection,
src_id,
label,
dst_id,
..
} => {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::Edge {
src: Box::from(src_id.as_str()),
label: Box::from(label.as_str()),
dst: Box::from(dst_id.as_str()),
}),
lsn,
);
}
GraphOp::EdgePutBatch { edges } | GraphOp::EdgeDeleteBatch { edges } => {
for edge in edges {
self.note_write_lsn(
db,
tenant,
&edge.collection,
Some(KeyRepr::Edge {
src: Box::from(edge.src_id.as_str()),
label: Box::from(edge.label.as_str()),
dst: Box::from(edge.dst_id.as_str()),
}),
lsn,
);
}
}
_ => {}
}
}
fn record_kv_version(
&mut self,
db: crate::types::DatabaseId,
tenant: TenantId,
op: &KvOp,
lsn: Lsn,
) {
match op {
KvOp::Put {
collection, key, ..
}
| KvOp::Insert {
collection, key, ..
}
| KvOp::InsertIfAbsent {
collection, key, ..
}
| KvOp::InsertOnConflictUpdate {
collection, key, ..
}
| KvOp::Expire {
collection, key, ..
}
| KvOp::Persist { collection, key }
| KvOp::FieldSet {
collection, key, ..
}
| KvOp::Incr {
collection, key, ..
}
| KvOp::IncrFloat {
collection, key, ..
}
| KvOp::Cas {
collection, key, ..
}
| KvOp::GetSet {
collection, key, ..
} => {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::KvKey(Box::from(key.as_slice()))),
lsn,
);
}
KvOp::Delete { collection, keys } => {
for key in keys {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::KvKey(Box::from(key.as_slice()))),
lsn,
);
}
}
KvOp::BatchPut {
collection,
entries,
..
} => {
for (key, _value) in entries {
self.note_write_lsn(
db,
tenant,
collection,
Some(KeyRepr::KvKey(Box::from(key.as_slice()))),
lsn,
);
}
}
KvOp::Truncate { collection } => {
// Whole-collection mutation: record the collection floor only.
self.note_write_lsn(db, tenant, collection, None, lsn);
}
_ => {}
}
}
}