Class KeyRanges
- Direct Known Subclasses:
Reads
KeyRange
instances that can be treated as a unified whole, in particular as a KeyFilter
.
Instances are not thread safe.
- See Also:
-
Constructor Summary
ConstructorDescriptionKeyRanges
(byte[] key) Constructor for an instance containing a single range containing a single key.KeyRanges
(byte[] min, byte[] max) Constructor for an instance containing a single range.Constructor for an instance containing a single range.Constructor.Copy constructor.KeyRanges
(InputStream input) Constructor to deserialize an instance created byserialize()
.KeyRanges
(InputStream input, boolean immutable) Constructor to deserialize an instance created byserialize()
.Constructor.Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add all the keys in the givenKeyRange
to this instance.void
Add all the key ranges in the givenKeyRanges
to this instance.asList()
Get theKeyRange
s underlying with this instance as a list.asSet()
Get a view of theKeyRange
s underlying with this instance as a sorted set.void
clear()
Remove all keys from this instance.clone()
Clone this instance.boolean
contains
(byte[] key) Determine whether this instance contains the given key.boolean
boolean
deserializeIterator
(InputStream input) Deserialize an instance created byserialize()
in the form of an iterator of the individualKeyRange
s.static KeyRanges
empty()
Create an empty instance containing zero ranges.boolean
KeyRange[]
findKey
(byte[] key) Find the contiguousKeyRange
(s) within this instance containing, or adjacent to, the given key.static KeyRanges
forPrefix
(byte[] prefix) Construct an instance containing a single range corresponding to all keys with the given prefix.static KeyRanges
full()
Create a "full" instance containing a singleKeyRange
that contains all keys.byte[]
getMax()
Get the maximum key contained by this instance (exclusive).byte[]
getMin()
Get the minimum key contained by this instance (inclusive).int
hashCode()
void
Remove all the keys not also in the givenKeyRange
from this instance.void
Remove all key ranges not also in the givenKeyRanges
from this instance.boolean
intersects
(KeyRange range) Determine whether this instance intersects the givenKeyRange
, i.e., there exists at least one key contained in both.inverse()
Create the inverse of this instance.boolean
isEmpty()
Determine whether this instance is empty, i.e., contains no keys.boolean
isFull()
Determine whether this instance is "full", i.e., contains all keys.iterator()
prefixedBy
(byte[] prefix) Create a new instance from this one, with eachKeyRange
prefixed by the given byte sequence.Return an immutable snapshot of this instance.void
Remove all the keys in the givenKeyRange
from this instance.void
Remove all the key ranges in the givenKeyRanges
from this instance.byte[]
seekHigher
(byte[] key) Skip over the largest possible uncontained region in an upward direction.byte[]
seekLower
(byte[] key) Skip over the largest possible uncontained region in a downward direction.void
serialize
(OutputStream out) Serialize this instance.long
Calculate the number of bytes required to serialize this instance viaserialize()
.int
size()
Determine the number of individualKeyRange
s contained in this instance.toString()
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
KeyRanges
Constructor.Creates an instance that contains all keys contained by any of the
KeyRange
s inranges
. The givenranges
may be empty, adjacent, overlap, and/or be listed in any order; this constructor will normalize them.- Parameters:
ranges
- individual key ranges- Throws:
IllegalArgumentException
- ifranges
or anyKeyRange
therein is null
-
KeyRanges
Constructor.Creates an instance that contains all keys contained by any of the
KeyRange
s inranges
. The givenranges
may be empty, adjacent, overlap, and/or be listed in any order; this constructor will normalize them.- Parameters:
ranges
- individual key ranges- Throws:
IllegalArgumentException
- ifranges
or anyKeyRange
therein is null
-
KeyRanges
Constructor.Creates an instance that contains all keys contained by any of the
KeyRange
s inranges
. The givenranges
may be empty, adjacent, overlap, and/or be listed in any order; this constructor will normalize them.- Parameters:
ranges
- individual key ranges- Throws:
IllegalArgumentException
- ifranges
or anyKeyRange
therein is null
-
KeyRanges
Copy constructor.- Parameters:
ranges
- value to copy- Throws:
IllegalArgumentException
- ifranges
is null
-
KeyRanges
Constructor for an instance containing a single range.- Parameters:
range
- single range- Throws:
IllegalArgumentException
- ifrange
is null
-
KeyRanges
public KeyRanges(byte[] key) Constructor for an instance containing a single range containing a single key.- Parameters:
key
- key in range; must not be null- Throws:
IllegalArgumentException
- ifkey
is null
-
KeyRanges
public KeyRanges(byte[] min, byte[] max) Constructor for an instance containing a single range.- Parameters:
min
- minimum key (inclusive); must not be nullmax
- maximum key (exclusive), or null for no maximum- Throws:
IllegalArgumentException
- ifmin > max
-
KeyRanges
Constructor to deserialize an instance created byserialize()
.Equivalent to
KeyRanges
(input, false)
.- Parameters:
input
- input stream containing data fromserialize()
- Throws:
IOException
- if an I/O error occursEOFException
- if the input ends unexpectedlyIllegalArgumentException
- ifinput
is nullIllegalArgumentException
- ifinput
is invalid
-
KeyRanges
Constructor to deserialize an instance created byserialize()
.- Parameters:
input
- input stream containing data fromserialize()
immutable
- whether this new instance should be immutable- Throws:
IOException
- if an I/O error occursEOFException
- if the input ends unexpectedlyIllegalArgumentException
- ifinput
is nullIllegalArgumentException
- ifinput
is invalid
-
-
Method Details
-
forPrefix
Construct an instance containing a single range corresponding to all keys with the given prefix.- Parameters:
prefix
- prefix of all keys in the range- Returns:
- instance containing all keys prefixed by
prefix
- Throws:
IllegalArgumentException
- ifprefix
is null
-
empty
Create an empty instance containing zero ranges.- Returns:
- empty instance
-
full
Create a "full" instance containing a singleKeyRange
that contains all keys.- Returns:
- instance spanning all keys
-
asList
Get theKeyRange
s underlying with this instance as a list.The returned
KeyRange
s will be listed in order. Modifications to the returned list do not affect this instance.- Returns:
- minimal list of
KeyRange
s sorted by key range
-
asSet
Get a view of theKeyRange
s underlying with this instance as a sorted set.The returned
KeyRange
s will be sorted in order according toKeyRange.SORT_BY_MIN
.- Returns:
- view of this instance as a minimal, unmodifiable sorted set of
KeyRange
s sorted by minimum key
-
size
public int size()Determine the number of individualKeyRange
s contained in this instance.- Returns:
- size of this instance
-
clear
public void clear()Remove all keys from this instance.- Throws:
UnsupportedOperationException
- if this instance is immutable
-
isEmpty
public boolean isEmpty()Determine whether this instance is empty, i.e., contains no keys.- Returns:
- true if this instance is empty
-
isFull
public boolean isFull()Determine whether this instance is "full", i.e., contains all keys.- Returns:
- true if this instance is full
-
getMin
public byte[] getMin()Get the minimum key contained by this instance (inclusive).- Returns:
- minimum key contained by this instance (inclusive), or null if this instance
isEmpty()
-
getMax
public byte[] getMax()Get the maximum key contained by this instance (exclusive).- Returns:
- maximum key contained by this instance (exclusive),
or null if there is no upper bound, or this instance
isEmpty()
-
prefixedBy
Create a new instance from this one, with eachKeyRange
prefixed by the given byte sequence.- Parameters:
prefix
- prefix to apply to this instance- Returns:
- prefixed instance
- Throws:
IllegalArgumentException
- ifprefix
is null
-
inverse
Create the inverse of this instance. The inverse contains all keys not contained by this instance.- Returns:
- the inverse of this instance
-
contains
Determine whether this instance contains the givenKeyRanges
, i.e., all keys contained by the givenKeyRanges
are also contained by this instance.- Parameters:
ranges
- other instance to test- Returns:
- true if this instance contains
ranges
, otherwise false - Throws:
IllegalArgumentException
- ifranges
is null
-
contains
Determine whether this instance contains the givenKeyRange
, i.e., all keys contained by the givenKeyRange
are also contained by this instance.- Parameters:
range
- key range to test- Returns:
- true if this instance contains
range
, otherwise false - Throws:
IllegalArgumentException
- ifrange
is null
-
intersects
Determine whether this instance intersects the givenKeyRange
, i.e., there exists at least one key contained in both.- Parameters:
range
- key range to test- Returns:
- true if this instance intersects
range
, otherwise false - Throws:
IllegalArgumentException
- ifrange
is null
-
findKey
Find the contiguousKeyRange
(s) within this instance containing, or adjacent to, the given key.This method returns an array of length two: if this instance contains
key
then both elements are the same Java object, namely, theKeyRange
that containskey
; otherwise, the first element is the nearestKeyRange
to the left ofkey
, or null if none exists, and the second element is theKeyRange
to the right ofkey
, or null if none exists. Note if this instance is empty then{ null, null }
is returned.- Parameters:
key
- key to find- Returns:
- array with the containing
KeyRange
or nearest neighbor to the left (or null) and the containingKeyRange
or nearest neighbor to the right (or null) - Throws:
IllegalArgumentException
- ifkey
is null
-
add
Add all the keys in the givenKeyRange
to this instance.- Parameters:
range
- key range to add- Throws:
IllegalArgumentException
- ifrange
is nullUnsupportedOperationException
- if this instance is immutable
-
remove
Remove all the keys in the givenKeyRange
from this instance.- Parameters:
range
- range to remove- Throws:
IllegalArgumentException
- ifrange
is nullUnsupportedOperationException
- if this instance is immutable
-
intersect
Remove all the keys not also in the givenKeyRange
from this instance.- Parameters:
range
- key range to intersect with- Throws:
IllegalArgumentException
- ifrange
is nullUnsupportedOperationException
- if this instance is immutable
-
add
Add all the key ranges in the givenKeyRanges
to this instance.- Parameters:
ranges
- key ranges to add- Throws:
IllegalArgumentException
- ifranges
is nullUnsupportedOperationException
- if this instance is immutable
-
remove
Remove all the key ranges in the givenKeyRanges
from this instance.- Parameters:
ranges
- key ranges to remove- Throws:
IllegalArgumentException
- ifranges
is null
-
intersect
Remove all key ranges not also in the givenKeyRanges
from this instance.Equivalent to
remove(ranges.inverse())
.- Parameters:
ranges
- key ranges to intersect with- Throws:
IllegalArgumentException
- ifranges
is nullUnsupportedOperationException
- if this instance is immutable
-
iterator
-
serialize
Serialize this instance.- Parameters:
out
- output- Throws:
IOException
- if an error occursIllegalArgumentException
- ifout
is null
-
serializedLength
public long serializedLength()Calculate the number of bytes required to serialize this instance viaserialize()
.- Returns:
- number of serialized bytes
-
deserializeIterator
Deserialize an instance created byserialize()
in the form of an iterator of the individualKeyRange
s.If an
IOException
is thrown while reading, the returnedIterator
will throw aRuntimeException
wrapping it. If invalid data is encountered, the returnedIterator
will throw anIllegalArgumentException
.- Parameters:
input
- input stream containing data fromserialize()
- Returns:
- deserialized iteration of
KeyRange
s - Throws:
IllegalArgumentException
- ifinput
is null
-
contains
public boolean contains(byte[] key) Description copied from interface:KeyFilter
Determine whether this instance contains the given key. -
seekHigher
public byte[] seekHigher(byte[] key) Description copied from interface:KeyFilter
Skip over the largest possible uncontained region in an upward direction.This method should return an inclusive lower bound on all keys greater than or equal to
key
that are contained by this instance. The bound does not have to be tight, but the tighter the better.A value of null may be returned to indicate that no key greater than or equal to
key
is contained by this instance.If
key
is contained by this instance, this method must returnkey
; ifkey
is not contained by this instance, this method must return a key strictly higher thankey
or null.- Specified by:
seekHigher
in interfaceKeyFilter
- Parameters:
key
- starting key- Returns:
- a lower bound (inclusive) for contained keys greater than or equal to
key
, or null if no key greater than or equal tokey
is contained by this instance
-
seekLower
public byte[] seekLower(byte[] key) Description copied from interface:KeyFilter
Skip over the largest possible uncontained region in a downward direction.This method should return an exclusive upper bound on all keys strictly less than
key
that are contained by this instance. The bound does not have to be tight, but the tighter the better.A value of null may be returned to indicate that no key strictly less than
key
is contained by this instance.For the purposes of this method, an empty
byte[]
array represents an upper bound greater than allbyte[]
keys. This interpretation applies both to thekey
parameter and returned value. Note that this implies an empty array cannot be returned to indicate that no keys exist (instead, return null).This method must either return null or a value less than or equal to
key
(using the above interpretation for empty arrays).- Specified by:
seekLower
in interfaceKeyFilter
- Parameters:
key
- starting key, or an empty array to indicate a maximal upper bound- Returns:
- an upper bound (exclusive) for contained keys strictly less that
key
, null if no key strictly less thankey
is contained by this instance, or an emptybyte[]
array to indicate an upper bound greater than allbyte[]
keys (which implieskey
was also empty)
-
clone
Clone this instance.The returned clone will always be mutable, even if this instance is not.
-
readOnlySnapshot
Return an immutable snapshot of this instance.- Returns:
- immutable snapshot
-
equals
-
hashCode
public int hashCode() -
toString
-