Skip to main content

reddb_engine/storage/pager/
batch.rs

1//! Exclusive-owner batches prepared in the original double-write zone.
2use super::*;
3
4impl<F: VfsFile, W: WalBarrier> Pager<F, W> {
5    pub(super) fn batch_active(&self) -> Result<bool, PagerError> {
6        Ok(self
7            .staged_pages
8            .lock()
9            .map_err(|_| PagerError::LockPoisoned)?
10            .is_some())
11    }
12
13    pub(super) fn bounded_writeback(&self) -> Result<bool, PagerError> {
14        Ok(self.batch_active()? || self.file_lock()?.max_read_buffer_bytes() < 2 * PAGE_SIZE)
15    }
16
17    /// The host must exclude readers and other mutations through completion.
18    /// Prepare at most one native DWB frame; neither old homes nor allocator
19    /// roots are changed while images are staged. This is not SQL isolation.
20    /// Guarded formats require independent frame publication and cannot use
21    /// this legacy preparation path.
22    pub fn begin_write_batch(&self) -> Result<(), PagerError> {
23        let _ownership = self.lock_ownership_mutations();
24        self.ensure_writeback_available()?;
25        if !self.dwb_enabled || self.batch_active()? {
26            return Err(PagerError::InvalidDatabase(
27                "write batch requires double-write protection and no active batch".into(),
28            ));
29        }
30        if pager_io::requires_dwb_publication(&mut *self.file_lock()?)? {
31            return Err(PagerError::InvalidDatabase(
32                "prepared batches cannot publish guarded DWB authority".into(),
33            ));
34        }
35        self.sync()?;
36        *self
37            .staged_pages
38            .lock()
39            .map_err(|_| PagerError::LockPoisoned)? = Some(pager_io::StagedPageBatch::default());
40        Ok(())
41    }
42
43    /// The complete original RDDW frame crosses its barrier before any home
44    /// write. Existing recovery finishes all images after an interrupted commit.
45    /// An error is ambiguous and fences this owner until reopen.
46    pub fn commit_write_batch(&self) -> Result<(), PagerError> {
47        let _ownership = self.lock_ownership_mutations();
48        self.ensure_writeback_available()?;
49        if !self.batch_active()? {
50            return Err(PagerError::InvalidDatabase(
51                "no prepared write batch".into(),
52            ));
53        }
54        let result = (|| {
55            self.flush()?;
56            self.wal.flush_page_ownership().map_err(PagerError::Io)?;
57            let mut file = self.file_lock()?;
58            let batch = self
59                .staged_pages
60                .lock()
61                .map_err(|_| PagerError::LockPoisoned)?
62                .take()
63                .expect("active batch under exclusive mutation fence");
64            batch.publish(&mut *file)?;
65            drop(file);
66            if *self.header_dirty_lock()? {
67                self.write_header_and_sync()?;
68            }
69            Ok(())
70        })();
71        if result.is_err() {
72            self.writeback_failed
73                .store(true, std::sync::atomic::Ordering::Release);
74        }
75        result
76    }
77
78    /// Discard only the live owner. Recovery discards incomplete staging or
79    /// finishes a sealed frame; provisional cache contents are never exposed.
80    pub fn fence_write_batch(&self) {
81        self.writeback_failed
82            .store(true, std::sync::atomic::Ordering::Release);
83    }
84}