From 5f1b315a5426a211340b902c235a79427a7ab06c Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 13 Nov 2026 21:48:16 +0000 Subject: [PATCH 1/4] fix(bigtable): avoid emitting empty open range (K, K\0) in RowSetUtil --- .../bigtable/data/v2/internal/RowSetUtil.java | 127 +++++------------- .../data/v2/internal/RowSetUtilTest.java | 70 ++++++++++ 2 files changed, 104 insertions(+), 93 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java index 4f44c5fd15da..f44f0e36bb56 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java @@ -47,20 +47,20 @@ */ @InternalApi public final class RowSetUtil { + private static final ByteString NULL_BYTE = ByteString.copyFrom(new byte[] {0}); + private RowSetUtil() {} /** Removes the {@code #excludePoint} rowkey from the {@code RowSet} */ public static RowSet eraseLargeRow(RowSet rowSet, ByteString excludePoint) { Preconditions.checkArgument(!excludePoint.isEmpty(), "Can't handle empty excludePoint"); - RowSet.Builder newRowSet = RowSet.newBuilder(); - if (rowSet.getRowKeysList().isEmpty() && rowSet.getRowRangesList().isEmpty()) { - // querying range (, excludePoint) and (excludePoint, ) - newRowSet.addRowRanges(RowRange.newBuilder().setEndKeyOpen(excludePoint).build()); - newRowSet.addRowRanges(RowRange.newBuilder().setStartKeyOpen(excludePoint).build()); + rowSet = RowSet.newBuilder().addRowRanges(RowRange.getDefaultInstance()).build(); } + RowSet.Builder newRowSet = RowSet.newBuilder(); + // remove large row key from point reads rowSet.getRowKeysList().stream() .filter(k -> !k.equals(excludePoint)) @@ -68,9 +68,13 @@ public static RowSet eraseLargeRow(RowSet rowSet, ByteString excludePoint) { // Handle ranges for (RowRange rowRange : rowSet.getRowRangesList()) { - List afterSplit = splitOnLargeRowKey(rowRange, excludePoint); - if (!afterSplit.isEmpty()) { - afterSplit.forEach(newRowSet::addRowRanges); + RowRange before = truncateRange(rowRange, excludePoint, false); + if (before != null) { + newRowSet.addRowRanges(before); + } + RowRange after = truncateRange(rowRange, excludePoint, true); + if (after != null) { + newRowSet.addRowRanges(after); } } @@ -134,72 +138,22 @@ private static RowRange truncateRange(RowRange range, ByteString split, boolean if (EndPoint.extract(range).compareTo(new EndPoint(split, true)) <= 0) { return null; } - } else { - // range is on or right of the split - if (StartPoint.extract(range).compareTo(new StartPoint(split, true)) >= 0) { - return null; - } - } - RowRange.Builder newRange = range.toBuilder(); - - if (fromStart) { // range start is on or left of the split if (StartPoint.extract(range).compareTo(new StartPoint(split, true)) <= 0) { - newRange.setStartKeyOpen(split); + return range.toBuilder().setStartKeyOpen(split).build(); } + return range; } else { + // range is on or right of the split: skip + if (StartPoint.extract(range).compareTo(new StartPoint(split, true)) >= 0) { + return null; + } // range end is on or right of the split if (EndPoint.extract(range).compareTo(new EndPoint(split, true)) >= 0) { - newRange.setEndKeyOpen(split); - } - } - - return newRange.build(); - } - - /** This method erases the {@code #split} key from the range */ - private static List splitOnLargeRowKey(RowRange range, ByteString largeRowKey) { - List rowRanges = new ArrayList<>(); - - ByteString startKey = StartPoint.extract(range).value; - ByteString endKey = EndPoint.extract(range).value; - - // Empty endKey means it's unbounded - boolean boundedEnd = !endKey.isEmpty(); - - // if end key is on the left of large row key, don't split. - if (boundedEnd && ByteStringComparator.INSTANCE.compare(endKey, largeRowKey) < 0) { - rowRanges.add(range); - return rowRanges; - } - - // if start key is on the right of the large row key, don't split - if (ByteStringComparator.INSTANCE.compare(startKey, largeRowKey) > 0) { - rowRanges.add(range); - return rowRanges; - } - - // if start key is on the left of the large row key, set the end key to be large row key open - if (ByteStringComparator.INSTANCE.compare(startKey, largeRowKey) < 0) { - RowRange beforeSplit = range.toBuilder().setEndKeyOpen(largeRowKey).build(); - rowRanges.add(beforeSplit); - } - - // if the end key is on the right of the large row key, set the start key to be large row key - // open. - if (!boundedEnd || ByteStringComparator.INSTANCE.compare(endKey, largeRowKey) > 0) { - // handle the edge case where (key, key\0) is an empty range and should be excluded - ByteString nextKey = largeRowKey.concat(ByteString.copyFrom(new byte[] {0})); - EndPoint endPoint = EndPoint.extract(range); - boolean isEmptyRange = !endPoint.isClosed && endPoint.value.equals(nextKey); - - if (!isEmptyRange) { - RowRange afterSplit = range.toBuilder().setStartKeyOpen(largeRowKey).build(); - rowRanges.add(afterSplit); + return range.toBuilder().setEndKeyOpen(split).build(); } + return range; } - - return rowRanges; } /** @@ -250,16 +204,9 @@ public static List shard( // Handle ranges while (!ranges.isEmpty()) { - // Break early when encountering the first start point that is past the split point. - // Ranges start on or after the split point lay to the right - StartPoint startPoint = StartPoint.extract(ranges.peek()); - int startCmp = - ComparisonChain.start() - .compareTrueFirst(startPoint.value.isEmpty(), false) - .compare(startPoint.value, splitPoint, ByteStringComparator.INSTANCE) - // when start point is on the split point, it will always be on the right - .result(); - if (startCmp >= 0) { + // Break early when encountering the first start point that is on or past the split point. + // Ranges starting on or after the split point lay to the right. + if (StartPoint.extract(ranges.peek()).compareTo(new StartPoint(splitPoint, true)) >= 0) { break; } RowRange range = ranges.poll(); @@ -267,13 +214,7 @@ public static List shard( @SuppressWarnings("ConstantConditions") EndPoint endPoint = EndPoint.extract(range); - int endCmp = - ComparisonChain.start() - .compareFalseFirst(endPoint.value.isEmpty(), false) - .compare(endPoint.value, splitPoint, ByteStringComparator.INSTANCE) - .compareFalseFirst(endPoint.isClosed, true) - .result(); - if (endCmp < 0) { + if (endPoint.compareTo(new EndPoint(splitPoint, false)) <= 0) { segment.addRowRanges(range); currentSegmentIsEmpty = false; } else { @@ -403,15 +344,13 @@ static StartPoint extract(@Nonnull RowRange rowRange) { this.isClosed = isClosed; } + private ByteString effectiveKey() { + return isClosed ? value : value.concat(NULL_BYTE); + } + @Override public int compareTo(@Nonnull StartPoint o) { - return ComparisonChain.start() - // Empty string comes first - .compareTrueFirst(value.isEmpty(), o.value.isEmpty()) - .compare(value, o.value, ByteStringComparator.INSTANCE) - // Closed start point comes before an open start point: [x,y] starts before (x,y]. - .compareTrueFirst(isClosed, o.isClosed) - .result(); + return ByteStringComparator.INSTANCE.compare(effectiveKey(), o.effectiveKey()); } } @@ -444,14 +383,16 @@ static EndPoint extract(@Nonnull RowRange rowRange) { this.isClosed = isClosed; } + private ByteString effectiveLimit() { + return (isClosed && !value.isEmpty()) ? value.concat(NULL_BYTE) : value; + } + @Override public int compareTo(@Nonnull EndPoint o) { return ComparisonChain.start() // Empty string comes last .compareFalseFirst(value.isEmpty(), o.value.isEmpty()) - .compare(value, o.value, ByteStringComparator.INSTANCE) - // Open end point comes before a closed end point: [x,y) ends before [x,y]. - .compareFalseFirst(isClosed, o.isClosed) + .compare(effectiveLimit(), o.effectiveLimit(), ByteStringComparator.INSTANCE) .result(); } } diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java index 25a0d3f8fd1b..05dcad17021d 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java @@ -344,6 +344,76 @@ public void eraseLargeRowEmptyRangeTest() { .build(); RowSet actual = RowSetUtil.eraseLargeRow(rowSet, key); assertThat(actual).isNull(); + + // (a, a\0] with large row a\0 should also result in an empty RowSet (null) instead of (a, a\0) + RowSet openStartRowSet = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder().setStartKeyOpen(key).setEndKeyClosed(keyTrailer).build()) + .build(); + assertThat(RowSetUtil.eraseLargeRow(openStartRowSet, keyTrailer)).isNull(); + + // (a, z) with large row a\0 should only emit (a\0, z) and not (a, a\0) on the left split + ByteString endKey = ByteString.copyFromUtf8("z"); + RowSet openStartWideRowSet = + RowSet.newBuilder() + .addRowRanges(RowRange.newBuilder().setStartKeyOpen(key).setEndKeyOpen(endKey).build()) + .build(); + assertThat(RowSetUtil.eraseLargeRow(openStartWideRowSet, keyTrailer)) + .isEqualTo( + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder().setStartKeyOpen(keyTrailer).setEndKeyOpen(endKey).build()) + .build()); + } + + @Test + public void testEraseEmptyOpenRange() { + ByteString key = ByteString.copyFromUtf8("a"); + ByteString keyTrailer = key.concat(ByteString.copyFrom(new byte[] {0})); + + // Forward scan: erasing "a" from ["0", "a\0") or ["a", "a\0") leaves (a, a\0) which is empty + RowSet forwardRowSet = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder() + .setStartKeyClosed(ByteString.copyFromUtf8("0")) + .setEndKeyOpen(keyTrailer) + .build()) + .addRowRanges( + RowRange.newBuilder().setStartKeyClosed(key).setEndKeyOpen(keyTrailer).build()) + .build(); + assertThat(RowSetUtil.erase(forwardRowSet, key, true)).isNull(); + + // Reverse scan: erasing "a\0" from ("a", "z"] or ("a", "a\0"] leaves (a, a\0) which is empty + RowSet reverseRowSet = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder() + .setStartKeyOpen(key) + .setEndKeyClosed(ByteString.copyFromUtf8("z")) + .build()) + .addRowRanges( + RowRange.newBuilder().setStartKeyOpen(key).setEndKeyClosed(keyTrailer).build()) + .build(); + assertThat(RowSetUtil.erase(reverseRowSet, keyTrailer, false)).isNull(); + } + + @Test + public void testShardEmptyOpenRange() { + ByteString key = ByteString.copyFromUtf8("a"); + ByteString keyTrailer = key.concat(ByteString.copyFrom(new byte[] {0})); + ByteString endKey = ByteString.copyFromUtf8("z"); + + // Sharding (a, z) at splitPoint a\0 should not emit an empty (a, a\0) range on the left + RowSet input = + RowSet.newBuilder() + .addRowRanges(RowRange.newBuilder().setStartKeyOpen(key).setEndKeyOpen(endKey).build()) + .build(); + SortedSet splits = new TreeSet<>(ByteStringComparator.INSTANCE); + splits.add(keyTrailer); + + assertThat(RowSetUtil.shard(input, splits)).containsExactly(input); } @Test From 733d7a24b250f26adb328906a10b727dec1142f3 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Mon, 16 Nov 2026 17:55:48 +0000 Subject: [PATCH 2/4] test(bigtable): add javadoc and boundary test cases for RowSetUtil --- .../bigtable/data/v2/internal/RowSetUtil.java | 10 +++- .../data/v2/internal/RowSetUtilTest.java | 50 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java index f44f0e36bb56..a57f3b32ab05 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java @@ -344,8 +344,12 @@ static StartPoint extract(@Nonnull RowRange rowRange) { this.isClosed = isClosed; } + /** + * Returns the smallest row key included by this start bound. An open start bound {@code (k} + * starts at the immediate lexicographical successor of {@code k}, which is {@code k\0}. + */ private ByteString effectiveKey() { - return isClosed ? value : value.concat(NULL_BYTE); + return (isClosed || value.isEmpty()) ? value : value.concat(NULL_BYTE); } @Override @@ -383,6 +387,10 @@ static EndPoint extract(@Nonnull RowRange rowRange) { this.isClosed = isClosed; } + /** + * Returns the exclusive upper bound key for bounded endpoints. A closed end bound {@code k]} is + * equivalent to the open end bound {@code k\0)} at its immediate lexicographical successor. + */ private ByteString effectiveLimit() { return (isClosed && !value.isEmpty()) ? value.concat(NULL_BYTE) : value; } diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java index 05dcad17021d..6b55cd526b20 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java @@ -365,6 +365,26 @@ public void eraseLargeRowEmptyRangeTest() { .addRowRanges( RowRange.newBuilder().setStartKeyOpen(keyTrailer).setEndKeyOpen(endKey).build()) .build()); + + // [a\0, z) with large row a should remain [a\0, z) untouched + RowSet closedTrailerStartRowSet = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder().setStartKeyClosed(keyTrailer).setEndKeyOpen(endKey).build()) + .build(); + assertThat(RowSetUtil.eraseLargeRow(closedTrailerStartRowSet, key)) + .isEqualTo(closedTrailerStartRowSet); + + // [0, a] with large row a\0 should remain [0, a] untouched + RowSet closedEndRowSet = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder() + .setStartKeyClosed(ByteString.copyFromUtf8("0")) + .setEndKeyClosed(key) + .build()) + .build(); + assertThat(RowSetUtil.eraseLargeRow(closedEndRowSet, keyTrailer)).isEqualTo(closedEndRowSet); } @Test @@ -414,6 +434,36 @@ public void testShardEmptyOpenRange() { splits.add(keyTrailer); assertThat(RowSetUtil.shard(input, splits)).containsExactly(input); + + // Sharding (a, a\0] at splitPoint a\0 should not split since (a is equivalent to [a\0 + RowSet singleElementOpenStart = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder().setStartKeyOpen(key).setEndKeyClosed(keyTrailer).build()) + .build(); + assertThat(RowSetUtil.shard(singleElementOpenStart, splits)) + .containsExactly(singleElementOpenStart); + + // Sharding [a, a\0] at splitPoint a\0 should split into [a, a\0) and [a\0, a\0] + RowSet twoElementClosed = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder().setStartKeyClosed(key).setEndKeyClosed(keyTrailer).build()) + .build(); + assertThat(RowSetUtil.shard(twoElementClosed, splits)) + .containsExactly( + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder().setStartKeyClosed(key).setEndKeyOpen(keyTrailer).build()) + .build(), + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder() + .setStartKeyClosed(keyTrailer) + .setEndKeyClosed(keyTrailer) + .build()) + .build()) + .inOrder(); } @Test From 375aa839a9c6c446fa9020ee0da5ba2eca995a1f Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Mon, 16 Nov 2026 18:06:37 +0000 Subject: [PATCH 3/4] fix(bigtable): handle overlapping ranges in RowSetUtil.shard and add tests --- .../bigtable/data/v2/internal/RowSetUtil.java | 14 ++++++++----- .../data/v2/internal/RowSetUtilTest.java | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java index a57f3b32ab05..35d70138744f 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java @@ -95,12 +95,13 @@ public static RowSet eraseLargeRow(RowSet rowSet, ByteString excludePoint) { */ public static RowSet erase(RowSet rowSet, ByteString splitPoint, boolean fromStart) { Preconditions.checkArgument(!splitPoint.isEmpty(), "Can't handle empty splitPoint"); - RowSet.Builder newRowSet = RowSet.newBuilder(); if (rowSet.getRowKeysList().isEmpty() && rowSet.getRowRangesList().isEmpty()) { rowSet = RowSet.newBuilder().addRowRanges(RowRange.getDefaultInstance()).build(); } + RowSet.Builder newRowSet = RowSet.newBuilder(); + // Handle point lookups for (ByteString key : rowSet.getRowKeysList()) { if (fromStart) { @@ -125,11 +126,10 @@ public static RowSet erase(RowSet rowSet, ByteString splitPoint, boolean fromSta } // Return the new rowset if there is anything left to read - RowSet result = newRowSet.build(); - if (result.getRowKeysList().isEmpty() && result.getRowRangesList().isEmpty()) { + if (newRowSet.getRowKeysList().isEmpty() && newRowSet.getRowRangesList().isEmpty()) { return null; } - return result; + return newRowSet.build(); } private static RowRange truncateRange(RowRange range, ByteString split, boolean fromStart) { @@ -203,6 +203,7 @@ public static List shard( } // Handle ranges + List splitRemainders = new ArrayList<>(); while (!ranges.isEmpty()) { // Break early when encountering the first start point that is on or past the split point. // Ranges starting on or after the split point lay to the right. @@ -220,9 +221,12 @@ public static List shard( } else { segment.addRowRanges(range.toBuilder().setEndKeyOpen(splitPoint)); currentSegmentIsEmpty = false; - ranges.addFirst(range.toBuilder().setStartKeyClosed(splitPoint).build()); + splitRemainders.add(range.toBuilder().setStartKeyClosed(splitPoint).build()); } } + for (int i = splitRemainders.size() - 1; i >= 0; i--) { + ranges.addFirst(splitRemainders.get(i)); + } if (!currentSegmentIsEmpty) { segments.add(segment.build()); diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java index 6b55cd526b20..a82588b63ce1 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java @@ -139,14 +139,24 @@ public void testShardMixedSplit() { assertThat(RowSetUtil.shard(input, splitPoints)) .containsExactly( - parse("0,-a)"), - parse("a,c,[a-a],-b],(c-d)"), + parse("0,-a),-a)"), + parse("a,c,[a-a],[a-b],(c-d)"), parse("[d-e],(d-f]"), parse("(m-o)"), parse("[o-")) .inOrder(); } + @Test + public void testShardOverlappingRanges() { + RowSet input = parse("[a-z),[b-d),[b-y)"); + SortedSet splitPoints = splitPoints("c"); + + assertThat(RowSetUtil.shard(input, splitPoints)) + .containsExactly(parse("[a-c),[b-c),[b-c)"), parse("[c-z),[c-d),[c-y)")) + .inOrder(); + } + @Test public void testShardUnsortedRequest() { RowSet input = @@ -435,6 +445,12 @@ public void testShardEmptyOpenRange() { assertThat(RowSetUtil.shard(input, splits)).containsExactly(input); + // Sharding (a, z) at both "a" and "a\0" should also not emit an empty range + SortedSet bothSplits = new TreeSet<>(ByteStringComparator.INSTANCE); + bothSplits.add(key); + bothSplits.add(keyTrailer); + assertThat(RowSetUtil.shard(input, bothSplits)).containsExactly(input); + // Sharding (a, a\0] at splitPoint a\0 should not split since (a is equivalent to [a\0 RowSet singleElementOpenStart = RowSet.newBuilder() From 25383d2075d0019496c2962abe3758ac5dff5776 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Mon, 16 Nov 2026 18:38:51 +0000 Subject: [PATCH 4/4] refactor(bigtable): add bound predicates to StartPoint and EndPoint in RowSetUtil --- .../bigtable/data/v2/internal/RowSetUtil.java | 62 ++++++++++++------- .../data/v2/internal/RowSetUtilTest.java | 12 ++++ 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java index 35d70138744f..aa5e6293fb92 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtil.java @@ -135,21 +135,21 @@ public static RowSet erase(RowSet rowSet, ByteString splitPoint, boolean fromSta private static RowRange truncateRange(RowRange range, ByteString split, boolean fromStart) { if (fromStart) { // range end is on or left of the split: skip - if (EndPoint.extract(range).compareTo(new EndPoint(split, true)) <= 0) { + if (EndPoint.extract(range).isBeforeOrAt(split)) { return null; } // range start is on or left of the split - if (StartPoint.extract(range).compareTo(new StartPoint(split, true)) <= 0) { + if (StartPoint.extract(range).isBeforeOrAt(split)) { return range.toBuilder().setStartKeyOpen(split).build(); } return range; } else { // range is on or right of the split: skip - if (StartPoint.extract(range).compareTo(new StartPoint(split, true)) >= 0) { + if (StartPoint.extract(range).isAtOrAfter(split)) { return null; } // range end is on or right of the split - if (EndPoint.extract(range).compareTo(new EndPoint(split, true)) >= 0) { + if (EndPoint.extract(range).isAtOrAfter(split)) { return range.toBuilder().setEndKeyOpen(split).build(); } return range; @@ -207,7 +207,7 @@ public static List shard( while (!ranges.isEmpty()) { // Break early when encountering the first start point that is on or past the split point. // Ranges starting on or after the split point lay to the right. - if (StartPoint.extract(ranges.peek()).compareTo(new StartPoint(splitPoint, true)) >= 0) { + if (StartPoint.extract(ranges.peek()).isAtOrAfter(splitPoint)) { break; } RowRange range = ranges.poll(); @@ -215,14 +215,13 @@ public static List shard( @SuppressWarnings("ConstantConditions") EndPoint endPoint = EndPoint.extract(range); - if (endPoint.compareTo(new EndPoint(splitPoint, false)) <= 0) { - segment.addRowRanges(range); - currentSegmentIsEmpty = false; - } else { + if (endPoint.isAtOrAfter(splitPoint)) { segment.addRowRanges(range.toBuilder().setEndKeyOpen(splitPoint)); - currentSegmentIsEmpty = false; splitRemainders.add(range.toBuilder().setStartKeyClosed(splitPoint).build()); + } else { + segment.addRowRanges(range); } + currentSegmentIsEmpty = false; } for (int i = splitRemainders.size() - 1; i >= 0; i--) { ranges.addFirst(splitRemainders.get(i)); @@ -323,6 +322,9 @@ public static Split of(RowSet left, RowSet right) { private static final class StartPoint implements Comparable { private final ByteString value; private final boolean isClosed; + // The smallest row key included by this start bound. An open start bound (k starts at the + // immediate lexicographical successor of k, which is k\0. + private final ByteString effectiveKey; @Nonnull static StartPoint extract(@Nonnull RowRange rowRange) { @@ -346,19 +348,22 @@ static StartPoint extract(@Nonnull RowRange rowRange) { StartPoint(@Nonnull ByteString value, boolean isClosed) { this.value = value; this.isClosed = isClosed; + this.effectiveKey = (isClosed || value.isEmpty()) ? value : value.concat(NULL_BYTE); } - /** - * Returns the smallest row key included by this start bound. An open start bound {@code (k} - * starts at the immediate lexicographical successor of {@code k}, which is {@code k\0}. - */ - private ByteString effectiveKey() { - return (isClosed || value.isEmpty()) ? value : value.concat(NULL_BYTE); + /** Returns true if this start bound is at or after {@code [key}. */ + boolean isAtOrAfter(@Nonnull ByteString key) { + return ByteStringComparator.INSTANCE.compare(effectiveKey, key) >= 0; + } + + /** Returns true if this start bound is before or at {@code [key}. */ + boolean isBeforeOrAt(@Nonnull ByteString key) { + return ByteStringComparator.INSTANCE.compare(effectiveKey, key) <= 0; } @Override public int compareTo(@Nonnull StartPoint o) { - return ByteStringComparator.INSTANCE.compare(effectiveKey(), o.effectiveKey()); + return ByteStringComparator.INSTANCE.compare(effectiveKey, o.effectiveKey); } } @@ -366,6 +371,9 @@ public int compareTo(@Nonnull StartPoint o) { private static final class EndPoint implements Comparable { private final ByteString value; private final boolean isClosed; + // The exclusive upper bound key for bounded endpoints. A closed end bound k] is equivalent to + // the open end bound k\0) at its immediate lexicographical successor. + private final ByteString effectiveLimit; @Nonnull static EndPoint extract(@Nonnull RowRange rowRange) { @@ -389,14 +397,24 @@ static EndPoint extract(@Nonnull RowRange rowRange) { EndPoint(@Nonnull ByteString value, boolean isClosed) { this.value = value; this.isClosed = isClosed; + this.effectiveLimit = (isClosed && !value.isEmpty()) ? value.concat(NULL_BYTE) : value; + } + + /** + * Returns true if this upper bound includes {@code key} or any keys after it (i.e. ends at or + * after {@code key]}). + */ + boolean isAtOrAfter(@Nonnull ByteString key) { + return value.isEmpty() || ByteStringComparator.INSTANCE.compare(effectiveLimit, key) > 0; } /** - * Returns the exclusive upper bound key for bounded endpoints. A closed end bound {@code k]} is - * equivalent to the open end bound {@code k\0)} at its immediate lexicographical successor. + * Returns true if this upper bound excludes all keys strictly after {@code key} (i.e. ends + * before or at {@code key]}). */ - private ByteString effectiveLimit() { - return (isClosed && !value.isEmpty()) ? value.concat(NULL_BYTE) : value; + boolean isBeforeOrAt(@Nonnull ByteString key) { + return !value.isEmpty() + && ByteStringComparator.INSTANCE.compare(effectiveLimit, key.concat(NULL_BYTE)) <= 0; } @Override @@ -404,7 +422,7 @@ public int compareTo(@Nonnull EndPoint o) { return ComparisonChain.start() // Empty string comes last .compareFalseFirst(value.isEmpty(), o.value.isEmpty()) - .compare(effectiveLimit(), o.effectiveLimit(), ByteStringComparator.INSTANCE) + .compare(effectiveLimit, o.effectiveLimit, ByteStringComparator.INSTANCE) .result(); } } diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java index a82588b63ce1..7f49766ec4c2 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RowSetUtilTest.java @@ -480,6 +480,18 @@ public void testShardEmptyOpenRange() { .build()) .build()) .inOrder(); + + // Sharding [0, a] at splitPoint a\0 should not split since a] is equivalent to a\0) + RowSet closedEndBeforeSplit = + RowSet.newBuilder() + .addRowRanges( + RowRange.newBuilder() + .setStartKeyClosed(ByteString.copyFromUtf8("0")) + .setEndKeyClosed(key) + .build()) + .build(); + assertThat(RowSetUtil.shard(closedEndBeforeSplit, splits)) + .containsExactly(closedEndBeforeSplit); } @Test