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

Optimize data fetching #4520

Merged
merged 16 commits into from
Mar 11, 2019
41 changes: 41 additions & 0 deletions src/main/java/org/jabref/logic/shared/DBMSProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,47 @@ public List<BibEntry> getSharedEntries() {
return getSharedEntryList(0);
}

public List<BibEntry> getSharedEntriesByIdList(List<Integer> idList) {
Copy link
Member

Choose a reason for hiding this comment

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

So this method should also replace getSharedEntryList below, right?

List<BibEntry> sharedEntries = new ArrayList<>();

StringBuilder query = new StringBuilder();
query.append("SELECT * from ")
.append(escape("ENTRY"))
.append(" inner join ")
.append(escape("FIELD"))
.append(" F on ")
.append(escape("ENTRY") + ".")
.append(escape("SHARED_ID") + " = F." + escape("ENTRY_SHARED_ID"))
.append(" where ")
.append(escape("SHARED_ID")).append(" in (");

for (int i = 0; i < idList.size() - 1; i++) {
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
query.append(idList.get(i)).append(", ");
}
query.append(idList.get(idList.size() - 1)).append(") order by ").append(escape("SHARED_ID")).append(";");

tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
try (ResultSet selectEntryResultSet = connection.createStatement().executeQuery(query.toString())) {
BibEntry bibEntry = null;
int lastId = -1;
while (selectEntryResultSet.next()) {
if (selectEntryResultSet.getInt("SHARED_ID") > lastId) {
bibEntry = new BibEntry();
bibEntry.getSharedBibEntryData().setSharedID(selectEntryResultSet.getInt("SHARED_ID"));
bibEntry.setType(selectEntryResultSet.getString("TYPE"));
bibEntry.getSharedBibEntryData().setVersion(selectEntryResultSet.getInt("VERSION"));
sharedEntries.add(bibEntry);
lastId = selectEntryResultSet.getInt("SHARED_ID");
}

bibEntry.setField(selectEntryResultSet.getString("NAME"), Optional.ofNullable(selectEntryResultSet.getString("VALUE")), EntryEventSource.SHARED);
}
} catch (SQLException e) {
LOGGER.error("SQL Error", e);
}

return sharedEntries;
}

/**
* @param sharedID Entry ID. If 0, all entries are going to be fetched.
* @return List of {@link BibEntry} instances
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -175,7 +176,7 @@ public void synchronizeLocalDatabase() {

// remove old entries locally
removeNotSharedEntries(localEntries, idVersionMap.keySet());

List<Integer> entryToDrag = new ArrayList<>();
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
// compare versions and update local entry if needed
for (Map.Entry<Integer, Integer> idVersionEntry : idVersionMap.entrySet()) {
boolean match = false;
Expand Down Expand Up @@ -205,12 +206,13 @@ public void synchronizeLocalDatabase() {
}
}
if (!match) {
Optional<BibEntry> bibEntry = dbmsProcessor.getSharedEntry(idVersionEntry.getKey());
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
if (bibEntry.isPresent()) {
bibDatabase.insertEntry(bibEntry.get(), EntryEventSource.SHARED);
}
entryToDrag.add(idVersionEntry.getKey());
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
}
}

for (BibEntry bibEntry : dbmsProcessor.getSharedEntriesByIdList(entryToDrag)) {
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
bibDatabase.insertEntry(bibEntry, EntryEventSource.SHARED);
}
}

/**
Expand Down Expand Up @@ -322,10 +324,10 @@ public void pullChanges() {
}

/**
* Checks whether the current SQL connection is valid.
Copy link
Member

Choose a reason for hiding this comment

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

The original formatting looked right to me.

* In case that the connection is not valid a new {@link ConnectionLostEvent} is going to be sent.
*Checks whether the current SQL connection is valid.
*In case that the connection is not valid a new {@link ConnectionLostEvent} is going to be sent.
*
* @return <code>true</code> if the connection is valid, else <code>false</code>.
*@return <code>true</code> if the connection is valid, else <code>false</code>.
*/
public boolean checkCurrentConnection() {
try {
Expand Down