Interface PermazenObject
- All Known Implementing Classes:
UntypedPermazenObject
Permazen
Java model objects.
All Permazen
database objects are instances of runtime-generated sub-classes of user-provided Java model types.
These generated subclasses will always implement this interface, providing convenient access to database operations.
Therefore, it is convenient to declare Java model classes abstract
and implements PermazenObject
.
However, this is not strictly necessary; all of the methods declared here ultimately delegate to one of the
PermazenTransaction
support methods.
Object Identity and State
Every PermazenObject
has a unique 64-bit object identifier, represented as an ObjId
.
All PermazenObject
instances are permanently associated with a specific
transaction, and are the unique representatives for their corresponding ObjId
in that transaction. All field state derives from the associated transaction.
There are two types of transactions: normal transactions reflecting an open transaction on the underlying key/value database, and detached transactions, which are in-memory containers of object data. Detached transactions are fully functional, supporting index queries, object versioning, etc.
Copying Objects
This interface provides methods for copying a graph of objects between transactions, for example, from an open
key/value database transaction into an in-memory detached transaction ("copy out"), or vice-versa ("copy in").
A graph of objects can be copied by specifying a starting object and a list of reference cascades.
(see @PermazenField
). Object ID's can be remapped during the copy if necessary, e.g., to ensure
existing objects are not overwritten (see CopyState
).
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault ObjIdSet
Find all objects reachable through the specified reference cascade(s).default PermazenObject
Copy this instance, and other instances reachable through the specified reference cascades (if any), into the transaction associated with the current thread.default PermazenObject
Copy this instance, and other instances reachable through the specified reference cascades (if any), into the in-memory, detached transaction associated with this instance's transaction.default PermazenObject
copyTo
(PermazenTransaction dest, int recursionLimit, CopyState copyState, String... cascades) Copy this instance, and other instances it references through the specified reference cascades, into the specified destination transaction.default boolean
delete()
Delete this instance, if it exists, in this instance's associated transaction.default boolean
exists()
Determine whether this instance still exists in its associated transaction.Class<?>
Get the original Java model class of which thisPermazenObject
is an instance.getObjId()
Get this instance's unique Permazen object identifier.default PermazenClass<?>
Get thePermazenClass
of which thisPermazenObject
is an instance.Get this instance's associatedPermazenTransaction
.default SchemaId
Get theSchemaId
that identifies this instance's current schema.default boolean
Determine whether this instance is a normal instance or is a detached instance associated with aDetachedPermazenTransaction
.default boolean
Migrate the schema of this instance, if necessary, so that it matches the schema of its associated transaction.default boolean
recreate()
Recreate a deleted instance, if it does not exist, in its associated transaction.void
Reset cached simple field values.default void
revalidate
(Class<?>... groups) Add this instance to the validation queue for validation in its associated transaction.
-
Method Details
-
getObjId
ObjId getObjId()Get this instance's unique Permazen object identifier.This method always succeeds, even if the object does not exist.
- Returns:
- unique database identifier for this instance
-
getPermazenTransaction
PermazenTransaction getPermazenTransaction()Get this instance's associatedPermazenTransaction
.- Returns:
- the
PermazenTransaction
that contains this instance's field state
-
getSchemaId
Get theSchemaId
that identifies this instance's current schema.- Returns:
- the ID of this instance's current schema
- Throws:
DeletedObjectException
- if this object does not exist in thePermazenTransaction
associated with this instanceStaleTransactionException
- if the transaction associated with this instance is no longer usable
-
delete
default boolean delete()Delete this instance, if it exists, in this instance's associated transaction.See
Transaction.delete()
for details on secondary deletions fromDeleteAction.DELETE
andPermazenField.forwardDelete()
.- Returns:
- true if instance was deleted, false if it did not exist
- Throws:
StaleTransactionException
- if the transaction associated with the current thread is no longer usableReferencedObjectException
- if the object is referenced by some other object through a reference field configured forDeleteAction.EXCEPTION
-
exists
default boolean exists()Determine whether this instance still exists in its associated transaction.- Returns:
- true if instance exists, otherwise false
- Throws:
StaleTransactionException
- if the transaction associated with this instance is no longer usable
-
isDetached
default boolean isDetached()Determine whether this instance is a normal instance or is a detached instance associated with aDetachedPermazenTransaction
.Equivalent to
getPermazenTransaction().isDetached()
.- Returns:
- true if instance lives in a detached transaction
-
recreate
default boolean recreate()Recreate a deleted instance, if it does not exist, in its associated transaction. The fields of a recreated object are set to their initial values. If the object already exists, nothing changes.- Returns:
- true if instance was recreated, false if it already existed
- Throws:
StaleTransactionException
- if the transaction associated with this instance is no longer usable
-
revalidate
Add this instance to the validation queue for validation in its associated transaction.The actual validation will occur either during
PermazenTransaction.commit()
or at the next invocation ofPermazenTransaction.validate()
, whichever occurs first. The specified validation groups, if any, will be used.If the associated transaction was opened with
ValidationMode.DISABLED
, no validation will be performed.- Parameters:
groups
- validation group(s) to use for validation; if empty,Default
is assumed- Throws:
DeletedObjectException
- if this object does not exist in thePermazenTransaction
associated with this instanceIllegalStateException
- if transaction commit is already in progressStaleTransactionException
- if the transaction associated with this instance is no longer usableIllegalArgumentException
- ifgroups
is or any group ingroups
null
-
migrateSchema
default boolean migrateSchema()Migrate the schema of this instance, if necessary, so that it matches the schema of its associated transaction.If a schema change occurs,
@OnSchemaChange
methods will be invoked prior to this method returning.- Returns:
- true if the object's schema changed, false if it was already migrated
- Throws:
DeletedObjectException
- if this object does not exist in thePermazenTransaction
associated with this instanceStaleTransactionException
- if the transaction associated with this instance is no longer usable
-
cascade
Find all objects reachable through the specified reference cascade(s).This method finds all objects reachable from this instance through reference field cascades having the specified name(s). In other words, a reference field is traversed in the forward or inverse direction if one of the
cascades
is specified in the corresponding @PermazenField annotation property.All objects found will be automatically migrated to this object's associated transaction's schema.
The
recursionLimit
parameter can be used to limit the maximum distance of any reachable object, measured in the number of reference field "hops" from the given starting object.- Parameters:
cascades
- zero or more reference cascades that identify additional objects to copyrecursionLimit
- the maximum number of reference fields to hop through, or -1 for infinity- Returns:
- the object ID's of the objects reachable from this object (including this object itself)
- Throws:
DeletedObjectException
- if any object containing a traversed reference field does not actually existIllegalArgumentException
- ifrecursionLimit
is less that -1IllegalArgumentException
- ifcascades
or any element therein is null- See Also:
-
copyOut
Copy this instance, and other instances reachable through the specified reference cascades (if any), into the in-memory, detached transaction associated with this instance's transaction.This method should only be invoked on regular database
PermazenObject
s. The returned object will always live in aDetachedPermazenTransaction
.This is a convenience method, and is equivalent to invoking:
this.copyTo(this.getPermazenTransaction().getDetachedTransaction(), -1, new CopyState(true), cascades);
Note that
@OnCreate
and@OnChange
notifications will not be delivered in the detached transaction.- Parameters:
cascades
- zero or more reference cascades that identify additional objects to copy- Returns:
- the detached
PermazenObject
copy of this instance - Throws:
SchemaMismatchException
- if the schema corresponding to this object's version is not identical in both transactionsIllegalArgumentException
- if this instance is itself a detached instanceIllegalArgumentException
- ifcascades
or any element therein is null- See Also:
-
copyIn
Copy this instance, and other instances reachable through the specified reference cascades (if any), into the transaction associated with the current thread.Normally this method would only be invoked on detached database objects. The returned object will be a
PermazenObject
in the open transaction associated with the current thread.This is a convenience method, and is equivalent to invoking:
this.copyTo(PermazenTransaction.getCurrent(), -1, new CopyState(false), cascades)
Note that
@OnCreate
and@OnChange
notifications will be delivered in the destination transaction as objects are created and updated.- Parameters:
cascades
- zero or more reference cascades that identify additional objects to copy- Returns:
- the regular database copy of this instance
- Throws:
DeletedObjectException
- if any copied object ends up with a reference to an object that does not exist indest
through a reference field configured to disallow deleted assignmentSchemaMismatchException
- if the schema corresponding to this object's version is not identical in both transactionsIllegalArgumentException
- ifcascades
or any element therein is null- See Also:
-
copyTo
default PermazenObject copyTo(PermazenTransaction dest, int recursionLimit, CopyState copyState, String... cascades) Copy this instance, and other instances it references through the specified reference cascades, into the specified destination transaction.This is a more general method; see
copyIn()
andcopyOut()
for more common and specific use cases.If a target object does not exist, it will be created, otherwise its schema will be migrated to match the source object if necessary (with resulting
@OnSchemaChange
notifications). IfCopyState.isSuppressNotifications()
returns false,@OnCreate
and@OnChange
notifications will also be delivered.The
copyState
tracks which objects have already been copied. For a "fresh" copy operation, pass a newly createdCopyState
; for a copy operation that is a continuation of a previous copy, reuse the previousCopyState
. TheCopyState
may also be configured to remap object ID's.This object's transaction and
dest
must be compatible in that for any schemas encountered, those schemas must be identical in both.If
dest
is not aDetachedPermazenTransaction
and any copied objects contain reference fields configured withPermazenField.allowDeleted()
= false
, then any objects referenced by those fields must also be copied, or else must already exist indest
. Otherwise, aDeletedObjectException
is thrown and it is indeterminate which objects were copied.The
recursionLimit
parameter can be used to limit the maximum distance of any reachable object, measured in the number of reference field "hops" from the given starting object.Note: if two threads attempt to copy objects between the same two transactions at the same time but in opposite directions, deadlock could result.
- Parameters:
dest
- destination transaction for copiescopyState
- tracks which objects have already been copiedrecursionLimit
- the maximum number of reference fields to hop through, or -1 for infinitycascades
- zero or more reference cascades that identify additional objects to copy- Returns:
- the copied version of this instance in
dest
- Throws:
DeletedObjectException
- if any object to be copied does not actually existDeletedObjectException
- if any copied object ends up with a reference to an object that does not exist indest
through a reference field configured to disallow deleted assignmentSchemaMismatchException
- if the schema corresponding to any copied object is not identical in both the transaction associated with this instance anddest
IllegalArgumentException
- if any parameter is nullIllegalArgumentException
- if any element incascades
is null- See Also:
-
resetCachedFieldValues
void resetCachedFieldValues()Reset cached simple field values.PermazenObject
s instances may cache simple field values after they have been read from the underlying key/value store for efficiency. This method causes any such cached values to be forgotten, so they will be re-read from the underlying key/value store on the next read of the field.Normally this method does not need to be used. It may be needed to maintain consistency in exotic situations, for example, where the underlying key/value store is being modified directly.
-
getPermazenClass
Get thePermazenClass
of which thisPermazenObject
is an instance.- Returns:
- associated
PermazenClass
- Throws:
TypeNotInSchemaException
- if this instance is anUntypedPermazenObject
-
getModelClass
Class<?> getModelClass()Get the original Java model class of which thisPermazenObject
is an instance.- Returns:
- Java model class
-