Class DbKey

java.lang.Object
com.exonum.binding.common.proofs.map.DbKey
All Implemented Interfaces:
java.lang.Comparable<DbKey>

public final class DbKey
extends java.lang.Object
implements java.lang.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.
  • Method Summary

    Modifier and Type Method Description
    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.
    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.
    boolean equals​(java.lang.Object o)  
    static DbKey fromBytes​(byte[] rawDbKey)
    Creates a database key.
    byte[] getKeySlice()
    Returns the key slice.
    DbKey.Type getNodeType()
    Returns the type of the Merkle-Patricia tree node corresponding to this database key.
    int getNumSignificantBits()
    Returns the number of significant bits in the key slice.
    byte[] getRawDbKey()  
    int hashCode()  
    boolean isPrefixOf​(DbKey other)
    Returns true if this DbKey is a prefix of that DbKey.
    KeyBitSet keyBits()
    Returns a key as a bit set.
    static DbKey newBranchKey​(byte[] keySlice, int numSignificantBits)
    Creates a new branch database key.
    static DbKey newLeafKey​(byte[] keySlice)
    Given key as a byte array, returns new leaf DbKey.
    static DbKey newLeafKey​(com.google.protobuf.ByteString key)
    Given key as a ByteString, returns new leaf DbKey.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • fromBytes

      public static DbKey fromBytes​(byte[] rawDbKey)
      Creates a database key.
      Parameters:
      rawDbKey - a raw database key, as used in the underlying storage
      Throws:
      java.lang.NullPointerException - if the key is null
      java.lang.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:
      java.lang.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.
    • equals

      public boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.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 java.lang.Comparable<DbKey>