speed up children() and add deck_and_child_ids()

This commit is contained in:
Damien Elmes 2020-05-16 15:40:07 +10:00
parent 2efda14200
commit 5590b22683
3 changed files with 17 additions and 10 deletions

View File

@ -4,7 +4,7 @@
from __future__ import annotations
import copy
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
import anki # pylint: disable=unused-import
import anki.backend_pb2 as pb
@ -476,9 +476,7 @@ class DeckManager:
# current deck
self.col.conf["curDeck"] = did
# and active decks (current + all children)
actv = self.children(did)
actv.sort()
self.col.conf["activeDecks"] = [did] + [a[1] for a in actv]
self.col.conf["activeDecks"] = self.deck_and_child_ids(did)
self.col.setMod()
# don't use this, it will likely go away
@ -489,11 +487,21 @@ class DeckManager:
"All children of did, as (name, id)."
name = self.get(did)["name"]
actv = []
for g in self.all():
if g["name"].startswith(name + "::"):
actv.append((g["name"], g["id"]))
for g in self.all_names_and_ids():
if g.name.startswith(name + "::"):
actv.append((g.name, g.id))
return actv
def child_ids(self, parent_name: str) -> Iterable[int]:
prefix = parent_name + "::"
return (d.id for d in self.all_names_and_ids() if d.name.startswith(prefix))
def deck_and_child_ids(self, deck_id: int) -> List[int]:
parent_name = self.get_legacy(deck_id)["name"]
out = [deck_id]
out.extend(self.child_ids(parent_name))
return out
def childDids(self, did: int, childMap: Dict[int, Any]) -> List:
def gather(node, arr):
for did, child in node.items():

View File

@ -171,7 +171,7 @@ class AnkiExporter(Exporter):
if self.cids:
return self.col.decks.for_card_ids(self.cids)
elif self.did:
return [self.did] + [x[1] for x in self.src.decks.children(self.did)]
return self.src.decks.deck_and_child_ids(self.did)
else:
return []

View File

@ -188,8 +188,7 @@ order by due"""
cur = self.col.decks.current()
parents = self.col.decks.parents(cur["id"])
children = [
self.col.decks.get(did)
for (name, did) in self.col.decks.children(cur["id"])
self.col.decks.get(did) for did in self.col.decks.child_ids(cur["id"])
]
for g in [cur] + parents + children:
self._update_stats(g, "new", -new)