From 241ca48e8395c116d44eb802f717fdf973ef6881 Mon Sep 17 00:00:00 2001 From: Harsh Garg Date: Fri, 30 Aug 2024 09:47:56 +0530 Subject: [PATCH] Fixing variable names Signed-off-by: Harsh Garg --- .../segments/IndicesSegmentResponse.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/org/opensearch/action/admin/indices/segments/IndicesSegmentResponse.java b/server/src/main/java/org/opensearch/action/admin/indices/segments/IndicesSegmentResponse.java index 4c59fbcf0364d..ad37887ed947a 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/segments/IndicesSegmentResponse.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/segments/IndicesSegmentResponse.java @@ -84,22 +84,30 @@ public Map getIndices() { if (indicesSegments != null) { return indicesSegments; } - - Arrays.sort(shards, Comparator.comparing(shardSegment -> shardSegment.getShardRouting().getIndexName())); Map indicesSegments = new HashMap<>(); + if (shards.length == 0) { + this.indicesSegments = indicesSegments; + return indicesSegments; + } - int lastIdx = 0; + Arrays.sort(shards, Comparator.comparing(shardSegment -> shardSegment.getShardRouting().getIndexName())); + int startIndexPos = 0; + String startIndexName = shards[startIndexPos].getShardRouting().getIndexName(); 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))); + if (!shards[i].getShardRouting().getIndexName().equals(startIndexName)) { + indicesSegments.put(startIndexName, new IndexSegments(startIndexName, Arrays.copyOfRange(shards, startIndexPos, i))); + startIndexPos = i; + startIndexName = shards[startIndexPos].getShardRouting().getIndexName(); } } + // Add the last shardSegment from shards list which would have got missed in the loop above + indicesSegments.put( + shards[shards.length - 1].getShardRouting().getIndexName(), + new IndexSegments( + shards[shards.length - 1].getShardRouting().getIndexName(), + Arrays.copyOfRange(shards, startIndexPos, shards.length) + ) + ); this.indicesSegments = indicesSegments; return indicesSegments;