use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class PersistFieldManager method processElementContainer.
private void processElementContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
TypeManager typeManager = op.getExecutionContext().getTypeManager();
ElementContainerHandler<Object, ElementContainerAdapter<Object>> elementContainerHandler = typeManager.getContainerHandler(mmd.getType());
// Process all elements of the container that are PC
ElementContainerAdapter containerAdapter = elementContainerHandler.getAdapter(container);
ApiAdapter api = op.getExecutionContext().getApiAdapter();
int objectType = elementContainerHandler.getObjectType(mmd);
if (objectType == ObjectProvider.PC) {
int elementPosition = 0;
for (Object element : containerAdapter) {
if (api.isPersistable(element)) {
Object newElement = processPersistable(element, -1, objectType);
ObjectProvider elementSM = op.getExecutionContext().findObjectProvider(newElement);
if (elementSM.getReferencedPC() != null) {
// Must be attaching this element, so swap element (detached -> attached)
if (containerAdapter instanceof SequenceAdapter) {
((SequenceAdapter) containerAdapter).update(newElement, elementPosition);
} else {
containerAdapter.remove(elementSM);
containerAdapter.add(newElement);
}
}
}
elementPosition++;
}
} else {
// Embedded/Serialized
for (Object element : containerAdapter) {
if (api.isPersistable(element)) {
processPersistable(element, fieldNumber, objectType);
}
}
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class ReachabilityFieldManager method processPersistable.
/**
* Utility method to process the passed persistable object.
* @param obj The persistable object
* @param mmd MetaData for the member storing this object
*/
protected void processPersistable(Object obj, AbstractMemberMetaData mmd) {
ExecutionContext ec = op.getExecutionContext();
ObjectProvider objOP = ec.findObjectProvider(obj);
if (objOP != null) {
Object objID = objOP.getInternalObjectId();
if (!reachables.contains(objID) && !objOP.isDeleted()) {
if (ec.isEnlistedInTransaction(objID)) {
// This object was enlisted so make sure all of its fields are loaded before continuing
objOP.loadUnloadedRelationFields();
}
// Add this object id since not yet reached
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("007000", StringUtils.toJVMIDString(obj), objID, objOP.getLifecycleState()));
}
reachables.add(objID);
// Recurse through relation fields of this object
ReachabilityFieldManager pcFM = new ReachabilityFieldManager(objOP, reachables);
int[] relationFieldNums = objOP.getClassMetaData().getRelationMemberPositions(ec.getClassLoaderResolver());
int[] loadedFieldNumbers = ClassUtils.getFlagsSetTo(objOP.getLoadedFields(), relationFieldNums, true);
if (loadedFieldNumbers != null && loadedFieldNumbers.length > 0) {
objOP.provideFields(loadedFieldNumbers, pcFM);
}
}
} else {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("007005", op.getExecutionContext().getApiAdapter().getIdForObject(obj), mmd.getFullFieldName()));
}
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class Properties method initialise.
/**
* Method to initialise the SCO from an existing value.
* @param m Object to set value using.
*/
public void initialise(java.util.Properties m) {
if (m != null) {
// Check for the case of serialised maps, and assign ObjectProviders to any PC keys/values without
if (SCOUtils.mapHasSerialisedKeysAndValues(ownerMmd) && (ownerMmd.getMap().keyIsPersistent() || ownerMmd.getMap().valueIsPersistent())) {
ExecutionContext ec = ownerOP.getExecutionContext();
Iterator iter = m.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
if (ownerMmd.getMap().keyIsPersistent()) {
ObjectProvider objSM = ec.findObjectProvider(key);
if (objSM == null) {
objSM = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, key, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
}
}
if (ownerMmd.getMap().valueIsPersistent()) {
ObjectProvider objSM = ec.findObjectProvider(value);
if (objSM == null) {
objSM = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, value, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
}
}
}
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size()));
}
delegate.putAll(m);
isCacheLoaded = true;
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class Queue method initialise.
public void initialise(java.util.Queue newValue, Object oldValue) {
if (newValue != null) {
// Check for the case of serialised PC elements, and assign ObjectProviders to the elements without
if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) {
ExecutionContext ec = ownerOP.getExecutionContext();
Iterator iter = newValue.iterator();
while (iter.hasNext()) {
Object pc = iter.next();
ObjectProvider objSM = ec.findObjectProvider(pc);
if (objSM == null) {
objSM = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
}
}
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + newValue.size()));
}
// TODO This does clear+addAll : Improve this and work out which elements are added and which deleted
if (backingStore != null) {
if (SCOUtils.useQueuedUpdate(ownerOP)) {
if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
ownerOP.getExecutionContext().addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore));
for (Object element : newValue) {
ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
}
}
} else {
backingStore.clear(ownerOP);
try {
backingStore.addAll(ownerOP, newValue, useCache ? 0 : -1);
} catch (NucleusDataStoreException dse) {
NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse));
}
}
}
delegate.addAll(newValue);
isCacheLoaded = true;
makeDirty();
}
}
use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.
the class Set method initialise.
/**
* Method to initialise the SCO from an existing value.
* @param c The object to set from
*/
public void initialise(java.util.Set c) {
if (c != null) {
// Check for the case of serialised PC elements, and assign ObjectProviders to the elements without
if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) {
ExecutionContext ec = ownerOP.getExecutionContext();
Iterator iter = c.iterator();
while (iter.hasNext()) {
Object pc = iter.next();
ObjectProvider objSM = ec.findObjectProvider(pc);
if (objSM == null) {
objSM = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
}
}
}
if (backingStore != null && useCache && !isCacheLoaded) {
// Mark as loaded
isCacheLoaded = true;
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size()));
}
delegate.clear();
delegate.addAll(c);
}
}
Aggregations