[ASTERIXDB-3394][STO] Follow up patch for addressing comments

Change-Id: I7beaa28e76e9a4c649dbcd331665479b1a582684
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18284
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ritik Raj <raj.ritik9835@gmail.com>
Reviewed-by: Wail Alkowaileet <wael.y.k@gmail.com>
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/SizeBoundedConcurrentMergePolicy.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/SizeBoundedConcurrentMergePolicy.java
index 1384f39..78fbf99 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/SizeBoundedConcurrentMergePolicy.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/SizeBoundedConcurrentMergePolicy.java
@@ -115,15 +115,32 @@
         }
     }
 
-    // getMergableIndexesRange gives the [startComponentIndex, endComponentIndex] which can be merged based on the provided condition
-    // 1. localMinMergeComponentCount <= range(startComponentIndex, endComponentIndex) <= maxMergeComponentCount
-    // 2. sumOfComponents(startComponentIndex, endComponentIndex) < maxComponentSize
-    // In order to satisfy range(startComponentIndex, endComponentIndex) <= maxMergeComponentCount, the search range is bounded by leftBoundary, and if we reach the maxComponentSize before
-    // reaching the leftBoundary, we get our boundary where startIndex > leftBoundary.
-    // but in case the components in the range does not contribute enough to exceed maxComponentSize then the candidate range
-    // will be [leftBoundary, endComponentIndex] which satisfies both 1 & 2.
+    /**
+     * <p>
+     * getMergableIndexesRange gives the [startComponentIndex, endComponentIndex] which can be merged based on the
+     * provided condition.
+     * </p>
+     * <ol>
+     *     <li>localMinMergeComponentCount <= range(startComponentIndex, endComponentIndex) <= maxMergeComponentCount</li>
+     *     <li>sumOfComponents(startComponentIndex, endComponentIndex) < maxComponentSize</li>
+     * </ol>
+     *
+     *<p>
+     * In order to satisfy range(startComponentIndex, endComponentIndex) <= maxMergeComponentCount,
+     * search range is bounded by leftBoundary, and if we reach the maxComponentSize before reaching the leftBoundary,
+     * we get our boundary where startIndex > leftBoundary.
+     *</p>
+     * <p>
+     * but in case the components in the range does not contribute enough to exceed maxComponentSize then the candidate
+     * range will be [leftBoundary, endComponentIndex] which satisfies both 1 & 2.
+     *</p>
+     * @param diskComponents The disk components within an Index
+     * @param localMinMergeComponentCount The min count of contiguous components required to call a mergable range.
+     * @param countFlag if enabled, will count all the components that can be merged, else will return on first found range
+     * @return MergableSolution
+     */
     private MergableSolution getMergableIndexesRange(List<ILSMDiskComponent> diskComponents,
-            int localMinMergeComponentCount, boolean getMergableComponentsCount) {
+            int localMinMergeComponentCount, boolean countFlag) {
         int numComponents = diskComponents.size();
         int candidateComponentsCount = 0;
         for (; candidateComponentsCount < numComponents; candidateComponentsCount++) {
@@ -134,7 +151,7 @@
         }
 
         if (candidateComponentsCount < localMinMergeComponentCount) {
-            return new MergableSolution(null, 0);
+            return MergableSolution.NO_SOLUTION;
         }
 
         // count the total number of non-overlapping components that can be merged.
@@ -165,13 +182,13 @@
                             (resultantComponentSize - currentComponentSize - lastComponentSize), sizeRatio,
                             lastComponentSize)) {
                         int mergableRangeCount = endComponentIndex - probableStartIndex;
-                        if (!getMergableComponentsCount) {
-                            return new MergableSolution(new Range(probableStartIndex + 1, endComponentIndex),
-                                    mergableRangeCount);
-                        } else {
+                        if (countFlag) {
                             aRangeFound = true;
                             endComponentIndex = probableStartIndex;
                             totalMergableComponentsCount += mergableRangeCount;
+                        } else {
+                            return new MergableSolution(new Range(probableStartIndex + 1, endComponentIndex),
+                                    mergableRangeCount);
                         }
                     }
                     // break as we already exceeded the maxComponentSize, and still haven't found the suitable range,
@@ -185,13 +202,13 @@
                 if (isRangeMergable(leftBoundaryIndex, endComponentIndex, localMinMergeComponentCount,
                         (resultantComponentSize - lastComponentSize), sizeRatio, lastComponentSize)) {
                     int mergableRangeCount = endComponentIndex - leftBoundaryIndex + 1;
-                    if (!getMergableComponentsCount) {
-                        return new MergableSolution(new Range(leftBoundaryIndex, endComponentIndex),
-                                mergableRangeCount);
-                    } else {
+                    if (countFlag) {
                         aRangeFound = true;
                         endComponentIndex = probableStartIndex;
                         totalMergableComponentsCount += mergableRangeCount;
+                    } else {
+                        return new MergableSolution(new Range(leftBoundaryIndex, endComponentIndex),
+                                mergableRangeCount);
                     }
                 }
             }
@@ -201,7 +218,7 @@
             }
         }
 
-        return new MergableSolution(null, totalMergableComponentsCount);
+        return new MergableSolution(totalMergableComponentsCount);
     }
 
     private void triggerScheduleMerge(ILSMIndex index, List<ILSMDiskComponent> diskComponents, int startIndex,
@@ -211,9 +228,15 @@
     }
 
     static class MergableSolution {
+        public static final MergableSolution NO_SOLUTION = new MergableSolution(null, 0);
         private final Range range;
         private final int componentsCount;
 
+        MergableSolution(int componentsCount) {
+            range = null;
+            this.componentsCount = componentsCount;
+        }
+
         MergableSolution(Range range, int componentsCount) {
             this.range = range;
             this.componentsCount = componentsCount;