Skip to content

Commit 2f5565b

Browse files
authored
Batch DBMSProcessor fields insertion (#5812)
Batch DBMSProcessor fields insertion
2 parents 0dd2c5f + 931ccad commit 2f5565b

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

src/main/java/org/jabref/logic/shared/DBMSProcessor.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,30 @@ private boolean checkForBibEntryExistence(BibEntry bibEntry) {
213213
private void insertIntoFieldTable(BibEntry bibEntry) {
214214
try {
215215
// Inserting into FIELD table
216-
for (Field field : bibEntry.getFields()) {
217-
StringBuilder insertFieldQuery = new StringBuilder()
218-
.append("INSERT INTO ")
219-
.append(escape("FIELD"))
220-
.append("(")
221-
.append(escape("ENTRY_SHARED_ID"))
222-
.append(", ")
223-
.append(escape("NAME"))
224-
.append(", ")
225-
.append(escape("VALUE"))
226-
.append(") VALUES(?, ?, ?)");
227-
228-
try (PreparedStatement preparedFieldStatement = connection.prepareStatement(insertFieldQuery.toString())) {
216+
// Coerce to ArrayList in order to use List.get()
217+
List<Field> fields = new ArrayList<>(bibEntry.getFields());
218+
StringBuilder insertFieldQuery = new StringBuilder()
219+
.append("INSERT INTO ")
220+
.append(escape("FIELD"))
221+
.append("(")
222+
.append(escape("ENTRY_SHARED_ID"))
223+
.append(", ")
224+
.append(escape("NAME"))
225+
.append(", ")
226+
.append(escape("VALUE"))
227+
.append(") VALUES(?, ?, ?)");
228+
// Number of commas is fields.size() - 1
229+
for (int i = 0; i < fields.size() - 1; i++) {
230+
insertFieldQuery.append(", (?, ?, ?)");
231+
}
232+
try (PreparedStatement preparedFieldStatement = connection.prepareStatement(insertFieldQuery.toString())) {
233+
for (int i = 0; i < fields.size(); i++) {
229234
// columnIndex starts with 1
230-
preparedFieldStatement.setInt(1, bibEntry.getSharedBibEntryData().getSharedID());
231-
preparedFieldStatement.setString(2, field.getName());
232-
preparedFieldStatement.setString(3, bibEntry.getField(field).get());
233-
preparedFieldStatement.executeUpdate();
235+
preparedFieldStatement.setInt((3 * i) + 1, bibEntry.getSharedBibEntryData().getSharedID());
236+
preparedFieldStatement.setString((3 * i) + 2, fields.get(i).getName());
237+
preparedFieldStatement.setString((3 * i) + 3, bibEntry.getField(fields.get(i)).get());
234238
}
239+
preparedFieldStatement.executeUpdate();
235240
}
236241
} catch (SQLException e) {
237242
LOGGER.error("SQL Error: ", e);

src/test/java/org/jabref/logic/shared/DBMSProcessorTest.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,59 @@ void testInsertEntry() throws SQLException {
9595
assertEquals(expectedFieldMap, actualFieldMap);
9696
}
9797

98+
@Test
99+
void testInsertMultipleEntries() throws SQLException {
100+
BibEntry firstEntry = getBibEntryExample();
101+
String firstId = firstEntry.getId();
102+
BibEntry secondEntry = getBibEntryExample2();
103+
String secondId = secondEntry.getId();
104+
BibEntry thirdEntry = getBibEntryExample3();
105+
String thirdId = thirdEntry.getId();
106+
107+
// This must eventually be changed to insertEntries() once that method is implemented
108+
dbmsProcessor.insertEntry(firstEntry);
109+
dbmsProcessor.insertEntry(secondEntry);
110+
dbmsProcessor.insertEntry(thirdEntry);
111+
112+
Map<Integer, Map<String, String>> actualFieldMap = new HashMap<>();
113+
114+
try (ResultSet entryResultSet = selectFrom("ENTRY", dbmsConnection, dbmsProcessor)) {
115+
assertTrue(entryResultSet.next());
116+
assertEquals(1, entryResultSet.getInt("SHARED_ID"));
117+
assertEquals("inproceedings", entryResultSet.getString("TYPE"));
118+
assertEquals(1, entryResultSet.getInt("VERSION"));
119+
assertTrue(entryResultSet.next());
120+
assertEquals(2, entryResultSet.getInt("SHARED_ID"));
121+
assertEquals("inproceedings", entryResultSet.getString("TYPE"));
122+
assertEquals(1, entryResultSet.getInt("VERSION"));
123+
assertTrue(entryResultSet.next());
124+
assertEquals(3, entryResultSet.getInt("SHARED_ID"));
125+
assertFalse(entryResultSet.next());
126+
127+
try (ResultSet fieldResultSet = selectFrom("FIELD", dbmsConnection, dbmsProcessor)) {
128+
while (fieldResultSet.next()) {
129+
if (actualFieldMap.keySet().contains(fieldResultSet.getInt("ENTRY_SHARED_ID"))) {
130+
actualFieldMap.get(fieldResultSet.getInt("ENTRY_SHARED_ID")).put(
131+
fieldResultSet.getString("NAME"), fieldResultSet.getString("VALUE"));
132+
} else {
133+
int sharedId = fieldResultSet.getInt("ENTRY_SHARED_ID");
134+
actualFieldMap.put(sharedId,
135+
new HashMap<>());
136+
actualFieldMap.get(sharedId).put(fieldResultSet.getString("NAME"),
137+
fieldResultSet.getString("VALUE"));
138+
}
139+
}
140+
}
141+
}
142+
List<BibEntry> entries = Arrays.asList(firstEntry, secondEntry, thirdEntry);
143+
Map<Integer, Map<String, String>> expectedFieldMap = entries.stream()
144+
.collect(Collectors.toMap(bibEntry -> bibEntry.getSharedBibEntryData().getSharedID(),
145+
(bibEntry) -> bibEntry.getFieldMap().entrySet().stream()
146+
.collect(Collectors.toMap((entry) -> entry.getKey().getName(), Map.Entry::getValue))));
147+
148+
assertEquals(expectedFieldMap, actualFieldMap);
149+
}
150+
98151
@Test
99152
void testUpdateEntry() throws Exception {
100153
BibEntry expectedEntry = getBibEntryExample();
@@ -156,7 +209,7 @@ void testUpdateEqualEntry() throws OfflineLockException, SQLException {
156209
@Test
157210
void testRemoveAllEntries() throws SQLException {
158211
BibEntry firstEntry = getBibEntryExample();
159-
BibEntry secondEntry = getBibEntryExample();
212+
BibEntry secondEntry = getBibEntryExample2();
160213
List<BibEntry> entriesToRemove = Arrays.asList(firstEntry, secondEntry);
161214
dbmsProcessor.insertEntry(firstEntry);
162215
dbmsProcessor.insertEntry(secondEntry);
@@ -170,8 +223,8 @@ void testRemoveAllEntries() throws SQLException {
170223
@Test
171224
void testRemoveSomeEntries() throws SQLException {
172225
BibEntry firstEntry = getBibEntryExample();
173-
BibEntry secondEntry = getBibEntryExample();
174-
BibEntry thirdEntry = getBibEntryExample();
226+
BibEntry secondEntry = getBibEntryExample2();
227+
BibEntry thirdEntry = getBibEntryExample3();
175228

176229
// Remove the first and third entries - the second should remain (SHARED_ID will be 2)
177230

@@ -311,6 +364,24 @@ private static BibEntry getBibEntryExample() {
311364
.withCiteKey("nanoproc1994");
312365
}
313366

367+
private static BibEntry getBibEntryExample2() {
368+
return new BibEntry(StandardEntryType.InProceedings)
369+
.withField(StandardField.AUTHOR, "Shelah, Saharon and Ziegler, Martin")
370+
.withField(StandardField.TITLE, "Algebraically closed groups of large cardinality")
371+
.withField(StandardField.JOURNAL, "The Journal of Symbolic Logic")
372+
.withField(StandardField.YEAR, "1979")
373+
.withCiteKey("algegrou1979");
374+
}
375+
376+
private static BibEntry getBibEntryExample3() {
377+
return new BibEntry(StandardEntryType.InProceedings)
378+
.withField(StandardField.AUTHOR, "Hodges, Wilfrid and Shelah, Saharon")
379+
.withField(StandardField.TITLE, "Infinite games and reduced products")
380+
.withField(StandardField.JOURNAL, "Annals of Mathematical Logic")
381+
.withField(StandardField.YEAR, "1981")
382+
.withCiteKey("infigame1981");
383+
}
384+
314385
private ResultSet selectFrom(String table, DBMSConnection dbmsConnection, DBMSProcessor dbmsProcessor) {
315386
try {
316387
return dbmsConnection.getConnection().createStatement().executeQuery("SELECT * FROM " + escape(table, dbmsProcessor));

0 commit comments

Comments
 (0)