Class KeyRange

java.lang.Object
io.permazen.kv.KeyRange

public class KeyRange extends Object
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 Details

  • Constructor Details

  • Method Details

    • forPrefix

      public static KeyRange forPrefix(ByteData prefix)
      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 - if prefix is null
    • getMin

      public ByteData getMin()
      Get range minimum (inclusive).
      Returns:
      inclusivie minimum, never null
    • getMax

      public ByteData getMax()
      Get range maximum (exclusive), or null if there is no upper bound.
      Returns:
      exclusivie maximum, or null for none
    • overlaps

      public boolean overlaps(KeyRange range)
      Determine if this key range overlaps the specified key range, i.e., there exists at least one byte[] key that both ranges have in common.
      Parameters:
      range - other instance
      Returns:
      true if this instance overlaps range
      Throws:
      IllegalArgumentException - if range is null
    • contains

      public boolean contains(KeyRange range)
      Determine if this key range fully contains the specified key range.
      Parameters:
      range - other instance
      Returns:
      true if this instance contains range
      Throws:
      IllegalArgumentException - if range is null
    • contains

      public boolean contains(ByteData key)
      Determine if this key range contains the specified key.
      Parameters:
      key - key to test
      Returns:
      true if this range contains key
      Throws:
      IllegalArgumentException - if key 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()
      Determine whether this instance contains zero keys (implying getMin()== getMax()).
      Returns:
      true if this instance contains no keys
    • prefixedBy

      public KeyRange prefixedBy(ByteData prefix)
      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 - if prefix is null
    • compareTo

      public int compareTo(ByteData 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 contains key, or 1 if this range is right of key,
      Throws:
      IllegalArgumentException - if key is null
    • compare

      public static int compare(ByteData key1, ByteData key2)
      Compare two byte[] keys using unsigned lexical ordering, while also accepting null values that represent "positive infinity".
      Parameters:
      key1 - first key, or null for "positive infinity"
      key2 - second key, or null for "positive infinity"
      Returns:
      -1 if key1 < key2, 1 if key1 > key2, or zero if key1 = key2
    • empty

      public static KeyRange empty(ByteData key)
      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 - if key is null
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object