Skip to content

Commit

Permalink
DBZ-8163 Simplify shard lineage
Browse files Browse the repository at this point in the history
  • Loading branch information
twthorn committed Aug 22, 2024
1 parent b723547 commit dab6558
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ public static Long getInheritedEpoch(String shardString, ShardEpochMap shardEpoc

private static class Shard {

private final ShardBound lowerBound;
private final ShardBound upperBound;
// A string lexicographically less than all other strings
public static final String NEGATIVE_INFINITY = "";
// A string lexicographically greater than all other strings
public static final String POSITIVE_INFINITY = "\uFFFF";

private final String lowerBound;
private final String upperBound;

Shard(String shard) {
String[] shardInterval = getShardInterval(shard);
String[] shardInterval = getShardInterval(shard.toLowerCase());
this.lowerBound = getLowerBound(shardInterval);
this.upperBound = getUpperBound(shardInterval);
validateBounds();
Expand All @@ -61,18 +66,18 @@ public boolean overlaps(Shard shard) {
return this.lowerBound.compareTo(shard.upperBound) < 0 && this.upperBound.compareTo(shard.lowerBound) > 0;
}

private static ShardBound getLowerBound(String[] shardInterval) {
private static String getLowerBound(String[] shardInterval) {
if (shardInterval.length < 1 || shardInterval[0].isEmpty()) {
return ShardBound.negativeInfinity();
return NEGATIVE_INFINITY;
}
return new ShardBound(shardInterval[0]);
return shardInterval[0];
}

private static ShardBound getUpperBound(String[] shardInterval) {
private static String getUpperBound(String[] shardInterval) {
if (shardInterval.length != 2 || shardInterval[1].isEmpty()) {
return ShardBound.positiveInfinity();
return POSITIVE_INFINITY;
}
return new ShardBound(shardInterval[1]);
return shardInterval[1];
}

private static String[] getShardInterval(String shard) {
Expand All @@ -82,46 +87,9 @@ private static String[] getShardInterval(String shard) {
@Override
public String toString() {
return "Shard{" +
"lowerBound=" + lowerBound.bound +
", upperBound=" + upperBound.bound +
"lowerBound=" + lowerBound +
", upperBound=" + upperBound +
"}";
}

private static class ShardBound implements Comparable<ShardBound> {
public static final String NEGATIVE_INFINITY = "NEG_INF";
public static final String POSITIVE_INFINITY = "POS_INF";
private final String bound;

ShardBound(String bound) {
this.bound = bound;
}

public static ShardBound negativeInfinity() {
return new ShardBound(NEGATIVE_INFINITY);
}

public static ShardBound positiveInfinity() {
return new ShardBound(POSITIVE_INFINITY);
}

@Override
public int compareTo(ShardBound o) {
if (this.bound.equals(NEGATIVE_INFINITY) || o.bound.equals(POSITIVE_INFINITY)) {
return -1;
}
if (this.bound.equals(POSITIVE_INFINITY) || o.bound.equals(NEGATIVE_INFINITY)) {
return 1;
}
return this.bound.compareTo(o.bound);
}

@Override
public String toString() {
return "Bound{" +
"value=" + this.bound +
"}";
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,20 @@ public void shouldGetInheritedEpoch_OneToTwoShardsHigherHexRange() {
Long epoch2 = ShardLineage.getInheritedEpoch("b750-b820", shardEpochMap);
assertThat(epoch2).isEqualTo(parentEpoch1 + 1);
}

@Test
public void shouldGetInheritedEpoch_OneToTwoShardsMixedCase() {
Long parentEpoch1 = 4L;
ShardEpochMap shardEpochMap = new ShardEpochMap(Map.of("B7-B8", parentEpoch1));
Long epoch = ShardLineage.getInheritedEpoch("b720-b750", shardEpochMap);
assertThat(epoch).isEqualTo(parentEpoch1 + 1);
Long epoch2 = ShardLineage.getInheritedEpoch("b750-b820", shardEpochMap);
assertThat(epoch2).isEqualTo(parentEpoch1 + 1);

ShardEpochMap shardEpochMap2 = new ShardEpochMap(Map.of("b7-b8", parentEpoch1));
Long epoch3 = ShardLineage.getInheritedEpoch("B720-B750", shardEpochMap);
assertThat(epoch3).isEqualTo(parentEpoch1 + 1);
Long epoch4 = ShardLineage.getInheritedEpoch("B750-B820", shardEpochMap);
assertThat(epoch4).isEqualTo(parentEpoch1 + 1);
}
}

0 comments on commit dab6558

Please sign in to comment.