use of org.datanucleus.store.types.SCOContainer in project datanucleus-core by datanucleus.
the class StateManagerImpl method loadUnloadedFields.
/**
* Fetch from the database all fields that are not currently loaded regardless of whether
* they are in the current fetch group or not. Called by lifecycle transitions.
*/
public void loadUnloadedFields() {
int[] fieldNumbers = ClassUtils.getFlagsSetTo(loadedFields, cmd.getAllMemberPositions(), false);
if (fieldNumbers == null || fieldNumbers.length == 0) {
// All loaded so return
return;
}
if (preDeleteLoadedFields != null && ((myLC.isDeleted() && myEC.isFlushing()) || activity == ActivityState.DELETING)) {
// During deletion process so we know what is really loaded so only load if necessary
fieldNumbers = ClassUtils.getFlagsSetTo(preDeleteLoadedFields, fieldNumbers, false);
}
if (fieldNumbers != null && fieldNumbers.length > 0) {
boolean callPostLoad = myFP.isToCallPostLoadFetchPlan(this.loadedFields);
int[] unloadedFieldNumbers = loadFieldsFromLevel2Cache(fieldNumbers);
if (unloadedFieldNumbers != null) {
loadFieldsFromDatastore(unloadedFieldNumbers);
}
// Make sure all SCO lazy-loaded fields have contents loaded
int[] secondClassMutableFieldNumbers = cmd.getSCOMutableMemberPositions();
for (int i = 0; i < secondClassMutableFieldNumbers.length; i++) {
SingleValueFieldManager sfv = new SingleValueFieldManager();
provideFields(new int[] { secondClassMutableFieldNumbers[i] }, sfv);
Object value = sfv.fetchObjectField(i);
if (value instanceof SCOContainer) {
((SCOContainer) value).load();
}
}
updateLevel2CacheForFields(fieldNumbers);
if (callPostLoad) {
postLoad();
}
}
}
use of org.datanucleus.store.types.SCOContainer in project datanucleus-core by datanucleus.
the class StateManagerImpl method loadUnloadedRelationFields.
public void loadUnloadedRelationFields() {
int[] fieldsConsidered = cmd.getRelationMemberPositions(myEC.getClassLoaderResolver());
int[] fieldNumbers = ClassUtils.getFlagsSetTo(loadedFields, fieldsConsidered, false);
if (fieldNumbers == null || fieldNumbers.length == 0) {
// All loaded so return
return;
}
if (preDeleteLoadedFields != null && ((myLC.isDeleted() && myEC.isFlushing()) || activity == ActivityState.DELETING)) {
// During deletion process so we know what is really loaded so only load if necessary
fieldNumbers = ClassUtils.getFlagsSetTo(preDeleteLoadedFields, fieldNumbers, false);
}
if (fieldNumbers != null && fieldNumbers.length > 0) {
boolean callPostLoad = myFP.isToCallPostLoadFetchPlan(this.loadedFields);
int[] unloadedFieldNumbers = loadFieldsFromLevel2Cache(fieldNumbers);
if (unloadedFieldNumbers != null) {
loadFieldsFromDatastore(unloadedFieldNumbers);
}
int[] secondClassMutableFieldNumbers = cmd.getSCOMutableMemberPositions();
for (int i = 0; i < secondClassMutableFieldNumbers.length; i++) {
// Make sure all SCO lazy-loaded relation fields have contents loaded
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(secondClassMutableFieldNumbers[i]);
if (mmd.getRelationType(myEC.getClassLoaderResolver()) != RelationType.NONE) {
SingleValueFieldManager sfv = new SingleValueFieldManager();
provideFields(new int[] { secondClassMutableFieldNumbers[i] }, sfv);
Object value = sfv.fetchObjectField(i);
if (value instanceof SCOContainer) {
((SCOContainer) value).load();
}
}
}
updateLevel2CacheForFields(fieldNumbers);
if (callPostLoad) {
postLoad();
}
}
}
use of org.datanucleus.store.types.SCOContainer in project datanucleus-core by datanucleus.
the class L2CachePopulateFieldManager method processContainer.
private void processContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
Object unwrappedContainer = container;
if (container instanceof SCOContainer) {
if (!((SCOContainer) container).isLoaded()) {
// Contents not loaded so just mark as unloaded
cachedPC.setLoadedField(fieldNumber, false);
return;
}
unwrappedContainer = ((SCO) container).getValue();
}
TypeManager typeManager = op.getExecutionContext().getTypeManager();
if (mmd.hasMap()) {
MapHandler mapHandler = typeManager.getContainerHandler(mmd.getType());
processMapContainer(fieldNumber, unwrappedContainer, mmd, mapHandler);
} else {
ElementContainerHandler elementContainerHandler = typeManager.getContainerHandler(mmd.getType());
processElementContainer(fieldNumber, unwrappedContainer, mmd, elementContainerHandler);
}
}
use of org.datanucleus.store.types.SCOContainer in project datanucleus-core by datanucleus.
the class AttachFieldManager method storeObjectField.
/**
* Method to store an object field into the attached instance.
* @param fieldNumber Number of the field to store
* @param value the value in the detached instance
*/
public void storeObjectField(int fieldNumber, Object value) {
// Note : when doing updates always do replaceField first and makeDirty after since the replaceField
// can cause flush() to be called meaning that an update with null would be made before the new value
// makes it into the field
AbstractClassMetaData cmd = attachedOP.getClassMetaData();
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
ExecutionContext ec = attachedOP.getExecutionContext();
RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
// boolean processWhenExisting = true;
if (mmd.hasExtension("attach")) {
if (mmd.getValueForExtension("attach").equalsIgnoreCase("never")) {
// Member is tagged to not attach, so put a null
attachedOP.replaceFieldMakeDirty(fieldNumber, null);
return;
}
// TODO Support attach only when not present in the datastore (only for PCs)
// else if (mmd.getValueForExtension("attach").equalsIgnoreCase("when-non-existing"))
// {
// processWhenExisting = false;
// }
}
// Use ContainerHandlers to support non-JDK Collections and single element collections
ApiAdapter api = ec.getApiAdapter();
if (value == null) {
Object oldValue = null;
if (mmd.isDependent() && persistent) {
// Get any old value of this field so we can do cascade-delete if being nulled
try {
attachedOP.loadFieldFromDatastore(fieldNumber);
} catch (Exception e) {
// Error loading the field so didn't exist before attaching anyway
}
oldValue = attachedOP.provideField(fieldNumber);
}
attachedOP.replaceField(fieldNumber, null);
if (dirtyFields[fieldNumber] || !persistent) {
attachedOP.makeDirty(fieldNumber);
}
if (mmd.isDependent() && !mmd.isEmbedded() && oldValue != null && api.isPersistable(oldValue)) {
// Check for a field storing a PC where it is being nulled and the other object is dependent
// Flush the nulling of the field
attachedOP.flush();
NucleusLogger.PERSISTENCE.debug(Localiser.msg("026026", oldValue, mmd.getFullFieldName()));
ec.deleteObjectInternal(oldValue);
}
} else if (secondClassMutableFields[fieldNumber]) {
if (mmd.isSerialized() && !RelationType.isRelationMultiValued(relationType)) {
// SCO Field is serialised, and no persistable elements so just update the column with this new value
attachedOP.replaceFieldMakeDirty(fieldNumber, value);
attachedOP.makeDirty(fieldNumber);
} else {
// Make sure that the value is a SCO wrapper
Object oldValue = null;
if (persistent && !attachedOP.isFieldLoaded(fieldNumber)) {
attachedOP.loadField(fieldNumber);
}
oldValue = attachedOP.provideField(fieldNumber);
boolean changed = dirtyFields[fieldNumber];
if (!changed) {
// Check if the new value is different (not detected if detached field was not a wrapper and mutable)
if (oldValue == null) {
changed = true;
} else {
if (mmd.hasCollection() && relationType != RelationType.NONE) {
boolean collsEqual = SCOUtils.collectionsAreEqual(api, (Collection) oldValue, (Collection) value);
changed = !collsEqual;
} else // TODO Do like the above for relational Maps
{
changed = !(oldValue.equals(value));
}
}
}
SCO sco;
if (oldValue == null || !(oldValue instanceof SCO)) {
// Detached object didn't use wrapped field
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("026029", StringUtils.toJVMIDString(attachedOP.getObject()), attachedOP.getInternalObjectId(), mmd.getName()));
}
sco = ec.getTypeManager().createSCOInstance(attachedOP, mmd, value != null ? value.getClass() : null, null, false);
if (sco instanceof SCOContainer) {
// Load any containers to avoid update issues
((SCOContainer) sco).load();
}
attachedOP.replaceFieldMakeDirty(fieldNumber, sco);
} else {
// The field is already a SCO wrapper, so just copy the new values to it
sco = (SCO) oldValue;
}
if (cascadeAttach) {
// Only trigger the cascade when required
if (copy) {
// Attach copy of the SCO
sco.attachCopy(value);
} else {
// Should be changed to do things like in attachCopy above.
if (sco instanceof Collection) {
// Attach all PC elements of the collection
SCOUtils.attachForCollection(attachedOP, ((Collection) value).toArray(), SCOUtils.collectionHasElementsWithoutIdentity(mmd));
} else if (sco instanceof Map) {
// Attach all PC keys/values of the map
SCOUtils.attachForMap(attachedOP, ((Map) value).entrySet(), SCOUtils.mapHasKeysWithoutIdentity(mmd), SCOUtils.mapHasValuesWithoutIdentity(mmd));
} else {
// Initialise the SCO with the new value
sco.initialise(value);
}
}
}
if (changed || !persistent) {
attachedOP.makeDirty(fieldNumber);
}
}
} else if (mmd.getType().isArray() && RelationType.isRelationMultiValued(relationType)) {
// Array of persistable objects
if (mmd.isSerialized() || mmd.isEmbedded()) {
// Field is serialised/embedded so just update the column with this new value TODO Make sure they have ObjectProviders
attachedOP.replaceField(fieldNumber, value);
if (dirtyFields[fieldNumber] || !persistent) {
attachedOP.makeDirty(fieldNumber);
}
} else {
Object oldValue = attachedOP.provideField(fieldNumber);
if (oldValue == null && !attachedOP.getLoadedFields()[fieldNumber] && persistent) {
// Retrieve old value for field
attachedOP.loadField(fieldNumber);
oldValue = attachedOP.provideField(fieldNumber);
}
if (cascadeAttach) {
// Only trigger the cascade when required
Object arr = Array.newInstance(mmd.getType().getComponentType(), Array.getLength(value));
for (int i = 0; i < Array.getLength(value); i++) {
Object elem = Array.get(value, i);
// TODO Compare with old value and handle delete dependent etc
if (copy) {
Object elemAttached = ec.attachObjectCopy(attachedOP, elem, false);
Array.set(arr, i, elemAttached);
} else {
ec.attachObject(attachedOP, elem, false);
Array.set(arr, i, elem);
}
}
attachedOP.replaceFieldMakeDirty(fieldNumber, arr);
}
if (dirtyFields[fieldNumber] || !persistent) {
attachedOP.makeDirty(fieldNumber);
}
}
} else if (RelationType.isRelationSingleValued(relationType)) {
// 1-1/N-1
ObjectProvider valueSM = ec.findObjectProvider(value);
if (valueSM != null && valueSM.getReferencedPC() != null && !api.isPersistent(value)) {
// Value has ObjectProvider and has referenced object so is being attached, so refer to attached PC
if (dirtyFields[fieldNumber]) {
attachedOP.replaceFieldMakeDirty(fieldNumber, valueSM.getReferencedPC());
} else {
attachedOP.replaceField(fieldNumber, valueSM.getReferencedPC());
}
}
if (cascadeAttach) {
// Determine if field is persisted into the owning object (embedded/serialised)
boolean sco = mmd.getEmbeddedMetaData() != null || mmd.isSerialized() || mmd.isEmbedded();
if (copy) {
// Attach copy of the PC
value = ec.attachObjectCopy(attachedOP, value, sco);
if (sco || dirtyFields[fieldNumber]) {
// Either embedded/serialised or marked as changed, so make it dirty
attachedOP.replaceFieldMakeDirty(fieldNumber, value);
} else {
attachedOP.replaceField(fieldNumber, value);
}
} else {
// Attach PC in-situ
ec.attachObject(attachedOP, value, sco);
}
// Make sure the field is marked as dirty
if (dirtyFields[fieldNumber] || !persistent) {
attachedOP.makeDirty(fieldNumber);
} else if (sco && value != null && api.isDirty(value)) {
attachedOP.makeDirty(fieldNumber);
}
} else if (dirtyFields[fieldNumber] || !persistent) {
attachedOP.makeDirty(fieldNumber);
}
} else {
attachedOP.replaceField(fieldNumber, value);
if (dirtyFields[fieldNumber] || !persistent) {
attachedOP.makeDirty(fieldNumber);
}
}
}
Aggregations