roll back the vec cache changes
prepare_cached() is sufficiently fast, and allowing the vec cache to persist across calls is complicated due to lifetime restrictions
This commit is contained in:
parent
1a1a00d50f
commit
2810d3883b
@ -39,7 +39,6 @@ slog-envlogger = "2.2.0"
|
|||||||
serde_repr = "0.1.5"
|
serde_repr = "0.1.5"
|
||||||
num_enum = "0.4.2"
|
num_enum = "0.4.2"
|
||||||
unicase = "2.6.0"
|
unicase = "2.6.0"
|
||||||
variant_count = "=1.0.0"
|
|
||||||
|
|
||||||
# pinned until rusqlite 0.22 comes out
|
# pinned until rusqlite 0.22 comes out
|
||||||
[target.'cfg(target_vendor="apple")'.dependencies.rusqlite]
|
[target.'cfg(target_vendor="apple")'.dependencies.rusqlite]
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
use super::sqlite::CachedStatementKind;
|
|
||||||
use crate::card::{Card, CardID, CardQueue, CardType};
|
use crate::card::{Card, CardID, CardQueue, CardType};
|
||||||
use crate::err::Result;
|
use crate::err::Result;
|
||||||
use crate::timestamp::TimestampMillis;
|
use crate::timestamp::TimestampMillis;
|
||||||
@ -34,10 +33,7 @@ impl FromSql for CardQueue {
|
|||||||
|
|
||||||
impl super::StorageContext<'_> {
|
impl super::StorageContext<'_> {
|
||||||
pub fn get_card(&mut self, cid: CardID) -> Result<Option<Card>> {
|
pub fn get_card(&mut self, cid: CardID) -> Result<Option<Card>> {
|
||||||
self.with_cached_stmt(
|
let mut stmt = self.db.prepare_cached(include_str!("get_card.sql"))?;
|
||||||
CachedStatementKind::GetCard,
|
|
||||||
include_str!("get_card.sql"),
|
|
||||||
|stmt| {
|
|
||||||
stmt.query_row(params![cid], |row| {
|
stmt.query_row(params![cid], |row| {
|
||||||
Ok(Card {
|
Ok(Card {
|
||||||
id: cid,
|
id: cid,
|
||||||
@ -62,15 +58,10 @@ impl super::StorageContext<'_> {
|
|||||||
})
|
})
|
||||||
.optional()
|
.optional()
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn update_card(&mut self, card: &Card) -> Result<()> {
|
pub(crate) fn update_card(&mut self, card: &Card) -> Result<()> {
|
||||||
self.with_cached_stmt(
|
let mut stmt = self.db.prepare_cached(include_str!("update_card.sql"))?;
|
||||||
CachedStatementKind::UpdateCard,
|
|
||||||
include_str!("update_card.sql"),
|
|
||||||
|stmt| {
|
|
||||||
stmt.execute(params![
|
stmt.execute(params![
|
||||||
card.nid,
|
card.nid,
|
||||||
card.did,
|
card.did,
|
||||||
@ -92,16 +83,11 @@ impl super::StorageContext<'_> {
|
|||||||
card.id,
|
card.id,
|
||||||
])?;
|
])?;
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_card(&mut self, card: &mut Card) -> Result<()> {
|
pub(crate) fn add_card(&mut self, card: &mut Card) -> Result<()> {
|
||||||
let now = TimestampMillis::now().0;
|
let now = TimestampMillis::now().0;
|
||||||
self.with_cached_stmt(
|
let mut stmt = self.db.prepare_cached(include_str!("add_card.sql"))?;
|
||||||
CachedStatementKind::AddCard,
|
|
||||||
include_str!("add_card.sql"),
|
|
||||||
|stmt| {
|
|
||||||
stmt.execute(params![
|
stmt.execute(params![
|
||||||
now,
|
now,
|
||||||
card.nid,
|
card.nid,
|
||||||
@ -122,9 +108,6 @@ impl super::StorageContext<'_> {
|
|||||||
card.flags,
|
card.flags,
|
||||||
card.data,
|
card.data,
|
||||||
])?;
|
])?;
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
card.id = CardID(self.db.last_insert_rowid());
|
card.id = CardID(self.db.last_insert_rowid());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ use std::{
|
|||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
use unicase::UniCase;
|
use unicase::UniCase;
|
||||||
use variant_count::VariantCount;
|
|
||||||
|
|
||||||
const SCHEMA_MIN_VERSION: u8 = 11;
|
const SCHEMA_MIN_VERSION: u8 = 11;
|
||||||
const SCHEMA_MAX_VERSION: u8 = 11;
|
const SCHEMA_MAX_VERSION: u8 = 11;
|
||||||
@ -204,53 +203,24 @@ impl SqliteStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, VariantCount)]
|
|
||||||
#[allow(clippy::enum_variant_names)]
|
|
||||||
pub(super) enum CachedStatementKind {
|
|
||||||
GetCard,
|
|
||||||
UpdateCard,
|
|
||||||
AddCard,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct StorageContext<'a> {
|
pub(crate) struct StorageContext<'a> {
|
||||||
pub(crate) db: &'a Connection,
|
pub(crate) db: &'a Connection,
|
||||||
server: bool,
|
server: bool,
|
||||||
usn: Option<Usn>,
|
usn: Option<Usn>,
|
||||||
|
|
||||||
timing_today: Option<SchedTimingToday>,
|
timing_today: Option<SchedTimingToday>,
|
||||||
|
|
||||||
cached_statements: Vec<Option<rusqlite::CachedStatement<'a>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StorageContext<'_> {
|
impl StorageContext<'_> {
|
||||||
fn new(db: &Connection, server: bool) -> StorageContext {
|
fn new(db: &Connection, server: bool) -> StorageContext {
|
||||||
let stmt_len = CachedStatementKind::VARIANT_COUNT;
|
|
||||||
let mut statements = Vec::with_capacity(stmt_len);
|
|
||||||
statements.resize_with(stmt_len, Default::default);
|
|
||||||
StorageContext {
|
StorageContext {
|
||||||
db,
|
db,
|
||||||
server,
|
server,
|
||||||
usn: None,
|
usn: None,
|
||||||
timing_today: None,
|
timing_today: None,
|
||||||
cached_statements: statements,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn with_cached_stmt<F, T>(
|
|
||||||
&mut self,
|
|
||||||
kind: CachedStatementKind,
|
|
||||||
sql: &str,
|
|
||||||
func: F,
|
|
||||||
) -> Result<T>
|
|
||||||
where
|
|
||||||
F: FnOnce(&mut rusqlite::CachedStatement) -> Result<T>,
|
|
||||||
{
|
|
||||||
if self.cached_statements[kind as usize].is_none() {
|
|
||||||
self.cached_statements[kind as usize] = Some(self.db.prepare_cached(sql)?);
|
|
||||||
}
|
|
||||||
func(self.cached_statements[kind as usize].as_mut().unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Standard transaction start/stop
|
// Standard transaction start/stop
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user