From f7c20e40b5c389bf6df137d5ccf1997468c3f7a5 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Fri, 26 Feb 2021 19:52:02 +0100 Subject: [PATCH] Make backend deck deletion take vec of ids --- rslib/backend.proto | 6 +++++- rslib/src/backend/generic.rs | 6 ++++++ rslib/src/backend/mod.rs | 4 ++-- rslib/src/decks/mod.rs | 21 +++++++++++---------- rslib/src/sync/mod.rs | 2 +- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/rslib/backend.proto b/rslib/backend.proto index 4fe09b5fd..2300ceb7f 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -68,6 +68,10 @@ message DeckID { int64 did = 1; } +message DeckIDs { + repeated int64 dids = 1; +} + message DeckConfigID { int64 dcid = 1; } @@ -146,7 +150,7 @@ service BackendService { rpc GetDeckLegacy(DeckID) returns (Json); rpc GetDeckNames(GetDeckNamesIn) returns (DeckNames); rpc NewDeckLegacy(Bool) returns (Json); - rpc RemoveDeck(DeckID) returns (Empty); + rpc RemoveDecks(DeckIDs) returns (Empty); rpc DragDropDecks(DragDropDecksIn) returns (Empty); // deck config diff --git a/rslib/src/backend/generic.rs b/rslib/src/backend/generic.rs index 0b3665f62..19edbe04a 100644 --- a/rslib/src/backend/generic.rs +++ b/rslib/src/backend/generic.rs @@ -69,6 +69,12 @@ impl From for DeckID { } } +impl From for Vec { + fn from(dids: pb::DeckIDs) -> Self { + dids.dids.into_iter().map(DeckID).collect() + } +} + impl From for DeckConfID { fn from(dcid: pb::DeckConfigId) -> Self { DeckConfID(dcid.dcid) diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 9dfece2c5..ba09fc9a4 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -898,8 +898,8 @@ impl BackendService for Backend { .map(Into::into) } - fn remove_deck(&self, input: pb::DeckId) -> BackendResult { - self.with_col(|col| col.remove_deck_and_child_decks(input.into())) + fn remove_decks(&self, input: pb::DeckIDs) -> BackendResult { + self.with_col(|col| col.remove_decks_and_child_decks(input.into())) .map(Into::into) } diff --git a/rslib/src/decks/mod.rs b/rslib/src/decks/mod.rs index 9c69af06d..ffd0b6a6f 100644 --- a/rslib/src/decks/mod.rs +++ b/rslib/src/decks/mod.rs @@ -442,22 +442,23 @@ impl Collection { self.storage.get_deck_id(&machine_name) } - pub fn remove_deck_and_child_decks(&mut self, did: DeckID) -> Result<()> { + pub fn remove_decks_and_child_decks(&mut self, dids: Vec) -> Result<()> { // fixme: vet cache clearing self.state.deck_cache.clear(); self.transact(None, |col| { let usn = col.usn()?; + for did in dids { + if let Some(deck) = col.storage.get_deck(did)? { + let child_decks = col.storage.child_decks(&deck)?; - if let Some(deck) = col.storage.get_deck(did)? { - let child_decks = col.storage.child_decks(&deck)?; - - // top level - col.remove_single_deck(&deck, usn)?; - - // remove children - for deck in child_decks { + // top level col.remove_single_deck(&deck, usn)?; + + // remove children + for deck in child_decks { + col.remove_single_deck(&deck, usn)?; + } } } Ok(()) @@ -775,7 +776,7 @@ mod test { // delete top level let top = col.get_or_create_normal_deck("one")?; - col.remove_deck_and_child_decks(top.id)?; + col.remove_decks_and_child_decks(vec![top.id])?; // should have come back as "Default+" due to conflict assert_eq!(sorted_names(&col), vec!["default", "Default+"]); diff --git a/rslib/src/sync/mod.rs b/rslib/src/sync/mod.rs index 48baa82f0..627a7facb 100644 --- a/rslib/src/sync/mod.rs +++ b/rslib/src/sync/mod.rs @@ -1522,7 +1522,7 @@ mod test { col1.remove_cards_and_orphaned_notes(&[cardid])?; let usn = col1.usn()?; col1.remove_note_only(noteid, usn)?; - col1.remove_deck_and_child_decks(deckid)?; + col1.remove_decks_and_child_decks(vec![deckid])?; let out = ctx.normal_sync(&mut col1).await; assert_eq!(out.required, SyncActionRequired::NoChanges);