sq3_rs/file_header/
file_change_counter.rs

1use std::ops::Deref;
2
3use sq3_derive::Name;
4use sq3_parser::TypeName;
5
6use crate::{result::SqliteResult, traits::ParseBytes};
7
8/// # File change counter (4 Bytes)
9///
10///  The file change counter is a 4-byte big-endian integer at offset 24 that is
11/// incremented whenever the database file is unlocked after having been
12/// modified. When two or more processes are reading the same database file,
13/// each process can detect database changes from other processes by monitoring
14/// the change counter. A process will normally want to flush its database page
15/// cache when another process modified the database, since the cache has become
16/// stale. The file change counter facilitates this.
17///
18/// In WAL mode, changes to the database are detected using the wal-index and so
19/// the change counter is not needed. Hence, the change counter might not be
20/// incremented on each transaction in WAL mode.
21#[derive(Debug, Default, Name)]
22pub struct FileChangeCounter(u32);
23impl Deref for FileChangeCounter {
24    type Target = u32;
25
26    fn deref(&self) -> &Self::Target {
27        &self.0
28    }
29}
30
31impl ParseBytes for FileChangeCounter {
32    const LENGTH_BYTES: usize = 4;
33
34    fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
35        let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;
36
37        Ok(Self(u32::from_be_bytes(buf)))
38    }
39}