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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
macro_rules! segmented_table_mutation_methods {
() => {
// =========================================================================
// DML — writes go to hot buffer, constraints checked against segments
// =========================================================================
fn insert(&mut self, row: Row) -> Result<Row> {
let _seal_guard = self.segment_mgr.acquire_seal_read();
if self.segment_mgr.has_segments() {
self.check_segment_constraints(&row)?;
}
let result = self.hot.insert(row)?;
if self.segment_mgr.has_segments() {
self.segment_mgr.record_txn_seal_generation(self.txn_id());
}
Ok(result)
}
fn insert_discard(&mut self, row: Row) -> Result<()> {
let _seal_guard = self.segment_mgr.acquire_seal_read();
if self.segment_mgr.has_segments() {
self.check_segment_constraints(&row)?;
}
self.hot.insert_discard(row)?;
if self.segment_mgr.has_segments() {
self.segment_mgr.record_txn_seal_generation(self.txn_id());
}
Ok(())
}
fn insert_batch(&mut self, rows: Vec<Row>) -> Result<()> {
let statement_boundary = get_fast_timestamp();
let result = (|| {
let _seal_guard = self.segment_mgr.acquire_seal_read();
if self.segment_mgr.has_segments() {
// Snapshot once for the entire batch — eliminates 3 lock reads per row.
let snapshot = self.segment_mgr.cold_snapshot();
let constraint_started = Instant::now();
let primary_key_started = Instant::now();
let primary_key_result =
self.check_integer_primary_key_batch_with_snapshot(&snapshot, &rows);
let primary_key_elapsed = primary_key_started.elapsed();
let constraint_result: Result<()> = (|| {
let primary_key_checked = primary_key_result?;
for row in &rows {
self.check_segment_constraints_with_snapshot_mode(
&snapshot,
row,
!primary_key_checked,
)?;
}
Ok(())
})();
crate::instrumentation::record_cold_constraint_batch(
rows.len(),
snapshot.seg_ids.len(),
primary_key_elapsed,
constraint_started.elapsed(),
);
constraint_result?;
}
self.hot.insert_batch(rows)?;
if self.segment_mgr.has_segments() {
self.segment_mgr.record_txn_seal_generation(self.txn_id());
}
Ok(())
})();
if result.is_err() {
self.rollback_statement_to(statement_boundary);
}
result
}
fn update(
&mut self,
where_expr: Option<&dyn Expression>,
setter: &mut dyn FnMut(Row) -> Result<(Row, bool)>,
) -> Result<i32> {
let statement_boundary = get_fast_timestamp();
let result = (|| {
self.preclaim_point_update(where_expr)?;
let _seal_guard = self.segment_mgr.acquire_seal_read();
let index_undo = ColdIndexRemovalStatementGuard::new(
Arc::clone(&self.segment_mgr),
self.txn_id(),
);
let mut count = self.hot.update(where_expr, setter)?;
let has_int_pk = self
.hot
.schema()
.columns
.iter()
.any(|c| c.primary_key && c.data_type == DataType::Integer);
let mut hot_skip: FxHashSet<i64> =
FxHashSet::with_capacity_and_hasher(10_000, Default::default());
self.hot.collect_hot_row_ids_into(&mut hot_skip);
self.segment_mgr
.insert_pending_tombstones_into(self.txn_id(), &mut hot_skip);
let schema_clone = self.hot.schema().clone();
let column_indices: Vec<usize> = (0..schema_clone.columns.len()).collect();
let mut scanners =
self.create_segment_scanners_filtered(&column_indices, where_expr, hot_skip);
for scanner in scanners.iter_mut() {
while scanner.next() {
let row_id = scanner.current_row_id()?;
// Claim before reading/evaluating. A previous cold-row writer
// promotes its committed version into hot; after waiting we
// must prefer that hot shadow over the scanner's old payload.
self.hot.try_claim_row(row_id)?;
if self.hot.has_row_id(row_id) {
let mut apply_if_still_matching = |row: Row| {
if let Some(expr) = where_expr {
if !expr.evaluate(&row)? {
return Ok((row, false));
}
}
setter(row)
};
let hot_count = self
.hot
.update_by_row_ids(&[row_id], &mut apply_if_still_matching)?;
count += hot_count;
continue;
}
// A writer may have deleted this row while we waited. Re-check
// cold membership after publication rather than applying the
// scanner payload captured before the wait.
if self.find_segment_row_for_read(row_id).is_none() {
continue;
}
let row = scanner.row().clone();
let old_row = row.clone();
let (new_row, changed) = setter(row)?;
if changed {
// Check unique constraints against cold segments.
if self.hot.has_unique_non_pk_indexes() {
self.check_cold_unique_for_update(&new_row, row_id)?;
}
// Keep the immutable predecessor visible to other
// transactions. Its index entries join the hot shadow's
// commit-time index transition and are never removed early.
self.stage_populated_cold_index_entries_for_row(row_id, &old_row)?;
// Insert the NEW row into hot. For int PK tables, first
// mirror the old row (so UPDATE can find it), then update.
// If any step fails, clean up to avoid phantoms.
if has_int_pk {
match self.hot.insert_discard(old_row.clone()) {
Ok(())
| Err(radixdb_core::Error::PrimaryKeyConstraint { .. })
| Err(radixdb_core::Error::UniqueConstraint { .. }) => {}
Err(e) => {
return Err(e);
}
}
let mut new_row_opt = Some(new_row);
let update_result =
self.hot.update_by_row_ids(&[row_id], &mut |_| {
Ok((new_row_opt.take().unwrap_or_else(Row::new), true))
});
if let Err(e) = update_result {
let _ = self.hot.delete_by_row_ids(&[row_id]);
return Err(e);
}
} else {
self.hot.insert_discard(new_row)?;
}
// Add tombstone so row_count() doesn't double-count.
// The hot version now shadows the cold version via skip set.
self.segment_mgr
.add_pending_tombstone(self.txn_id(), row_id);
count += 1;
}
}
if let Some(err) = scanner.err() {
return Err(err.clone());
}
scanner.close()?;
}
if count > 0 {
self.segment_mgr.record_txn_seal_generation(self.txn_id());
}
index_undo.finish();
Ok(count)
})();
if result.is_err() {
self.rollback_statement_to(statement_boundary);
}
result
}
fn update_by_row_ids(
&mut self,
row_ids: &[i64],
setter: &mut dyn FnMut(Row) -> Result<(Row, bool)>,
) -> Result<i32> {
let statement_boundary = get_fast_timestamp();
let result = (|| {
self.hot.try_claim_rows(row_ids)?;
let _seal_guard = self.segment_mgr.acquire_seal_read();
let index_undo = ColdIndexRemovalStatementGuard::new(
Arc::clone(&self.segment_mgr),
self.txn_id(),
);
let mut count = 0i32;
let mut hot_ids = Vec::new();
let schema = self.hot.schema().clone();
let has_int_pk = schema
.columns
.iter()
.any(|c| c.primary_key && c.data_type == DataType::Integer);
for &row_id in row_ids {
if let Some((_seg_id, cold, idx)) = self.find_segment_row_for_read(row_id) {
self.hot.try_claim_row(row_id)?;
if self.hot.has_row_id(row_id) {
let hot_count = self.hot.update_by_row_ids(&[row_id], setter)?;
count += hot_count;
continue;
}
if self.find_segment_row_for_read(row_id).is_none() {
continue;
}
let mapping = self.segment_mgr.get_cold_segment_mapping(&cold, &schema);
let row =
self.materialize_volume_row_for_read(&cold.volume, idx, &mapping)?;
let old_row = row.clone();
let (new_row, changed) = setter(row)?;
if changed {
if self.hot.has_unique_non_pk_indexes() {
self.check_cold_unique_for_update(&new_row, row_id)?;
}
self.stage_populated_cold_index_entries_for_row(row_id, &old_row)?;
let result = if has_int_pk {
let insert_ok = match self.hot.insert_discard(old_row.clone()) {
Ok(()) => true,
Err(radixdb_core::Error::PrimaryKeyConstraint { .. }) => true,
Err(radixdb_core::Error::UniqueConstraint { .. }) => true,
Err(e) => return Err(e),
};
if insert_ok {
let mut new_row_opt = Some(new_row);
self.hot
.update_by_row_ids(&[row_id], &mut |_| {
Ok((new_row_opt.take().unwrap_or_else(Row::new), true))
})
.map(|_| ())
} else {
Ok(())
}
} else {
self.hot.insert_discard(new_row)
};
result?;
// Add tombstone so row_count() doesn't double-count.
// The hot version now shadows the cold version via skip set.
self.segment_mgr
.add_pending_tombstone(self.txn_id(), row_id);
count += 1;
}
} else {
hot_ids.push(row_id);
}
}
if !hot_ids.is_empty() {
// Same reasoning as update(): skip cold unique check for hot path
// to avoid false violations during ON CONFLICT DO UPDATE.
count += self.hot.update_by_row_ids(&hot_ids, setter)?;
}
if count > 0 {
self.segment_mgr.record_txn_seal_generation(self.txn_id());
}
index_undo.finish();
Ok(count)
})();
if result.is_err() {
self.rollback_statement_to(statement_boundary);
}
result
}
fn delete_by_row_ids(&mut self, row_ids: &[i64]) -> Result<i32> {
self.stage_delete_candidates(row_ids, None, None)
}
fn collect_delete_candidate_row_ids(
&self,
where_expr: Option<&dyn Expression>,
) -> Result<Vec<i64>> {
// Fence the topology before testing the hot-only shortcut. The first
// seal changes `has_segments` from false to true and removes the same
// rows from hot storage. Checking the flag before taking this guard
// allowed DELETE discovery to scan a hot store while its rows were
// being published cold, silently returning an incomplete candidate
// set.
let segment_mgr = Arc::clone(&self.segment_mgr);
let _seal_guard = segment_mgr.acquire_seal_read();
if !self.segment_mgr.has_segments() {
return self.hot.collect_delete_candidate_row_ids(where_expr);
}
// Hot row IDs shadow older cold copies even when the current hot value
// no longer matches the DELETE predicate. Keep one authoritative hot
// snapshot for both shadowing and candidate discovery.
let hot_snapshot = self.hot.collect_all_rows(None)?;
let mut hot_skip = FxHashSet::with_capacity_and_hasher(
hot_snapshot.len().saturating_mul(2).max(1),
Default::default(),
);
let mut candidates = Vec::new();
for (row_id, row) in hot_snapshot {
hot_skip.insert(row_id);
let matches = match where_expr {
Some(expr) => expr.evaluate(&row)?,
None => true,
};
if matches {
candidates.push(row_id);
}
}
self.segment_mgr
.insert_pending_tombstones_into(self.txn_id(), &mut hot_skip);
let mut scanners =
self.create_segment_scanners_filtered_exact_projection(&[], where_expr, hot_skip);
for scanner in scanners.iter_mut() {
if !scanner.collect_remaining_row_ids(&mut candidates)? {
while scanner.next() {
candidates.push(scanner.current_row_id()?);
}
}
if let Some(error) = scanner.err() {
return Err(error.clone());
}
scanner.close()?;
}
candidates.sort_unstable();
candidates.dedup();
Ok(candidates)
}
fn delete_candidate_row_ids(
&mut self,
row_ids: &[i64],
recheck_expr: Option<&dyn Expression>,
) -> Result<i32> {
self.stage_delete_candidates(row_ids, recheck_expr, None)
}
fn delete_candidate_row_ids_collect(
&mut self,
row_ids: &[i64],
recheck_expr: Option<&dyn Expression>,
deleted_row_ids: &mut Vec<i64>,
) -> Result<i32> {
self.stage_delete_candidates(row_ids, recheck_expr, Some(deleted_row_ids))
}
fn get_active_row_ids(&self) -> Vec<i64> {
let hot_ids = self.hot.get_active_row_ids();
if !self.segment_mgr.has_segments() {
return hot_ids;
}
// Build hot_skip from hot row_ids + pending tombstones.
// Committed tombstones are kept as a shared Arc (no clone).
let volumes = self.segment_mgr.get_volumes_newest_first_lazy();
let tombstones_arc = self.segment_mgr.tombstone_set_arc();
let mut hot_skip: FxHashSet<i64> =
FxHashSet::with_capacity_and_hasher(10_000, Default::default());
for id in &hot_ids {
hot_skip.insert(*id);
}
self.segment_mgr
.insert_pending_tombstones_into(self.txn_id(), &mut hot_skip);
let mut ids = Vec::new();
for (_, cs) in volumes.iter() {
let vol = &cs.volume;
for i in 0..vol.meta.row_count {
if !cs.is_visible(i) {
continue;
}
let id = vol.meta.row_ids.at(i);
if !self.is_row_tombstoned(&tombstones_arc, id) && !hot_skip.contains(&id) {
ids.push(id);
}
}
}
ids.extend(hot_ids);
ids
}
fn delete(&mut self, where_expr: Option<&dyn Expression>) -> Result<i32> {
let mut count = self.hot.delete(where_expr)?;
// Candidate discovery and mutation are deliberately separate. The
// initial hot pass handles the snapshot visible at statement start;
// stage_delete_candidates revalidates cold candidates after acquiring
// claims and catches rows concurrently promoted into hot MVCC. Keep
// the discovery fence in this scope only: stage_delete_candidates
// takes the same read fence after claim acquisition. Retaining this
// guard across that call deadlocks when a queued compaction writer
// gives parking_lot writer priority to the nested read acquisition.
let candidates = {
let segment_mgr = Arc::clone(&self.segment_mgr);
let _seal_guard = segment_mgr.acquire_seal_read();
let mut hot_skip: FxHashSet<i64> =
FxHashSet::with_capacity_and_hasher(10_000, Default::default());
self.hot.collect_hot_row_ids_into(&mut hot_skip);
self.segment_mgr
.insert_pending_tombstones_into(self.txn_id(), &mut hot_skip);
let mut candidates = Vec::new();
if let Some(expr) = where_expr {
// The scanner adds predicate columns to its internal needed set;
// exact-empty output therefore discovers row IDs without unrelated
// payload materialization. Populated partial/HNSW cleanup is done
// only after claims, against the authoritative current cold row.
let mut scanners = self.create_segment_scanners_filtered_exact_projection(
&[],
Some(expr),
hot_skip,
);
for scanner in scanners.iter_mut() {
if !scanner.collect_remaining_row_ids(&mut candidates)? {
while scanner.next() {
candidates.push(scanner.current_row_id()?);
}
}
if let Some(err) = scanner.err() {
return Err(err.clone());
}
scanner.close()?;
}
} else {
let volumes = self.segment_mgr.get_volumes_newest_first_lazy();
let tombstones_arc = self.segment_mgr.tombstone_set_arc();
for (_, cs) in volumes.iter() {
let vol = &cs.volume;
for i in 0..vol.meta.row_count {
if !cs.is_visible(i) {
continue;
}
let row_id = vol.meta.row_ids.at(i);
if self.is_row_tombstoned(&tombstones_arc, row_id)
|| hot_skip.contains(&row_id)
{
continue;
}
candidates.push(row_id);
}
}
}
candidates
};
count += self.stage_delete_candidates(&candidates, where_expr, None)?;
Ok(count)
}
fn truncate(&mut self) -> Result<i32> {
let _seal_guard = self.segment_mgr.acquire_seal_read();
let seg_rows = self.segment_mgr.total_row_count() as i32;
let txn_id = self.txn_id();
let manager = Arc::clone(&self.segment_mgr);
let mut clear_cold = move || {
// This callback runs only after the hot store has proved there are
// no active claims and while new hot commits remain excluded.
manager.rollback_pending_tombstones(txn_id);
manager.clear();
Ok(())
};
let hot_rows = self.hot.truncate_after(&mut clear_cold)?;
Ok(hot_rows.saturating_add(seg_rows))
}
};
}
pub(super) use segmented_table_mutation_methods;