cache timing_today in collection, update it when cutover reached

This commit is contained in:
Damien Elmes 2020-03-29 12:26:24 +10:00
parent fdeca610b0
commit d1ecf33c72
2 changed files with 26 additions and 10 deletions

View File

@ -4,7 +4,8 @@
use crate::err::{AnkiError, Result};
use crate::i18n::I18n;
use crate::log::Logger;
use crate::storage::SqliteStorage;
use crate::timestamp::TimestampSecs;
use crate::{sched::cutoff::SchedTimingToday, storage::SqliteStorage};
use std::path::PathBuf;
pub fn open_collection<P: Into<PathBuf>>(
@ -25,17 +26,16 @@ pub fn open_collection<P: Into<PathBuf>>(
media_db: media_db.into(),
i18n,
log,
state: CollectionState {
task_state: CollectionTaskState::Normal,
},
state: CollectionState::default(),
};
Ok(col)
}
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct CollectionState {
task_state: CollectionTaskState,
timing_today: Option<SchedTimingToday>,
}
#[derive(Debug, PartialEq)]
@ -45,6 +45,12 @@ pub enum CollectionTaskState {
MediaSyncRunning,
}
impl Default for CollectionTaskState {
fn default() -> Self {
Self::Normal
}
}
pub struct Collection {
pub(crate) storage: SqliteStorage,
#[allow(dead_code)]
@ -105,4 +111,14 @@ impl Collection {
pub(crate) fn can_close(&self) -> bool {
self.state.task_state == CollectionTaskState::Normal
}
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());
}
}
self.state.timing_today = Some(self.storage.timing_today()?);
Ok(self.state.timing_today.clone().unwrap())
}
}

View File

@ -129,7 +129,7 @@ impl SqlWriter<'_> {
}
fn write_rated(&mut self, days: u32, ease: Option<u8>) -> Result<()> {
let today_cutoff = self.col.storage.timing_today()?.next_day_at;
let today_cutoff = self.col.timing_today()?.next_day_at;
let days = days.min(365) as i64;
let target_cutoff_ms = (today_cutoff - 86_400 * days) * 1_000;
write!(
@ -148,7 +148,7 @@ impl SqlWriter<'_> {
}
fn write_prop(&mut self, op: &str, kind: &PropertyKind) -> Result<()> {
let timing = self.col.storage.timing_today()?;
let timing = self.col.timing_today()?;
match kind {
PropertyKind::Due(days) => {
let day = days + (timing.days_elapsed as i32);
@ -173,7 +173,7 @@ impl SqlWriter<'_> {
}
fn write_state(&mut self, state: &StateKind) -> Result<()> {
let timing = self.col.storage.timing_today()?;
let timing = self.col.timing_today()?;
match state {
StateKind::New => write!(self.sql, "c.type = {}", CardQueue::New as i8),
StateKind::Review => write!(self.sql, "c.type = {}", CardQueue::Review as i8),
@ -354,7 +354,7 @@ impl SqlWriter<'_> {
}
fn write_added(&mut self, days: u32) -> Result<()> {
let timing = self.col.storage.timing_today()?;
let timing = self.col.timing_today()?;
let cutoff = (timing.next_day_at - (86_400 * (days as i64))) * 1_000;
write!(self.sql, "c.id > {}", cutoff).unwrap();
Ok(())
@ -466,7 +466,7 @@ mod test {
);
// added
let timing = ctx.storage.timing_today().unwrap();
let timing = ctx.timing_today().unwrap();
assert_eq!(
s(ctx, "added:3").0,
format!("(c.id > {})", (timing.next_day_at - (86_400 * 3)) * 1_000)