Class TokenRange
- java.lang.Object
-
- com.datastax.driver.core.TokenRange
-
- All Implemented Interfaces:
Comparable<TokenRange>
public final class TokenRange extends Object implements Comparable<TokenRange>
A range of tokens on the Cassandra ring.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().
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(TokenRange other)booleancontains(Token token)Checks whether this range contains a given token.booleanequals(Object other)TokengetEnd()Return the end of the range.TokengetStart()Return the start of the range.inthashCode()booleanintersects(TokenRange that)Returns whether this range intersects another one.List<TokenRange>intersectWith(TokenRange that)Computes the intersection of this range with another one.booleanisEmpty()Returns whether this range is empty.booleanisWrappedAround()Returns whether this range wraps around the end of the ring.TokenRangemergeWith(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).StringtoString()List<TokenRange>unwrap()Splits this range into a list of two non-wrapping ranges.
-
-
-
Method Detail
-
getStart
public Token getStart()
Return the start of the range.- Returns:
- the start of the range (exclusive).
-
getEnd
public Token getEnd()
Return the end of the range.- Returns:
- the end of the range (inclusive).
-
splitEvenly
public 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).Splitting an empty range is not permitted. But note that, in edge cases, splitting a range might produce one or more empty ranges.
- Parameters:
numberOfSplits- the number of splits to create.- Returns:
- the splits.
- Throws:
IllegalArgumentException- if the range is empty or if numberOfSplits < 1.
-
isEmpty
public boolean isEmpty()
Returns whether this range is empty.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).
- Returns:
- whether the range is empty.
-
isWrappedAround
public boolean isWrappedAround()
Returns whether this range wraps around the end of the ring.- Returns:
- whether this range wraps around.
-
unwrap
public List<TokenRange> unwrap()
Splits this range into a list of two non-wrapping ranges. This will return the range itself if it is non-wrapping, or two ranges otherwise.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()); }- Returns:
- the list of non-wrapping ranges.
-
intersects
public boolean intersects(TokenRange that)
Returns whether this range intersects another one.For example:
]3,5]intersects]1,4],]4,5]...]3,5]does not intersect]1,2],]2,3],]5,7]...
- Parameters:
that- the other range.- Returns:
- whether they intersect.
-
intersectWith
public List<TokenRange> intersectWith(TokenRange that)
Computes the intersection of this range with another one.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.- Parameters:
that- the other range.- Returns:
- the range(s) resulting from the intersection.
- Throws:
IllegalArgumentException- if the ranges do not intersect.
-
contains
public boolean contains(Token token)
Checks whether this range contains a given token.- Parameters:
token- the token to check for.- Returns:
- whether this range contains the token, i.e.
range.start < token <= range.end.
-
mergeWith
public TokenRange mergeWith(TokenRange that)
Merges this range with another one.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:
- merging
]3,5]with]4,7]produces]3,7]; - merging
]3,5]with]4,5]produces]3,5]; - merging
]3,5]with]5,8]produces]3,8]; - merging
]3,5]with]6,8]fails.
- Parameters:
that- the other range.- Returns:
- the resulting range.
- Throws:
IllegalArgumentException- if the ranges neither intersect nor are adjacent.
- merging
-
compareTo
public int compareTo(TokenRange other)
- Specified by:
compareToin interfaceComparable<TokenRange>
-
-