Class DbKey

  • All Implemented Interfaces:
    Comparable<DbKey>

    public final class DbKey
    extends Object
    implements Comparable<DbKey>
    A map proof database key.

    It includes:

    • Node type: a branch or a leaf
    • The key to which the value is mapped for leaf nodes; the common prefix of keys in the left and right sub-trees for branch (= intermediate) nodes
    • The size of the common prefix in branch nodes.

    The binary layout of the database key is the following:

           Offset:   0           1 …                 32  33                   34
     Database key: | node type | 32-byte long user key | common prefix size |
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DbKey.Type
      Type of the node in a Merkle-Patricia tree.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DB_KEY_SIZE
      Size of the database key in bytes.
      static int KEY_SIZE
      Size of the user key in bytes.
      static int KEY_SIZE_BITS
      Size of the user key in bits.
    • Field Detail

      • KEY_SIZE

        public static final int KEY_SIZE
        Size of the user key in bytes.
        See Also:
        Constant Field Values
      • KEY_SIZE_BITS

        public static final int KEY_SIZE_BITS
        Size of the user key in bits.
        See Also:
        Constant Field Values
      • DB_KEY_SIZE

        public static final int DB_KEY_SIZE
        Size of the database key in bytes.
        See Also:
        Constant Field Values
    • Method Detail

      • fromBytes

        public static DbKey fromBytes​(byte[] rawDbKey)
        Creates a database key.
        Parameters:
        rawDbKey - a raw database key, as used in the underlying storage
        Throws:
        NullPointerException - if the key is null
        IllegalArgumentException - if the specified database key is not valid
      • newLeafKey

        public static DbKey newLeafKey​(com.google.protobuf.ByteString key)
        Given key as a ByteString, returns new leaf DbKey.
      • newLeafKey

        public static DbKey newLeafKey​(byte[] keySlice)
        Given key as a byte array, returns new leaf DbKey.
      • newBranchKey

        public static DbKey newBranchKey​(byte[] keySlice,
                                         int numSignificantBits)
        Creates a new branch database key.
        Parameters:
        keySlice - key as a byte array, must be 32-byte long
        numSignificantBits - the number of significant bits in the key (= the prefix size)
        Throws:
        IllegalArgumentException - if key has invalid length or contains set bits after the prefix; if numSignificantBits is not in range [0, 255]
      • getNodeType

        public DbKey.Type getNodeType()
        Returns the type of the Merkle-Patricia tree node corresponding to this database key.
      • getKeySlice

        public byte[] getKeySlice()
        Returns the key slice. It's size is equal to KEY_SIZE, but the number of significant bits is equal to the getNumSignificantBits().
      • getNumSignificantBits

        public int getNumSignificantBits()
        Returns the number of significant bits in the key slice.
      • getRawDbKey

        public byte[] getRawDbKey()
      • keyBits

        public KeyBitSet keyBits()
        Returns a key as a bit set.
      • commonPrefix

        public DbKey commonPrefix​(DbKey other)
        Returns new branch DbKey (unless common prefix of two equals DbKeys is requested, in which case this DbKey itself is returned), which is a common prefix of this and another DbKey.
      • isPrefixOf

        public boolean isPrefixOf​(DbKey other)
        Returns true if this DbKey is a prefix of that DbKey.
      • hashCode

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

        public int compareTo​(DbKey other)
        The following algorithm is used for comparison: Try to find a first bit index at which this key is greater than the other key (i.e., a bit of this key is 1 and the corresponding bit of the other key is 0), and vice versa. The smaller of these indexes indicates the greater key. If there is no such bit, then lengths of these keys are compared and the key with greater length is considered a greater key.
        Specified by:
        compareTo in interface Comparable<DbKey>