sqlite_rs/header/
file_change_counter.rs

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