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 all 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
6 changes: 4 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupSidePane.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.groups;

import java.util.Collections;
import java.util.List;

import javafx.application.Platform;
Expand All @@ -16,6 +17,7 @@
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.logic.groups.DefaultGroupsFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;
import org.jabref.model.entry.FieldName;
Expand Down Expand Up @@ -83,8 +85,8 @@ public synchronized void listen(FieldChangedEvent event) {

private void updateShownEntriesAccordingToSelectedGroups(List<GroupTreeNode> selectedGroups) {
if ((selectedGroups == null) || selectedGroups.isEmpty()) {
// No selected group, nothing to do
return;
// No selected group, show all entries
selectedGroups = Collections.singletonList(new GroupTreeNode(DefaultGroupsFactory.getAllEntriesGroup()));
}

final MatcherSet searchRules = MatcherSets.build(
Expand Down
16 changes: 13 additions & 3 deletions 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 @@ -41,12 +42,15 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.ViewModelTreeTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.groups.AllEntriesGroup;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 +91,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), () -> {
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 Expand Up @@ -241,12 +251,12 @@ public void initialize() {
}

private void updateSelection(List<TreeItem<GroupNodeViewModel>> newSelectedGroups) {
if (newSelectedGroups == null) {
if (newSelectedGroups == null || newSelectedGroups.isEmpty()) {
viewModel.selectedGroupsProperty().clear();
} else {
List<GroupNodeViewModel> list = new ArrayList<>();
for (TreeItem<GroupNodeViewModel> model : newSelectedGroups) {
if (model != null && model.getValue() != null) {
if (model != null && model.getValue() != null && !(model.getValue().getGroupNode().getGroup() instanceof AllEntriesGroup)) {
list.add(model.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void onSelectedGroupChanged(ObservableList<GroupNodeViewModel> newValue)
}

currentDatabase.ifPresent(database -> {
if (newValue == null) {
if (newValue == null || newValue.isEmpty()) {
stateManager.clearSelectedGroups(database);
} else {
stateManager.setSelectedGroups(database, newValue.stream().map(GroupNodeViewModel::getGroupNode).collect(Collectors.toList()));
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);
}
}