VMware GemFire Java API Reference
Interface PdxInstance
-
- All Superinterfaces:
Document,java.io.Serializable
- All Known Subinterfaces:
WritablePdxInstance
public interface PdxInstance extends Document, java.io.Serializable
PdxInstance provides run time access to the fields of a PDX without deserializing the PDX. Preventing deserialization saves time and memory and does not require the domain class. This interface is implemented by GemFire. The PdxInstance implementation is a light weight wrapper that simply refers to the raw bytes of the PDX that are kept in the cache. Applications can choose to access PdxInstances instead of Java objects by configuring the Cache to prefer PDX instances during deserialization. This can be done incache.xmlby setting the attributeread-serializedto true on thepdxelement. Or it can be done programmatically using either thesetPdxReadSerializedorclient setPdxReadSerializedmethod. Once this preference is configured, then any time deserialization of a PDX is done it will deserialize into a PdxInstance.PdxInstances are immutable. If you want to change one call
createWriter().A PdxInstance's fields will always be those of the version it represents. So if you add a field to your domain class you can end up with a PdxInstance for version 1 (that does not have the field) and a PdxInstance for version 2. The PdxInstance for version 1 will not have the added field and the PdxInstance for version 2 will have the field. This differs from deserialization of a pdx back to a domain class. In that case if version 2 is deserializing version 1 PdxReader will return a default value for the added field even though version 1 has no knowledge of it.
- Since:
- GemFire 6.6
-
-
Field Summary
-
Fields inherited from interface org.apache.geode.cache.Document
FIELD_NOT_FOUND
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description WritablePdxInstancecreateWriter()Creates and returns aWritablePdxInstancewhose initial values are those of this PdxInstance.booleanequals(java.lang.Object other)Returns true if the given object is equals to this instance.java.lang.StringgetClassName()Return the full name of the class that this pdx instance represents.java.lang.ObjectgetField(java.lang.String fieldName)Reads the named field and returns its value.java.lang.ObjectgetObject()Deserializes and returns the domain object that this instance represents.inthashCode()Generates a hashCode based on the identity fields of this PdxInstance.default booleanisDeserializable()Returns false if this instance will never be deserialized to a domain class.booleanisEnum()Returns true if this instance represents an enum.booleanisIdentityField(java.lang.String fieldName)Checks if the named field wasmarkedas an identity field.java.lang.StringtoString()Prints out all of the identity fields of this PdxInstance.-
Methods inherited from interface org.apache.geode.cache.Document
foreach, getAndCheckField, getFieldNames, hasField
-
-
-
-
Method Detail
-
getClassName
java.lang.String getClassName()
Return the full name of the class that this pdx instance represents. An empty string will be returned if no class is associated with this instance.- Returns:
- the name of the class that this pdx instance represents.
- Since:
- GemFire 6.6.2
-
isEnum
boolean isEnum()
Returns true if this instance represents an enum. Enum's have a String field named "name" and an int field named "ordinal". It is ok to cast a PdxInstance that represents an enum toComparable. PdxInstances representing enums are not writable.- Returns:
- true if this instance represents an enum.
-
getObject
java.lang.Object getObject()
Deserializes and returns the domain object that this instance represents. If this instance is one that never deserializes then getObject returns "this".- Returns:
- the deserialized domain object or "this" if this instance never deserializes
- Throws:
PdxSerializationException- if the instance could not be deserialized
-
isIdentityField
boolean isIdentityField(java.lang.String fieldName)
Checks if the named field wasmarkedas an identity field.Note that if no fields have been marked then all the fields are used as identity fields even though this method will return
falsesince none of them have been marked.- Parameters:
fieldName- the name of the field to check- Returns:
trueif the named field exists and was marked as an identify field; otherwisefalse
-
getField
java.lang.Object getField(java.lang.String fieldName)
Reads the named field and returns its value. If the field does not existnullis returned.A
nullresult indicates that the field does not exist or that it exists and its value is currentlynull. ThehasFieldmethod can be used to figure out which if these two cases is true.If an Object[] is deserialized by this call then that array's component type will be
Object.classinstead of the original class that the array had when it was serialized. This is done so that PdxInstance objects can be added to the array.- Specified by:
getFieldin interfaceDocument- Parameters:
fieldName- name of the field to read- Returns:
- If this instance has the named field then the field's value is returned, otherwise
nullis returned. - Throws:
PdxSerializationException- if the field could not be deserialized
-
equals
boolean equals(java.lang.Object other)
Returns true if the given object is equals to this instance.If
otheris not a PdxInstance then it is not equal to this instance. NOTE: Even ifotheris the result of callinggetObject()it will not be equal to this instance.Otherwise equality of two PdxInstances is determined as follows:
- The domain class name must be equal for both PdxInstances
- Each identity field must be equal.
markIdentityFieldthen only the marked identity fields are its identity fields. Otherwise all its fields are identity fields.An identity field is equal if all the following are true:
- The field name is equal.
- The field type is equal.
- The field value is equal.
If a field's type is
OBJECTthen its value must be deserialized to determine if it is equals. If the deserialized object is an array thendeepEqualsis used to determine equality. Otherwiseequalsis used.If a field's type is
OBJECT[]then its value must be deserialized anddeepEqualsis used to determine equality.For all other field types then the value does not need to be deserialized. Instead the serialized raw bytes are compared and used to determine equality.
Note that any fields that have objects that do not override
equalswill cause equals to return false when you might have expected it to return true. The only exceptions to this are those that calldeepEqualsas noted above. You should either override equals and hashCode in these cases or mark other fields as your identity fields.
-
hashCode
int hashCode()
Generates a hashCode based on the identity fields of this PdxInstance.If a PdxInstance has marked identity fields using
markIdentityFieldthen only the marked identity fields are its identity fields. Otherwise all its fields are identity fields.If an identity field is of type
OBJECTthen it is deserialized. If the deserialized object is an array thendeepHashCodeis used. OtherwisehashCodeis used.If an identity field is of type
OBJECT[]this it is deserialized anddeepHashCodeis used.Otherwise the field is not deserialized and the raw bytes of its value are used to compute the hash code.
The algorithm used to compute the hashCode is: hashCode = 1; foreach (field: sortedIdentityFields()) { if (field.isDefaultValue()) continue; if (field.isArray()) { hashCode = hashCode*31 + Arrays.deepHashCode(field); } else { hashCode = hashCode*31 + field.hashCode(); } } if (hashCode == 0) { hashCode = 1; }
-
toString
java.lang.String toString()
Prints out all of the identity fields of this PdxInstance.If a PdxInstance has marked identity fields using
markIdentityFieldthen only the marked identity fields are its identity fields. Otherwise all its fields are identity fields.- Overrides:
toStringin classjava.lang.Object
-
createWriter
WritablePdxInstance createWriter()
Creates and returns aWritablePdxInstancewhose initial values are those of this PdxInstance. This call returns a copy of the current field values so modifications made to the returned value will not modify this PdxInstance.- Returns:
- a
WritablePdxInstance - Throws:
java.lang.IllegalStateException- if the PdxInstance is an enum.
-
isDeserializable
default boolean isDeserializable()
Returns false if this instance will never be deserialized to a domain class. Instances that never deserialize can be created usingPdxInstanceFactory.neverDeserialize()or by creating a factory with an empty string as the class name.- Returns:
- false if this instance will never be deserialized to a domain class.
- Since:
- Geode 1.9
-
-