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            let mut file = self.file_lock()?;
57            let batch = self
58                .staged_pages
59                .lock()
60                .map_err(|_| PagerError::LockPoisoned)?
61                .take()
62                .expect("active batch under exclusive mutation fence");
63            batch.publish(&mut *file)?;
64            drop(file);
65            if *self.header_dirty_lock()? {
66                self.write_header_and_sync()?;
67            }
68            Ok(())
69        })();
70        if result.is_err() {
71            self.writeback_failed
72                .store(true, std::sync::atomic::Ordering::Release);
73        }
74        result
75    }
76
77    /// Discard only the live owner. Recovery discards incomplete staging or
78    /// finishes a sealed frame; provisional cache contents are never exposed.
79    pub fn fence_write_batch(&self) {
80        self.writeback_failed
81            .store(true, std::sync::atomic::Ordering::Release);
82    }
83}