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 thisDbKey
is a prefix of thatDbKey
.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.
-
Field Details
-
KEY_SIZE
public static final int KEY_SIZESize of the user key in bytes.- See Also:
- Constant Field Values
-
KEY_SIZE_BITS
public static final int KEY_SIZE_BITSSize of the user key in bits.- See Also:
- Constant Field Values
-
DB_KEY_SIZE
public static final int DB_KEY_SIZESize of the database key in bytes.- See Also:
- Constant Field Values
-
-
Method Details
-
fromBytes
Creates a database key.- Parameters:
rawDbKey
- a raw database key, as used in the underlying storage- Throws:
java.lang.NullPointerException
- if the key is nulljava.lang.IllegalArgumentException
- if the specified database key is not valid
-
newLeafKey
Given key as a ByteString, returns new leaf DbKey. -
newLeafKey
Given key as a byte array, returns new leaf DbKey. -
newBranchKey
Creates a new branch database key.- Parameters:
keySlice
- key as a byte array, must be 32-byte longnumSignificantBits
- 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
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 toKEY_SIZE
, but the number of significant bits is equal to thegetNumSignificantBits()
. -
getNumSignificantBits
public int getNumSignificantBits()Returns the number of significant bits in the key slice. -
getRawDbKey
public byte[] getRawDbKey() -
keyBits
Returns a key as a bit set. -
commonPrefix
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
Returns true if thisDbKey
is a prefix of thatDbKey
. -
equals
public boolean equals(java.lang.Object o)- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-
compareTo
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 interfacejava.lang.Comparable<DbKey>
-