Skip to content

Commit 2d5d646

Browse files
authored
Improve IdSubstitutionMap performance (#7156)
* Improve IdSubstitutionMap performance * Add changelog
1 parent 2426c44 commit 2d5d646

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
type: perf
3+
issue: 7156
4+
title: "When performing FHIR transaction operations which create a large number of resources,
5+
a CPU bottleneck in the transaction processor was resolved."

hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/dao/IdSubstitutionMap.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import org.apache.commons.lang3.tuple.Pair;
2525
import org.hl7.fhir.instance.model.api.IIdType;
2626

27+
import java.util.Collection;
2728
import java.util.HashMap;
2829
import java.util.List;
2930
import java.util.Map;
30-
import java.util.Objects;
3131
import java.util.stream.Collectors;
3232

3333
public class IdSubstitutionMap {
@@ -80,8 +80,10 @@ public List<Pair<IIdType, IIdType>> entrySet() {
8080
}
8181

8282
public void put(IIdType theSource, IIdType theTarget) {
83-
myMap.put(new Entry(theSource), new Entry(theTarget));
84-
myReverseMap.put(new Entry(theTarget), new Entry(theSource));
83+
Entry sourceEntry = new Entry(theSource);
84+
Entry targetEntry = new Entry(theTarget);
85+
myMap.put(sourceEntry, targetEntry);
86+
myReverseMap.put(targetEntry, sourceEntry);
8587
}
8688

8789
public boolean isEmpty() {
@@ -93,15 +95,16 @@ public boolean isEmpty() {
9395
* the same ResourceType and IdPart as the target id.
9496
*/
9597
public void updateTargets(IIdType theNewId) {
96-
if (theNewId == null) {
98+
if (theNewId == null || theNewId.getValue() == null) {
9799
return;
98100
}
99-
String newUnqualifiedVersionLessId = theNewId.toUnqualifiedVersionless().getValue();
100-
entrySet().stream()
101-
.map(Pair::getValue)
102-
.filter(targetId ->
103-
Objects.equals(targetId.toUnqualifiedVersionless().getValue(), newUnqualifiedVersionLessId))
104-
.forEach(targetId -> targetId.setValue(theNewId.getValue()));
101+
102+
Entry newEntry = new Entry(theNewId);
103+
Collection<Entry> targets = myReverseMap.removeAll(newEntry);
104+
for (Entry nextTarget : targets) {
105+
myMap.put(nextTarget, newEntry);
106+
}
107+
myReverseMap.putAll(newEntry, targets);
105108
}
106109

107110
private static class Entry {
@@ -135,6 +138,11 @@ public boolean equals(Object theOther) {
135138
public int hashCode() {
136139
return myUnversionedId.hashCode();
137140
}
141+
142+
@Override
143+
public String toString() {
144+
return "Entry[" + "myUnversionedId='" + myUnversionedId + "', myId=" + myId + ']';
145+
}
138146
}
139147

140148
static String toVersionlessValue(IIdType theId) {

0 commit comments

Comments
 (0)