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
// SPDX-License-Identifier: BUSL-1.1
//! Document write handlers: PointPut, BatchInsert, Upsert, Register.
//! Secondary-index lookup / fetch handlers live in `index_fetch`; index
//! backfill / drop handlers live in `index_maintenance`.
use tracing::{debug, warn};
use crate::bridge::envelope::{ErrorCode, Response, WriteSetEntry};
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::handlers::point::apply_put::PointPutParams;
use crate::data::executor::task::ExecutionTask;
use crate::engine::document::store::surrogate_to_doc_id;
impl CoreLoop {
pub(in crate::data::executor) fn execute_document_batch_insert(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
documents: &[(String, Vec<u8>)],
surrogates: &[nodedb_types::Surrogate],
) -> Response {
debug!(core = self.core_id, %collection, count = documents.len(), "document batch insert");
// When per-row surrogates are parallel to the documents, run the batch
// as ONE atomic, fully cross-engine-indexed insert: each row is applied
// via `apply_point_put` (document store + FTS + vector + spatial +
// secondary indexes) inside a single redb transaction, keyed by the
// row's stable surrogate. A search hit from any cross-engine index then
// resolves back to the row's identity, and the whole page lands or none
// of it does (any per-row error rolls the transaction back). The legacy
// raw `batch_put` path below is kept only for callers that do not supply
// parallel surrogates (no cross-engine identity available).
if !documents.is_empty() && surrogates.len() == documents.len() {
return self.execute_document_batch_insert_indexed(
task, tid, collection, documents, surrogates,
);
}
let converted: Vec<(String, Vec<u8>)> = documents
.iter()
.map(|(id, val)| {
(
id.clone(),
super::super::super::doc_format::canonicalize_document_for_storage(val),
)
})
.collect();
let refs: Vec<(&str, &[u8])> = converted
.iter()
.map(|(id, val)| (id.as_str(), val.as_slice()))
.collect();
// FTS indexing requires a valid Surrogate per document. When `surrogates`
// is parallel to `documents` (same length), each entry can be used. When
// the field is absent/mismatched (legacy callers), FTS indexing is skipped
// — surface this loudly so missing search results are diagnosable.
let fts_enabled = surrogates.len() == documents.len();
if !fts_enabled && !documents.is_empty() {
warn!(
core = self.core_id,
%collection,
doc_count = documents.len(),
surrogate_count = surrogates.len(),
"document batch insert without parallel surrogates: FTS indexing skipped"
);
}
match self
.sparse
.batch_put(task.request.database_id.as_u64(), tid, collection, &refs)
{
Ok(()) => {
// Auto-index text fields for full-text search (same as PointPut).
// Also extract secondary indexes for any registered collection config.
let config_key = (
task.request.database_id,
crate::types::TenantId::new(tid),
collection.to_string(),
);
let index_paths: Vec<crate::engine::document::store::IndexPath> = self
.doc_configs
.get(&config_key)
.map(|c| c.index_paths.clone())
.unwrap_or_default();
for (i, (doc_id, val)) in documents.iter().enumerate() {
if let Some(doc) = super::super::super::doc_format::decode_document(val) {
// Full-text inverted index (includes nested block content).
// Only index when a valid Surrogate is available.
if fts_enabled {
let surrogate = surrogates[i];
// Surrogate::ZERO is the "unassigned" sentinel — the
// upstream allocator hasn't assigned a real id, so we
// must not write it into the FTS index.
if surrogate != nodedb_types::Surrogate::ZERO {
let text_content =
super::text_extract::extract_indexable_text(&doc);
if !text_content.is_empty() {
let _ = self.inverted.index_document(
task.request.database_id.as_u64(),
crate::types::TenantId::new(tid),
collection,
surrogate,
&text_content,
);
}
}
}
// Secondary index extraction (insert-only path: no prior
// document, so the diff is pure adds; tuples unused).
let _ = self.apply_secondary_indexes(
crate::data::executor::core_loop::maintenance::SecondaryIndexInputs {
database_id: task.request.database_id.as_u64(),
tid,
collection,
old_doc: None,
new_doc: &doc,
doc_id,
index_paths: &index_paths,
},
);
}
}
if let Some(ref m) = self.metrics {
m.record_document_insert();
}
match super::super::super::response_codec::encode_count("inserted", documents.len())
{
Ok(bytes) => self.response_with_payload(task, bytes),
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
/// Atomic, fully-indexed batch insert (surrogates parallel to documents).
///
/// Applies every row through [`CoreLoop::apply_point_put`] under ONE redb
/// write transaction so the document store, FTS inverted index, HNSW vector
/// index, spatial R-tree, and secondary indexes are all maintained and keyed
/// by each row's stable surrogate. Any per-row error (including a UNIQUE
/// constraint violation) drops the transaction, leaving the whole page
/// unchanged. On success the transaction commits once and one Insert write
/// event is emitted per row.
fn execute_document_batch_insert_indexed(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
documents: &[(String, Vec<u8>)],
surrogates: &[nodedb_types::Surrogate],
) -> Response {
let database_id = task.request.database_id.as_u64();
let txn = match self.sparse.begin_write() {
Ok(t) => t,
Err(e) => return self.response_error(task, e),
};
// Gate post-apply write-set accumulation once for the whole batch so a
// collection with no vector/sparse field pays nothing. Each row's
// `apply_point_put` above reconciles storage + the btree/FTS/graph/HNSW/
// sparse overlays, but `wal_append_document_op` mints no redo for
// `BatchInsert` (row durability is redb-synchronous). On a WAL-only
// restart the HNSW and sparse indexes are rebuilt only from redo `Put`
// records, so a vector- or sparse-indexed batch insert that journals
// nothing would lose its rows' index entries. Carrying the surrogate +
// post-image back per row lets the Control Plane mint a durable `Put`
// redo for each (see `plan_post_apply_redo` / `append_write_set_redo`).
let has_vectors = self.collection_has_vectors(database_id, tid, collection)
|| self.collection_has_sparse(database_id, tid, collection);
// Row key for post-commit event emission, captured as each row applies
// successfully; the value bytes are re-borrowed from `documents` after
// commit rather than cloned here. On any error we return early
// (dropping `txn`, which rolls back every row applied so far).
let mut applied: Vec<String> = Vec::with_capacity(documents.len());
let mut write_set: Vec<WriteSetEntry> = Vec::new();
// Per-row secondary-index tuples (added ∪ removed ∪ bitemporal),
// parallel to `applied`. Recorded into the per-index write-value
// substrate only after `txn.commit()` succeeds below — a row that
// never commits touched no durable index state.
let mut row_index_tuples: Vec<Vec<(String, String)>> = Vec::with_capacity(documents.len());
for (i, (_document_id, value)) in documents.iter().enumerate() {
let surrogate = surrogates[i];
let row_key = surrogate_to_doc_id(surrogate);
let outcome = match self.apply_point_put(
&txn,
PointPutParams {
database_id,
tid,
collection,
document_id: &row_key,
surrogate,
value,
index_text: true,
user_roles: &task.request.user_roles,
enforce: true,
wal_lsn: task.wal_lsn(),
},
) {
Ok(o) => o,
Err(e) => return self.response_error(task, e),
};
if has_vectors {
write_set.push(WriteSetEntry {
surrogate: surrogate.as_u32(),
is_delete: false,
value: value.clone(),
});
}
if task.wal_lsn().is_some() {
let mut tuples = outcome.secondary_index_added;
tuples.extend(outcome.secondary_index_removed);
tuples.extend(outcome.bitemporal_index_tuples);
row_index_tuples.push(tuples);
}
applied.push(row_key);
}
if let Err(e) = txn.commit() {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("batch insert commit: {e}"),
},
);
}
// Record each committed row's touched secondary-index values into the
// per-index write-value substrate, now that the batch has durably
// committed.
if let Some(lsn) = task.wal_lsn() {
for tuples in &row_index_tuples {
self.note_index_write_values(
task.request.database_id,
crate::types::TenantId::new(tid),
collection,
tuples,
lsn,
);
}
}
self.checkpoint_coordinator
.mark_dirty("sparse", documents.len());
if let Some(ref m) = self.metrics {
m.record_document_insert();
}
for (i, row_key) in applied.iter().enumerate() {
self.emit_put_event(task, tid, collection, row_key, &documents[i].1, None);
}
let mut response =
match super::super::super::response_codec::encode_count("inserted", documents.len()) {
Ok(bytes) => self.response_with_payload(task, bytes),
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
if !write_set.is_empty() {
response.write_set = write_set;
}
response
}
}
/// Parameters for [`CoreLoop::execute_register_document_collection`].
pub(in crate::data::executor) struct RegisterDocumentCollectionParams<'a> {
pub tid: u64,
pub collection: &'a str,
pub indexes: &'a [nodedb_physical::physical_plan::RegisteredIndex],
pub crdt_enabled: bool,
pub storage_mode: &'a nodedb_physical::physical_plan::StorageMode,
pub enforcement: &'a nodedb_physical::physical_plan::EnforcementOptions,
pub bitemporal: bool,
/// Durable CRDT conflict-resolution policy (JSON-serialized
/// `CollectionPolicy`), persisted on the collection's catalog record.
/// `Some` rehydrates this core's `PolicyRegistry` so the policy survives
/// register/reboot instead of falling back to `CollectionPolicy::ephemeral()`.
pub conflict_policy: Option<&'a str>,
}
impl CoreLoop {
/// Register a document collection's secondary index configuration.
///
/// Stores the `CollectionConfig` in `self.doc_configs` so that subsequent
/// `PointPut` and `DocumentBatchInsert` operations extract and write secondary
/// index entries automatically.
pub(in crate::data::executor) fn execute_register_document_collection(
&mut self,
task: &ExecutionTask,
params: RegisterDocumentCollectionParams<'_>,
) -> Response {
let RegisterDocumentCollectionParams {
tid,
collection,
indexes,
crdt_enabled,
storage_mode,
enforcement,
bitemporal,
conflict_policy,
} = params;
let mode_label = match storage_mode {
nodedb_physical::physical_plan::StorageMode::Schemaless => "document_schemaless",
nodedb_physical::physical_plan::StorageMode::Strict { .. } => "document_strict",
};
debug!(
core = self.core_id,
%collection,
index_count = indexes.len(),
crdt_enabled,
storage_mode = mode_label,
append_only = enforcement.append_only,
hash_chain = enforcement.hash_chain,
balanced = enforcement.balanced.is_some(),
"register document collection"
);
let mut config = crate::engine::document::store::CollectionConfig::new(collection);
config.crdt_enabled = crdt_enabled;
config.storage_mode = storage_mode.clone();
config.enforcement = enforcement.clone();
config.bitemporal = bitemporal;
config.conflict_policy = conflict_policy.map(str::to_string);
config.index_paths = indexes
.iter()
.map(crate::engine::document::store::IndexPath::from_registered)
.collect();
let config_key = (
task.request.database_id,
crate::types::TenantId::new(tid),
collection.to_string(),
);
self.doc_configs.insert(config_key, config);
// Rehydrate the durable CRDT conflict-resolution policy (if any) into
// this core's `PolicyRegistry`. Runs on every `Register` — live DDL
// apply AND boot rehydration replay — so `ALTER COLLECTION ... SET ON
// CONFLICT ...` survives a restart instead of silently reverting to
// `CollectionPolicy::ephemeral()`.
if let Some(policy_json) = conflict_policy {
match self.get_crdt_engine(task.request.database_id, crate::types::TenantId::new(tid)) {
Ok(engine) => {
if let Err(e) = engine.set_collection_policy(collection, policy_json) {
warn!(
core = self.core_id,
%collection,
error = %e,
"failed to rehydrate persisted conflict policy on register"
);
}
}
Err(e) => {
warn!(
core = self.core_id,
%collection,
error = %e,
"failed to create CRDT engine for conflict policy rehydration"
);
}
}
}
self.response_ok(task)
}
}