Package io.crums.util.mrkl
Class Builder
java.lang.Object
io.crums.util.mrkl.Builder
- Direct Known Subclasses:
FixedLeafBuilder
public class Builder
extends java.lang.Object
Collects items (byte arrays) and builds a Merkle tree. If all the items (the leaves of the tree)
added are fixed-width (and the tree's data fits under 1GB memory), then the instance
builds a FixedLeafTree; otherwise, it builds a FreeLeafTree instance.
-
Field Summary
Fields Modifier and Type Field Description protected boolean
copyOnWrite
protected java.util.List<java.util.List<byte[]>>
data
Breadth-first view of the nodes' data.protected java.security.MessageDigest
digest
protected java.lang.Object
lock
Synchronization lock. -
Constructor Summary
Constructors Modifier Constructor Description protected
Builder(Builder copy)
Copy constructor.Builder(java.lang.String algo)
Creates a new copy-on-write (copy-on-add) instance with a dedicated MessageDigest using the given hashing algorithm.Builder(java.lang.String algo, boolean copyOnWrite)
Creates a new instance with a dedicated MessageDigest using the given hashing algorithm. -
Method Summary
Modifier and Type Method Description int
add(byte[] item)
Adds the specified item as the next leaf node are returns its leaf index.int
add(byte[] item, int off, int len)
Adds the specified item as the next leaf node are returns its leaf index.Tree
build()
Builds and returns the tree.void
clear()
Clears the state of the instance, as if new.protected void
completeTree()
int
count()
Returns the number of items added.java.lang.String
getHashAlgo()
byte[]
hash(byte[] data)
Convenience method to hash using this instance's digest algo.int
hashWidth()
byte[]
item(int index)
Returns (a copy of) of the item at the given index.int
leafWidth()
protected java.util.List<byte[]>
level(int index)
protected int
levelSize(int level)
protected java.util.List<byte[]>
newByteArrayList(int level)
protected Tree
packageTree()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
lock
protected final java.lang.Object lockSynchronization lock. -
data
protected final java.util.List<java.util.List<byte[]>> dataBreadth-first view of the nodes' data. -
digest
protected final java.security.MessageDigest digest -
copyOnWrite
protected final boolean copyOnWrite
-
-
Constructor Details
-
Builder
public Builder(java.lang.String algo) throws java.lang.IllegalArgumentExceptionCreates a new copy-on-write (copy-on-add) instance with a dedicated MessageDigest using the given hashing algorithm.- Parameters:
algo
- the digest algorithm (e.g. MD5, SHA-1, SHA-256)- Throws:
java.lang.IllegalArgumentException
- in lieu of checked NoSuchAlgorithmException
-
Builder
public Builder(java.lang.String algo, boolean copyOnWrite) throws java.lang.IllegalArgumentExceptionCreates a new instance with a dedicated MessageDigest using the given hashing algorithm.- Parameters:
algo
- the digest algorithm (e.g. MD5, SHA-1, SHA-256)copyOnWrite
- if true, then every add(byte[]) is argument is copied (the argument's value is considered volatile). When you know you won't be modifying the input arguments set this to false- Throws:
java.lang.IllegalArgumentException
- in lieu of checked NoSuchAlgorithmException
-
Builder
Copy constructor. The new instance shares the same fields as this instance.- Parameters:
copy
-
-
-
Method Details
-
add
public int add(byte[] item)Adds the specified item as the next leaf node are returns its leaf index.- Parameters:
item
- the item's data (copied)- Returns:
- the item's leaf node index in the to-be built tree
- See Also:
add(byte[], int, int)
-
hash
public byte[] hash(byte[] data)Convenience method to hash using this instance's digest algo. Does not affect the state of the builder.- Parameters:
data
- to be hashed- Returns:
- a new array containing the hash
-
hashWidth
public final int hashWidth() -
add
public int add(byte[] item, int off, int len) throws java.lang.IndexOutOfBoundsExceptionAdds the specified item as the next leaf node are returns its leaf index.- Parameters:
item
- the item's data (copied)off
- starting offset into itemlen
- the number of bytes following off- Returns:
- the item's leaf node index in the to-be built tree
- Throws:
java.lang.IndexOutOfBoundsException
-
build
Builds and returns the tree. On return the builder is cleared.- See Also:
clear()
-
completeTree
protected void completeTree() -
packageTree
-
leafWidth
public int leafWidth() -
clear
public void clear()Clears the state of the instance, as if new. -
count
public final int count()Returns the number of items added. -
item
public byte[] item(int index) throws java.lang.IndexOutOfBoundsExceptionReturns (a copy of) of the item at the given index.- Parameters:
index
- $lt; count()- Throws:
java.lang.IndexOutOfBoundsException
-
getHashAlgo
public final java.lang.String getHashAlgo() -
levelSize
protected final int levelSize(int level) -
newByteArrayList
protected java.util.List<byte[]> newByteArrayList(int level) -
level
protected final java.util.List<byte[]> level(int index)
-