Skip to content

Commit

Permalink
Revert "Demonstrate post-migration changes"
Browse files Browse the repository at this point in the history
This reverts commit 6e20638ce210615ab650d3b5dfe4e60e7ea0beed.
  • Loading branch information
dae committed Jun 11, 2022
1 parent 5fa2b05 commit 2364212
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 2 deletions.
46 changes: 46 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/Sched.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,52 @@ private void unburyCardsForDeck(@NonNull List<Long> allDecks) {
getTime().intTime(), mCol.usn());
}

/*
Deck list **************************************************************** *******************************
*/


/**
* Returns [deckname, did, rev, lrn, new]
*/
@Override
protected @Nullable List<DeckDueTreeNode> deckDueList(@Nullable CancelListener cancelListener) {
_checkDay();
mCol.getDecks().checkIntegrity();
List<Deck> allDecksSorted = mCol.getDecks().allSorted();
HashMap<String, Integer[]> lims = HashUtil.HashMapInit(allDecksSorted.size());
ArrayList<DeckDueTreeNode> deckNodes = new ArrayList<>(allDecksSorted.size());
for (Deck deck : allDecksSorted) {
if (isCancelled(cancelListener)) {
return null;
}
String deckName = deck.getString("name");
String p = Decks.parent(deckName);
// new
int nlim = _deckNewLimitSingle(deck, false);
int rlim = _deckRevLimitSingle(deck, false);
if (!TextUtils.isEmpty(p)) {
Integer[] parentLims = lims.get(Decks.normalizeName(p));
// 'temporary for diagnosis of bug #6383'
Assert.that(parentLims != null, "Deck %s is supposed to have parent %s. It has not be found.", deckName, p);
nlim = Math.min(nlim, parentLims[0]);
// review
rlim = Math.min(rlim, parentLims[1]);
}
int _new = _newForDeck(deck.getLong("id"), nlim);
// learning
int lrn = _lrnForDeck(deck.getLong("id"));
// reviews
int rev = _revForDeck(deck.getLong("id"), rlim);
// save to list
deckNodes.add(new DeckDueTreeNode(deck.getString("name"), deck.getLong("id"), rev, lrn, _new));
// add deck as a parent
lims.put(Decks.normalizeName(deck.getString("name")), new Integer[]{nlim, rlim});
}
return deckNodes;
}


/*
Getting the next card ****************************************************
*******************************************
Expand Down
152 changes: 150 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/SchedV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,62 @@ protected int _walkingCount(@NonNull LimitMethod limFn, @NonNull CountMethod cnt
return tot;
}


/*
Deck list **************************************************************** *******************************
*/


/**
* Returns [deckname, did, rev, lrn, new]
*
* Return nulls when deck task is cancelled.
*/
private @NonNull List<DeckDueTreeNode> deckDueList() {
return deckDueList(null);
}

// Overridden
/**
* Return sorted list of all decks.*/
protected @Nullable List<DeckDueTreeNode> deckDueList(@Nullable CancelListener collectionTask) {
_checkDay();
mCol.getDecks().checkIntegrity();
List<Deck> allDecksSorted = mCol.getDecks().allSorted();
HashMap<String, Integer[]> lims = HashUtil.HashMapInit(allDecksSorted.size());
ArrayList<DeckDueTreeNode> deckNodes = new ArrayList<>(allDecksSorted.size());
Decks.Node childMap = mCol.getDecks().childMap();
for (Deck deck : allDecksSorted) {
if (isCancelled(collectionTask)) {
return null;
}
String deckName = deck.getString("name");
String p = Decks.parent(deckName);
// new
int nlim = _deckNewLimitSingle(deck, false);
Integer plim = null;
if (!TextUtils.isEmpty(p)) {
Integer[] parentLims = lims.get(Decks.normalizeName(p));
// 'temporary for diagnosis of bug #6383'
Assert.that(parentLims != null, "Deck %s is supposed to have parent %s. It has not be found.", deckName, p);
nlim = Math.min(nlim, parentLims[0]);
// reviews
plim = parentLims[1];
}
int _new = _newForDeck(deck.getLong("id"), nlim);
// learning
int lrn = _lrnForDeck(deck.getLong("id"));
// reviews
int rlim = _deckRevLimitSingle(deck, plim, false);
int rev = _revForDeck(deck.getLong("id"), rlim, childMap);
// save to list
deckNodes.add(new DeckDueTreeNode(deck.getString("name"), deck.getLong("id"), rev, lrn, _new));
// add deck as a parent
lims.put(Decks.normalizeName(deck.getString("name")), new Integer[]{nlim, rlim});
}
return deckNodes;
}

