Skip to content

Commit 7b77db9

Browse files
author
Abdelrahman Mostafa
committedMar 17, 2025
Fix for issue 12296
1 parent e3b5368 commit 7b77db9

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed
 

‎src/main/java/org/jabref/logic/search/indexing/BibFieldsIndexer.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import java.sql.SQLException;
66
import java.util.Arrays;
77
import java.util.Collection;
8+
import java.util.HashSet;
89
import java.util.Map;
910
import java.util.Optional;
11+
import java.util.Set;
1012
import java.util.regex.Pattern;
1113

1214
import org.jabref.logic.l10n.Localization;
@@ -47,7 +49,7 @@ public class BibFieldsIndexer {
4749
private final String splitValuesTable;
4850
private final String schemaSplitValuesTableReference;
4951
private final Character keywordSeparator;
50-
private final Field[] dateFields = new Field[] {StandardField.DATE, StandardField.YEAR, StandardField.MONTH, StandardField.DAY};
52+
private final Set<Field> dateFields = new HashSet<>(Arrays.asList(StandardField.DATE, StandardField.YEAR, StandardField.MONTH, StandardField.DAY));
5153

5254
public BibFieldsIndexer(BibEntryPreferences bibEntryPreferences, BibDatabaseContext databaseContext, Connection connection) {
5355
this.databaseContext = databaseContext;
@@ -217,7 +219,7 @@ private void addToIndex(BibEntry bibEntry) {
217219
// To uncover these flaws, we add the "assert" statement.
218220
// One potential future flaw is that the bibEntry is modified concurrently and the field being deleted.
219221
// Skip indexing of date-related fields separately to ensure proper handling later in the process.
220-
if (field != StandardField.DATE && field != StandardField.YEAR && field != StandardField.MONTH && field != StandardField.DAY) {
222+
if (!dateFields.contains(field)) {
221223
Optional<String> resolvedFieldLatexFree = bibEntry.getResolvedFieldOrAliasLatexFree(field, this.databaseContext.getDatabase());
222224
assert resolvedFieldLatexFree.isPresent();
223225
addBatch(preparedStatement, entryId, field, value, resolvedFieldLatexFree.orElse(""));
@@ -245,8 +247,7 @@ private void addToIndex(BibEntry bibEntry) {
245247
// ensure all date-related fields are indexed.
246248
for (Field dateField : dateFields) {
247249
Optional<String> resolvedDateValue = bibEntry.getResolvedFieldOrAlias(dateField, this.databaseContext.getDatabase());
248-
String dateValue = resolvedDateValue.orElse("");
249-
addBatch(preparedStatement, entryId, dateField, dateValue);
250+
resolvedDateValue.ifPresent(dateValue -> addBatch(preparedStatement, entryId, dateField, dateValue));
250251
}
251252
// add entry type
252253
addBatch(preparedStatement, entryId, TYPE_HEADER, bibEntry.getType().getName());
@@ -309,6 +310,7 @@ private void insertField(BibEntry entry, Field field) {
309310
FIELD_NAME,
310311
FIELD_VALUE_LITERAL,
311312
FIELD_VALUE_TRANSFORMED);
313+
// Inserts a new record into the table, or updates the existing record if there's a conflict
312314
String insertDateFieldQuery = """
313315
INSERT INTO %s ("%s", "%s", "%s", "%s")
314316
VALUES (?, ?, ?, ?)
@@ -325,12 +327,11 @@ ON CONFLICT ("%s", "%s")
325327
FIELD_VALUE_TRANSFORMED, FIELD_VALUE_TRANSFORMED);
326328
String entryId = entry.getId();
327329
// If the updated field is date-related, re-index all date fields to overwrite the previous value.
328-
if (field == StandardField.DATE || field == StandardField.YEAR || field == StandardField.MONTH || field == StandardField.DAY) {
330+
if (dateFields.contains(field)) {
329331
try (PreparedStatement preparedStatement = connection.prepareStatement(insertDateFieldQuery)) {
330332
for (Field dateField : dateFields) {
331333
Optional<String> resolvedDateValue = entry.getResolvedFieldOrAlias(dateField, this.databaseContext.getDatabase());
332-
String dateValue = resolvedDateValue.orElse("");
333-
addBatch(preparedStatement, entryId, dateField, dateValue);
334+
resolvedDateValue.ifPresent(dateValue -> addBatch(preparedStatement, entryId, dateField, dateValue));
334335
}
335336
preparedStatement.executeBatch();
336337
} catch (SQLException e) {
@@ -348,6 +349,7 @@ ON CONFLICT ("%s", "%s")
348349
LOGGER.error("Could not add an entry to the index.", e);
349350
}
350351
}
352+
351353
String insertIntoSplitTable = """
352354
INSERT INTO %s ("%s", "%s", "%s", "%s")
353355
VALUES (?, ?, ?, ?)

0 commit comments

Comments
 (0)