cache scheduling info

Saves us having to recalculate it for each browser row
This commit is contained in:
Damien Elmes 2021-04-05 17:09:53 +10:00
parent ebf7cc61d4
commit 097121424b
6 changed files with 38 additions and 13 deletions

View File

@ -1,7 +1,6 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::log::Logger;
use crate::types::Usn; use crate::types::Usn;
use crate::{ use crate::{
decks::{Deck, DeckId}, decks::{Deck, DeckId},
@ -12,6 +11,7 @@ use crate::{
}; };
use crate::{error::Result, scheduler::queue::CardQueues}; use crate::{error::Result, scheduler::queue::CardQueues};
use crate::{i18n::I18n, ops::StateChanges}; use crate::{i18n::I18n, ops::StateChanges};
use crate::{log::Logger, scheduler::SchedulerInfo};
use std::{collections::HashMap, path::PathBuf, sync::Arc}; use std::{collections::HashMap, path::PathBuf, sync::Arc};
pub fn open_collection<P: Into<PathBuf>>( pub fn open_collection<P: Into<PathBuf>>(
@ -64,6 +64,7 @@ pub struct CollectionState {
pub(crate) undo: UndoManager, pub(crate) undo: UndoManager,
pub(crate) notetype_cache: HashMap<NotetypeId, Arc<Notetype>>, pub(crate) notetype_cache: HashMap<NotetypeId, Arc<Notetype>>,
pub(crate) deck_cache: HashMap<DeckId, Arc<Deck>>, pub(crate) deck_cache: HashMap<DeckId, Arc<Deck>>,
pub(crate) scheduler_info: Option<SchedulerInfo>,
pub(crate) card_queues: Option<CardQueues>, pub(crate) card_queues: Option<CardQueues>,
/// True if legacy Python code has executed SQL that has modified the /// True if legacy Python code has executed SQL that has modified the
/// database, requiring modification time to be bumped. /// database, requiring modification time to be bumped.

View File

@ -72,7 +72,7 @@ pub(crate) enum ConfigKey {
#[derive(PartialEq, Serialize_repr, Deserialize_repr, Clone, Copy, Debug)] #[derive(PartialEq, Serialize_repr, Deserialize_repr, Clone, Copy, Debug)]
#[repr(u8)] #[repr(u8)]
pub(crate) enum SchedulerVersion { pub enum SchedulerVersion {
V1 = 1, V1 = 1,
V2 = 2, V2 = 2,
} }
@ -166,6 +166,7 @@ impl Collection {
} }
pub(crate) fn set_creation_utc_offset(&mut self, mins: Option<i32>) -> Result<()> { pub(crate) fn set_creation_utc_offset(&mut self, mins: Option<i32>) -> Result<()> {
self.state.scheduler_info = None;
if let Some(mins) = mins { if let Some(mins) = mins {
self.set_config(ConfigKey::CreationOffset, &mins) self.set_config(ConfigKey::CreationOffset, &mins)
} else { } else {
@ -178,6 +179,7 @@ impl Collection {
} }
pub(crate) fn set_configured_utc_offset(&mut self, mins: i32) -> Result<()> { pub(crate) fn set_configured_utc_offset(&mut self, mins: i32) -> Result<()> {
self.state.scheduler_info = None;
self.set_config(ConfigKey::LocalOffset, &mins) self.set_config(ConfigKey::LocalOffset, &mins)
} }
@ -187,6 +189,7 @@ impl Collection {
} }
pub(crate) fn set_v2_rollover(&mut self, hour: u32) -> Result<()> { pub(crate) fn set_v2_rollover(&mut self, hour: u32) -> Result<()> {
self.state.scheduler_info = None;
self.set_config(ConfigKey::Rollover, &hour) self.set_config(ConfigKey::Rollover, &hour)
} }
@ -213,6 +216,7 @@ impl Collection {
/// Caution: this only updates the config setting. /// Caution: this only updates the config setting.
pub(crate) fn set_scheduler_version_config_key(&mut self, ver: SchedulerVersion) -> Result<()> { pub(crate) fn set_scheduler_version_config_key(&mut self, ver: SchedulerVersion) -> Result<()> {
self.state.scheduler_info = None;
self.set_config(ConfigKey::SchedulerVersion, &ver) self.set_config(ConfigKey::SchedulerVersion, &ver)
} }

View File

@ -180,7 +180,7 @@ mod test {
// move creation time back and it should succeed // move creation time back and it should succeed
let mut stamp = col.storage.creation_stamp().unwrap(); let mut stamp = col.storage.creation_stamp().unwrap();
stamp.0 -= 86_400; stamp.0 -= 86_400;
col.storage.set_creation_stamp(stamp).unwrap(); col.set_creation_stamp(stamp).unwrap();
let timing = col.timing_today().unwrap(); let timing = col.timing_today().unwrap();
col.unbury_if_day_rolled_over(timing).unwrap(); col.unbury_if_day_rolled_over(timing).unwrap();
assert_count(&mut col, 0); assert_count(&mut col, 0);

View File

@ -23,9 +23,29 @@ use timing::{
SchedTimingToday, SchedTimingToday,
}; };
#[derive(Debug, Clone, Copy)]
pub struct SchedulerInfo {
pub version: SchedulerVersion,
pub timing: SchedTimingToday,
}
impl Collection { impl Collection {
pub fn scheduler_info(&mut self) -> Result<SchedulerInfo> {
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<SchedTimingToday> { pub fn timing_today(&mut self) -> Result<SchedTimingToday> {
self.timing_for_timestamp(TimestampSecs::now()) self.scheduler_info().map(|info| info.timing)
} }
pub fn current_due_day(&mut self, delta: i32) -> Result<u32> { pub fn current_due_day(&mut self, delta: i32) -> Result<u32> {
@ -102,14 +122,15 @@ impl Collection {
pub(crate) fn set_rollover_for_current_scheduler(&mut self, hour: u8) -> Result<()> { pub(crate) fn set_rollover_for_current_scheduler(&mut self, hour: u8) -> Result<()> {
match self.scheduler_version() { match self.scheduler_version() {
SchedulerVersion::V1 => { SchedulerVersion::V1 => self.set_creation_stamp(TimestampSecs(
self.storage v1_creation_date_adjusted_to_hour(self.storage.creation_stamp()?.0, hour),
.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), 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)
}
} }

View File

@ -116,7 +116,6 @@ impl Collection {
// nothing to do // nothing to do
return Ok(()); return Ok(());
} }
self.storage.upgrade_revlog_to_v2()?; self.storage.upgrade_revlog_to_v2()?;
self.upgrade_cards_to_v2()?; self.upgrade_cards_to_v2()?;
self.set_scheduler_version_config_key(SchedulerVersion::V2)?; self.set_scheduler_version_config_key(SchedulerVersion::V2)?;

View File

@ -829,7 +829,7 @@ impl Collection {
self.merge_deck_config(remote.decks_and_config.config)?; self.merge_deck_config(remote.decks_and_config.config)?;
self.merge_tags(remote.tags, latest_usn)?; self.merge_tags(remote.tags, latest_usn)?;
if let Some(crt) = remote.creation_stamp { if let Some(crt) = remote.creation_stamp {
self.storage.set_creation_stamp(crt)?; self.set_creation_stamp(crt)?;
} }
if let Some(config) = remote.config { if let Some(config) = remote.config {
self.storage self.storage