public final class TokenRange extends Object implements Comparable<TokenRange>
A range is start-exclusive and end-inclusive. It is empty when start and end are the same token, except if that is the minimum token, in which case the range covers the whole ring (this is consistent with the behavior of CQL range queries).
Note that CQL does not handle wrapping. To query all partitions in a range, see unwrap()
.
Modifier and Type | Method and Description |
---|---|
int |
compareTo(TokenRange other) |
boolean |
contains(Token token)
Checks whether this range contains a given token.
|
boolean |
equals(Object other) |
Token |
getEnd()
Return the end of the range.
|
Token |
getStart()
Return the start of the range.
|
int |
hashCode() |
boolean |
intersects(TokenRange that)
Returns whether this range intersects another one.
|
List<TokenRange> |
intersectWith(TokenRange that)
Computes the intersection of this range with another one.
|
boolean |
isEmpty()
Returns whether this range is empty.
|
boolean |
isWrappedAround()
Returns whether this range wraps around the end of the ring.
|
TokenRange |
mergeWith(TokenRange that)
Merges this range with another one.
|
List<TokenRange> |
splitEvenly(int numberOfSplits)
Splits this range into a number of smaller ranges of equal "size" (referring to the number of
tokens, not the actual amount of data).
|
String |
toString() |
List<TokenRange> |
unwrap()
Splits this range into a list of two non-wrapping ranges.
|
public Token getStart()
public Token getEnd()
public List<TokenRange> splitEvenly(int numberOfSplits)
Splitting an empty range is not permitted. But note that, in edge cases, splitting a range might produce one or more empty ranges.
numberOfSplits
- the number of splits to create.IllegalArgumentException
- if the range is empty or if numberOfSplits < 1.public boolean isEmpty()
A range is empty when start and end are the same token, except if that is the minimum token, in which case the range covers the whole ring (this is consistent with the behavior of CQL range queries).
public boolean isWrappedAround()
public List<TokenRange> unwrap()
For example:
]1,10]
unwraps to itself;
]10,1]
unwraps to ]10,min_token]
and ]min_token,1]
.
This is useful for CQL range queries, which do not handle wrapping:
List<Row> rows = new ArrayList<Row>();
for (TokenRange subRange : range.unwrap()) {
ResultSet rs = session.execute("SELECT * FROM mytable WHERE token(pk) > ? and token(pk) <= ?",
subRange.getStart(), subRange.getEnd());
rows.addAll(rs.all());
}
public boolean intersects(TokenRange that)
For example:
]3,5]
intersects ]1,4]
, ]4,5]
...
]3,5]
does not intersect ]1,2]
, ]2,3]
, ]5,7]
...
that
- the other range.public List<TokenRange> intersectWith(TokenRange that)
If either of these ranges overlap the the ring, they are unwrapped and the unwrapped tokens are compared with one another.
This call will fail if the two ranges do not intersect, you must check by calling intersects(TokenRange)
beforehand.
that
- the other range.IllegalArgumentException
- if the ranges do not intersect.public boolean contains(Token token)
token
- the token to check for.range.start < token <= range.end
.public TokenRange mergeWith(TokenRange that)
The two ranges should either intersect or be adjacent; in other words, the merged range should not include tokens that are in neither of the original ranges.
For example:
]3,5]
with ]4,7]
produces ]3,7]
;
]3,5]
with ]4,5]
produces ]3,5]
;
]3,5]
with ]5,8]
produces ]3,8]
;
]3,5]
with ]6,8]
fails.
that
- the other range.IllegalArgumentException
- if the ranges neither intersect nor are adjacent.public int compareTo(TokenRange other)
compareTo
in interface Comparable<TokenRange>
Copyright © 2012–2023. All rights reserved.