Search in sources :

Example 61 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.

the class LinkedList method initialise.

public void initialise(java.util.LinkedList newValue, Object oldValue) {
    if (newValue != null) {
        // Check for the case of serialised PC elements, and assign ObjectProviders to the elements without
        ExecutionContext ec = ownerOP.getExecutionContext();
        if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) {
            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();
    }
}
Also used : CollectionClearOperation(org.datanucleus.flush.CollectionClearOperation) CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 62 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.

the class LinkedList method initialise.

/**
 * Method to initialise the SCO from an existing value.
 * @param c The object to set from
 */
public void initialise(java.util.LinkedList c) {
    if (c != null) {
        // Check for the case of serialised PC elements, and assign ObjectProviders to the elements without
        ExecutionContext ec = ownerOP.getExecutionContext();
        if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) {
            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);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 63 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.

the class PriorityQueue method initialise.

public void initialise(java.util.PriorityQueue 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) || !ownerOP.getLifecycleState().isNew()) {
                if (ownerOP.isFlushedToDatastore()) {
                    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();
    }
}
Also used : CollectionClearOperation(org.datanucleus.flush.CollectionClearOperation) CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 64 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-core by datanucleus.

the class PriorityQueue method initialise.

/**
 * Method to initialise the SCO from an existing value.
 * @param c The object to set from
 */
public void initialise(java.util.PriorityQueue 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);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 65 with ObjectProvider

use of org.datanucleus.state.ObjectProvider in project datanucleus-api-jdo by datanucleus.

the class NucleusJDOHelper method getDirtyFields.

/**
 * Accessor for the names of the dirty fields of the persistable object.
 * @param obj The persistable object
 * @param pm The Persistence Manager (only required if the object is detached)
 * @return Names of the dirty fields
 */
public static String[] getDirtyFields(Object obj, PersistenceManager pm) {
    if (obj == null || !(obj instanceof Persistable)) {
        return null;
    }
    Persistable pc = (Persistable) obj;
    if (isDetached(pc)) {
        ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
        // Temporarily attach a StateManager to access the detached field information
        ObjectProvider op = ec.getNucleusContext().getObjectProviderFactory().newForDetached(ec, pc, getObjectId(pc), null);
        pc.dnReplaceStateManager(op);
        op.retrieveDetachState(op);
        String[] dirtyFieldNames = op.getDirtyFieldNames();
        pc.dnReplaceStateManager(null);
        return dirtyFieldNames;
    }
    ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
    ObjectProvider op = ec.findObjectProvider(pc);
    if (op == null) {
        return null;
    }
    return op.getDirtyFieldNames();
}
Also used : Persistable(org.datanucleus.enhancement.Persistable) ExecutionContext(org.datanucleus.ExecutionContext) ObjectProvider(org.datanucleus.state.ObjectProvider)

Aggregations

ObjectProvider (org.datanucleus.state.ObjectProvider)160 ExecutionContext (org.datanucleus.ExecutionContext)85 Iterator (java.util.Iterator)43 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)34 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)25 Map (java.util.Map)22 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)22 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)21 SQLException (java.sql.SQLException)17 Collection (java.util.Collection)16 ApiAdapter (org.datanucleus.api.ApiAdapter)16 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)16 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)16 ArrayList (java.util.ArrayList)14 HashSet (java.util.HashSet)14 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)14 NucleusException (org.datanucleus.exceptions.NucleusException)13 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)13 ListIterator (java.util.ListIterator)12 SQLController (org.datanucleus.store.rdbms.SQLController)12