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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
use super::allocator::BlockStateTracker;
use super::reader::ColReaderInfo;
use super::{ReadConsistency, Walrus};
use crate::wal::block::{Block, Entry, Metadata};
use crate::wal::config::{MAX_BATCH_ENTRIES, PREFIX_META_SIZE, checksum64, debug_print};
use std::io;
use std::sync::{Arc, RwLock};
use rkyv::{AlignedVec, Deserialize};
#[cfg(target_os = "linux")]
use crate::wal::config::USE_FD_BACKEND;
#[cfg(target_os = "linux")]
use std::sync::atomic::Ordering;
#[cfg(target_os = "linux")]
use io_uring;
#[cfg(target_os = "linux")]
use std::os::unix::io::AsRawFd;
impl Walrus {
pub fn read_next(&self, col_name: &str, checkpoint: bool) -> io::Result<Option<Entry>> {
const TAIL_FLAG: u64 = 1u64 << 63;
let info_arc = if let Some(arc) = {
let map = self.reader.data.read().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "reader map read lock poisoned")
})?;
map.get(col_name).cloned()
} {
arc
} else {
let mut map = self.reader.data.write().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "reader map write lock poisoned")
})?;
map.entry(col_name.to_string())
.or_insert_with(|| {
Arc::new(RwLock::new(ColReaderInfo {
chain: Vec::new(),
cur_block_idx: 0,
cur_block_offset: 0,
reads_since_persist: 0,
tail_block_id: 0,
tail_offset: 0,
hydrated_from_index: false,
}))
})
.clone()
};
let mut info = info_arc
.write()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "col info write lock poisoned"))?;
debug_print!(
"[reader] read_next start: col={}, chain_len={}, idx={}, offset={}",
col_name,
info.chain.len(),
info.cur_block_idx,
info.cur_block_offset
);
// Load persisted position (supports tail sentinel)
let mut persisted_tail: Option<(u64 /*block_id*/, u64 /*offset*/)> = None;
if !info.hydrated_from_index {
if let Ok(idx_guard) = self.read_offset_index.read() {
if let Some(pos) = idx_guard.get(col_name) {
if (pos.cur_block_idx & TAIL_FLAG) != 0 {
let tail_block_id = pos.cur_block_idx & (!TAIL_FLAG);
persisted_tail = Some((tail_block_id, pos.cur_block_offset));
// sealed state is considered caught up
info.cur_block_idx = info.chain.len();
info.cur_block_offset = 0;
} else {
let mut ib = pos.cur_block_idx as usize;
if ib > info.chain.len() {
ib = info.chain.len();
}
info.cur_block_idx = ib;
if ib < info.chain.len() {
let used = info.chain[ib].used;
info.cur_block_offset = pos.cur_block_offset.min(used);
} else {
info.cur_block_offset = 0;
}
}
info.hydrated_from_index = true;
} else {
// No persisted state present; mark hydrated to avoid re-checking every call
info.hydrated_from_index = true;
}
}
}
// If we have a persisted tail and some sealed blocks were recovered, fold into the last block
if let Some((tail_block_id, tail_off)) = persisted_tail {
if !info.chain.is_empty() {
if let Some((idx, block)) = info
.chain
.iter()
.enumerate()
.find(|(_, b)| b.id == tail_block_id)
{
let used = block.used;
info.cur_block_idx = idx;
info.cur_block_offset = tail_off.min(used);
} else {
info.cur_block_idx = 0;
info.cur_block_offset = 0;
}
}
persisted_tail = None;
}
// Important: release the per-column lock; we'll reacquire each iteration
drop(info);
loop {
// Reacquire column lock at the start of each iteration
let mut info = info_arc.write().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "col info write lock poisoned")
})?;
// Sealed chain path
if info.cur_block_idx < info.chain.len() {
let idx = info.cur_block_idx;
let off = info.cur_block_offset;
let block = info.chain[idx].clone();
if off >= block.used {
debug_print!(
"[reader] read_next: advance block col={}, block_id={}, offset={}, used={}",
col_name,
block.id,
off,
block.used
);
BlockStateTracker::set_checkpointed_true(block.id as usize);
info.cur_block_idx += 1;
info.cur_block_offset = 0;
continue;
}
match block.read(off) {
Ok((entry, consumed)) => {
// Compute new offset and decide whether to commit progress
let new_off = off + consumed as u64;
let mut maybe_persist = None;
if checkpoint {
info.cur_block_offset = new_off;
maybe_persist = if self.should_persist(&mut info, false) {
Some((info.cur_block_idx as u64, new_off))
} else {
None
};
}
// Drop the column lock before touching the index to avoid lock inversion
drop(info);
if checkpoint {
if let Some((idx_val, off_val)) = maybe_persist {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ = idx_guard.set(col_name.to_string(), idx_val, off_val);
}
}
}
debug_print!(
"[reader] read_next: OK col={}, block_id={}, consumed={}, new_offset={}",
col_name,
block.id,
consumed,
new_off
);
return Ok(Some(entry));
}
Err(_) => {
debug_print!(
"[reader] read_next: read error col={}, block_id={}, offset={}",
col_name,
block.id,
off
);
return Ok(None);
}
}
}
// Tail path
let tail_snapshot = (info.tail_block_id, info.tail_offset);
drop(info);
let writer_arc = {
let map = self.writers.read().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "writers read lock poisoned")
})?;
match map.get(col_name) {
Some(w) => w.clone(),
None => return Ok(None),
}
};
let (active_block, written) = writer_arc.snapshot_block()?;
// If persisted tail points to a different block and that block is now sealed in chain, fold it
// Reacquire column lock for folding/rebasing decisions
let mut info = info_arc.write().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "col info write lock poisoned")
})?;
if let Some((tail_block_id, tail_off)) = persisted_tail {
if tail_block_id != active_block.id {
if let Some((idx, _)) = info
.chain
.iter()
.enumerate()
.find(|(_, b)| b.id == tail_block_id)
{
info.cur_block_idx = idx;
info.cur_block_offset = tail_off.min(info.chain[idx].used);
if checkpoint {
if self.should_persist(&mut info, true) {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ = idx_guard.set(
col_name.to_string(),
info.cur_block_idx as u64,
info.cur_block_offset,
);
}
}
}
persisted_tail = None; // sealed now
drop(info);
continue;
} else {
// rebase tail to current active block at 0
persisted_tail = Some((active_block.id, 0));
if checkpoint {
if self.should_persist(&mut info, true) {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ = idx_guard.set(
col_name.to_string(),
active_block.id | TAIL_FLAG,
0,
);
}
}
}
}
}
} else {
// No persisted tail; init at current active block start
persisted_tail = Some((active_block.id, 0));
if checkpoint {
if self.should_persist(&mut info, true) {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ =
idx_guard.set(col_name.to_string(), active_block.id | TAIL_FLAG, 0);
}
}
}
}
drop(info);
// Choose the best known tail offset: prefer in-memory snapshot for current active block
let (tail_block_id, mut tail_off) = match persisted_tail {
Some(v) => v,
None => return Ok(None),
};
if tail_block_id == active_block.id {
let (snap_id, snap_off) = tail_snapshot;
if snap_id == active_block.id {
tail_off = tail_off.max(snap_off);
}
} else {
// If writer rotated and persisted tail points elsewhere, loop above will fold/rebase
}
// If writer rotated after we set persisted_tail, loop to fold/rebase
if tail_block_id != active_block.id {
// Loop to next iteration; `info` will be reacquired at loop top
continue;
}
if tail_off < written {
match active_block.read(tail_off) {
Ok((entry, consumed)) => {
let new_off = tail_off + consumed as u64;
// Reacquire column lock to update in-memory progress, then decide persistence
let mut info = info_arc.write().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "col info write lock poisoned")
})?;
let mut maybe_persist = None;
if checkpoint {
info.tail_block_id = active_block.id;
info.tail_offset = new_off;
maybe_persist = if self.should_persist(&mut info, false) {
Some((tail_block_id | TAIL_FLAG, new_off))
} else {
None
};
}
drop(info);
if checkpoint {
if let Some((idx_val, off_val)) = maybe_persist {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ = idx_guard.set(col_name.to_string(), idx_val, off_val);
}
}
}
debug_print!(
"[reader] read_next: tail OK col={}, block_id={}, consumed={}, new_tail_off={}",
col_name,
active_block.id,
consumed,
new_off
);
return Ok(Some(entry));
}
Err(_) => {
debug_print!(
"[reader] read_next: tail read error col={}, block_id={}, offset={}",
col_name,
active_block.id,
tail_off
);
return Ok(None);
}
}
} else {
debug_print!(
"[reader] read_next: tail caught up col={}, block_id={}, off={}, written={}",
col_name,
active_block.id,
tail_off,
written
);
return Ok(None);
}
}
}
fn should_persist(&self, info: &mut ColReaderInfo, force: bool) -> bool {
match self.read_consistency {
ReadConsistency::StrictlyAtOnce => true,
ReadConsistency::AtLeastOnce { persist_every } => {
let every = persist_every.max(1);
if force {
info.reads_since_persist = 0;
return true;
}
let next = info.reads_since_persist.saturating_add(1);
if next >= every {
info.reads_since_persist = 0;
true
} else {
info.reads_since_persist = next;
false
}
}
}
}
pub fn batch_read_for_topic(
&self,
col_name: &str,
max_bytes: usize,
checkpoint: bool,
) -> io::Result<Vec<Entry>> {
// Helper struct for read planning
struct ReadPlan {
blk: Block,
start: u64,
end: u64,
is_tail: bool,
chain_idx: Option<usize>,
}
const TAIL_FLAG: u64 = 1u64 << 63;
// Pre-snapshot active writer state to avoid lock-order inversion later
let writer_snapshot: Option<(Block, u64)> = {
let map = self
.writers
.read()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "writers read lock poisoned"))?;
match map.get(col_name).cloned() {
Some(w) => match w.snapshot_block() {
Ok(snapshot) => Some(snapshot),
Err(_) => None,
},
None => None,
}
};
// 1) Get or create reader info
let info_arc = if let Some(arc) = {
let map = self.reader.data.read().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "reader map read lock poisoned")
})?;
map.get(col_name).cloned()
} {
arc
} else {
let mut map = self.reader.data.write().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "reader map write lock poisoned")
})?;
map.entry(col_name.to_string())
.or_insert_with(|| {
Arc::new(RwLock::new(ColReaderInfo {
chain: Vec::new(),
cur_block_idx: 0,
cur_block_offset: 0,
reads_since_persist: 0,
tail_block_id: 0,
tail_offset: 0,
hydrated_from_index: false,
}))
})
.clone()
};
let mut info = info_arc
.write()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "col info write lock poisoned"))?;
// Hydrate from index if needed
let mut persisted_tail_for_fold: Option<(u64 /*block_id*/, u64 /*offset*/)> = None;
if !info.hydrated_from_index {
if let Ok(idx_guard) = self.read_offset_index.read() {
if let Some(pos) = idx_guard.get(col_name) {
if (pos.cur_block_idx & TAIL_FLAG) != 0 {
let tail_block_id = pos.cur_block_idx & (!TAIL_FLAG);
info.tail_block_id = tail_block_id;
info.tail_offset = pos.cur_block_offset;
info.cur_block_idx = info.chain.len();
info.cur_block_offset = 0;
persisted_tail_for_fold = Some((tail_block_id, pos.cur_block_offset));
} else {
let mut ib = pos.cur_block_idx as usize;
if ib > info.chain.len() {
ib = info.chain.len();
}
info.cur_block_idx = ib;
if ib < info.chain.len() {
let used = info.chain[ib].used;
info.cur_block_offset = pos.cur_block_offset.min(used);
} else {
info.cur_block_offset = 0;
}
}
info.hydrated_from_index = true;
} else {
info.hydrated_from_index = true;
}
}
}
// Fold persisted tail into sealed blocks if possible
if let Some((tail_block_id, tail_off)) = persisted_tail_for_fold {
if let Some(idx) = info
.chain
.iter()
.enumerate()
.find(|(_, b)| b.id == tail_block_id)
.map(|(idx, _)| idx)
{
let used = info.chain[idx].used;
info.cur_block_idx = idx;
info.cur_block_offset = tail_off.min(used);
}
}
// 2) Build read plan up to byte and entry limits
let mut plan: Vec<ReadPlan> = Vec::new();
let mut planned_bytes: usize = 0;
let chain_len_at_plan = info.chain.len();
let mut cur_idx = info.cur_block_idx;
let mut cur_off = info.cur_block_offset;
while cur_idx < info.chain.len() && planned_bytes < max_bytes {
let block = info.chain[cur_idx].clone();
if cur_off >= block.used {
BlockStateTracker::set_checkpointed_true(block.id as usize);
cur_idx += 1;
cur_off = 0;
continue;
}
let end = block.used.min(cur_off + (max_bytes - planned_bytes) as u64);
if end > cur_off {
plan.push(ReadPlan {
blk: block.clone(),
start: cur_off,
end,
is_tail: false,
chain_idx: Some(cur_idx),
});
planned_bytes += (end - cur_off) as usize;
}
cur_idx += 1;
cur_off = 0;
}
// Plan tail if we're at the end of sealed chain
if cur_idx >= chain_len_at_plan {
if let Some((active_block, written)) = writer_snapshot.clone() {
// Use in-memory tail progress if available for this block
let tail_start = if info.tail_block_id == active_block.id {
info.tail_offset
} else {
0
};
if tail_start < written {
let end = written; // read up to current writer offset
plan.push(ReadPlan {
blk: active_block.clone(),
start: tail_start,
end,
is_tail: true,
chain_idx: None,
});
}
}
}
if plan.is_empty() {
return Ok(Vec::new());
}
// Hold lock across IO/parse for StrictlyAtOnce to avoid duplicate consumption
let hold_lock_during_io = matches!(self.read_consistency, ReadConsistency::StrictlyAtOnce);
// Manage the guard explicitly to satisfy the borrow checker
let mut info_opt = Some(info);
if !hold_lock_during_io {
// Release lock for AtLeastOnce before IO
drop(info_opt.take().unwrap());
}
// 3) Read ranges via io_uring (FD backend) or mmap
#[cfg(target_os = "linux")]
let buffers = if USE_FD_BACKEND.load(Ordering::Relaxed) {
// io_uring path
let ring_size = (plan.len() + 64).min(4096) as u32;
let mut ring = io_uring::IoUring::new(ring_size).map_err(|e| {
io::Error::new(io::ErrorKind::Other, format!("io_uring init failed: {}", e))
})?;
let mut temp_buffers: Vec<Vec<u8>> = vec![Vec::new(); plan.len()];
let mut expected_sizes: Vec<usize> = vec![0; plan.len()];
for (plan_idx, read_plan) in plan.iter().enumerate() {
let size = (read_plan.end - read_plan.start) as usize;
expected_sizes[plan_idx] = size;
let mut buffer = vec![0u8; size];
let file_offset = read_plan.blk.offset + read_plan.start;
let fd = if let Some(fd_backend) = read_plan.blk.mmap.storage().as_fd() {
io_uring::types::Fd(fd_backend.file().as_raw_fd())
} else {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"batch reads require FD backend when io_uring is enabled",
));
};
let read_op = io_uring::opcode::Read::new(fd, buffer.as_mut_ptr(), size as u32)
.offset(file_offset)
.build()
.user_data(plan_idx as u64);
temp_buffers[plan_idx] = buffer;
unsafe {
ring.submission().push(&read_op).map_err(|e| {
io::Error::new(io::ErrorKind::Other, format!("io_uring push failed: {}", e))
})?;
}
}
// Submit and wait for all reads
ring.submit_and_wait(plan.len())?;
// Process completions and validate read lengths
for _ in 0..plan.len() {
if let Some(cqe) = ring.completion().next() {
let plan_idx = cqe.user_data() as usize;
let got = cqe.result();
if got < 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("io_uring read failed: {}", got),
));
}
if (got as usize) != expected_sizes[plan_idx] {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!(
"short read: got {} bytes, expected {}",
got, expected_sizes[plan_idx]
),
));
}
}
}
temp_buffers
} else {
plan.iter()
.map(|read_plan| {
let size = (read_plan.end - read_plan.start) as usize;
let mut buffer = vec![0u8; size];
let file_offset = (read_plan.blk.offset + read_plan.start) as usize;
read_plan.blk.mmap.read(file_offset, &mut buffer);
buffer
})
.collect()
};
#[cfg(not(target_os = "linux"))]
let buffers: Vec<Vec<u8>> = plan
.iter()
.map(|read_plan| {
let size = (read_plan.end - read_plan.start) as usize;
let mut buffer = vec![0u8; size];
let file_offset = (read_plan.blk.offset + read_plan.start) as usize;
read_plan.blk.mmap.read(file_offset, &mut buffer);
buffer
})
.collect();
// 4) Parse entries from buffers in plan order
let mut entries = Vec::new();
let mut total_data_bytes = 0usize;
let mut final_block_idx = 0usize;
let mut final_block_offset = 0u64;
let mut final_tail_block_id = 0u64;
let mut final_tail_offset = 0u64;
let mut entries_parsed = 0u32;
let mut saw_tail = false;
for (plan_idx, read_plan) in plan.iter().enumerate() {
if entries.len() >= MAX_BATCH_ENTRIES {
break;
}
let buffer = &buffers[plan_idx];
let mut buf_offset = 0usize;
while buf_offset < buffer.len() {
if entries.len() >= MAX_BATCH_ENTRIES {
break;
}
// Try to read metadata header
if buf_offset + PREFIX_META_SIZE > buffer.len() {
break; // Not enough data for header
}
let meta_len =
(buffer[buf_offset] as usize) | ((buffer[buf_offset + 1] as usize) << 8);
if meta_len == 0 || meta_len > PREFIX_META_SIZE - 2 {
// Invalid or zeroed header - stop parsing this block
break;
}
// Deserialize metadata
let mut aligned = AlignedVec::with_capacity(meta_len);
aligned.extend_from_slice(&buffer[buf_offset + 2..buf_offset + 2 + meta_len]);
let archived = unsafe { rkyv::archived_root::<Metadata>(&aligned[..]) };
let meta: Metadata = match archived.deserialize(&mut rkyv::Infallible) {
Ok(m) => m,
Err(_) => break, // Parse error - stop
};
let data_size = meta.read_size;
let entry_consumed = PREFIX_META_SIZE + data_size;
// Check if we have enough buffer space for the data
if buf_offset + entry_consumed > buffer.len() {
break; // Incomplete entry
}
// Enforce byte budget on payload bytes, but always allow at least one entry.
let next_total = total_data_bytes
.checked_add(data_size)
.unwrap_or(usize::MAX);
if next_total > max_bytes && !entries.is_empty() {
break;
}
// Extract and verify data
let data_start = buf_offset + PREFIX_META_SIZE;
let data_end = data_start + data_size;
let data_slice = &buffer[data_start..data_end];
// Verify checksum
if checksum64(data_slice) != meta.checksum {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"checksum mismatch in batch read",
));
}
// Add to results
entries.push(Entry {
data: data_slice.to_vec(),
});
total_data_bytes = next_total;
entries_parsed += 1;
// Update position tracking
let in_block_offset = read_plan.start + buf_offset as u64 + entry_consumed as u64;
if read_plan.is_tail {
saw_tail = true;
final_tail_block_id = read_plan.blk.id;
final_tail_offset = in_block_offset;
} else if let Some(idx) = read_plan.chain_idx {
final_block_idx = idx;
final_block_offset = in_block_offset;
}
buf_offset += entry_consumed;
}
}
// 5) Commit progress (optional)
if entries_parsed > 0 {
enum PersistTarget {
Tail { blk_id: u64, off: u64 },
Sealed { idx: u64, off: u64 },
None,
}
let mut target = PersistTarget::None;
if hold_lock_during_io {
// We still hold the original write guard here
let mut info = info_opt.take().expect("column lock should be held");
if checkpoint {
if saw_tail {
info.cur_block_idx = chain_len_at_plan;
info.cur_block_offset = 0;
info.tail_block_id = final_tail_block_id;
info.tail_offset = final_tail_offset;
target = PersistTarget::Tail {
blk_id: final_tail_block_id,
off: final_tail_offset,
};
} else {
info.cur_block_idx = final_block_idx;
info.cur_block_offset = final_block_offset;
target = PersistTarget::Sealed {
idx: final_block_idx as u64,
off: final_block_offset,
};
}
}
drop(info);
} else {
// Reacquire to update
let mut info2 = info_arc.write().map_err(|_| {
io::Error::new(io::ErrorKind::Other, "col info write lock poisoned")
})?;
if checkpoint {
if saw_tail {
info2.cur_block_idx = chain_len_at_plan;
info2.cur_block_offset = 0;
info2.tail_block_id = final_tail_block_id;
info2.tail_offset = final_tail_offset;
if let ReadConsistency::AtLeastOnce { persist_every } =
self.read_consistency
{
// Clamp contribution so a single call can't reach the threshold
let room = persist_every
.saturating_sub(1)
.saturating_sub(info2.reads_since_persist);
let add = entries_parsed.min(room);
info2.reads_since_persist =
info2.reads_since_persist.saturating_add(add);
// target remains None here to avoid persisting to end in one batch
}
} else {
info2.cur_block_idx = final_block_idx;
info2.cur_block_offset = final_block_offset;
if let ReadConsistency::AtLeastOnce { persist_every } =
self.read_consistency
{
let room = persist_every
.saturating_sub(1)
.saturating_sub(info2.reads_since_persist);
let add = entries_parsed.min(room);
info2.reads_since_persist =
info2.reads_since_persist.saturating_add(add);
}
}
}
drop(info2);
}
if checkpoint {
match target {
PersistTarget::Tail { blk_id, off } => {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ = idx_guard.set(col_name.to_string(), blk_id | TAIL_FLAG, off);
}
}
PersistTarget::Sealed { idx, off } => {
if let Ok(mut idx_guard) = self.read_offset_index.write() {
let _ = idx_guard.set(col_name.to_string(), idx, off);
}
}
PersistTarget::None => {}
}
}
}
Ok(entries)
}
}