Skip to main content

sqlive/
page_cache.rs

1//! A connection's page cache: how much it holds, and giving it back (ยง4).
2
3use super::HostSql;
4use crate::authorization::Policy;
5
6impl<P: Policy> HostSql<P> {
7    /// The bytes this connection's page cache holds now, SQLite's
8    /// `SQLITE_DBSTATUS_CACHE_USED`. It counts retained pages even when
9    /// global memory accounting is disabled.
10    pub(crate) fn cache_used(&self) -> rusqlite::Result<i32> {
11        let (mut used, mut high) = (0, 0);
12        // SAFETY: this owner exclusively uses its live connection.
13        let code = unsafe {
14            rusqlite::ffi::sqlite3_db_status(
15                self.connection.handle(),
16                rusqlite::ffi::SQLITE_DBSTATUS_CACHE_USED,
17                &mut used,
18                &mut high,
19                0,
20            )
21        };
22        if code != rusqlite::ffi::SQLITE_OK {
23            return Err(rusqlite::Error::SqliteFailure(rusqlite::ffi::Error::new(code), None));
24        }
25        Ok(used)
26    }
27
28    /// Give back the cached pages no statement is using, as
29    /// `sqlite3_db_release_memory` does. Pages a transaction in progress
30    /// changed and hasn't written out stay; the next read loads what it needs
31    /// again.
32    pub fn release_memory(&self) -> rusqlite::Result<()> {
33        self.connection.release_memory()
34    }
35}
36
37#[cfg(test)]
38#[path = "page_cache_tests.rs"]
39mod tests;