Skip to content

Commit

Permalink
Add ADR-0038
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Aug 28, 2024
1 parent 8faad33 commit cd71f11
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/decisions/0038-use-identityhashcode-for-bibentries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Use System.identityHashCode for BibEntry at indexing
nav_order: 38
parent: Decision Records
---

<!-- markdownlint-disable-next-line MD025 -->
# Use `System.identityHashCode` for BibEntries at Indexing

## Context and Problem Statement

The `BibEntry` class has `equals` and `hashCode` implemented on the content of the bib entry.
Thus, if two bib entries have the same type, the same fields, and the same content, they are equal.

This, however, is not useful in the UI, where equal entries are not the same entries.

## Decision Drivers

* Simple code
* Not changing much other JabRef code
* Working Lucene

## Considered Options

* Use `System.identityHashCode` for indexing `BibEntry`
* Rewrite `BibEntry` logic

## Decision Outcome

Chosen option: "Use `System.identityHashCode` for indexing `BibEntry`", because is the "natural" thing to ensure distinction between two instances of a `BibEntry` object - regardless of equality.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public synchronized List<BibEntry> getToAdd() {
}

public synchronized void remove(BibEntry entry) {
// ADR-0038
toRemove.put(System.identityHashCode(entry), entry);
duplicates++;
}
Expand All @@ -250,6 +251,7 @@ public synchronized void replace(BibEntry entry, BibEntry replacement) {
}

public synchronized boolean isToRemove(BibEntry entry) {
// ADR-0038
return toRemove.containsKey(System.identityHashCode(entry));
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,21 @@ private void onDatabaseChanged(ListChangeListener.Change<? extends BibEntry> cha
} else if (change.wasUpdated()) {
for (BibEntry changedEntry : change.getList().subList(change.getFrom(), change.getTo())) {
if (groupNode.matches(changedEntry)) {
// ADR-0038
matchedEntries.put(System.identityHashCode(changedEntry), changedEntry);
} else {
// ADR-0038
matchedEntries.remove(System.identityHashCode(changedEntry));
}
}
} else {
for (BibEntry removedEntry : change.getRemoved()) {
// ADR-0038
matchedEntries.remove(System.identityHashCode(removedEntry));
}
for (BibEntry addedEntry : change.getAddedSubList()) {
if (groupNode.matches(addedEntry)) {
// ADR-0038
matchedEntries.put(System.identityHashCode(addedEntry), addedEntry);
}
}
Expand Down Expand Up @@ -296,6 +300,7 @@ private void updateMatchedEntries() {
.wrap(() -> groupNode.findMatches(databaseContext.getDatabase()))
.onSuccess(entries -> {
matchedEntries.clear();
// ADR-0038
entries.forEach(entry -> matchedEntries.put(System.identityHashCode(entry), entry));
})
.executeWith(taskExecutor);
Expand Down Expand Up @@ -545,8 +550,10 @@ public void listen(IndexAddedOrUpdatedEvent event) {
for (BibEntry entry : event.entries()) {
searchGroup.updateMatches(entry);
if (groupNode.matches(entry)) {
// ADR-0038
matchedEntries.put(System.identityHashCode(entry), entry);
} else {
// ADR-0038
matchedEntries.remove(System.identityHashCode(entry));
}
}
Expand All @@ -561,6 +568,7 @@ public void listen(IndexRemovedEvent event) {
BackgroundTask.wrap(() -> {
for (BibEntry entry : event.entries()) {
searchGroup.updateMatches(entry);
// ADR-0038
matchedEntries.remove(System.identityHashCode(entry));
}
}).executeWith(taskExecutor);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/model/groups/SearchGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void updateMatches() {
}
matchedEntries.clear();
// TODO: Search should be done in a background thread
// ADR-0038
luceneManager.search(query).getMatchedEntries().forEach(entry -> matchedEntries.put(System.identityHashCode(entry), entry));
}

Expand All @@ -82,8 +83,10 @@ public void updateMatches(BibEntry entry) {
return;
}
if (luceneManager.isMatched(entry, query)) {
// ADR-0038
matchedEntries.put(System.identityHashCode(entry), entry);
} else {
// ADR-0038
matchedEntries.remove(System.identityHashCode(entry));
}
}
Expand All @@ -104,6 +107,7 @@ public boolean equals(Object o) {

@Override
public boolean contains(BibEntry entry) {
// ADR-0038
return matchedEntries.containsKey(System.identityHashCode(entry));
}

Expand Down

0 comments on commit cd71f11

Please sign in to comment.