From dee0a2fc1862911ba58d7db47c793d6bf7fa873e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 16 Dec 2020 10:54:10 +1000 Subject: [PATCH] fix reposition not being applied in browser sort order https://forums.ankiweb.net/t/reposition-function-not-working/5817 Also changed id->cid in the non-sorting case, as otherwise when using all_searched_cards() on a sorted list, the results will be wrong. --- rslib/src/card.rs | 2 +- rslib/src/sched/bury_and_suspend.rs | 4 ++-- rslib/src/sched/new.rs | 9 +++++---- rslib/src/sched/reviews.rs | 2 +- rslib/src/storage/card/mod.rs | 17 +++++++++++++---- rslib/src/storage/card/search_cids_setup.sql | 2 +- rslib/src/storage/revlog/mod.rs | 2 +- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/rslib/src/card.rs b/rslib/src/card.rs index 4841adeb2..5a3fc44b4 100644 --- a/rslib/src/card.rs +++ b/rslib/src/card.rs @@ -202,7 +202,7 @@ impl Collection { if deck.is_filtered() { return Err(AnkiError::DeckIsFiltered); } - self.storage.set_search_table_to_card_ids(cards)?; + self.storage.set_search_table_to_card_ids(cards, false)?; let sched = self.sched_ver(); let usn = self.usn()?; self.transact(None, |col| { diff --git a/rslib/src/sched/bury_and_suspend.rs b/rslib/src/sched/bury_and_suspend.rs index 01fdd0ad0..3c0f985e1 100644 --- a/rslib/src/sched/bury_and_suspend.rs +++ b/rslib/src/sched/bury_and_suspend.rs @@ -83,7 +83,7 @@ impl Collection { pub fn unbury_or_unsuspend_cards(&mut self, cids: &[CardID]) -> Result<()> { self.transact(None, |col| { - col.storage.set_search_table_to_card_ids(cids)?; + col.storage.set_search_table_to_card_ids(cids, false)?; col.unsuspend_or_unbury_searched_cards() }) } @@ -143,7 +143,7 @@ impl Collection { mode: pb::bury_or_suspend_cards_in::Mode, ) -> Result<()> { self.transact(None, |col| { - col.storage.set_search_table_to_card_ids(cids)?; + col.storage.set_search_table_to_card_ids(cids, false)?; col.bury_or_suspend_searched_cards(mode) }) } diff --git a/rslib/src/sched/new.rs b/rslib/src/sched/new.rs index e85c79ddc..4cf7bee3e 100644 --- a/rslib/src/sched/new.rs +++ b/rslib/src/sched/new.rs @@ -72,8 +72,8 @@ impl Collection { let usn = self.usn()?; let mut position = self.get_next_card_position(); self.transact(None, |col| { - col.storage.set_search_table_to_card_ids(cids)?; - let cards = col.storage.all_searched_cards()?; + col.storage.set_search_table_to_card_ids(cids, true)?; + let cards = col.storage.all_searched_cards_in_search_order()?; for mut card in cards { let original = card.clone(); col.log_manually_scheduled_review(&card, usn, 0)?; @@ -113,8 +113,8 @@ impl Collection { if shift { self.shift_existing_cards(starting_from, step * cids.len() as u32, usn)?; } - self.storage.set_search_table_to_card_ids(cids)?; - let cards = self.storage.all_searched_cards()?; + self.storage.set_search_table_to_card_ids(cids, true)?; + let cards = self.storage.all_searched_cards_in_search_order()?; let sorter = NewCardSorter::new(&cards, starting_from, step, random); for mut card in cards { let original = card.clone(); @@ -138,6 +138,7 @@ impl Collection { card.set_new_position(card.due as u32 + by); self.update_card(&mut card, &original, usn)?; } + self.storage.clear_searched_cards_table()?; Ok(()) } } diff --git a/rslib/src/sched/reviews.rs b/rslib/src/sched/reviews.rs index dd6d95150..c2db2d5b2 100644 --- a/rslib/src/sched/reviews.rs +++ b/rslib/src/sched/reviews.rs @@ -36,7 +36,7 @@ impl Collection { let mut rng = rand::thread_rng(); let distribution = Uniform::from(min_days..=max_days); self.transact(None, |col| { - col.storage.set_search_table_to_card_ids(cids)?; + col.storage.set_search_table_to_card_ids(cids, false)?; for mut card in col.storage.all_searched_cards()? { let original = card.clone(); let interval = distribution.sample(&mut rng); diff --git a/rslib/src/storage/card/mod.rs b/rslib/src/storage/card/mod.rs index 99bb48c17..abbf2f286 100644 --- a/rslib/src/storage/card/mod.rs +++ b/rslib/src/storage/card/mod.rs @@ -267,7 +267,7 @@ impl super::SqliteStorage { self.db .prepare_cached(concat!( include_str!("get_card.sql"), - " where id in (select id from search_cids)" + " where id in (select cid from search_cids)" ))? .query_and_then(NO_PARAMS, |r| row_to_card(r).map_err(Into::into))? .collect() @@ -283,13 +283,14 @@ impl super::SqliteStorage { .collect() } + /// Cards will arrive in card id order, not search order. pub(crate) fn for_each_card_in_search(&self, mut func: F) -> Result<()> where F: FnMut(Card) -> Result<()>, { let mut stmt = self.db.prepare_cached(concat!( include_str!("get_card.sql"), - " where id in (select id from search_cids)" + " where id in (select cid from search_cids)" ))?; let mut rows = stmt.query(NO_PARAMS)?; while let Some(row) = rows.next()? { @@ -358,8 +359,16 @@ impl super::SqliteStorage { /// Injects the provided card IDs into the search_cids table, for /// when ids have arrived outside of a search. /// Clear with clear_searched_cards(). - pub(crate) fn set_search_table_to_card_ids(&mut self, cards: &[CardID]) -> Result<()> { - self.setup_searched_cards_table()?; + pub(crate) fn set_search_table_to_card_ids( + &mut self, + cards: &[CardID], + preserve_order: bool, + ) -> Result<()> { + if preserve_order { + self.setup_searched_cards_table_to_preserve_order()?; + } else { + self.setup_searched_cards_table()?; + } let mut stmt = self .db .prepare_cached("insert into search_cids values (?)")?; diff --git a/rslib/src/storage/card/search_cids_setup.sql b/rslib/src/storage/card/search_cids_setup.sql index c49ea0206..485b63ec7 100644 --- a/rslib/src/storage/card/search_cids_setup.sql +++ b/rslib/src/storage/card/search_cids_setup.sql @@ -1,2 +1,2 @@ drop table if exists search_cids; -create temporary table search_cids (id integer primary key not null); \ No newline at end of file +create temporary table search_cids (cid integer primary key not null); \ No newline at end of file diff --git a/rslib/src/storage/revlog/mod.rs b/rslib/src/storage/revlog/mod.rs index 991f16929..eee062c2e 100644 --- a/rslib/src/storage/revlog/mod.rs +++ b/rslib/src/storage/revlog/mod.rs @@ -98,7 +98,7 @@ impl SqliteStorage { self.db .prepare_cached(concat!( include_str!("get.sql"), - " where cid in (select id from search_cids) and id >= ?" + " where cid in (select cid from search_cids) and id >= ?" ))? .query_and_then(&[after.0 * 1000], |r| { row_to_revlog_entry(r).map(Into::into)