Skip to content

Commit

Permalink
Avoid creating new map for storing index to shardsSegments
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Garg <[email protected]>
  • Loading branch information
Harsh Garg committed Aug 19, 2024
1 parent cdc2809 commit 637dccb
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
import org.opensearch.index.engine.Segment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -83,19 +84,21 @@ public Map<String, IndexSegments> getIndices() {
if (indicesSegments != null) {
return indicesSegments;
}

Arrays.sort(shards, Comparator.comparing(shardSegment -> shardSegment.getShardRouting().getIndexName()));
Map<String, IndexSegments> indicesSegments = new HashMap<>();

Map<String, List<ShardSegments>> indicesShardSegments = new HashMap<>();
for (ShardSegments shard : shards) {
List<ShardSegments> shardSegList = indicesShardSegments.get(shard.getShardRouting().getIndexName());
if (shardSegList == null) {
shardSegList = new ArrayList<>();
indicesShardSegments.put(shard.getShardRouting().getIndexName(), shardSegList);
int lastIdx = 0;
for (int i = 0; i < shards.length; i++) {
String lastIndexName = shards[lastIdx].getShardRouting().getIndexName();
String currentIndexName = shards[i].getShardRouting().getIndexName();
if (!currentIndexName.equals(lastIndexName)) {
indicesSegments.put(lastIndexName, new IndexSegments(lastIndexName, Arrays.copyOfRange(shards, lastIdx, i)));
lastIdx = i;
}
if (i == shards.length - 1) {
indicesSegments.put(currentIndexName, new IndexSegments(currentIndexName, Arrays.copyOfRange(shards, lastIdx, i + 1)));
}
shardSegList.add(shard);
}
for (Map.Entry<String, List<ShardSegments>> entry : indicesShardSegments.entrySet()) {
indicesSegments.put(entry.getKey(), new IndexSegments(entry.getKey(), entry.getValue().toArray(new ShardSegments[0])));
}

this.indicesSegments = indicesSegments;
Expand Down

0 comments on commit 637dccb

Please sign in to comment.