sq3_rs/file_header/
incremental_vacuum_settings.rs

1use std::ops::Deref;
2
3use sq3_derive::Name;
4use sq3_parser::TypeName;
5
6use crate::{result::SqliteResult, traits::ParseBytes};
7
8/// Incremental vacuum settings (8 Bytes)
9///
10///  The two 4-byte big-endian integers at offsets 52 and 64 are used to manage
11/// the auto_vacuum and incremental_vacuum modes. If the integer at offset 52
12/// is zero then pointer-map (ptrmap) pages are omitted from the database file
13/// and neither auto_vacuum nor incremental_vacuum are supported. If the integer
14/// at offset 52 is non-zero then it is the page number of the largest root page
15/// in the database file, the database file will contain ptrmap pages, and the
16/// mode must be either auto_vacuum or incremental_vacuum. In this latter case,
17/// the integer at offset 64 is true for incremental_vacuum and false for
18/// auto_vacuum. If the integer at offset 52 is zero then the integer at
19/// offset 64 must also be zero.
20#[derive(Debug, Default, Name)]
21pub struct IncrementalVacuumSettings {
22    pub largest_root_btree_page: LargestRootBtreePage,
23    pub incremental_vacuum_mode: IncrementalVacuumMode,
24}
25
26// TODO:  If the integer at offset 52 is non-zero then it is the page number of
27// TODO: the largest root page in the database file, the database file will
28// TODO: contain ptrmap pages, and the mode must be either auto_vacuum or
29// TODO: incremental_vacuum.
30
31impl IncrementalVacuumSettings {
32    pub fn largest_root_btree_page(&self) -> &LargestRootBtreePage {
33        &self.largest_root_btree_page
34    }
35
36    pub fn incremental_vacuum_mode(&self) -> &IncrementalVacuumMode {
37        &self.incremental_vacuum_mode
38    }
39}
40
41///  #  Largest root b-tree page (4 Bytes)
42/// The page number of the largest root b-tree page when in auto-vacuum
43/// or incremental-vacuum modes, or zero otherwise.
44#[derive(Debug, Default, Name)]
45pub struct LargestRootBtreePage(u32);
46
47impl Deref for LargestRootBtreePage {
48    type Target = u32;
49
50    fn deref(&self) -> &Self::Target {
51        &self.0
52    }
53}
54
55impl ParseBytes for LargestRootBtreePage {
56    const LENGTH_BYTES: usize = 4;
57
58    fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
59        let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;
60
61        let value = u32::from_be_bytes(buf);
62
63        Ok(Self(value))
64    }
65}
66
67/// # Incremental-vacuum mode (4 Bytes)
68/// True (non-zero) for incremental-vacuum mode. False (zero) otherwise.
69#[derive(Debug, Default, Name)]
70pub enum IncrementalVacuumMode {
71    #[default]
72    False,
73    True,
74}
75impl From<&IncrementalVacuumMode> for bool {
76    fn from(value: &IncrementalVacuumMode) -> Self {
77        match value {
78            IncrementalVacuumMode::True => true,
79            IncrementalVacuumMode::False => false,
80        }
81    }
82}
83impl From<&IncrementalVacuumMode> for u32 {
84    fn from(value: &IncrementalVacuumMode) -> Self {
85        match value {
86            IncrementalVacuumMode::True => 1,
87            IncrementalVacuumMode::False => 0,
88        }
89    }
90}
91
92impl ParseBytes for IncrementalVacuumMode {
93    const LENGTH_BYTES: usize = 4;
94
95    fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
96        let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;
97
98        let number = u32::from_be_bytes(buf);
99        let value = if number > 0 { Self::True } else { Self::False };
100
101        Ok(value)
102    }
103}