Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix group search performance #3553

Merged
merged 5 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/org/jabref/gui/groups/GroupTreeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -47,6 +48,8 @@
import org.controlsfx.control.textfield.CustomTextField;
import org.controlsfx.control.textfield.TextFields;
import org.fxmisc.easybind.EasyBind;
import org.reactfx.util.FxTimer;
import org.reactfx.util.Timer;

public class GroupTreeController extends AbstractController<GroupTreeViewModel> {

Expand Down Expand Up @@ -87,7 +90,13 @@ public void initialize() {
this::updateSelection
);

viewModel.filterTextProperty().bind(searchField.textProperty());
// We try to to prevent publishing changes in the searchfield directly to the search task that takes some time
// for larger group structures.
final Timer searchTask = FxTimer.create(Duration.ofMillis(400), () -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it makes sense to extract this code to a helper method in BindingsHelper.

LOGGER.info("Run Search " + searchField.getText());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think debug level is enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That log is really only for testing so that I could inspect when a new search was triggered and if my "reset the timer on new key" does actually work. This will be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the log level be changed to "debug" and the logging settings adjusted accordingly for you?

viewModel.filterTextProperty().setValue(searchField.textProperty().getValue());
});
searchField.textProperty().addListener((observable, oldValue, newValue) -> searchTask.restart());

groupTree.rootProperty().bind(
EasyBind.map(viewModel.rootGroupProperty(),
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/jabref/gui/util/RecursiveTreeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javafx.scene.Node;
import javafx.scene.control.TreeItem;
import javafx.util.Callback;
import javafx.util.Pair;

/**
* Taken from https://gist.github.com/lestard/011e9ed4433f9eb791a8
Expand All @@ -25,6 +26,7 @@ public class RecursiveTreeItem<T> extends TreeItem<T> {
private Callback<T, ObservableList<T>> childrenFactory;
private ObjectProperty<Predicate<T>> filter = new SimpleObjectProperty<>();
private FilteredList<T> children;
private Pair<T, Boolean> lastVisibility;

public RecursiveTreeItem(final T value, Callback<T, ObservableList<T>> func) {
this(value, func, null, null);
Expand Down Expand Up @@ -52,7 +54,7 @@ private RecursiveTreeItem(final T value, Node graphic, Callback<T, ObservableLis
bindExpandedProperty(value, expandedProperty);
}

valueProperty().addListener((obs, oldValue, newValue)-> {
valueProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null) {
addChildrenListener(newValue);
bindExpandedProperty(newValue, expandedProperty);
Expand All @@ -76,7 +78,7 @@ private void addChildrenListener(T value) {
while (change.next()) {

if (change.wasRemoved()) {
change.getRemoved().forEach(t-> {
change.getRemoved().forEach(t -> {
final List<TreeItem<T>> itemsToRemove = getChildren().stream().filter(treeItem -> treeItem.getValue().equals(t)).collect(Collectors.toList());
getChildren().removeAll(itemsToRemove);
});
Expand All @@ -99,12 +101,19 @@ private boolean showNode(T t) {
return true;
}

if (lastVisibility != null && lastVisibility.getKey().equals(t)) {
return lastVisibility.getValue();
}

if (filter.get().test(t)) {
// Node is directly matched -> so show it
lastVisibility = new Pair<>(t, true);
return true;
}

// Are there children (or children of children...) that are matched? If yes we also need to show this node
return childrenFactory.call(t).stream().anyMatch(this::showNode);
final boolean isShown = childrenFactory.call(t).stream().anyMatch(this::showNode);
lastVisibility = new Pair<>(t, isShown);
return isShown;
}
}