Package org.datanucleus.state

Examples of org.datanucleus.state.StateManager


        assertIsOpen();

        Class pcClass = pcType.getType();

        // Create StateManager to generate an identity NOTE THIS IS VERY INEFFICIENT
        StateManager sm = (StateManager) ObjectProviderFactory.newForHollowPopulatedAppId(this, pcClass, fv);
        if (!ignoreCache)
        {
            // Check the cache
            Object oid = sm.getInternalObjectId();
            Object pc = getObjectFromCache(oid);
            if (pc != null)
            {
                sm = findStateManager(pc);
                // Note that this can cause problems like NUCRDBMS-402 due to attempt to re-read the field values
                sm.loadFieldValues(fv); // Load the values retrieved by the query
                return pc;
            }
            if (checkInheritance)
            {
                ApiAdapter api = getApiAdapter();
                if (oid instanceof OID || api.isSingleFieldIdentity(oid))
                {
                    // Check if this id for any known subclasses is in the cache to save searching
                    String[] subclasses = getMetaDataManager().getSubclassesForClass(pcClass.getName(), true);
                    if (subclasses != null)
                    {
                        for (int i=0;i<subclasses.length;i++)
                        {
                            if (api.isDatastoreIdentity(oid))
                            {
                                oid = OIDFactory.getInstance(getNucleusContext(), subclasses[i], ((OID)oid).getKeyValue());
                            }
                            else if (api.isSingleFieldIdentity(oid))
                            {
                                oid = api.getNewSingleFieldIdentity(oid.getClass(), clr.classForName(subclasses[i]),
                                    api.getTargetKeyForSingleFieldIdentity(oid));
                            }
                            pc = getObjectFromCache(oid);
                            if (pc != null)
                            {
                                sm = findStateManager(pc);
                                sm.loadFieldValues(fv); // Load the values retrieved by the query
                                putObjectIntoLevel2Cache(sm, false);
                                return pc;
                            }
                        }
                    }
                }
            }
        }

        if (checkInheritance)
        {
            sm.checkInheritance(fv); // Find the correct PC class for this object, hence updating the object id
            if (!ignoreCache)
            {
                // Check the cache in case this updated object id is present (since we should use that if available)
                Object oid = sm.getInternalObjectId();
                Object pc = getObjectFromCache(oid);
                if (pc != null)
                {
                    // We have an object with this new object id already so return it with the retrieved field values imposed
                    sm = findStateManager(pc);
                    sm.loadFieldValues(fv); // Load the values retrieved by the query
                    putObjectIntoLevel2Cache(sm, false);
                    return pc;
                }
            }
        }

        // Cache the object as required
        putObjectIntoCache(sm);
        if (txCachedIds != null && !txCachedIds.contains(sm.getInternalObjectId()))
        {
            putObjectIntoLevel2Cache(sm, false);
        }

        return sm.getObject();
    }
