From 4b936a377ac26cdf5c9015bd444d74f81d4cf6dd Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 19 Feb 2022 12:41:25 +0100 Subject: [PATCH] experiment with threading --- .../jabref/gui/groups/GroupNodeViewModel.java | 10 +++++++--- .../java/org/jabref/gui/util/UiThreadList.java | 9 ++++++++- .../jabref/logic/shared/DBMSSynchronizer.java | 18 +++++++++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java index f63eafc2470..395b5f9c365 100644 --- a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java @@ -5,6 +5,7 @@ import java.util.Optional; import java.util.stream.Collectors; +import javafx.application.Platform; import javafx.beans.InvalidationListener; import javafx.beans.WeakInvalidationListener; import javafx.beans.binding.Bindings; @@ -57,7 +58,7 @@ public class GroupNodeViewModel { private final BooleanBinding allSelectedEntriesMatched; private final TaskExecutor taskExecutor; private final CustomLocalDragboard localDragBoard; - private final ObservableList entriesList; + private ObservableList entriesList; private final PreferencesService preferencesService; private final InvalidationListener onInvalidatedGroup = (listener) -> refreshGroup(); @@ -91,8 +92,11 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager state // Register listener // The wrapper created by the FXCollections will set a weak listener on the wrapped list. This weak listener gets garbage collected. Hence, we need to maintain a reference to this list. - entriesList = databaseContext.getDatabase().getEntries(); - entriesList.addListener(this::onDatabaseChanged); + Platform.runLater(()-> { + entriesList = databaseContext.getDatabase().getEntries(); + entriesList.addListener(this::onDatabaseChanged); + }); + EasyObservableList selectedEntriesMatchStatus = EasyBind.map(stateManager.getSelectedEntries(), groupNode::matches); anySelectedEntriesMatched = selectedEntriesMatchStatus.anyMatch(matched -> matched); diff --git a/src/main/java/org/jabref/gui/util/UiThreadList.java b/src/main/java/org/jabref/gui/util/UiThreadList.java index 86c4614cccd..26a85cfa29f 100644 --- a/src/main/java/org/jabref/gui/util/UiThreadList.java +++ b/src/main/java/org/jabref/gui/util/UiThreadList.java @@ -23,7 +23,14 @@ protected void sourceChanged(ListChangeListener.Change change) { } else { CountDownLatch latch = new CountDownLatch(1); Platform.runLater(() -> { - fireChange(change); + try { + fireChange(change); + + } + finally + { + latch.countDown(); + } }); try { diff --git a/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java b/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java index 6c0e4ec37ee..6917da02ac4 100644 --- a/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java +++ b/src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java @@ -191,7 +191,7 @@ public void initializeDatabases() throws DatabaseNotSupportedException { * {@link BibEntry}. */ @Override - public void synchronizeLocalDatabase() { + public synchronized void synchronizeLocalDatabase() { if (!checkCurrentConnection()) { return; } @@ -206,8 +206,8 @@ public void synchronizeLocalDatabase() { for (Map.Entry idVersionEntry : idVersionMap.entrySet()) { boolean remoteEntryMatchingOneLocalEntryFound = false; for (BibEntry localEntry : localEntries) { - if (idVersionEntry.getKey().equals(localEntry.getSharedBibEntryData().getSharedID())) { - remoteEntryMatchingOneLocalEntryFound = true; + remoteEntryMatchingOneLocalEntryFound = true; + if (idVersionEntry.getValue() > localEntry.getSharedBibEntryData().getVersion()) { Optional sharedEntry = dbmsProcessor.getSharedEntry(idVersionEntry.getKey()); if (sharedEntry.isPresent()) { @@ -215,6 +215,11 @@ public void synchronizeLocalDatabase() { localEntry.setType(sharedEntry.get().getType(), EntriesEventSource.SHARED); localEntry.getSharedBibEntryData() .setVersion(sharedEntry.get().getSharedBibEntryData().getVersion()); + + + taskExecutor.runInFXThread(()-> { + + sharedEntry.get().getFieldMap().forEach( // copy remote values to local entry (field, value) -> localEntry.setField(field, value, EntriesEventSource.SHARED) @@ -226,18 +231,21 @@ public void synchronizeLocalDatabase() { .forEach( field -> localEntry.clearField(field, EntriesEventSource.SHARED) ); + }); } } } - } + if (!remoteEntryMatchingOneLocalEntryFound) { entriesToInsertIntoLocalDatabase.add(idVersionEntry.getKey()); } } if (!entriesToInsertIntoLocalDatabase.isEmpty()) { + + taskExecutor.runInFXThread( () -> bibDatabase.insertEntries(dbmsProcessor.getSharedEntries(entriesToInsertIntoLocalDatabase), EntriesEventSource.SHARED)); // in case entries should be added into the local database, insert them - bibDatabase.insertEntries(dbmsProcessor.getSharedEntries(entriesToInsertIntoLocalDatabase), EntriesEventSource.SHARED); + // bibDatabase.insertEntries(dbmsProcessor.getSharedEntries(entriesToInsertIntoLocalDatabase), EntriesEventSource.SHARED); } }