From 097121424b3acd43e0518a6574a43ee7376fb306 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 5 Apr 2021 17:09:53 +1000 Subject: [PATCH] cache scheduling info Saves us having to recalculate it for each browser row --- rslib/src/collection.rs | 3 +- rslib/src/config/mod.rs | 6 +++- rslib/src/scheduler/bury_and_suspend.rs | 2 +- rslib/src/scheduler/mod.rs | 37 +++++++++++++++++++------ rslib/src/scheduler/upgrade.rs | 1 - rslib/src/sync/mod.rs | 2 +- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/rslib/src/collection.rs b/rslib/src/collection.rs index 928cd2c99..6e8348c9d 100644 --- a/rslib/src/collection.rs +++ b/rslib/src/collection.rs @@ -1,7 +1,6 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -use crate::log::Logger; use crate::types::Usn; use crate::{ decks::{Deck, DeckId}, @@ -12,6 +11,7 @@ use crate::{ }; use crate::{error::Result, scheduler::queue::CardQueues}; use crate::{i18n::I18n, ops::StateChanges}; +use crate::{log::Logger, scheduler::SchedulerInfo}; use std::{collections::HashMap, path::PathBuf, sync::Arc}; pub fn open_collection>( @@ -64,6 +64,7 @@ pub struct CollectionState { pub(crate) undo: UndoManager, pub(crate) notetype_cache: HashMap>, pub(crate) deck_cache: HashMap>, + pub(crate) scheduler_info: Option, pub(crate) card_queues: Option, /// True if legacy Python code has executed SQL that has modified the /// database, requiring modification time to be bumped. diff --git a/rslib/src/config/mod.rs b/rslib/src/config/mod.rs index a076934f3..c5cfcc59f 100644 --- a/rslib/src/config/mod.rs +++ b/rslib/src/config/mod.rs @@ -72,7 +72,7 @@ pub(crate) enum ConfigKey { #[derive(PartialEq, Serialize_repr, Deserialize_repr, Clone, Copy, Debug)] #[repr(u8)] -pub(crate) enum SchedulerVersion { +pub enum SchedulerVersion { V1 = 1, V2 = 2, } @@ -166,6 +166,7 @@ impl Collection { } pub(crate) fn set_creation_utc_offset(&mut self, mins: Option) -> Result<()> { + self.state.scheduler_info = None; if let Some(mins) = mins { self.set_config(ConfigKey::CreationOffset, &mins) } else { @@ -178,6 +179,7 @@ impl Collection { } pub(crate) fn set_configured_utc_offset(&mut self, mins: i32) -> Result<()> { + self.state.scheduler_info = None; self.set_config(ConfigKey::LocalOffset, &mins) } @@ -187,6 +189,7 @@ impl Collection { } pub(crate) fn set_v2_rollover(&mut self, hour: u32) -> Result<()> { + self.state.scheduler_info = None; self.set_config(ConfigKey::Rollover, &hour) } @@ -213,6 +216,7 @@ impl Collection { /// Caution: this only updates the config setting. pub(crate) fn set_scheduler_version_config_key(&mut self, ver: SchedulerVersion) -> Result<()> { + self.state.scheduler_info = None; self.set_config(ConfigKey::SchedulerVersion, &ver) } diff --git a/rslib/src/scheduler/bury_and_suspend.rs b/rslib/src/scheduler/bury_and_suspend.rs index 0f51b313d..640e98c42 100644 --- a/rslib/src/scheduler/bury_and_suspend.rs +++ b/rslib/src/scheduler/bury_and_suspend.rs @@ -180,7 +180,7 @@ mod test { // move creation time back and it should succeed let mut stamp = col.storage.creation_stamp().unwrap(); stamp.0 -= 86_400; - col.storage.set_creation_stamp(stamp).unwrap(); + col.set_creation_stamp(stamp).unwrap(); let timing = col.timing_today().unwrap(); col.unbury_if_day_rolled_over(timing).unwrap(); assert_count(&mut col, 0); diff --git a/rslib/src/scheduler/mod.rs b/rslib/src/scheduler/mod.rs index df7c4734a..577d69ff4 100644 --- a/rslib/src/scheduler/mod.rs +++ b/rslib/src/scheduler/mod.rs @@ -23,9 +23,29 @@ use timing::{ SchedTimingToday, }; +#[derive(Debug, Clone, Copy)] +pub struct SchedulerInfo { + pub version: SchedulerVersion, + pub timing: SchedTimingToday, +} + impl Collection { + pub fn scheduler_info(&mut self) -> Result { + let now = TimestampSecs::now(); + if let Some(info) = self.state.scheduler_info { + if now < info.timing.next_day_at { + return Ok(info); + } + } + let version = self.scheduler_version(); + let timing = self.timing_for_timestamp(now)?; + let info = SchedulerInfo { version, timing }; + self.state.scheduler_info = Some(info); + Ok(info) + } + pub fn timing_today(&mut self) -> Result { - self.timing_for_timestamp(TimestampSecs::now()) + self.scheduler_info().map(|info| info.timing) } pub fn current_due_day(&mut self, delta: i32) -> Result { @@ -102,14 +122,15 @@ impl Collection { pub(crate) fn set_rollover_for_current_scheduler(&mut self, hour: u8) -> Result<()> { match self.scheduler_version() { - SchedulerVersion::V1 => { - self.storage - .set_creation_stamp(TimestampSecs(v1_creation_date_adjusted_to_hour( - self.storage.creation_stamp()?.0, - hour, - ))) - } + SchedulerVersion::V1 => self.set_creation_stamp(TimestampSecs( + v1_creation_date_adjusted_to_hour(self.storage.creation_stamp()?.0, hour), + )), SchedulerVersion::V2 => self.set_v2_rollover(hour as u32), } } + + pub(crate) fn set_creation_stamp(&mut self, stamp: TimestampSecs) -> Result<()> { + self.state.scheduler_info = None; + self.storage.set_creation_stamp(stamp) + } } diff --git a/rslib/src/scheduler/upgrade.rs b/rslib/src/scheduler/upgrade.rs index 3f0a39f90..e657cd639 100644 --- a/rslib/src/scheduler/upgrade.rs +++ b/rslib/src/scheduler/upgrade.rs @@ -116,7 +116,6 @@ impl Collection { // nothing to do return Ok(()); } - self.storage.upgrade_revlog_to_v2()?; self.upgrade_cards_to_v2()?; self.set_scheduler_version_config_key(SchedulerVersion::V2)?; diff --git a/rslib/src/sync/mod.rs b/rslib/src/sync/mod.rs index 609573e0c..6634aa2bc 100644 --- a/rslib/src/sync/mod.rs +++ b/rslib/src/sync/mod.rs @@ -829,7 +829,7 @@ impl Collection { self.merge_deck_config(remote.decks_and_config.config)?; self.merge_tags(remote.tags, latest_usn)?; if let Some(crt) = remote.creation_stamp { - self.storage.set_creation_stamp(crt)?; + self.set_creation_stamp(crt)?; } if let Some(config) = remote.config { self.storage