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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::Database;
use crate::search::{
run_scheduled_search_projection_catch_up, run_search_projection_catch_up,
validate_search_projection_catch_up_request, SearchIndex,
};
use crate::{
DatabaseReadTransaction, HawDBError, Result, SearchProjectionChangeBatch,
SearchProjectionRelationalDelta,
};
pub use hawdb_search::{
ScheduledSearchProjectionCatchUpReport, SearchProjectionCatchUpReport,
SearchProjectionCatchUpStopReason,
};
impl Database {
/// Catches a persistent search projection up through whole graph
/// changefeed commits, at most `max_batches` batches of
/// `max_operations_per_batch` changefeed operations each.
///
/// Every batch must reach the pinned source head; see
/// [`Self::catch_up_search_projection_with_batch_hydrator`] for the typed
/// [`HawDBError::SearchProjectionPrefixBehindSource`] and
/// [`HawDBError::SearchProjectionBatchBudgetExceeded`] refusals and the
/// recommended host reaction.
pub fn catch_up_search_projection(
&self,
search_index: &mut SearchIndex,
max_operations_per_batch: usize,
max_batches: usize,
) -> Result<SearchProjectionCatchUpReport> {
self.catch_up_search_projection_with_batch_applier(
search_index,
max_operations_per_batch,
max_batches,
|database, search_index, max_operations| {
let Some(request) = database
.build_search_projection_graph_delta_request_from_freshness(
search_index,
Some(max_operations),
)?
else {
return Ok(None);
};
let report = database.apply_search_projection_graph_delta(search_index, request)?;
Ok(Some(report.operation_count))
},
)
}
/// Catches a persistent search projection up through whole unified
/// graph-and-relational changefeed commits.
///
/// The hydrator receives the exact selected batch and one pinned database
/// read transaction. It must account for every relational primary key in
/// the batch. HawDB validates that count before applying either the graph
/// or relational delta, then checkpoints the combined projection before
/// selecting another batch. Hydration errors and incomplete accounting do
/// not advance the projection watermark. A prefix ending before the pinned
/// snapshot epoch is rejected before the hydrator runs with the typed
/// [`HawDBError::SearchProjectionPrefixBehindSource`]; the selected interval
/// must cover the complete source head within the caller's existing budgets.
/// See [`Self::catch_up_search_projection_with_batch_hydrator`] for progress
/// limitations and retry options under ongoing writes.
pub fn catch_up_search_projection_with_relational<F>(
&self,
search_index: &mut SearchIndex,
max_operations_per_batch: usize,
max_batches: usize,
mut relational_hydrator: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(
&mut DatabaseReadTransaction,
&SearchProjectionChangeBatch,
) -> Result<SearchProjectionRelationalDelta>,
{
self.catch_up_search_projection_with_batch_hydrator(
search_index,
max_operations_per_batch,
max_operations_per_batch,
max_batches,
|snapshot, batch| {
if batch.has_relational_changes() {
relational_hydrator(snapshot, batch)
} else {
Ok(SearchProjectionRelationalDelta::default())
}
},
)
}
/// Catches a persistent search projection up through whole unified
/// graph-and-relational changefeed commits while allowing the host to
/// derive additional projection work from every selected batch.
///
/// Unlike [`Self::catch_up_search_projection_with_relational`], the
/// hydrator runs for graph-only batches. This supports bounded host-owned
/// dependency fan-out without moving application projection semantics into
/// HawDB. The hydrator must still account for every relational primary key
/// in the batch. `max_change_operations_per_batch` bounds changefeed
/// selection, while `max_projection_operations_per_batch` independently
/// bounds the combined graph and host-derived projection delta. Hydration
/// errors, output-budget overflow, and incomplete accounting do not advance
/// the projection watermark. A prefix ending before the pinned source epoch
/// is rejected before invoking the host. Partial-prefix progress requires the
/// separately qualified bounded preparation protocol tracked by #1043.
///
/// A prefix refusal is the typed
/// [`HawDBError::SearchProjectionPrefixBehindSource`] (#1924). It is not
/// transient: while the pending interval exceeds one batch, retrying with
/// the same budgets refuses again, even with no concurrent writes, and
/// increasing `max_batches` alone cannot make a refused prefix
/// publishable. Probe the interval with
/// [`Self::build_search_projection_change_batch_from_freshness`]`(index,
/// Some(cap))` (or `EmbeddedStoreHandle::search_projection_change_batch_probe`)
/// and catch up with a batch covering all of it, or re-prepare through
/// the staged registered-consumer path (`docs/REGISTERED_CONSUMER_CATCH_UP.md`).
/// Under sustained writes a bounded batch can repeatedly miss the pinned
/// head; a fresh probe covers a write that landed after the previous one.
/// A single source commit, a graph delta, or a combined projection delta
/// (including host-derived rows) larger than its per-batch limit is the
/// typed [`HawDBError::SearchProjectionBatchBudgetExceeded`], whose
/// `budget` names the limit.
/// Neither refusal is a storage failure, poisons the handle or advances
/// the watermark; both keep their previous display text.
pub fn catch_up_search_projection_with_batch_hydrator<F>(
&self,
search_index: &mut SearchIndex,
max_change_operations_per_batch: usize,
max_projection_operations_per_batch: usize,
max_batches: usize,
mut batch_hydrator: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(
&mut DatabaseReadTransaction,
&SearchProjectionChangeBatch,
) -> Result<SearchProjectionRelationalDelta>,
{
validate_search_projection_catch_up_request(
search_index,
max_change_operations_per_batch,
max_batches,
)?;
if max_projection_operations_per_batch == 0 {
return Err(HawDBError::Semantic(
"search projection catch-up max_projection_operations_per_batch must be greater than zero"
.to_string(),
));
}
self.catch_up_search_projection_with_batch_applier(
search_index,
max_change_operations_per_batch,
max_batches,
|database, search_index, max_operations| {
let Some(mut batch) = database
.build_search_projection_change_batch_from_freshness(
search_index,
Some(max_operations),
)?
else {
return Ok(None);
};
let operation_count = batch.operation_count();
let mut snapshot = database.begin_read_transaction()?;
super::require_search_projection_source_epoch(
batch.complete_through_commit_epoch(),
snapshot.commit_epoch(),
)?;
let hydrated = batch_hydrator(&mut snapshot, &batch)?;
batch.graph_delta_mut().max_operations = Some(max_projection_operations_per_batch);
database.apply_search_projection_change_batch(search_index, batch, hydrated)?;
Ok(Some(operation_count))
},
)
}
fn catch_up_search_projection_with_batch_applier<F>(
&self,
search_index: &mut SearchIndex,
max_operations_per_batch: usize,
max_batches: usize,
mut apply_next_batch: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(&Database, &mut SearchIndex, usize) -> Result<Option<usize>>,
{
run_search_projection_catch_up(
search_index,
self.runtime.get()?.store.commit_epoch(),
max_operations_per_batch,
max_batches,
|search_index, max_operations| apply_next_batch(self, search_index, max_operations),
)
}
/// Scheduled variant of [`Self::catch_up_search_projection`]. Admission
/// deferral stops with a report; prefix and per-batch budget refusals are
/// the same typed errors as the unscheduled catch-up.
pub fn catch_up_search_projection_scheduled(
&self,
search_index: &mut SearchIndex,
max_operations_per_batch: usize,
max_batches: usize,
) -> Result<ScheduledSearchProjectionCatchUpReport> {
self.ensure_runtime_capability(hawdb_core::RuntimeCapability::BackgroundMaintenance)?;
let scheduler = self.local_qos_scheduler_for_work();
run_scheduled_search_projection_catch_up(
search_index,
self.runtime.get()?.store.commit_epoch(),
&scheduler,
max_operations_per_batch,
max_batches,
|search_index, max_operations| {
let Some(request) = self
.build_search_projection_graph_delta_request_from_freshness(
search_index,
Some(max_operations),
)?
else {
return Ok(None);
};
let work_request = request.background_work_request();
Ok(Some((request, work_request)))
},
|search_index, request| {
self.apply_search_projection_graph_delta(search_index, request)
.map(|report| report.operation_count)
},
)
}
}