sqlite_rs/header/
incremental_vacuum_settings.rs

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