Skip to content

Commit

Permalink
optimize getIndices in IndicesSegmentResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
mushao999 committed Oct 29, 2021
1 parent 5a41fa4 commit 8b8e29b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class IndexSegments implements Iterable<IndexShardSegments> {

private final Map<Integer, IndexShardSegments> indexShards;

IndexSegments(String index, ShardSegments[] shards) {
IndexSegments(String index, List<ShardSegments> shards) {
this.index = index;

Map<Integer, List<ShardSegments>> tmpIndexShards = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class IndicesSegmentResponse extends BroadcastResponse {

Expand Down Expand Up @@ -61,19 +59,16 @@ public Map<String, IndexSegments> getIndices() {
}
Map<String, IndexSegments> indicesSegments = new HashMap<>();

Set<String> indices = new HashSet<>();
Map<String, List<ShardSegments>> tmpIndicesSegment = new HashMap<>();
for (ShardSegments shard : shards) {
indices.add(shard.getShardRouting().getIndexName());
List<ShardSegments> indexSegments = tmpIndicesSegment.computeIfAbsent(
shard.getShardRouting().getIndexName(),
k -> new ArrayList<>()
);
indexSegments.add(shard);
}

for (String indexName : indices) {
List<ShardSegments> shards = new ArrayList<>();
for (ShardSegments shard : this.shards) {
if (shard.getShardRouting().getIndexName().equals(indexName)) {
shards.add(shard);
}
}
indicesSegments.put(indexName, new IndexSegments(indexName, shards.toArray(new ShardSegments[shards.size()])));
for (Map.Entry<String, List<ShardSegments>> entry : tmpIndicesSegment.entrySet()) {
indicesSegments.put(entry.getKey(), new IndexSegments(entry.getKey(), entry.getValue()));
}
this.indicesSegments = indicesSegments;
return indicesSegments;
Expand Down

0 comments on commit 8b8e29b

Please sign in to comment.