public List getPartitionRangesWithCoverage()

in spanner-data-validator-java/src/main/java/com/google/migration/partitioning/UUIDPartitionRangeListFetcher.java [47:117]


  public List<PartitionRange> getPartitionRangesWithCoverage(String startStr,
      String endStr,
      Integer partitionCount,
      BigDecimal coveragePercent) {
    UUID start = UUID.fromString(startStr);
    UUID end = UUID.fromString(endStr);

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

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

    // UUID max
    BigInteger uuidMax = UUIDHelpers.uuidToBigInt(end);
    BigInteger uuidMin = UUIDHelpers.uuidToBigInt(start);
    BigInteger fullRange = uuidMax.subtract(uuidMin);
    BigInteger stepSize = fullRange.divide(BigInteger.valueOf(partitionCount.intValue()));
    BigInteger constrainedStepSize = stepSize;

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

      LOG.info(String.format("Step size: %s in "
          + "UUIDPartitionRangeListFetcher.getPartitionRangesWithCoverage", stepSize));

      if(constrainedStepSize.compareTo(BigInteger.ONE) <= 0) {
        throw new RuntimeException("UUID constrainedStepSize 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) {
        BigInteger calculatedEndRange = UUIDHelpers.uuidToBigInt(start).add(constrainedStepSize);
        calculatedEndRangeStr = UUIDHelpers.bigIntToUUID(calculatedEndRange).toString();
      }

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

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

        bRanges.add(range);

        maxRange = minRange.add(stepSize).subtract(BigInteger.ONE);
      }

      BigInteger calculatedEndRange = maxRange.add(constrainedStepSize);
      if (!partialCoverage) {
        calculatedEndRange = uuidMax;
      }
      PartitionRange range = new PartitionRange(UUIDHelpers.bigIntToUUID(maxRange).toString(),
          UUIDHelpers.bigIntToUUID(calculatedEndRange).toString());
      bRanges.add(range);
    }

    return bRanges;
  }