Skip to content

Commit

Permalink
experiment with threading
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Feb 19, 2022
1 parent 08f243a commit 4b936a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class GroupNodeViewModel {
private final BooleanBinding allSelectedEntriesMatched;
private final TaskExecutor taskExecutor;
private final CustomLocalDragboard localDragBoard;
private final ObservableList<BibEntry> entriesList;
private ObservableList<BibEntry> entriesList;
private final PreferencesService preferencesService;
private final InvalidationListener onInvalidatedGroup = (listener) -> refreshGroup();

Expand Down Expand Up @@ -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<Boolean> selectedEntriesMatchStatus = EasyBind.map(stateManager.getSelectedEntries(), groupNode::matches);
anySelectedEntriesMatched = selectedEntriesMatchStatus.anyMatch(matched -> matched);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/jabref/gui/util/UiThreadList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ protected void sourceChanged(ListChangeListener.Change<? extends T> change) {
} else {
CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(() -> {
fireChange(change);
try {
fireChange(change);

}
finally
{
latch.countDown();
}
});

try {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void initializeDatabases() throws DatabaseNotSupportedException {
* {@link BibEntry}.
*/
@Override
public void synchronizeLocalDatabase() {
public synchronized void synchronizeLocalDatabase() {
if (!checkCurrentConnection()) {
return;
}
Expand All @@ -206,15 +206,20 @@ public void synchronizeLocalDatabase() {
for (Map.Entry<Integer, Integer> 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<BibEntry> sharedEntry = dbmsProcessor.getSharedEntry(idVersionEntry.getKey());
if (sharedEntry.isPresent()) {
// update fields
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)
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 4b936a3

Please sign in to comment.