Package convex.core.data
Class ACell
java.lang.Object
convex.core.data.AObject
convex.core.data.ACell
- All Implemented Interfaces:
IValidated
,IWriteable
- Direct Known Subclasses:
ACountable
,AFn
,AOp
,APrimitive
,ASignature
,ASymbolic
,ATransaction
,Order
,SignedData
,Syntax
Abstract base class for Cells.
Cells may contain Refs to other Cells, which can be tested with getRefCount()
All data objects intended for on-chain usage / serialisation should extend this.
"It is better to have 100 functions operate on one data structure than
to have 10 functions operate on 10 data structures." - Alan Perlis
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T extends ACell>
Tannounce()
<T extends ACell>
Tvoid
attachMemorySize(long memorySize)
Updates the memorySize of this Cell Not valid for embedded Cells, may throw IllegalOperationException()void
Updates the cached ref of this CellGets the cached blob representing this Cell's Encoding in binary format, if it exists.protected Hash
Gets the Hash if already computed, or null if not yet availableprotected long
Calculates the Memory Size for this Cell.static <T extends ACell>
TcreateAnnounced(T value, Consumer<Ref<ACell>> noveltyHandler)
Creates an ANNOUNCED Ref with the given value in the current store.protected Blob
Creates the encoding for this cell.createPersisted(T value)
Creates a persisted Ref with the given value in the current store.createPersisted(T value, Consumer<Ref<ACell>> noveltyHandler)
Creates a persisted Ref with the given value in the current store.Creates a new Ref for this Cellabstract int
encode(byte[] bs, int pos)
Writes this Cell's encoding to a byte array, including a tag byte which will be written first.protected abstract int
encodeRaw(byte[] bs, int pos)
Writes this Cell's encoding to a byte array, excluding the tag byte.boolean
Checks for equality with another object.boolean
Gets the canonical representation of this Cell.Gets an array of child Refs for this Cell, in the same order as order accessible by getRef.Gets the canonical encoded byte representation of this cell.long
Method to calculate the encoding length of a Cell.getHash()
Hash of data Encoding of this cell, equivalent to the Value ID.long
Gets the Memory Size of this Cell, computing it if required.getRef()
Gets the Ref for this Cell, creating a new direct reference if necessarygetRef(int i)
Gets a numbered child Ref from within this Cell.abstract int
Gets the number of Refs contained within this Cell.abstract byte
getTag()
Gets the tag byte for this cell.getType()
Gets the most specific known runtime Type for this Cell.int
hashCode()
Gets the Java hashCode for this cell.abstract boolean
Returns true if this Cell is in a canonical representation for message writing.boolean
Tests if this Cell is completely encoded, i.e.abstract boolean
Returns true if this Cell represents a first class CVM Value.boolean
Determines if this Cell Represents an embedded object.<T extends ACell>
Tmark()
<T extends ACell>
Tabstract ACell
Converts this Cell to its canonical version.toCVMString(long limit)
Returns the CVM String representation of this Cell.toString()
Returns the Java String representation of this Cell.updateRefs(IRefFunction func)
Updates all Refs in this object using the given function.void
validate()
Validates the complete structure of this object.abstract void
Validates the local structure and invariants of this cell.write(ByteBuffer bb)
Writes this Cell's encoding to a ByteBuffer, including a tag byte which will be written firstMethods inherited from class convex.core.data.AObject
attachEncoding, print, print, print
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface convex.core.data.IWriteable
estimatedEncodingSize
-
Field Details
-
EMPTY_ARRAY
An empty Java array of cells -
cachedRef
Cached Ref. This is useful to manage persistence
-
-
Constructor Details
-
ACell
public ACell()
-
-
Method Details
-
validate
Description copied from interface:IValidated
Validates 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:
validate
in interfaceIValidated
- Throws:
InvalidDataException
- If the data Valie is invalid in any way
-
validateCell
Validates 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.- Throws:
InvalidDataException
- If the Cell is invalid
-
getHash
Hash of data Encoding of this cell, equivalent to the Value ID. Calling this method may force hash computation if needed.- Returns:
- The Hash of this cell's encoding.
-
getTag
public abstract byte getTag()Gets the tag byte for this cell. The tag byte is always written as the first byte of the Cell's Encoding- Returns:
- Tag byte for this Cell
-
cachedHash
Gets the Hash if already computed, or null if not yet available- Returns:
- Cached Hash value, or null if not available
-
hashCode
public int hashCode()Gets the Java hashCode for this cell. Must be consistent with equals. Default is the first bytes (big-endian) of the Cell Encoding's hash, since this is consistent with encoding-based equality. However, different Types may provide more efficient hashcodes provided that the usual invariants are preserved -
equals
-
getEncoding
Gets the canonical encoded byte representation of this cell.- Overrides:
getEncoding
in classAObject
- Returns:
- A Blob representing this cell in encoded form
-
getCanonical
Gets the canonical representation of this Cell. O(1) if canonical representation is already generated, may be O(n) otherwise.- Returns:
- A Blob representing this cell in encoded form
-
equals
Checks for equality with another object. In general, data objects should be considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if offer have a more efficient equals implementation. MUST NOT require reads from Store.- Parameters:
a
- Cell to compare with. May be null??- Returns:
- True if this cell is equal to the other object
-
write
Writes this Cell's encoding to a ByteBuffer, including a tag byte which will be written first- Specified by:
write
in interfaceIWriteable
- Parameters:
bb
- A ByteBuffer to which to write the encoding- Returns:
- The passed ByteBuffer, after the representation of this object has been written.
-
encode
public abstract int encode(byte[] bs, int pos)Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encode
in interfaceIWriteable
- Parameters:
bs
- A byte array to which to write the encodingpos
- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
protected abstract int encodeRaw(byte[] bs, int pos)Writes this Cell's encoding to a byte array, excluding the tag byte.- Parameters:
bs
- A byte array to which to write the encodingpos
- The offset into the byte array- Returns:
- New position after writing
-
createEncoding
Creates the encoding for this cell. Cell must be canonical, or else an error may occur. The encoding itself is a raw Blob, which may be non-canonical.- Specified by:
createEncoding
in classAObject
- Returns:
- Blob Encoding of Object
-
toString
Returns the Java String representation of this Cell. The String representation is intended to be a easy-to-read textual representation of the Cell's data content. -
toCVMString
Returns the CVM String representation of this Cell. Normally, this is as printed, but may be different for some types. MUST return null in O(1) time if the length of the CVM String would exceed limit. The String representation is intended to be a easy-to-read textual representation of the Cell's data content.- Parameters:
limit
- Limit of CVM String length in UTF-8 bytes- Returns:
- CVM String, or null if limit exceeded by too much
-
cachedEncoding
Gets the cached blob representing this Cell's Encoding in binary format, if it exists.- Returns:
- The cached blob for this cell, or null if not available.
-
calcMemorySize
protected long calcMemorySize()Calculates the Memory Size for this Cell. Requires any child Refs to be either Direct or of persisted status at minimum, or you might get a MissingDataException- Returns:
- Memory Size of this Cell
-
getEncodingLength
public long getEncodingLength()Method to calculate the encoding length of a Cell. May be overridden to avoid creating encodings during memory size calculations. This reduces hashing!- Returns:
- Exact encoding length of this Cell
-
getMemorySize
public final long getMemorySize()Gets the Memory Size of this Cell, computing it if required. The memory size is the total storage requirement for this cell. Embedded cells do not require storage for their own encoding, but may require storage for nested non-embedded Refs.- Returns:
- Memory Size of this Cell
-
isEmbedded
public boolean isEmbedded()Determines if this Cell Represents an embedded object. Embedded objects are encoded directly into the encoding of the containing Cell (avoiding the need for a hashed reference). Subclasses should override this if they have a cheap O(1) way to determine if they are embedded or otherwise.- Returns:
- true if Cell is embedded, false otherwise
-
isCanonical
public abstract boolean isCanonical()Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).- Returns:
- true if the object is in canonical format, false otherwise
-
toCanonical
Converts this Cell to its canonical version. Returns this Cell if already canonical, may be O(n) in size of value otherwise.- Returns:
- Canonical version of Cell
-
isCVMValue
public abstract boolean isCVMValue()Returns true if this Cell represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false, pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures- Returns:
- true if the object is a CVM Value, false otherwise
-
getRefCount
public abstract int getRefCount()Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.- Returns:
- The number of Refs in this Cell
-
getRef
Gets the Ref for this Cell, creating a new direct reference if necessary- Type Parameters:
R
- Type of Cell- Returns:
- Ref for this Cell
-
createRef
Creates a new Ref for this Cell- Type Parameters:
R
- Type of Cell- Returns:
- New Ref instance
-
getRef
Gets a numbered child Ref from within this Cell.- Type Parameters:
R
- Type of referenced Cell- Parameters:
i
- Index of ref to get- Returns:
- The Ref at the specified index
-
updateRefs
Updates 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.- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
getChildRefs
Gets an array of child Refs for this Cell, in the same order as order accessible by getRef. Concrete implementations may override this to optimise performance.- Type Parameters:
R
- Type of referenced Cell- Returns:
- Array of Refs
-
getType
Gets the most specific known runtime Type for this Cell.- Returns:
- The Type of this Call
-
attachMemorySize
public void attachMemorySize(long memorySize)Updates the memorySize of this Cell Not valid for embedded Cells, may throw IllegalOperationException()- Parameters:
memorySize
- Memory size to assign
-
attachRef
Updates the cached ref of this Cell- Parameters:
ref
- Ref to assign
-
createAnnounced
Creates an ANNOUNCED Ref with the given value in the current store. Novelty handler is called for all new Refs that are persisted (recursively), starting from lowest levels.- Type Parameters:
T
- Type of Value- Parameters:
value
- Value to announcenoveltyHandler
- Novelty handler to call for any Novelty (may be null)- Returns:
- Persisted Ref
-
announce
-
announce
-
mark
-
mark
-
createPersisted
public static <T extends ACell> Ref<T> createPersisted(T value, Consumer<Ref<ACell>> noveltyHandler)Creates a persisted Ref with the given value in the current store. Novelty handler is called for all new Refs that are persisted (recursively), starting from lowest levels (depth first order)- Type Parameters:
T
- Type of Value- Parameters:
value
- Any CVM value to persistnoveltyHandler
- Novelty handler to call for any Novelty (may be null)- Returns:
- Persisted Ref
-
createPersisted
Creates a persisted Ref with the given value in the current store. Returns the current Ref if already persisted- Type Parameters:
T
- Type of Value- Parameters:
value
- Any CVM value to persist- Returns:
- Ref to the given value
-
isCompletelyEncoded
public boolean isCompletelyEncoded()Tests if this Cell is completely encoded, i.e. has no external Refs. This implies that the complete Cell can be represented in a single encoding.- Returns:
- true if completely encoded, false otherwise
-