Class ByteKeyRange
- All Implemented Interfaces:
Serializable,HasDefaultTracker<ByteKeyRange,ByteKeyRangeTracker>
ByteKeys.
Instances of ByteKeyRange are immutable.
A ByteKeyRange enforces the restriction that its start and end keys must form a valid,
non-empty range [startKey, endKey) that is inclusive of the start key and exclusive of
the end key.
When the end key is empty, it is treated as the largest possible key.
Interpreting ByteKey in a ByteKeyRange
The primary role of ByteKeyRange is to provide functionality for estimateFractionForKey(ByteKey), interpolateKey(double), and split(int).
ByteKeyRange implements these features by treating a ByteKey's underlying
byte[] as the binary expansion of floating point numbers in the range [0.0, 1.0].
For example, the keys ByteKey.of(0x80), ByteKey.of(0xc0), and
ByteKey.of(0xe0) are interpreted as 0.5, 0.75, and 0.875 respectively.
The empty ByteKey.EMPTY is interpreted as 0.0 when used as the start of a range
and 1.0 when used as the end key.
Key interpolation, fraction estimation, and range splitting are all interpreted in these
floating-point semantics. See the respective implementations for further details. Note:
the underlying implementations of these functions use BigInteger and BigDecimal,
so they can be slow and should not be called in hot loops. Dynamic work rebalancing will only
invoke these functions during periodic control operations, so they are not called on the critical
path.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ByteKeyRangeThe range of all keys, with empty start and end keys. -
Method Summary
Modifier and TypeMethodDescriptioncontainsKey(ByteKey key) Returnstrueif the specifiedByteKeyis contained within this range.booleandoubleReturns the fraction of this range[startKey, endKey)that is in the interval[startKey, key).Returns theByteKeyrepresenting the upper bound of thisByteKeyRange.Returns theByteKeyrepresenting the lower bound of thisByteKeyRange.inthashCode()interpolateKey(double fraction) Returns aByteKeykeysuch that[startKey, key)represents approximately the specified fraction of the range[startKey, endKey).Creates a new tracker forthis.static ByteKeyRangeCreates a newByteKeyRangewith the given start and end keys.overlaps(ByteKeyRange other) Returnstrueif the specifiedByteKeyRangeoverlaps this range.split(int numSplits) Returns a list of up tonumSplits + 1ByteKeysin ascending order, where the keys have been interpolated to form roughly equal sub-ranges of thisByteKeyRange, assuming a uniform distribution of keys within this range.toString()withEndKey(ByteKey endKey) Returns newByteKeyRangelike this one, but with the specified end key.withStartKey(ByteKey startKey) Returns newByteKeyRangelike this one, but with the specified start key.
-
Field Details
-
ALL_KEYS
The range of all keys, with empty start and end keys.
-
-
Method Details
-
of
Creates a newByteKeyRangewith the given start and end keys.Note that if
endKeyis empty, it is treated as the largest possible key.- Throws:
IllegalArgumentException- ifendKeyis less than or equal tostartKey, unlessendKeyis empty indicating the maximum possibleByteKey.- See Also:
-
getStartKey
Returns theByteKeyrepresenting the lower bound of thisByteKeyRange. -
getEndKey
Returns theByteKeyrepresenting the upper bound of thisByteKeyRange.Note that if
endKeyis empty, it is treated as the largest possible key. -
containsKey
Returnstrueif the specifiedByteKeyis contained within this range. -
overlaps
Returnstrueif the specifiedByteKeyRangeoverlaps this range. -
split
Returns a list of up tonumSplits + 1ByteKeysin ascending order, where the keys have been interpolated to form roughly equal sub-ranges of thisByteKeyRange, assuming a uniform distribution of keys within this range.The first
ByteKeyin the result is guaranteed to be equal togetStartKey(), and the lastByteKeyin the result is guaranteed to be equal togetEndKey(). Thus the resulting list exactly spans the same key range as thisByteKeyRange.Note that the number of keys returned is not always equal to
numSplits + 1. Specifically, if this range is unsplittable (e.g., because the start and end keys are equal up to padding by zero bytes), the list returned will only contain the start and end key.- Throws:
IllegalArgumentException- if the specified number of splits is less than 1- See Also:
-
estimateFractionForKey
Returns the fraction of this range[startKey, endKey)that is in the interval[startKey, key).- Throws:
IllegalArgumentException- ifkeydoes not fall within this range- See Also:
-
interpolateKey
Returns aByteKeykeysuch that[startKey, key)represents approximately the specified fraction of the range[startKey, endKey). The interpolation is computed assuming a uniform distribution of keys.For example, given the largest possible range (defined by empty start and end keys), the fraction
0.5will return theByteKey.of(0x80), which will also be returned for ranges[0x40, 0xc0)and[0x6f, 0x91).The key returned will never be empty.
- Throws:
IllegalArgumentException- iffractionis outside the range [0, 1)IllegalStateException- if this range cannot be interpolated- See Also:
-
withStartKey
Returns newByteKeyRangelike this one, but with the specified start key. -
withEndKey
Returns newByteKeyRangelike this one, but with the specified end key. -
toString
-
equals
-
hashCode
public int hashCode() -
newTracker
Description copied from interface:HasDefaultTrackerCreates a new tracker forthis.- Specified by:
newTrackerin interfaceHasDefaultTracker<ByteKeyRange,ByteKeyRangeTracker>
-