remove usn cache

SqliteStorage no longer contains any mutable state
This commit is contained in:
Damien Elmes 2020-03-29 12:50:30 +10:00
parent d1ecf33c72
commit f90e5dbe2c
4 changed files with 27 additions and 36 deletions

View File

@ -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)
}
}

View File

@ -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<P: Into<PathBuf>>(
log: Logger,
) -> Result<Collection> {
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<P: Into<PathBuf>>(
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<SchedTimingToday> {
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<Usn> {
// if we cache this in the future, must make sure to invalidate cache when usn bumped in sync.finish()
self.storage.usn(self.server)
}
}

View File

@ -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;

View File

@ -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<Usn>,
// fixme: stored in wrong location?
path: PathBuf,
}
fn open_or_create_collection_db(path: &Path) -> Result<Connection> {
@ -166,7 +156,7 @@ fn trace(s: &str) {
}
impl SqliteStorage {
pub(crate) fn open_or_create(path: &Path, server: bool) -> Result<Self> {
pub(crate) fn open_or_create(path: &Path) -> Result<Self> {
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<Usn> {
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<Usn> {
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<SchedTimingToday> {
pub(crate) fn timing_today(&self, server: bool) -> Result<SchedTimingToday> {
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,