Skip to content

Commit e333cdc

Browse files
committed
check then put
1 parent 643c574 commit e333cdc

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/FirstDocIdGroupingAggregatorFunction.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.core.RefCounted;
2323
import org.elasticsearch.core.Releasables;
2424

25+
import java.util.Collections;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
@@ -169,11 +170,11 @@ private void collectOneDoc(int groupId, DocVector docVector, int valuePosition)
169170
segments.set(groupId, docVector.segments().getInt(valuePosition));
170171
docIds = bigArrays.grow(docIds, groupId + 1);
171172
docIds.set(groupId, docVector.docs().getInt(valuePosition));
172-
contextRefs.computeIfAbsent(shard, s -> {
173+
if (contextRefs.containsKey(shard) == false) {
173174
RefCounted refCounted = docVector.shardRefCounted().get(shard);
174175
refCounted.incRef();
175-
return refCounted;
176-
});
176+
contextRefs.put(shard, refCounted);
177+
}
177178
}
178179

179180
@Override
@@ -226,12 +227,8 @@ public void evaluateIntermediate(Block[] blocks, int offset, IntVector selected)
226227
try {
227228
segmentVector = segmentBuilder.build();
228229
docVector = docBuilder.build();
229-
var shardRefs = new HashMap<>(contextRefs);
230-
try {
231-
blocks[offset] = new DocVector(shardRefs::get, shardVector, segmentVector, docVector, null).asBlock();
232-
} catch (Exception e) {
233-
throw e;
234-
}
230+
var unmodifiedContextRefs = Collections.unmodifiableMap(contextRefs);
231+
blocks[offset] = new DocVector(unmodifiedContextRefs::get, shardVector, segmentVector, docVector, null).asBlock();
235232
} finally {
236233
if (blocks[offset] == null) {
237234
Releasables.closeExpectNoException(shardVector, segmentVector, docVector);
@@ -246,7 +243,6 @@ public void close() {
246243
for (RefCounted ref : contextRefs.values()) {
247244
ref.decRef();
248245
}
249-
contextRefs.clear();
250246
});
251247
}
252248

0 commit comments

Comments
 (0)