forked from apache/cassandra
-
Notifications
You must be signed in to change notification settings - Fork 21
CNDB-15594:Add AbstractBounds#intersects and fix Range#intersects to handle full range #2060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jasonstack
wants to merge
4
commits into
cndb-main-release-202505
Choose a base branch
from
cndb-15594-restore
base: cndb-main-release-202505
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a4487f5
CNDB-15594: make SimpleShardTracker public
jasonstack 5cc4811
Add AbstractBounds#intersects and fix Range#intersects(Bounds<T> that…
blambov 10bd2df
fix compilation
jasonstack 8665f3b
do not make SimpleShardTracker public and update comments in Range#in…
jasonstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
test/unit/org/apache/cassandra/dht/AbstractBoundsQuickTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.cassandra.dht; | ||
|
||
import org.junit.Test; | ||
|
||
import org.quicktheories.core.Gen; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.quicktheories.QuickTheory.qt; | ||
import static org.quicktheories.generators.SourceDSL.integers; | ||
import static org.quicktheories.generators.SourceDSL.longs; | ||
|
||
public class AbstractBoundsQuickTest | ||
{ | ||
private static final long MAX_TOKEN = Murmur3Partitioner.MAXIMUM; | ||
|
||
@Test | ||
public void testIntersects() | ||
{ | ||
qt().forAll(bounds(), bounds()) | ||
.check((r1, r2) -> { | ||
boolean intersects = r1.intersects(r2); | ||
// Check commutativity | ||
assertThat(r2.intersects(r1)).isEqualTo(intersects); | ||
|
||
assertThat(intersects).isEqualTo(stupidIntersects(r1, r2)); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
boolean stupidIntersects(AbstractBounds<Token> l, AbstractBounds<Token> r) | ||
{ | ||
if (isPoint(l)) | ||
return l.left.isMinimum() || r.contains(l.left); | ||
if (isPoint(r)) | ||
return r.left.isMinimum() || l.contains(r.left); | ||
|
||
// Range.intersects is already tested | ||
return toRange(l).intersects(r); | ||
} | ||
|
||
private static Range<Token> toRange(AbstractBounds<Token> bounds) | ||
{ | ||
if (bounds instanceof Range) | ||
return (Range<Token>) bounds; | ||
|
||
Token l = bounds.left; | ||
if (bounds.inclusiveLeft()) | ||
l = new Murmur3Partitioner.LongToken(l.getLongValue() - 1); | ||
Token r = bounds.right; | ||
if (!bounds.inclusiveRight()) | ||
r = new Murmur3Partitioner.LongToken(r.getLongValue() - 1); | ||
|
||
return new Range<>(l, r); | ||
} | ||
|
||
private static boolean isPoint(AbstractBounds<Token> l) | ||
{ | ||
return l.inclusiveLeft() && l.inclusiveRight() && l.left.equals(l.right); | ||
} | ||
|
||
private Gen<AbstractBounds<Token>> bounds() | ||
{ | ||
return longs().between(0, MAX_TOKEN) | ||
.zip(integers().between(0, 6), this::createBoundary) // 14% chance min | ||
.zip(longs().between(0, MAX_TOKEN) | ||
.zip(integers().between(-1, 6), this::createBoundary), // 12.5% chance point, 12.5% chance min | ||
this::createAbstractBounds); | ||
} | ||
|
||
private AbstractBounds.Boundary<Token> createBoundary(long pos, int minOrInclusive) | ||
{ | ||
if (minOrInclusive < 0) | ||
return null; // point bounds | ||
Token t; | ||
if (minOrInclusive == 0) | ||
t = Murmur3Partitioner.instance.getMinimumToken(); | ||
else | ||
t = new Murmur3Partitioner.LongToken(pos); | ||
|
||
return new AbstractBounds.Boundary(t, minOrInclusive % 2 == 0); | ||
} | ||
|
||
private AbstractBounds<Token> createAbstractBounds(AbstractBounds.Boundary<Token> left, AbstractBounds.Boundary<Token> right) | ||
{ | ||
if (right == null) | ||
{ | ||
return AbstractBounds.bounds(left.boundary, true, left.boundary, true); | ||
} | ||
|
||
if (!left.inclusive && right.inclusive) | ||
return new Range<>(left.boundary, right.boundary); // ranges can be wraparound | ||
|
||
// For other cases, create a normal bounds | ||
int cmp = left.boundary.compareTo(right.boundary); | ||
if (cmp < 0) | ||
return AbstractBounds.bounds(left, right); | ||
else if (cmp > 0) | ||
return AbstractBounds.bounds(right, left); | ||
else | ||
return AbstractBounds.bounds(left.boundary, true, left.boundary, true); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.