View Full Code Here


                        throw new NucleusUserException(msg, e);
                    }
                }

                createdHollow = true;
                StateManager sm = (StateManager) ObjectProviderFactory.newForHollowPopulated(this, cls, id, fv);
                pc = sm.getObject();
                putObjectIntoCache(sm);
                putObjectIntoLevel2Cache(sm, false);
            }
        }

        if (pc != null && fv != null && !createdHollow)
        {
            // Object found in the cache so load the requested fields
            StateManager sm = findStateManager(pc);
            if (sm != null)
            {
                // Load the requested fields
                fv.fetchNonLoadedFields(sm);
            }
View Full Code Here

                // Try the "findObjects" results in case supported by StoreManager
                pc = foundPcs[foundPcIdx];
                foundPcIdx++;
            }

            StateManager sm = null;
            if (pc == null)
            {
                // Object not found yet, so work out class name
                String className = null;
                String originalClassName = null;
                boolean checkedClassName = false;
                if (id instanceof SCOID)
                {
                    throw new NucleusUserException(LOCALISER.msg("010006"));
                }
                else if (id instanceof DatastoreUniqueOID)
                {
                    // Should have been found using "persistenceHandler.findObject()"
                    throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                }
                else if (api.isDatastoreIdentity(id) || api.isSingleFieldIdentity(id))
                {
                    // OID or SingleFieldIdentity, so check that the implied class is managed
                    originalClassName = getStoreManager().manageClassForIdentity(id, clr);
                }
                else
                {
                    // We dont know the object class so try to deduce it from what is known by the StoreManager
                    originalClassName = getStoreManager().getClassNameForObjectID(id, clr, this);
                    checkedClassName = true;
                }

                if (validate)
                {
                    // Validate the inheritance level
                    className =
                        (checkedClassName ? originalClassName : getStoreManager().getClassNameForObjectID(id, clr, this));
                    if (className == null)
                    {
                        throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                    }

                    if (originalClassName != null && !originalClassName.equals(className))
                    {
                        // Inheritance check implies different inheritance level, so retry
                        if (api.isDatastoreIdentity(id))
                        {
                            // Create new OID using correct target class, and recheck cache
                            id = OIDFactory.getInstance(getNucleusContext(), className, ((OID)id).getKeyValue());
                            pc = getObjectFromCache(id);
                        }
                        else if (api.isSingleFieldIdentity(id))
                        {
                            // Create new SingleFieldIdentity using correct targetClass, and recheck cache
                            id = api.getNewSingleFieldIdentity(id.getClass(), clr.classForName(className),
                                api.getTargetKeyForSingleFieldIdentity(id));
                            pc = getObjectFromCache(id);
                        }
                    }
                }
                else
                {
                    className = originalClassName;
                }

                if (pc == null)
                {
                    // Still not found so create a Hollow instance with the supplied field values
                    try
                    {
                        Class pcClass = clr.classForName(className, (id instanceof OID) ? null : id.getClass().getClassLoader());
                        sm = (StateManager) ObjectProviderFactory.newForHollow(this, pcClass, id);
                        pc = sm.getObject();
                        fromCache = false;
                    }
                    catch (ClassNotResolvedException e)
                    {
                        NucleusLogger.PERSISTENCE.warn(LOCALISER.msg("010027", getIdentityAsString(id)));
                        throw new NucleusUserException(LOCALISER.msg("010027", getIdentityAsString(id)), e);
                    }
                }
            }

            // TODO Perform locate of uncached objects in single operation using persistenceHandler.locateObjects
            boolean performValidationWhenCached =
                (context.getPersistenceConfiguration().getBooleanProperty("datanucleus.findObject.validateWhenCached"));
            if (validate && (!fromCache || performValidationWhenCached))
            {
                if (fromCache && pc != null && api.isTransactional(pc))
                {
                    // JDO2 [12.6.5] Already an object with the same id and it's transactional, so use it
                    objs[i] = pc;
                    continue;
                }

                // User requests validation of the instance so go to the datastore to validate it
                // loading any fetchplan fields that are needed in the process.
                sm = findStateManager(pc);

                if (sm != null && !fromCache)
                {
                    // Cache the object in case we have bidirectional relations that would need to find this
                    putObjectIntoCache(sm);
                }

                try
                {
                    sm.validate();
                }
                catch (NucleusObjectNotFoundException onfe)
                {
                    // Object doesn't exist, so remove from L1 cache
                    removeObjectFromCache(sm.getInternalObjectId());
                    throw onfe;
                }

                if (sm.getObject() != pc)
                {
                    // Underlying object was changed in the validation process
                    // Can happen when datastore is responsible for managing object refs e.g db4o
                    // and needs to create the objects itself
                    removeObjectFromCache(sm.getInternalObjectId());
                    fromCache = false;
                    pc = sm.getObject();
                }
            }

            objs[i] = pc;