/** Similar to deck due tree, but ignore the number of cards.
It may takes a lot of time to compute the number of card, it
Expand All @@ -520,7 +576,19 @@ protected int _walkingCount(@NonNull LimitMethod limFn, @NonNull CountMethod cnt
@Override
public @NonNull
List<? extends TreeNode<? extends AbstractDeckTreeNode>> quickDeckDueTree() {
return mCol.getBackend().legacyDeckDueTree(false);
if (AnkiDroidApp.TESTING_USE_V16_BACKEND) {
return mCol.getBackend().legacyDeckDueTree(false);
}

// Similar to deckDueList
ArrayList<DeckTreeNode> allDecksSorted = new ArrayList<>();
for (JSONObject deck : mCol.getDecks().allSorted()) {
DeckTreeNode g = new DeckTreeNode(deck.getString("name"), deck.getLong("id"));
allDecksSorted.add(g);
}
// End of the similar part.

return _groupChildren(allDecksSorted, false);
}


Expand All @@ -532,9 +600,89 @@ List<? extends TreeNode<? extends AbstractDeckTreeNode>> quickDeckDueTree() {
@RustCleanup("enable for v2 once backend is updated to 2.1.41+")
@RustCleanup("once both v1 and v2 are using backend, cancelListener can be removed")
public List<TreeNode<DeckDueTreeNode>> deckDueTree(@Nullable CancelListener cancelListener) {
return mCol.getBackend().legacyDeckDueTree(true);
if (AnkiDroidApp.TESTING_USE_V16_BACKEND) {
return mCol.getBackend().legacyDeckDueTree(true);
} else {
List<DeckDueTreeNode> allDecksSorted = deckDueList(cancelListener);
if (allDecksSorted == null) {
return null;
}
return _groupChildren(allDecksSorted, true);
}
}

/**
* @return the tree with `allDecksSorted` content.
* @param allDecksSorted the set of all decks of the collection. Sorted.
* @param checkDone Whether the set of deck was checked. If false, we can't assume all decks have parents
* and that there is no duplicate. Instead, we'll ignore problems.*/
protected @NonNull <T extends AbstractDeckTreeNode> List<TreeNode<T>> _groupChildren(@NonNull List<T> allDecksSorted, boolean checkDone) {
return _groupChildren(allDecksSorted, 0, checkDone);
}

/**
@return the tree structure of all decks from @descendants, starting
at specified depth.
@param sortedDescendants a list of decks of dept at least depth, having all
the same first depth name elements, sorted in deck order.
@param depth The depth of the tree we are creating
@param checkDone whether the set of deck was checked. If
false, we can't assume all decks have parents and that there
is no duplicate. Instead, we'll ignore problems.
*/
protected @NonNull <T extends AbstractDeckTreeNode> List<TreeNode<T>> _groupChildren(@NonNull List<T> sortedDescendants, int depth, boolean checkDone) {
List<TreeNode<T>> sortedChildren = new ArrayList<>();
// group and recurse
ListIterator<T> it = sortedDescendants.listIterator();
while (it.hasNext()) {
T child = it.next();
String head = child.getDeckNameComponent(depth);
List<T> sortedDescendantsOfChild = new ArrayList<>();
/* Compose the "sortedChildren" node list. The sortedChildren is a
* list of all the nodes that proceed the current one that
* contain the same at depth `depth`, except for the
* current one itself. I.e., they are subdecks that stem
* from this descendant. This is our version of python's
* itertools.groupby. */
if (!checkDone && child.getDepth() != depth) {
Deck deck = mCol.getDecks().get(child.getDid());
Timber.d("Deck %s (%d)'s parent is missing. Ignoring for quick display.", deck.getString("name"), child.getDid());
continue;
}
while (it.hasNext()) {
T descendantOfChild = it.next();
if (head.equals(descendantOfChild.getDeckNameComponent(depth))) {
// Same head - add to tail of current head.
if (!checkDone && descendantOfChild.getDepth() == depth) {
Deck deck = mCol.getDecks().get(descendantOfChild.getDid());
Timber.d("Deck %s (%d)'s is a duplicate name. Ignoring for quick display.", deck.getString("name"), descendantOfChild.getDid());
continue;
}
sortedDescendantsOfChild.add(descendantOfChild);
} else {
// We've iterated past this head, so step back in order to use this descendant as the
// head in the next iteration of the outer loop.
it.previous();
break;
}
}
// the childrenNode set contains direct child of `child`, but not
// any descendants of the children of `child`...
List<TreeNode<T>> childrenNode = _groupChildren(sortedDescendantsOfChild, depth + 1, checkDone);

// Add the child nodes, and process the addition
TreeNode<T> toAdd = new TreeNode<>(child);
toAdd.getChildren().addAll(childrenNode);
List<T> childValues = childrenNode.stream().map(TreeNode::getValue).collect(Collectors.toList());
child.processChildren(mCol, childValues, "std".equals(getName()));

sortedChildren.add(toAdd);
}
return sortedChildren;
}


/*
Getting the next card ****************************************************
*******************************************
Expand Down

0 comments on commit 2364212

Please sign in to comment.