mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
cache timing_today in collection, update it when cutover reached
This commit is contained in:
parent
fdeca610b0
commit
d1ecf33c72
2 changed files with 26 additions and 10 deletions
|
@ -4,7 +4,8 @@
|
||||||
use crate::err::{AnkiError, Result};
|
use crate::err::{AnkiError, Result};
|
||||||
use crate::i18n::I18n;
|
use crate::i18n::I18n;
|
||||||
use crate::log::Logger;
|
use crate::log::Logger;
|
||||||
use crate::storage::SqliteStorage;
|
use crate::timestamp::TimestampSecs;
|
||||||
|
use crate::{sched::cutoff::SchedTimingToday, storage::SqliteStorage};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub fn open_collection<P: Into<PathBuf>>(
|
pub fn open_collection<P: Into<PathBuf>>(
|
||||||
|
@ -25,17 +26,16 @@ pub fn open_collection<P: Into<PathBuf>>(
|
||||||
media_db: media_db.into(),
|
media_db: media_db.into(),
|
||||||
i18n,
|
i18n,
|
||||||
log,
|
log,
|
||||||
state: CollectionState {
|
state: CollectionState::default(),
|
||||||
task_state: CollectionTaskState::Normal,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(col)
|
Ok(col)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
pub struct CollectionState {
|
pub struct CollectionState {
|
||||||
task_state: CollectionTaskState,
|
task_state: CollectionTaskState,
|
||||||
|
timing_today: Option<SchedTimingToday>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
|
@ -45,6 +45,12 @@ pub enum CollectionTaskState {
|
||||||
MediaSyncRunning,
|
MediaSyncRunning,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for CollectionTaskState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Normal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Collection {
|
pub struct Collection {
|
||||||
pub(crate) storage: SqliteStorage,
|
pub(crate) storage: SqliteStorage,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -105,4 +111,14 @@ impl Collection {
|
||||||
pub(crate) fn can_close(&self) -> bool {
|
pub(crate) fn can_close(&self) -> bool {
|
||||||
self.state.task_state == CollectionTaskState::Normal
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,7 +129,7 @@ impl SqlWriter<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_rated(&mut self, days: u32, ease: Option<u8>) -> Result<()> {
|
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 days = days.min(365) as i64;
|
||||||
let target_cutoff_ms = (today_cutoff - 86_400 * days) * 1_000;
|
let target_cutoff_ms = (today_cutoff - 86_400 * days) * 1_000;
|
||||||
write!(
|
write!(
|
||||||
|
@ -148,7 +148,7 @@ impl SqlWriter<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_prop(&mut self, op: &str, kind: &PropertyKind) -> Result<()> {
|
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 {
|
match kind {
|
||||||
PropertyKind::Due(days) => {
|
PropertyKind::Due(days) => {
|
||||||
let day = days + (timing.days_elapsed as i32);
|
let day = days + (timing.days_elapsed as i32);
|
||||||
|
@ -173,7 +173,7 @@ impl SqlWriter<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_state(&mut self, state: &StateKind) -> Result<()> {
|
fn write_state(&mut self, state: &StateKind) -> Result<()> {
|
||||||
let timing = self.col.storage.timing_today()?;
|
let timing = self.col.timing_today()?;
|
||||||
match state {
|
match state {
|
||||||
StateKind::New => write!(self.sql, "c.type = {}", CardQueue::New as i8),
|
StateKind::New => write!(self.sql, "c.type = {}", CardQueue::New as i8),
|
||||||
StateKind::Review => write!(self.sql, "c.type = {}", CardQueue::Review 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<()> {
|
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;
|
let cutoff = (timing.next_day_at - (86_400 * (days as i64))) * 1_000;
|
||||||
write!(self.sql, "c.id > {}", cutoff).unwrap();
|
write!(self.sql, "c.id > {}", cutoff).unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -466,7 +466,7 @@ mod test {
|
||||||
);
|
);
|
||||||
|
|
||||||
// added
|
// added
|
||||||
let timing = ctx.storage.timing_today().unwrap();
|
let timing = ctx.timing_today().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
s(ctx, "added:3").0,
|
s(ctx, "added:3").0,
|
||||||
format!("(c.id > {})", (timing.next_day_at - (86_400 * 3)) * 1_000)
|
format!("(c.id > {})", (timing.next_day_at - (86_400 * 3)) * 1_000)
|
||||||
|
|
Loading…
Reference in a new issue