Package io.permazen.kv
Class KeyRange
java.lang.Object
io.permazen.kv.KeyRange
Represents a contiguous range of
byte[]
keys, when keys are sorted in unsigned lexical order.
Instances are defined by an inclusive lower bound and an exclusive upper bound.
The upper bound may be specified as null to represent no maximum.
Instances are immutable: the minimum and maximum byte[]
arrays are copied during
construction and when accessed by getMin()
and getMax()
.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final KeyRange
TheKeyRange
containing the full range (i.e., all keys).protected final byte[]
Upper bound (exclusive), or null for no maximum.protected final byte[]
Lower bound (inclusive), or null for no minimum.static final Comparator<KeyRange>
static final Comparator<KeyRange>
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
compare
(byte[] key1, byte[] key2) Compare twobyte[]
keys using unsigned lexical ordering, while also accepting null values that represent "positive infinity".int
compareTo
(byte[] key) Determine if this range is left of, contains, or is right of the given key.boolean
contains
(byte[] key) Determine if this key range contains the specified key.boolean
Determine if this key range fully contains the specified key range.static KeyRange
empty
(byte[] key) Create an empty key range at the specified key.boolean
static KeyRange
forPrefix
(byte[] prefix) Construct an instance containing all keys with the given prefix.byte[]
getMax()
Get range maximum (exclusive), or null if there is no upper bound.byte[]
getMin()
Get range minimum (inclusive).int
hashCode()
boolean
isEmpty()
boolean
isFull()
Determine whether this instance contains the full range covering all keys.boolean
Determine whether this instance contains all keys having some common prefix.boolean
Determine whether this instance contains exactly one key.boolean
Determine if this key range overlaps the specified key range, i.e., there exists at least onebyte[]
key that both ranges have in common.prefixedBy
(byte[] prefix) Create a new instance whose minimum and maximum keys are the same as this instance's but with the given byte sequence prepended.toString()
-
Field Details
-
FULL
TheKeyRange
containing the full range (i.e., all keys). -
SORT_BY_MIN
-
SORT_BY_MAX
-
min
protected final byte[] minLower bound (inclusive), or null for no minimum. Subclasses must not modify the array (to preserve immutability). -
max
protected final byte[] maxUpper bound (exclusive), or null for no maximum. Subclasses must not modify the array (to preserve immutability).
-
-
Constructor Details
-
KeyRange
public KeyRange(byte[] min, byte[] max) Constructor.- Parameters:
min
- minimum key (inclusive); must not be nullmax
- maximum key (exclusive), or null for no maximum- Throws:
IllegalArgumentException
- ifmin
is nullIllegalArgumentException
- ifmin > max
-
KeyRange
public KeyRange(byte[] key) Construct key range containing a single key.- Parameters:
key
- the key contained in the range- Throws:
IllegalArgumentException
- ifkey
is null
-
-
Method Details
-
forPrefix
Construct an instance containing all keys with the given prefix.- Parameters:
prefix
- prefix of all keys in the range- Returns:
- range of keys prefixed by
prefix
- Throws:
IllegalArgumentException
- ifprefix
is null
-
getMin
public byte[] getMin()Get range minimum (inclusive).This method returns a copy of the minimum, so changes do not affect this instance.
- Returns:
- inclusivie minimum, never null
-
getMax
public byte[] getMax()Get range maximum (exclusive), or null if there is no upper bound.This method returns a copy of the maximum, so changes do not affect this instance.
- Returns:
- exclusivie maximum, or null for none
-
overlaps
Determine if this key range overlaps the specified key range, i.e., there exists at least onebyte[]
key that both ranges have in common.- Parameters:
range
- other instance- Returns:
- true if this instance overlaps
range
- Throws:
IllegalArgumentException
- ifrange
is null
-
contains
Determine if this key range fully contains the specified key range.- Parameters:
range
- other instance- Returns:
- true if this instance contains
range
- Throws:
IllegalArgumentException
- ifrange
is null
-
contains
public boolean contains(byte[] key) Determine if this key range contains the specified key.- Parameters:
key
- key to test- Returns:
- true if this range contains
key
- Throws:
IllegalArgumentException
- ifkey
is null
-
isFull
public boolean isFull()Determine whether this instance contains the full range covering all keys.- Returns:
- true if this instance contains all keys
-
isSingleKey
public boolean isSingleKey()Determine whether this instance contains exactly one key.If so,
getMin()
returns the key.- Returns:
- true if this instance contains exactly one key, otherwise false
-
isPrefixRange
public boolean isPrefixRange()Determine whether this instance contains all keys having some common prefix.If so,
getMin()
returns the prefix.- Returns:
- true if this instance contains all keys having some common prefix, otherwise false
-
isEmpty
public boolean isEmpty()- Returns:
- true if this instance contains no keys
-
prefixedBy
Create a new instance whose minimum and maximum keys are the same as this instance's but with the given byte sequence prepended.- Parameters:
prefix
- key range prefix- Returns:
- this range prefixed by
prefix
- Throws:
IllegalArgumentException
- ifprefix
is null
-
compareTo
public int compareTo(byte[] key) Determine if this range is left of, contains, or is right of the given key.- Parameters:
key
- key for comparison- Returns:
- -1 if this range is left of
key
, 0 if this range containskey
, or 1 if this range is right ofkey
, - Throws:
IllegalArgumentException
- ifkey
is null
-
compare
public static int compare(byte[] key1, byte[] key2) Compare twobyte[]
keys using unsigned lexical ordering, while also accepting null values that represent "positive infinity".- Parameters:
key1
- first keykey2
- second key- Returns:
- -1 if
key1 < key2
, 1 ifkey1 > key2
, or zero ifkey1 = key2
-
empty
Create an empty key range at the specified key.- Parameters:
key
- the minimum and maximum key- Returns:
- the empty key range
[key,key)
- Throws:
IllegalArgumentException
- ifkey
is null
-
equals
-
hashCode
public int hashCode() -
toString
-