Search in sources :

Example 6 with SingleValueFieldManager

use of org.datanucleus.store.fieldmanager.SingleValueFieldManager 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();
        }
    }
}
Also used : SCOContainer(org.datanucleus.store.types.SCOContainer) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 7 with SingleValueFieldManager

use of org.datanucleus.store.fieldmanager.SingleValueFieldManager in project datanucleus-core by datanucleus.

the class StateManagerImpl method initialiseForPersistentNew.

/**
 * Initialises a state manager to manage a transient instance that is becoming newly persistent.
 * A new object ID for the instance is obtained from the store manager and the object is inserted in the data store.
 * <p>
 * This constructor is used for assigning state managers to existing instances that are transitioning to a persistent state.
 * @param pc the instance being make persistent.
 * @param preInsertChanges Any changes to make before inserting
 */
public void initialiseForPersistentNew(Persistable pc, FieldValues preInsertChanges) {
    myPC = pc;
    myLC = myEC.getNucleusContext().getApiAdapter().getLifeCycleState(LifeCycleState.P_NEW);
    persistenceFlags = Persistable.READ_OK;
    for (int i = 0; i < loadedFields.length; ++i) {
        loadedFields[i] = true;
    }
    // Assign this StateManager to the PC
    replaceStateManager(myPC, this);
    myPC.dnReplaceFlags();
    saveFields();
    // Populate all fields that have "value-strategy" and are not datastore populated
    populateValueGenerationFields();
    if (preInsertChanges != null) {
        // Apply any pre-insert field updates
        preInsertChanges.fetchFields(this);
    }
    if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        // Make sure any PK fields that are Persistable are persisted first, so we have an id to assign our identity
        int[] pkFieldNumbers = cmd.getPKMemberPositions();
        for (int i = 0; i < pkFieldNumbers.length; i++) {
            int fieldNumber = pkFieldNumbers[i];
            AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
            if (myEC.getMetaDataManager().getMetaDataForClass(fmd.getType(), getExecutionContext().getClassLoaderResolver()) != null) {
                try {
                    if (myEC.getMultithreaded()) {
                        myEC.getLock().lock();
                        lock.lock();
                    }
                    FieldManager prevFM = currFM;
                    try {
                        currFM = new SingleValueFieldManager();
                        myPC.dnProvideField(fieldNumber);
                        Persistable pkFieldPC = (Persistable) ((SingleValueFieldManager) currFM).fetchObjectField(fieldNumber);
                        if (pkFieldPC == null) {
                            throw new NucleusUserException(Localiser.msg("026016", fmd.getFullFieldName()));
                        }
                        if (!myEC.getApiAdapter().isPersistent(pkFieldPC)) {
                            // Make sure the PC field is persistent - can cause the insert of our object being managed by this SM via flush() when bidir relation
                            Object persistedFieldPC = myEC.persistObjectInternal(pkFieldPC, null, null, -1, ObjectProvider.PC);
                            replaceField(myPC, fieldNumber, persistedFieldPC, false);
                        }
                    } finally {
                        currFM = prevFM;
                    }
                } finally {
                    if (myEC.getMultithreaded()) {
                        lock.unlock();
                        myEC.getLock().unlock();
                    }
                }
            }
        }
    }
    // Set the identity of this object
    setIdentity(false);
    if (myEC.getTransaction().isActive()) {
        myEC.enlistInTransaction(this);
    }
    // Now in PERSISTENT_NEW so call any callbacks/listeners
    getCallbackHandler().postCreate(myPC);
    if (myEC.getManageRelations()) {
        // Managed Relations : register non-null bidir fields for later processing
        ClassLoaderResolver clr = myEC.getClassLoaderResolver();
        int[] relationPositions = cmd.getRelationMemberPositions(clr);
        if (relationPositions != null) {
            for (int i = 0; i < relationPositions.length; i++) {
                AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(relationPositions[i]);
                if (RelationType.isBidirectional(mmd.getRelationType(clr))) {
                    Object value = provideField(relationPositions[i]);
                    if (value != null) {
                        // Store the field with value of null so it gets checked
                        myEC.getRelationshipManager(this).relationChange(relationPositions[i], null, value);
                    }
                }
            }
        }
    }
}
Also used : LoadFieldManager(org.datanucleus.store.fieldmanager.LoadFieldManager) MakeTransientFieldManager(org.datanucleus.store.fieldmanager.MakeTransientFieldManager) AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager) UnsetOwnerFieldManager(org.datanucleus.store.fieldmanager.UnsetOwnerFieldManager) DetachFieldManager(org.datanucleus.store.fieldmanager.DetachFieldManager) PersistFieldManager(org.datanucleus.store.fieldmanager.PersistFieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) DeleteFieldManager(org.datanucleus.store.fieldmanager.DeleteFieldManager) L2CachePopulateFieldManager(org.datanucleus.cache.L2CachePopulateFieldManager) FieldManager(org.datanucleus.store.fieldmanager.FieldManager) SingleTypeFieldManager(org.datanucleus.store.fieldmanager.SingleTypeFieldManager) L2CacheRetrieveFieldManager(org.datanucleus.cache.L2CacheRetrieveFieldManager) Persistable(org.datanucleus.enhancement.Persistable) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

SingleValueFieldManager (org.datanucleus.store.fieldmanager.SingleValueFieldManager)7 FieldManager (org.datanucleus.store.fieldmanager.FieldManager)5 L2CachePopulateFieldManager (org.datanucleus.cache.L2CachePopulateFieldManager)3 L2CacheRetrieveFieldManager (org.datanucleus.cache.L2CacheRetrieveFieldManager)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 AttachFieldManager (org.datanucleus.store.fieldmanager.AttachFieldManager)3 DeleteFieldManager (org.datanucleus.store.fieldmanager.DeleteFieldManager)3 DetachFieldManager (org.datanucleus.store.fieldmanager.DetachFieldManager)3 LoadFieldManager (org.datanucleus.store.fieldmanager.LoadFieldManager)3 MakeTransientFieldManager (org.datanucleus.store.fieldmanager.MakeTransientFieldManager)3 PersistFieldManager (org.datanucleus.store.fieldmanager.PersistFieldManager)3 SingleTypeFieldManager (org.datanucleus.store.fieldmanager.SingleTypeFieldManager)3 UnsetOwnerFieldManager (org.datanucleus.store.fieldmanager.UnsetOwnerFieldManager)3 ObjectProvider (org.datanucleus.state.ObjectProvider)2 SCOContainer (org.datanucleus.store.types.SCOContainer)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ExecutionContext (org.datanucleus.ExecutionContext)1 Persistable (org.datanucleus.enhancement.Persistable)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 FieldMetaData (org.datanucleus.metadata.FieldMetaData)1