diff --git a/rslib/src/card.rs b/rslib/src/card.rs index f84aa33ec..d23f2532b 100644 --- a/rslib/src/card.rs +++ b/rslib/src/card.rs @@ -92,7 +92,7 @@ impl Collection { return Err(AnkiError::invalid_input("card id not set")); } card.mtime = TimestampSecs::now(); - card.usn = self.storage.usn()?; + card.usn = self.usn()?; self.storage.update_card(card) } @@ -102,7 +102,7 @@ impl Collection { return Err(AnkiError::invalid_input("card id already set")); } card.mtime = TimestampSecs::now(); - card.usn = self.storage.usn()?; + card.usn = self.usn()?; self.storage.add_card(card) } } diff --git a/rslib/src/collection.rs b/rslib/src/collection.rs index 5c5797385..4f5c621e5 100644 --- a/rslib/src/collection.rs +++ b/rslib/src/collection.rs @@ -5,6 +5,7 @@ use crate::err::{AnkiError, Result}; use crate::i18n::I18n; use crate::log::Logger; use crate::timestamp::TimestampSecs; +use crate::types::Usn; use crate::{sched::cutoff::SchedTimingToday, storage::SqliteStorage}; use std::path::PathBuf; @@ -17,7 +18,7 @@ pub fn open_collection>( log: Logger, ) -> Result { let col_path = path.into(); - let storage = SqliteStorage::open_or_create(&col_path, server)?; + let storage = SqliteStorage::open_or_create(&col_path)?; let col = Collection { storage, @@ -26,6 +27,7 @@ pub fn open_collection>( media_db: media_db.into(), i18n, log, + server, state: CollectionState::default(), }; @@ -59,6 +61,7 @@ pub struct Collection { pub(crate) media_db: PathBuf, pub(crate) i18n: I18n, pub(crate) log: Logger, + pub(crate) server: bool, state: CollectionState, } @@ -115,10 +118,15 @@ impl Collection { pub fn timing_today(&mut self) -> Result { if let Some(timing) = &self.state.timing_today { if timing.next_day_at > TimestampSecs::now().0 { - return Ok(timing.clone()); + return Ok(*timing); } } - self.state.timing_today = Some(self.storage.timing_today()?); + self.state.timing_today = Some(self.storage.timing_today(self.server)?); Ok(self.state.timing_today.clone().unwrap()) } + + pub(crate) fn usn(&self) -> Result { + // if we cache this in the future, must make sure to invalidate cache when usn bumped in sync.finish() + self.storage.usn(self.server) + } } diff --git a/rslib/src/storage/card/mod.rs b/rslib/src/storage/card/mod.rs index e3afe74c0..89d45fcd0 100644 --- a/rslib/src/storage/card/mod.rs +++ b/rslib/src/storage/card/mod.rs @@ -120,7 +120,7 @@ mod test { #[test] fn add_card() { - let storage = SqliteStorage::open_or_create(Path::new(":memory:"), false).unwrap(); + let storage = SqliteStorage::open_or_create(Path::new(":memory:")).unwrap(); let mut card = Card::default(); storage.add_card(&mut card).unwrap(); let id1 = card.id; diff --git a/rslib/src/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index 9a6911994..0a3aad607 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -18,11 +18,7 @@ use crate::{ use regex::Regex; use rusqlite::{functions::FunctionFlags, params, Connection, NO_PARAMS}; use std::cmp::Ordering; -use std::{ - borrow::Cow, - collections::HashMap, - path::{Path, PathBuf}, -}; +use std::{borrow::Cow, collections::HashMap, path::Path}; use unicase::UniCase; const SCHEMA_MIN_VERSION: u8 = 11; @@ -37,12 +33,6 @@ fn unicase_compare(s1: &str, s2: &str) -> Ordering { pub struct SqliteStorage { // currently crate-visible for dbproxy pub(crate) db: Connection, - - server: bool, - usn: Option, - - // fixme: stored in wrong location? - path: PathBuf, } fn open_or_create_collection_db(path: &Path) -> Result { @@ -166,7 +156,7 @@ fn trace(s: &str) { } impl SqliteStorage { - pub(crate) fn open_or_create(path: &Path, server: bool) -> Result { + pub(crate) fn open_or_create(path: &Path) -> Result { let db = open_or_create_collection_db(path)?; let (create, ver) = schema_version(&db)?; @@ -193,12 +183,7 @@ impl SqliteStorage { } }; - let storage = Self { - db, - path: path.to_owned(), - server, - usn: None, - }; + let storage = Self { db }; Ok(storage) } @@ -258,6 +243,8 @@ impl SqliteStorage { Ok(()) } + ////////////////////////////////////////// + pub(crate) fn mark_modified(&self) -> Result<()> { self.db .prepare_cached("update col set mod=?")? @@ -265,16 +252,12 @@ impl SqliteStorage { Ok(()) } - pub(crate) fn usn(&mut self) -> Result { - if self.server { - if self.usn.is_none() { - self.usn = Some( - self.db - .prepare_cached("select usn from col")? - .query_row(NO_PARAMS, |row| row.get(0))?, - ); - } - Ok(self.usn.clone().unwrap()) + pub(crate) fn usn(&self, server: bool) -> Result { + if server { + Ok(Usn(self + .db + .prepare_cached("select usn from col")? + .query_row(NO_PARAMS, |row| row.get(0))?)) } else { Ok(Usn(-1)) } @@ -310,13 +293,13 @@ impl SqliteStorage { Ok(note_types) } - pub(crate) fn timing_today(&self) -> Result { + pub(crate) fn timing_today(&self, server: bool) -> Result { let crt: i64 = self .db .prepare_cached("select crt from col")? .query_row(NO_PARAMS, |row| row.get(0))?; let conf = self.all_config()?; - let now_offset = if self.server { conf.local_offset } else { None }; + let now_offset = if server { conf.local_offset } else { None }; Ok(sched_timing_today( crt,