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
// SPDX-License-Identifier: BUSL-1.1
//! Document secondary-index maintenance: BackfillIndex, DropIndex.
use tracing::debug;
use crate::bridge::envelope::Response;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::task::ExecutionTask;
/// Parameters for [`CoreLoop::execute_backfill_index`].
pub(in crate::data::executor) struct BackfillIndexParams<'a> {
pub tid: u64,
pub collection: &'a str,
pub path: &'a str,
pub is_array: bool,
pub unique: bool,
pub case_insensitive: bool,
pub predicate: Option<&'a str>,
}
impl CoreLoop {
/// Backfill an index: scan every document in the collection and
/// populate sparse-index entries for the given field. Atomic — one
/// write transaction covers the whole backfill and UNIQUE
/// violations abort it, leaving the index empty (the caller's
/// Building→Ready flip is skipped, so readers never see a
/// partial-index view).
pub(in crate::data::executor) fn execute_backfill_index(
&mut self,
task: &ExecutionTask,
params: BackfillIndexParams<'_>,
) -> Response {
let BackfillIndexParams {
tid,
collection,
path,
is_array,
unique,
case_insensitive,
predicate,
} = params;
debug!(
core = self.core_id,
%collection,
%path,
unique,
case_insensitive,
partial = predicate.is_some(),
"backfill index"
);
if let Some(ref m) = self.metrics {
m.record_document_index_backfill();
}
// Parse the partial-index predicate once, up front. An
// unparsable predicate is a catalog-level bug — the DDL layer
// already validates the text at CREATE INDEX time, so a
// failure here means the stored entry drifted from what the
// grammar accepts. Refuse the backfill rather than silently
// over-populating a "partial" index.
let parsed_predicate = match predicate {
Some(text) => match crate::engine::document::predicate::IndexPredicate::parse(text) {
Some(p) => Some(p),
None => {
return self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!(
"backfill: partial-index predicate failed to parse: {text}"
),
},
);
}
},
None => None,
};
// Snapshot existing documents outside the write txn. 1,000,000
// cap matches the Data Plane's other collection-wide scans; rows
// beyond this are handled by a future chunked backfill (see
// `scan_documents_chunked`).
let docs = match self.sparse.scan_documents(
task.request.database_id.as_u64(),
tid,
collection,
1_000_000,
) {
Ok(d) => d,
Err(e) => {
return self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("backfill scan: {e}"),
},
);
}
};
// Deduplicate-unique-as-we-go: track `(normalized_value → doc_id)`
// so a dup within the existing set is flagged before we ever
// touch the index table.
let mut seen: std::collections::HashMap<String, String> = std::collections::HashMap::new();
let txn = match self.sparse.begin_write() {
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("backfill txn: {e}"),
},
);
}
};
for (doc_id, bytes) in &docs {
let Some(doc) = super::super::super::doc_format::decode_document(bytes) else {
continue;
};
// Partial-index predicate: skip rows that don't satisfy
// the `WHERE` clause. `evaluate` treats NULL / non-bool as
// false (Postgres partial-index semantics), so only rows
// for which the predicate is explicitly true are indexed.
if let Some(ref p) = parsed_predicate
&& !p.evaluate_json(&doc)
{
continue;
}
let values = crate::engine::document::store::extract_index_values(&doc, path, is_array);
for raw in values {
let stored = if case_insensitive {
raw.to_lowercase()
} else {
raw
};
if unique
&& let Some(prev) = seen.get(&stored)
&& prev != doc_id
{
return self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!(
"unique index backfill: duplicate value '{stored}' on '{path}' \
(existing '{prev}', new '{doc_id}')"
),
},
);
}
if unique {
seen.insert(stored.clone(), doc_id.clone());
}
if let Err(e) = self.sparse.index_put_in_txn(
&txn,
crate::engine::sparse::btree_index::IndexEntryTxn {
database_id: task.request.database_id.as_u64(),
tenant_id: tid,
collection,
field: path,
value: &stored,
document_id: doc_id,
},
) {
return self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("backfill index_put: {e}"),
},
);
}
}
}
if let Err(e) = txn.commit() {
return self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("backfill commit: {e}"),
},
);
}
self.response_ok(task)
}
/// Drop all secondary index entries for a field across the entire collection.
///
/// Calls `SparseEngine::delete_index_entries_for_field` directly.
/// Returns `{"removed": N}` as the response payload.
pub(in crate::data::executor) fn execute_drop_document_index(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
field: &str,
) -> Response {
debug!(
core = self.core_id,
%collection,
%field,
"drop document index"
);
match self.sparse.delete_index_entries_for_field(
task.request.database_id.as_u64(),
tid,
collection,
field,
) {
Ok(removed) => {
match super::super::super::response_codec::encode_count("removed", removed) {
Ok(bytes) => self.response_with_payload(task, bytes),
Err(e) => self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: format!("drop index encode: {e}"),
},
),
}
}
Err(e) => self.response_error(
task,
crate::bridge::envelope::ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
}