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    pub fn begin_write_batch(&self) -> Result<(), PagerError> {
21        let _ownership = self.lock_ownership_mutations();
22        self.ensure_writeback_available()?;
23        if !self.dwb_enabled || self.batch_active()? {
24            return Err(PagerError::InvalidDatabase(
25                "write batch requires double-write protection and no active batch".into(),
26            ));
27        }
28        self.sync()?;
29        *self
30            .staged_pages
31            .lock()
32            .map_err(|_| PagerError::LockPoisoned)? = Some(pager_io::StagedPageBatch::default());
33        Ok(())
34    }
35
36    /// The complete original RDDW frame crosses its barrier before any home
37    /// write. Existing recovery finishes all images after an interrupted commit.
38    /// An error is ambiguous and fences this owner until reopen.
39    pub fn commit_write_batch(&self) -> Result<(), PagerError> {
40        let _ownership = self.lock_ownership_mutations();
41        self.ensure_writeback_available()?;
42        if !self.batch_active()? {
43            return Err(PagerError::InvalidDatabase(
44                "no prepared write batch".into(),
45            ));
46        }
47        let result = (|| {
48            self.flush()?;
49            let mut file = self.file_lock()?;
50            let batch = self
51                .staged_pages
52                .lock()
53                .map_err(|_| PagerError::LockPoisoned)?
54                .take()
55                .expect("active batch under exclusive mutation fence");
56            batch.publish(&mut *file)?;
57            drop(file);
58            if *self.header_dirty_lock()? {
59                self.write_header_and_sync()?;
60            }
61            Ok(())
62        })();
63        if result.is_err() {
64            self.writeback_failed
65                .store(true, std::sync::atomic::Ordering::Release);
66        }
67        result
68    }
69
70    /// Discard only the live owner. Recovery discards incomplete staging or
71    /// finishes a sealed frame; provisional cache contents are never exposed.
72    pub fn fence_write_batch(&self) {
73        self.writeback_failed
74            .store(true, std::sync::atomic::Ordering::Release);
75    }
76}