private static void calculateTableMerges(int maxMergeCount, List<SortedSet<RegionInfo>> merges,
    Collection<Pair<RegionInfo, RegionInfo>> overlaps) {
    SortedSet<RegionInfo> currentMergeSet = new TreeSet<>();
    HashSet<RegionInfo> regionsInMergeSet = new HashSet<>();
    RegionInfo regionInfoWithlargestEndKey = null;
    for (Pair<RegionInfo, RegionInfo> pair : overlaps) {
      if (regionInfoWithlargestEndKey != null) {
        if (
          !isOverlap(regionInfoWithlargestEndKey, pair) || currentMergeSet.size() >= maxMergeCount
        ) {
          // Log when we cut-off-merge because we hit the configured maximum merge limit.
          if (currentMergeSet.size() >= maxMergeCount) {
            LOG.warn("Ran into maximum-at-a-time merges limit={}", maxMergeCount);
          }

          // In the case of the merge set contains only 1 region or empty, it does not need to
          // submit this merge request as no merge is going to happen. currentMergeSet can be
          // reused in this case.
          if (currentMergeSet.size() <= 1) {
            for (RegionInfo ri : currentMergeSet) {
              regionsInMergeSet.remove(ri);
            }
            currentMergeSet.clear();
          } else {
            merges.add(currentMergeSet);
            currentMergeSet = new TreeSet<>();
          }
        }
      }

      // Do not add the same region into multiple merge set, this will fail
      // the second merge request.
      if (!regionsInMergeSet.contains(pair.getFirst())) {
        currentMergeSet.add(pair.getFirst());
        regionsInMergeSet.add(pair.getFirst());
      }
      if (!regionsInMergeSet.contains(pair.getSecond())) {
        currentMergeSet.add(pair.getSecond());
        regionsInMergeSet.add(pair.getSecond());
      }

      regionInfoWithlargestEndKey = getRegionInfoWithLargestEndKey(
        getRegionInfoWithLargestEndKey(pair.getFirst(), pair.getSecond()),
        regionInfoWithlargestEndKey);
    }
    merges.add(currentMergeSet);
  }
Run through <code>overlaps</code> and return a list of merges to run. Presumes overlaps are
ordered (which they are coming out of the CatalogJanitor consistency report).
@param maxMergeCount Maximum regions to merge at a time (avoid merging 100k regions in one go!)
hbase-server/src/main/java/org/apache/hadoop/hbase/master/janitor/MetaFixer.java