From e652170188fadfb5cece0365f6b60892d5279286 Mon Sep 17 00:00:00 2001 From: mushao999 Date: Fri, 29 Oct 2021 20:07:08 +0800 Subject: [PATCH] update code from David's suggestion --- .../action/admin/indices/segments/IndexSegments.java | 11 +++-------- .../indices/segments/IndicesSegmentResponse.java | 11 +++++------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndexSegments.java b/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndexSegments.java index fe501e41f2d9a..3107727f5174d 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndexSegments.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndexSegments.java @@ -23,17 +23,12 @@ public class IndexSegments implements Iterable { IndexSegments(String index, List shards) { this.index = index; - Map> tmpIndexShards = new HashMap<>(); + final Map> segmentsByShardId = new HashMap<>(); for (ShardSegments shard : shards) { - List lst = tmpIndexShards.get(shard.getShardRouting().id()); - if (lst == null) { - lst = new ArrayList<>(); - tmpIndexShards.put(shard.getShardRouting().id(), lst); - } - lst.add(shard); + segmentsByShardId.computeIfAbsent(shard.getShardRouting().id(), k -> new ArrayList<>()).add(shard); } indexShards = new HashMap<>(); - for (Map.Entry> entry : tmpIndexShards.entrySet()) { + for (Map.Entry> entry : segmentsByShardId.entrySet()) { indexShards.put( entry.getKey(), new IndexShardSegments( diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java index 340201be57b1a..32d264907c054 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java @@ -35,7 +35,7 @@ public class IndicesSegmentResponse extends BroadcastResponse { private final ShardSegments[] shards; - private Map indicesSegments; + private volatile Map indicesSegments; IndicesSegmentResponse(StreamInput in) throws IOException { super(in); @@ -59,15 +59,14 @@ public Map getIndices() { } Map indicesSegments = new HashMap<>(); - Map> tmpIndicesSegment = new HashMap<>(); + final Map> segmentsByIndex = new HashMap<>(); for (ShardSegments shard : shards) { - List indexSegments = tmpIndicesSegment.computeIfAbsent( + segmentsByIndex.computeIfAbsent( shard.getShardRouting().getIndexName(), k -> new ArrayList<>() - ); - indexSegments.add(shard); + ).add(shard); } - for (Map.Entry> entry : tmpIndicesSegment.entrySet()) { + for (Map.Entry> entry : segmentsByIndex.entrySet()) { indicesSegments.put(entry.getKey(), new IndexSegments(entry.getKey(), entry.getValue())); } this.indicesSegments = indicesSegments;