Package convex.core.data
Class BlobTree
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ABlob>
Implementation of a large Blob data structure consisting of 2 or more chunks.
Intention is to enable relatively large binary content to be handled without
too many tree levels, and without too many references in a single tree node
We choose a branching factor of 16 as a reasonable tradeoff.
Level 1 can hold up to 64k Level 2 can hold up to 1mb Level 3 can hold up to
16mb Level 4 can hold up to 256mb ... Level 15 (max) should be big enough for
the moment
One smart reference is maintained for each child node at each level
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intstatic intstatic intMaximum byte length of an encoded BlobTree nodeFields inherited from class convex.core.data.ABlob
contentHashFields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY -
Method Summary
Modifier and TypeMethodDescriptionAppend an additional data object to this, creating a new data object.static longcalcChunks(long length)Computes the number of chunks for a BlobTree of the given lengthlongComputes the length of the longest common hex prefix between two blobslongcount()Gets the length of this Blobstatic BlobTreeCreate a BlobTree from an arbitrary Blob.intencode(byte[] bs, int pos)Writes this Cell's encoding to a byte array, including a tag byte which will be written firstintencodeRaw(byte[] bs, int pos)Writes this Cell's encoding to a byte array, excluding the tag bytebooleanDetermines if this Blob is equal to another Blob.booleanbooleanequalsBytes(byte[] bytes, int byteOffset)Tests if this Blob is equal to a subset of a byte arraybooleanequalsBytes(ABlob b)Tests if this Blob has exactly the same bytes as another BlobintEstimate the encoded data size for this Cell.Gets a byte buffer containing this Blob's data.voidgetBytes(byte[] dest, int destOffset)Copies the bytes from this blob to a given destinationgetChunk(long chunkIndex)Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk sizegetRef(int i)Gets a numbered child Ref from within this Cell.intGets the number of Refs contained within this Cell.bytegetTag()Gets the tag byte for this cell.bytegetUnchecked(long i)Gets the byte at the specified position in this data object, without bounds checking.longhexMatchLength(ABlob b, long start, long length)Returns the number of matching hex digits in the given hex range of another blob.booleanReturns true if this Cell is in a canonical format for message writing.booleanReturns true if this object represents a first class CVM Value.booleanReturns true if this object is a regular blob (i.e.longGets the long value of this Blob if the length is exactly 8 bytes, otherwise throws an Exceptionstatic BlobTreestatic BlobTreeread(ByteBuffer bb, long count)Reads a BlobTree from a bytebuffer.slice(long start, long length)Gets a contiguous slice of this blob, as a new Blob.toBlob()Converts this object to a Blob instanceConverts this Cell to its canonical version.Converts this data object to a lowercase hex string representationvoidtoHexString(StringBuilder sb)longtoLong()Converts this Blob to the corresponding long value.protected voidupdateDigest(MessageDigest digest)updateRefs(IRefFunction func)Updates all Refs in this object using the given function.voidvalidate()Validates the complete structure of this object.voidValidates the local structure and invariants of this cell.intwriteToBuffer(byte[] bs, int pos)Writes the raw byte contents of this blob to a byte arraywriteToBuffer(ByteBuffer bb)Writes the raw byte contents of this blob to a ByteBuffer.Methods inherited from class convex.core.data.ABlob
byteAt, compareTo, computeHash, empty, equals, get, getBytes, getContentHash, getElementRef, getHexDigit, getType, hashCode, hexEquals, hexEquals, hexLength, print, slice, toByteBuffer, toHexStringMethods inherited from class convex.core.data.ACountable
isEmpty, sizeMethods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, isEmbedded, toString, writeMethods inherited from class convex.core.data.AObject
attachEncoding, print
-
Field Details
-
BIT_SHIFT_PER_LEVEL
public static final int BIT_SHIFT_PER_LEVEL- See Also:
- Constant Field Values
-
FANOUT
public static final int FANOUT- See Also:
- Constant Field Values
-
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTHMaximum byte length of an encoded BlobTree node
-
-
Method Details
-
create
Create a BlobTree from an arbitrary Blob. Must be of sufficient size to convert to BlobTree- Parameters:
blob- Source of BlobTree data- Returns:
- New BolobTree instance
-
calcChunks
public static long calcChunks(long length)Computes the number of chunks for a BlobTree of the given length- Parameters:
length- The length of the Blob in bytes- Returns:
- Number of chunks needed for a given byte length.
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical format for message writing. Reading or writing a non-canonical value should be considered illegal, but non-canonical objects may be used on a temporary internal basis.- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACellReturns true if this object represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false. CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValuein classACell- Returns:
- true if the object is a CVM Value, false otherwise
-
getBytes
public void getBytes(byte[] dest, int destOffset)Description copied from class:ABlobCopies the bytes from this blob to a given destination -
count
public long count()Description copied from class:ABlobGets the length of this Blob -
slice
Description copied from class:ABlobGets a contiguous slice of this blob, as a new Blob. Shares underlying backing data where possible -
toBlob
Description copied from class:ABlobConverts this object to a Blob instance -
updateDigest
- Specified by:
updateDigestin classABlob
-
getUnchecked
public byte getUnchecked(long i)Description copied from class:ABlobGets the byte at the specified position in this data object, without bounds checking. Only safe if index is known to be in bounds, otherwise result is undefined.- Specified by:
getUncheckedin classABlob- Parameters:
i- Index of the byte to get- Returns:
- The byte at the specified position
-
equals
Description copied from class:ABlobDetermines if this Blob is equal to another Blob. Blobs are defined to be equal if they have the same on-chain representation, i.e. if and only if all of the following are true: - Blob is of the same general type - Blobs are of the same length - All byte values are equal -
equals
-
equalsBytes
public boolean equalsBytes(byte[] bytes, int byteOffset)Description copied from class:ABlobTests if this Blob is equal to a subset of a byte array- Specified by:
equalsBytesin classABlob- Parameters:
bytes- Byte array to compare withbyteOffset- Offset into byte array- Returns:
- true if exactly equal, false otherwise
-
equalsBytes
Description copied from class:ABlobTests if this Blob has exactly the same bytes as another Blob- Specified by:
equalsBytesin classABlob- Parameters:
b- Blob to compare with- Returns:
- True if byte content is exactly equal, false otherwise
-
encode
public int encode(byte[] bs, int pos)Description copied from class:ACellWrites this Cell's encoding to a byte array, including a tag byte which will be written first- Specified by:
encodein interfaceIWriteable- Specified by:
encodein classACell- Parameters:
bs- A byte array to which to write the encodingpos- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
public int encodeRaw(byte[] bs, int pos)Description copied from class:ACellWrites this Cell's encoding to a byte array, excluding the tag byte -
writeToBuffer
Description copied from class:ABlobWrites the raw byte contents of this blob to a ByteBuffer.- Specified by:
writeToBufferin classABlob- Parameters:
bb- ByteBuffer to write to- Returns:
- The passed ByteBuffer, after writing byte content
-
writeToBuffer
public int writeToBuffer(byte[] bs, int pos)Description copied from class:ABlobWrites the raw byte contents of this blob to a byte array- Specified by:
writeToBufferin classABlob- Parameters:
bs- Byte array to write topos- Starting position in byte array to write to- Returns:
- The position in the array after writing
-
read
Reads a BlobTree from a bytebuffer. Assumes that tag byte and count are already read- Parameters:
bb- ByteBuffercount- Count of bytes in BlobTree being read- Returns:
- Decoded BlobTree
- Throws:
BadFormatException- if the encoding was invalid
-
read
- Throws:
BadFormatException
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteableEstimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.- Returns:
- The estimated size for the binary representation of this object.
-
append
Description copied from class:ABlobAppend an additional data object to this, creating a new data object. -
getChunk
Description copied from class:ABlobGets a chunk of this Blob, as a canonical Blob up to the maximum chunk size -
validate
Description copied from interface:IValidatedValidates the complete structure of this object. It is necessary to ensure all child Refs are validated, so the general contract for validate is:- Call super.validate() - which will indirectly call validateCell()
- Call validate() on any contained cells in this class
- Specified by:
validatein interfaceIValidated- Overrides:
validatein classABlob- Throws:
InvalidDataException- If the data Valie is invalid in any way
-
getByteBuffer
Description copied from class:ABlobGets a byte buffer containing this Blob's data. Will have remaining bytes equal to this Blob's size.- Specified by:
getByteBufferin classABlob- Returns:
- A ByteBuffer containing the Blob's data.
-
toHexString
Description copied from class:ABlobConverts this data object to a lowercase hex string representation- Specified by:
toHexStringin classABlob- Returns:
- Hex String representation
-
toHexString
- Specified by:
toHexStringin classABlob
-
validateCell
Description copied from class:ACellValidates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Overrides:
validateCellin classABlob- Throws:
InvalidDataException- If the Cell is invalid
-
commonHexPrefixLength
Description copied from class:ABlobComputes the length of the longest common hex prefix between two blobs- Specified by:
commonHexPrefixLengthin classABlob- Parameters:
b- Blob to compare with- Returns:
- The length of the longest common prefix in hex digits
-
hexMatchLength
Description copied from class:ABlobReturns the number of matching hex digits in the given hex range of another blob. Assumes range is valid for both blobs. Returns length if this Blob is exactly equal to the specified hex range.- Specified by:
hexMatchLengthin classABlob- Parameters:
b- Blob to compare withstart- Start position (in hex digits)length- Length to compare (in hex digits)- Returns:
- The number of matching hex characters
-
longValue
public long longValue()Description copied from class:ABlobGets the long value of this Blob if the length is exactly 8 bytes, otherwise throws an Exception -
toLong
public long toLong()Description copied from class:ABlobConverts this Blob to the corresponding long value. Assumes big-endian format, as if the entire blob is interpreted as a signed big integer. Higher bytes outside the Long range will be ignored. -
getRefCount
public int getRefCount()Description copied from class:ACellGets the number of Refs contained within this Cell. This number is final / immutable for any given instance. Contained Refs may be either external or embedded.- Specified by:
getRefCountin classACell- Returns:
- The number of Refs in this Cell
-
getRef
Description copied from class:ACellGets a numbered child Ref from within this Cell. -
updateRefs
Description copied from class:ACellUpdates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.- Overrides:
updateRefsin classACell- Parameters:
func- Ref update function- Returns:
- Cell with updated Refs
-
isRegularBlob
public boolean isRegularBlob()Description copied from class:ABlobReturns true if this object is a regular blob (i.e. not a special blob type like Hash or Address)- Overrides:
isRegularBlobin classABlob- Returns:
- True if a regular blob
-
getTag
public byte getTag()Description copied from class:ACellGets the tag byte for this cell. The tag byte will be the first byte of the encoding -
toCanonical
Description copied from class:ACellConverts this Cell to its canonical version. Returns this if already canonical- Specified by:
toCanonicalin classABlob- Returns:
- Canonical version of Cell
-