cache scheduling info
Saves us having to recalculate it for each browser row
This commit is contained in:
parent
ebf7cc61d4
commit
097121424b
@ -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<P: Into<PathBuf>>(
|
||||
@ -64,6 +64,7 @@ pub struct CollectionState {
|
||||
pub(crate) undo: UndoManager,
|
||||
pub(crate) notetype_cache: HashMap<NotetypeId, Arc<Notetype>>,
|
||||
pub(crate) deck_cache: HashMap<DeckId, Arc<Deck>>,
|
||||
pub(crate) scheduler_info: Option<SchedulerInfo>,
|
||||
pub(crate) card_queues: Option<CardQueues>,
|
||||
/// True if legacy Python code has executed SQL that has modified the
|
||||
/// database, requiring modification time to be bumped.
|
||||
|
@ -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<i32>) -> 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)
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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<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> {
|
||||
self.timing_for_timestamp(TimestampSecs::now())
|
||||
self.scheduler_info().map(|info| info.timing)
|
||||
}
|
||||
|
||||
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<()> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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)?;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user