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
52 changes: 52 additions & 0 deletions src/main/java/org/jabref/logic/shared/DBMSProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.jabref.logic.shared.exception.OfflineLockException;
import org.jabref.model.database.shared.DBMSType;
Expand Down Expand Up @@ -427,6 +428,57 @@ public Optional<BibEntry> getSharedEntry(int sharedID) {
return Optional.empty();
}

public List<BibEntry> getSharedEntryByIdList(List<Integer> idList) {
List<BibEntry> sharedEntries = new ArrayList<>();

StringBuilder query = new StringBuilder();
query.append("SELECT ")
.append(escape("ENTRY")).append(".").append(escape("SHARED_ID")).append(", ")
.append(escape("ENTRY")).append(".").append(escape("TYPE")).append(", ")
.append(escape("ENTRY")).append(".").append(escape("VERSION")).append(", ")
.append("F.").append(escape("ENTRY_SHARED_ID")).append(", ")
.append("F.").append(escape("NAME")).append(", ")
.append("F.").append(escape("VALUE"))
.append(" FROM ")
.append(escape("ENTRY"))
.append(" inner join ")
.append(escape("FIELD"))
.append(" F on ")
.append(escape("ENTRY")).append(".").append(escape("SHARED_ID"))
.append(" = F.").append(escape("ENTRY_SHARED_ID"))
.append(" where ")
.append(escape("SHARED_ID")).append(" in (");

String idListAsString = idList.stream().map(String::valueOf).collect(Collectors.joining(", "));

query.append(idListAsString)
.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;
}

public List<BibEntry> getSharedEntries() {
return getSharedEntryList(0);
}
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> entriesToDrag = new ArrayList<>();
// 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);
}
entriesToDrag.add(idVersionEntry.getKey());
}
}

for (BibEntry bibEntry : dbmsProcessor.getSharedEntryByIdList(entriesToDrag)) {
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
18 changes: 18 additions & 0 deletions src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ void testUpdateEntry(DBMSType dbmsType, DBMSConnection dbmsConnection, DBMSProce
assertEquals(expectedEntry, actualEntryOptional.get());
}

@ParameterizedTest
@MethodSource("getTestingDatabaseSystems")
void testGetEntriesByIdList(DBMSType dbmsType, DBMSConnection dbmsConnection, DBMSProcessor dbmsProcessor) throws OfflineLockException, SQLException {
dbmsProcessor.setupSharedDatabase();
BibEntry firstEntry = getBibEntryExample();
firstEntry.setId("1");
BibEntry secondEntry = getBibEntryExample();
secondEntry.setId("2");

dbmsProcessor.insertEntry(firstEntry);
dbmsProcessor.insertEntry(secondEntry);

List<BibEntry> sharedEntriesByIdList = dbmsProcessor.getSharedEntryByIdList(Arrays.asList(1, 2));

assertEquals(firstEntry.getId(), sharedEntriesByIdList.get(0).getId());
assertEquals(secondEntry.getId(), sharedEntriesByIdList.get(1).getId());
}

@ParameterizedTest
@MethodSource("getTestingDatabaseSystems")
void testUpdateNewerEntry(DBMSType dbmsType, DBMSConnection dbmsConnection, DBMSProcessor dbmsProcessor) throws OfflineLockException, SQLException {
Expand Down