in spanner-data-validator-java/src/main/java/com/google/migration/partitioning/LongPartitionRangeListFetcher.java [46:111]
public List<PartitionRange> getPartitionRangesWithCoverage(String startStr,
String endStr,
Integer partitionCount,
BigDecimal coveragePercent) {
Long start = Long.parseLong(startStr);
Long end = Long.parseLong(endStr);
BigInteger fullRange = BigInteger.valueOf(end).subtract(BigInteger.valueOf(start));
Long stepSize = fullRange.divide(BigInteger.valueOf(partitionCount)).longValue();
Long 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).longValue();
LOG.info(String.format("Constrained step size: %d in "
+ "LongPartitionRangeListFetcher.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 {
Long maxRange = start - 1;
for (Integer i = 0; i < partitionCount - 1; i++) {
Long minRange = maxRange + 1;
maxRange = minRange + constrainedStepSize - 1;
PartitionRange range = new PartitionRange(minRange.toString(), maxRange.toString());
bRanges.add(range);
maxRange = minRange + stepSize - 1;
}
Long calculatedEndRange = maxRange + constrainedStepSize;
if (!partialCoverage) {
calculatedEndRange = end;
}
PartitionRange range = new PartitionRange(maxRange.toString(), calculatedEndRange.toString());
bRanges.add(range);
}
return bRanges;
}