public List getPartitionRangesWithCoverage()

in spanner-data-validator-java/src/main/java/com/google/migration/partitioning/IntegerPartitionRangeListFetcher.java [45:109]


  public List<PartitionRange> getPartitionRangesWithCoverage(String startStr,
      String endStr,
      Integer partitionCount,
      BigDecimal coveragePercent) {

    Integer start = Integer.parseInt(startStr);
    Integer end = Integer.parseInt(endStr);
    Integer fullRange = end - start;
    Integer stepSize = fullRange/partitionCount;
    Integer constrainedStepSize = stepSize;

    if(coveragePercent.compareTo(BigDecimal.ONE) > 0) {
      throw new IllegalArgumentException("Coverage percent must be <= 1");
    }

    Boolean partialCoverage = (coveragePercent.compareTo(BigDecimal.ONE) < 0);

    // Simple implementation of "coverage" - just reduce the step size
    if(partialCoverage) {
      constrainedStepSize = BigDecimal.valueOf(stepSize).multiply(coveragePercent).intValue();

      LOG.info(String.format("Constrained step size: %d in "
          + "IntegerPartitionRangeListFetcher.getPartitionRangesWithCoverage", constrainedStepSize));

      if(constrainedStepSize <= 0) {
        throw new RuntimeException("Integer step size <= 0!");
      }
    }

    ArrayList<PartitionRange> bRanges = new ArrayList<>();

    if(partitionCount <= 0) {
      throw new IllegalArgumentException("Partition count must be > 0");
    } else if (partitionCount == 1) {
      String calculatedEndRangeStr = endStr;

      if(partialCoverage) {
        calculatedEndRangeStr = String.valueOf(start + constrainedStepSize);
      }

      PartitionRange range = new PartitionRange(startStr, calculatedEndRangeStr);
      bRanges.add(range);
    } else {
      Integer maxRange = start - 1;
      for (Integer i = 0; i < partitionCount - 1; i++) {
        Integer minRange = maxRange + 1;
        maxRange = minRange + constrainedStepSize - 1;

        PartitionRange range = new PartitionRange(minRange.toString(), maxRange.toString());

        bRanges.add(range);

        maxRange = minRange + stepSize - 1;
      }

      Integer calculatedEndRange = maxRange + constrainedStepSize;
      if (!partialCoverage) {
        calculatedEndRange = end;
      }
      PartitionRange range = new PartitionRange(maxRange.toString(), calculatedEndRange.toString());
      bRanges.add(range);
    }

    return bRanges;
  }