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 4 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 search field 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.debug("Run group search " + searchField.getText());
viewModel.filterTextProperty().setValue(searchField.textProperty().getValue());
});
searchField.textProperty().addListener((observable, oldValue, newValue) -> searchTask.restart());

groupTree.rootProperty().bind(
EasyBind.map(viewModel.rootGroupProperty(),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/util/BindingsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String getName() {
* the items are converted when the are inserted (and at the initialization) instead of when they are accessed.
* Thus the initial CPU overhead and memory consumption is higher but the access to list items is quicker.
*/
public static <A, B> MappedList mapBacked(ObservableList<A> source, Function<A, B> mapper) {
public static <A, B> MappedList<B, A> mapBacked(ObservableList<A> source, Function<A, B> mapper) {
return new MappedList<>(source, mapper);
}

Expand Down
32 changes: 12 additions & 20 deletions src/main/java/org/jabref/gui/util/RecursiveTreeItem.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.jabref.gui.util;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
Expand All @@ -24,7 +22,7 @@ public class RecursiveTreeItem<T> extends TreeItem<T> {
private final Callback<T, BooleanProperty> expandedProperty;
private Callback<T, ObservableList<T>> childrenFactory;
private ObjectProperty<Predicate<T>> filter = new SimpleObjectProperty<>();
private FilteredList<T> children;
private FilteredList<RecursiveTreeItem<T>> children;

public RecursiveTreeItem(final T value, Callback<T, ObservableList<T>> func) {
this(value, func, null, null);
Expand Down Expand Up @@ -52,7 +50,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 @@ -67,44 +65,38 @@ private void bindExpandedProperty(T value, Callback<T, BooleanProperty> expanded
}

private void addChildrenListener(T value) {
children = new FilteredList<>(childrenFactory.call(value));
children = new FilteredList<>(
BindingsHelper.mapBacked(childrenFactory.call(value),
child -> new RecursiveTreeItem<>(child, getGraphic(), childrenFactory, expandedProperty, filter)));
children.predicateProperty().bind(Bindings.createObjectBinding(() -> this::showNode, filter));

addAsChildren(children, 0);
getChildren().addAll(0, children);

children.addListener((ListChangeListener<T>) change -> {
children.addListener((ListChangeListener<RecursiveTreeItem<T>>) change -> {
while (change.next()) {

if (change.wasRemoved()) {
change.getRemoved().forEach(t-> {
final List<TreeItem<T>> itemsToRemove = getChildren().stream().filter(treeItem -> treeItem.getValue().equals(t)).collect(Collectors.toList());
getChildren().removeAll(itemsToRemove);
});
getChildren().removeAll(change.getRemoved());
}

if (change.wasAdded()) {
addAsChildren(change.getAddedSubList(), change.getFrom());
getChildren().addAll(change.getFrom(), change.getAddedSubList());
}
}
});
}

private void addAsChildren(List<? extends T> children, int startIndex) {
List<RecursiveTreeItem<T>> treeItems = children.stream().map(child -> new RecursiveTreeItem<>(child, getGraphic(), childrenFactory, expandedProperty, filter)).collect(Collectors.toList());
getChildren().addAll(startIndex, treeItems);
}

private boolean showNode(T t) {
private boolean showNode(RecursiveTreeItem<T> node) {
if (filter.get() == null) {
return true;
}

if (filter.get().test(t)) {
if (filter.get().test(node.getValue())) {
// Node is directly matched -> so show it
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);
return node.children.getSource().stream().anyMatch(this::showNode);
}
}