Repository: libsql/libsql
File: libsql-storage/src/local_cache.rs (Rust)

Code before:

    pub fn get_frame(&self, frame_no: u64) -> Result<Option<Vec<u8>>> {
        let mut stmt = self
            .conn
            .prepare("SELECT data FROM frames WHERE frame_no = ?1")?;
        match stmt.query_row(params![frame_no], |row| row.get(0)) {
            Ok(frame_data) => Ok(Some(frame_data)),
            Err(Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e),
        }
    }

    pub fn get_frame_by_page(&self, page_no: u32, max_frame_no: u64) -> Result<Option<Vec<u8>>> {
        let mut stmt = self.conn.prepare(
            "SELECT data FROM frames WHERE page_no=?1 AND frame_no <= ?2
            ORDER BY frame_no DESC LIMIT 1",
        )?;
        match stmt.query_row(params![page_no, max_frame_no], |row| row.get(0)) {
            Ok(frame_data) => Ok(Some(frame_data)),
            Err(Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e),
        }
    }

    pub fn get_max_frame_num(&self) -> Result<u64> {
        match self
            .conn
            .query_row("SELECT MAX(frame_no) from frames", (), |row| {
                row.get::<_, Option<u64>>(0)
            }) {
            Ok(Some(frame_no)) => Ok(frame_no),
            Ok(None) | Err(Error::QueryReturnedNoRows) => Ok(0),
            Err(e) => Err(e),
        }
    }

    pub fn insert_page(&self, txn_id: &str, page_no: u32, frame_data: &[u8]) -> Result<()> {
        self.conn.execute(
            "INSERT INTO transactions (txn_id, page_no, data) VALUES (?1, ?2, ?3)
                 ON CONFLICT(txn_id, page_no) DO UPDATE SET data = ?3",
            params![txn_id, page_no, frame_data],
        )?;
        Ok(())
    }

    pub fn get_page(&self, txn_id: &str, page_no: u32) -> Result<Option<Vec<u8>>> {
        let mut stmt = self
            .conn
            .prepare("SELECT data FROM transactions WHERE txn_id = ?1 AND page_no = ?2")?;
        match stmt.query_row(params![txn_id, page_no], |row| row.get(0)) {
            Ok(frame_data) => Ok(Some(frame_data)),

Review comment (suggestion):
follow up PRs: I plan to keep a `meta` table to store `max_frame_no`, number of pages and other info which I might need in replication.

Code after:
        Ok(())
    }

    pub fn get_frame_by_page(&self, page_no: u32, max_frame_no: u64) -> Result<Option<Vec<u8>>> {
        let mut stmt = self.conn.prepare(
            "SELECT data FROM frames WHERE page_no=?1 AND frame_no <= ?2
            ORDER BY frame_no DESC LIMIT 1",
        )?;
        match stmt.query_row(params![page_no, max_frame_no], |row| row.get(0)) {
            Ok(frame_data) => Ok(Some(frame_data)),
            Err(Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e),
        }
    }

    pub fn get_max_frame_num(&self) -> Result<u64> {
        match self
            .conn
            .query_row("SELECT MAX(frame_no) from frames", (), |row| {
                row.get::<_, Option<u64>>(0)
            }) {
            Ok(Some(frame_no)) => Ok(frame_no),
            Ok(None) | Err(Error::QueryReturnedNoRows) => Ok(0),
            Err(e) => Err(e),
        }
    }

    pub fn insert_page(&self, txn_id: &str, page_no: u32, frame_data: &[u8]) -> Result<()> {
        self.conn.execute(
            "INSERT INTO transactions (txn_id, page_no, data) VALUES (?1, ?2, ?3)
                 ON CONFLICT(txn_id, page_no) DO UPDATE SET data = ?3",
            params![txn_id, page_no, frame_data],
        )?;
        Ok(())
    }

    pub fn get_page(&self, txn_id: &str, page_no: u32) -> Result<Option<Vec<u8>>> {
        let mut stmt = self
            .conn
            .prepare("SELECT data FROM transactions WHERE txn_id = ?1 AND page_no = ?2")?;
        match stmt.query_row(params![txn_id, page_no], |row| row.get(0)) {
            Ok(frame_data) => Ok(Some(frame_data)),
            Err(Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e),
        }
    }

    pub fn get_all_pages(&self, txn_id: &str) -> Result<Vec<(u32, Vec<u8>)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT page_no, data FROM transactions WHERE txn_id = ?1")?;