View Full Code Here

        {
            // JDO2 [12.6.5] If there's already an object with the same id and it's transactional, return it
            return pc;
        }

        StateManager sm = null;
        if (pc == null)
        {
            // Find it direct from the store if the store supports that
            pc = getStoreManager().getPersistenceHandler().findObject(this, id);

            if (pc == null)
            {
                // Object not found in cache(s) with this identity
                String className = null;
                String originalClassName = null;
                boolean checkedClassName = false;
                if (id instanceof SCOID)
                {
                    throw new NucleusUserException(LOCALISER.msg("010006"));
                }
                else if (id instanceof DatastoreUniqueOID)
                {
                    throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                }
                else if (api.isDatastoreIdentity(id) || api.isSingleFieldIdentity(id))
                {
                    // DatastoreIdentity or SingleFieldIdentity, so check that the implied class is managed
                    originalClassName = getStoreManager().manageClassForIdentity(id, clr);
                }
                else if (objectClassName != null)
                {
                    // Object class name specified so use that directly
                    originalClassName = objectClassName;
                }
                else
                {
                    // We dont know the object class so try to deduce it from what is known by the StoreManager
                    originalClassName = getStoreManager().getClassNameForObjectID(id, clr, this);
                    checkedClassName = true;
                }

                if (checkInheritance)
                {
                    // Verify if correct class inheritance level is set
                    if (!checkedClassName)
                    {
                        className = getStoreManager().getClassNameForObjectID(id, clr, this);
                    }
                    else
                    {
                        // We just checked the name of the class in the section above so just use that
                        className = originalClassName;
                    }

                    if (className == null)
                    {
                        throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                    }

                    if (originalClassName != null && !originalClassName.equals(className))
                    {
                        // Inheritance checking has found a different inherited
                        // object with this identity so create new id
                        if (api.isDatastoreIdentity(id))
                        {
                            // Create new OID using correct target class and recheck the cache
                            id = OIDFactory.getInstance(getNucleusContext(), className, ((OID)id).getKeyValue());
                            pc = getObjectFromCache(id);
                        }
                        else if (api.isSingleFieldIdentity(id))
                        {
                            // Create new SingleFieldIdentity using correct targetClass and recheck the cache
                            id = api.getNewSingleFieldIdentity(id.getClass(), clr.classForName(className),
                                    api.getTargetKeyForSingleFieldIdentity(id));
                            pc = getObjectFromCache(id);
                        }
                    }
                }
                else
                {
                    className = originalClassName;
                }

                if (pc == null)
                {
                    // Still not found, so create a Hollow instance with supplied PK values if possible
                    try
                    {
                        Class pcClass = clr.classForName(className, (id instanceof OID) ? null : id.getClass().getClassLoader());
                        if (Modifier.isAbstract(pcClass.getModifiers()))
                        {
                            // This class is abstract so impossible to have an instance of this type
                            throw new NucleusObjectNotFoundException(LOCALISER.msg("010027",
                                getIdentityAsString(id), className));
                        }

                        sm = (StateManager) ObjectProviderFactory.newForHollow(this, pcClass, id);
                        pc = sm.getObject();
                        fromCache = false;

                        if (!checkInheritance && !validate)
                        {
                            // Mark the StateManager as needing to validate this object before loading fields
                            sm.markForInheritanceValidation();
                        }
                    }
                    catch (ClassNotResolvedException e)
                    {
                        NucleusLogger.PERSISTENCE.warn(LOCALISER.msg("010027", getIdentityAsString(id)));
                        throw new NucleusUserException(LOCALISER.msg("010027", getIdentityAsString(id)), e);
                    }
                }
            }
        }

        // TODO If we have serializeReadObjects set and in pessimistic txn and get the object from the cache
        // we need to apply a lock here
        boolean performValidationWhenCached = (context.getPersistenceConfiguration().getBooleanProperty("datanucleus.findObject.validateWhenCached"));
        if (validate && (!fromCache || performValidationWhenCached))
        {
            // User requests validation of the instance so go to the datastore to validate it
            // loading any fetchplan fields that are needed in the process.
            if (sm == null)
            {
                sm = findStateManager(pc);
            }

            if (sm != null && !fromCache)
            {
                // Cache the object in case we have bidirectional relations that would need to find this
                putObjectIntoCache(sm);
            }

            try
            {
                sm.validate();
            }
            catch (NucleusObjectNotFoundException onfe)
            {
                // Object doesn't exist, so remove from L1 cache
                removeObjectFromCache(sm.getInternalObjectId());
                throw onfe;
            }

            if (sm.getObject() != pc)
            {
                // Underlying object was changed in the validation process. This can happen when the datastore
                // is responsible for managing object references and it no longer recognises the cached value.
                fromCache = false;
                removeObjectFromCache(sm.getInternalObjectId());
            }
            if (!fromCache)
            {
                // We created a Hollow PC earlier but then went to the datastore and let it find the real object
                // This allows the datastore to replace this temporary Hollow object with the real datastore object if required
                // This doesnt change with RDBMS datastores since we just pull in fields, but for DB4O we pull in object graphs
                pc = sm.getObject();
            }
        }

        if (sm != null && !fromCache)
        {
View Full Code Here

     * @param id The id
     * @return The attached object
     */
    public Object getAttachedObjectForId(Object id)
    {
        StateManager sm = enlistedSMCache.get(id);
        if (sm != null)
        {
            return sm.getObject();
        }
        if (cache != null)
        {
            sm = (StateManager)cache.get(id);
            if (sm != null)
            {
                return sm.getObject();
            }
        }
        return null;
    }
View Full Code Here

     * @return The StateManager, null if StateManager not found.
     *     See JDO spec for the calling behavior when null is returned
     */
    public synchronized StateManager findStateManager(Object pc)
    {
        StateManager sm = null;
        Object previousLookingFor = objectLookingForStateManager;
        StateManager previousFound = foundStateManager;
        try
        {
            objectLookingForStateManager = pc;
            foundStateManager = null;
            // We call "ObjectManagerHelper.getObjectManager(pc)".
View Full Code Here

     */
    public ObjectProvider newObjectProviderForEmbedded(AbstractMemberMetaData ownerMmd, AbstractClassMetaData cmd,
            ObjectProvider ownerOP, int ownerFieldNumber)
    {
        Class pcClass = getClassLoaderResolver().classForName(cmd.getFullClassName());
        StateManager sm = (StateManager) ObjectProviderFactory.newForHollow(this, pcClass, null);
        sm.initialiseForEmbedded(sm.getObject(), false);
        if (ownerOP != null)
        {
            sm.addEmbeddedOwner(ownerOP, ownerFieldNumber);
        }
        return sm;
    }
View Full Code Here

            // any ConcurrentModification issues)
            Collection cachedSMsClone = new HashSet(cache.values());
            Iterator iter = cachedSMsClone.iterator();
            while (iter.hasNext())
            {
                StateManager sm = (StateManager) iter.next();
                if (sm != null)
                {
                    sm.disconnect();
                }
            }
            cache.clear();
            if (NucleusLogger.CACHE.isDebugEnabled())
            {
View Full Code Here

        {
            // Make sure all non-tx dirty objects are enlisted so they get lifecycle changes
            Iterator<StateManager> iter = dirtySMs.iterator();
            while (iter.hasNext())
            {
                StateManager sm = iter.next();
                enlistedSMCache.put(sm.getInternalObjectId(), sm);
            }

            synchronized (readWriteLock) // Lock since don't want any changes to objects during this step
            {
                // Flush any outstanding changes to the datastore
View Full Code Here

            assertNotDetached(obj);

            // we do not directly remove from cache level 1 here. The cache level 1 will be evicted
            // automatically on garbage collection, if the object can be evicted. it means not all
            // jdo states allows the object to be evicted.
            StateManager sm = findStateManager(obj);
            if (sm == null)
            {
                throw new NucleusUserException(LOCALISER.msg("010007", getApiAdapter().getIdForObject(obj)));
            }
            sm.evict();
        }
        finally
        {
            clr.unsetPrimary();
        }
View Full Code Here

TOP

Related Classes of org.datanucleus.state.StateManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.