Skip to content

Commit

Permalink
update code from David's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
mushao999 committed Oct 29, 2021
1 parent 8b8e29b commit e652170
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ public class IndexSegments implements Iterable<IndexShardSegments> {
IndexSegments(String index, List<ShardSegments> shards) {
this.index = index;

Map<Integer, List<ShardSegments>> tmpIndexShards = new HashMap<>();
final Map<Integer, List<ShardSegments>> segmentsByShardId = new HashMap<>();
for (ShardSegments shard : shards) {
List<ShardSegments> 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<Integer, List<ShardSegments>> entry : tmpIndexShards.entrySet()) {
for (Map.Entry<Integer, List<ShardSegments>> entry : segmentsByShardId.entrySet()) {
indexShards.put(
entry.getKey(),
new IndexShardSegments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class IndicesSegmentResponse extends BroadcastResponse {

private final ShardSegments[] shards;

private Map<String, IndexSegments> indicesSegments;
private volatile Map<String, IndexSegments> indicesSegments;

IndicesSegmentResponse(StreamInput in) throws IOException {
super(in);
Expand All @@ -59,15 +59,14 @@ public Map<String, IndexSegments> getIndices() {
}
Map<String, IndexSegments> indicesSegments = new HashMap<>();

Map<String, List<ShardSegments>> tmpIndicesSegment = new HashMap<>();
final Map<String, List<ShardSegments>> segmentsByIndex = new HashMap<>();
for (ShardSegments shard : shards) {
List<ShardSegments> indexSegments = tmpIndicesSegment.computeIfAbsent(
segmentsByIndex.computeIfAbsent(
shard.getShardRouting().getIndexName(),
k -> new ArrayList<>()
);
indexSegments.add(shard);
).add(shard);
}
for (Map.Entry<String, List<ShardSegments>> entry : tmpIndicesSegment.entrySet()) {
for (Map.Entry<String, List<ShardSegments>> entry : segmentsByIndex.entrySet()) {
indicesSegments.put(entry.getKey(), new IndexSegments(entry.getKey(), entry.getValue()));
}
this.indicesSegments = indicesSegments;
Expand Down

0 comments on commit e652170

Please sign in to comment.