Search in sources :

Example 41 with ObjectProvider

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

the class ExecutionContextImpl method evictObject.

// ----------------------------- Lifecycle Methods ------------------------------------
/**
 * Internal method to evict an object from L1 cache.
 * @param obj The object
 * @throws NucleusException if an error occurs evicting the object
 */
public void evictObject(Object obj) {
    if (obj == null) {
        return;
    }
    try {
        clr.setPrimary(obj.getClass().getClassLoader());
        assertClassPersistable(obj.getClass());
        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.
        ObjectProvider op = findObjectProvider(obj);
        if (op == null) {
            throw new NucleusUserException(Localiser.msg("010048", StringUtils.toJVMIDString(obj), getApiAdapter().getIdForObject(obj), "evict"));
        }
        op.evict();
    } finally {
        clr.unsetPrimary();
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 42 with ObjectProvider

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

the class ExecutionContextImpl method findObject.

/**
 * Accessor for an object given the object id and a set of field values to apply to it.
 * This is intended for use where we have done a query and have the id from the results, and we want to
 * create the object, preferably using the cache, and then apply any field values to it.
 * @param id Id of the object.
 * @param fv Field values for the object (to copy in)
 * @param cls the type which the object is (optional). Used to instantiate the object
 * @param ignoreCache true if it must ignore the cache
 * @param checkInheritance Whether to check the inheritance on the id of the object
 * @return The Object
 */
public Object findObject(Object id, FieldValues fv, Class cls, boolean ignoreCache, boolean checkInheritance) {
    assertIsOpen();
    Object pc = null;
    ObjectProvider op = null;
    if (!ignoreCache) {
        // Check if an object exists in the L1/L2 caches for this id
        pc = getObjectFromCache(id);
    }
    if (pc == null) {
        // Find direct from the datastore if supported
        pc = getStoreManager().getPersistenceHandler().findObject(this, id);
    }
    boolean createdHollow = false;
    if (pc == null) {
        // Determine the class details for this "id" if not provided, including checking of inheritance level
        String className = cls != null ? cls.getName() : null;
        if (!(id instanceof SCOID)) {
            ClassDetailsForId details = getClassDetailsForId(id, className, checkInheritance);
            if (details.className != null && cls != null && !cls.getName().equals(details.className)) {
                cls = clr.classForName(details.className);
            }
            className = details.className;
            id = details.id;
            if (details.pc != null) {
                // Found following inheritance check via the cache
                pc = details.pc;
                op = findObjectProvider(pc);
            }
        }
        if (pc == null) {
            // Still not found so create a Hollow instance with the supplied field values
            if (cls == null) {
                try {
                    cls = clr.classForName(className, id.getClass().getClassLoader());
                } catch (ClassNotResolvedException e) {
                    String msg = Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id));
                    NucleusLogger.PERSISTENCE.warn(msg);
                    throw new NucleusUserException(msg, e);
                }
            }
            createdHollow = true;
            // Will put object in L1 cache
            op = nucCtx.getObjectProviderFactory().newForHollow(this, cls, id, fv);
            pc = op.getObject();
            putObjectIntoLevel2Cache(op, false);
        }
    }
    if (pc != null && fv != null && !createdHollow) {
        // Object found in the cache so load the requested fields
        if (op == null) {
            op = findObjectProvider(pc);
        }
        if (op != null) {
            // Load the requested fields
            fv.fetchNonLoadedFields(op);
        }
    }
    return pc;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider) SCOID(org.datanucleus.identity.SCOID) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 43 with ObjectProvider

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

the class ExecutionContextImpl method getObjectFromLevel1Cache.

/**
 * Convenience method to access an object in the Level 1 cache.
 * @param id Id of the object
 * @return Persistable object (with connected ObjectProvider).
 */
public Object getObjectFromLevel1Cache(Object id) {
    Object pc = null;
    ObjectProvider op = null;
    if (cache != null) {
        op = cache.get(id);
        if (op != null) {
            pc = op.getObject();
            if (NucleusLogger.CACHE.isDebugEnabled()) {
                NucleusLogger.CACHE.debug(Localiser.msg("003008", StringUtils.toJVMIDString(pc), IdentityUtils.getPersistableIdentityForId(id), StringUtils.booleanArrayToString(op.getLoadedFields())));
            }
            // Wipe the detach state that may have been added if the object has been serialised in the meantime
            op.resetDetachState();
            return pc;
        }
        if (NucleusLogger.CACHE.isDebugEnabled()) {
            NucleusLogger.CACHE.debug(Localiser.msg("003007", IdentityUtils.getPersistableIdentityForId(id)));
        }
    }
    return null;
}
Also used : ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 44 with ObjectProvider

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

the class ManagedRelationsHandler method execute.

public void execute() {
    if (managedRelationDetails.isEmpty()) {
        return;
    }
    try {
        executing = true;
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("013000"));
        }
        if (performChecks) {
            // Tests for negative situations where inconsistently assigned
            Iterator<Map.Entry<ObjectProvider, RelationshipManager>> managedRelEntryIter = managedRelationDetails.entrySet().iterator();
            while (managedRelEntryIter.hasNext()) {
                Map.Entry<ObjectProvider, RelationshipManager> managedRelEntry = managedRelEntryIter.next();
                ObjectProvider op = managedRelEntry.getKey();
                LifeCycleState lc = op.getLifecycleState();
                if (lc == null || lc.isDeleted()) {
                    // Has been deleted so ignore all relationship changes
                    continue;
                }
                managedRelEntry.getValue().checkConsistency();
            }
        }
        // Process updates to manage the other side of the relations
        Iterator<Map.Entry<ObjectProvider, RelationshipManager>> managedRelEntryIter = managedRelationDetails.entrySet().iterator();
        while (managedRelEntryIter.hasNext()) {
            Map.Entry<ObjectProvider, RelationshipManager> managedRelEntry = managedRelEntryIter.next();
            ObjectProvider op = managedRelEntry.getKey();
            LifeCycleState lc = op.getLifecycleState();
            if (lc == null || lc.isDeleted()) {
                // Has been deleted so ignore all relationship changes
                continue;
            }
            RelationshipManager relMgr = managedRelEntry.getValue();
            relMgr.process();
            relMgr.clearFields();
        }
        managedRelationDetails.clear();
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("013001"));
        }
    } finally {
        executing = false;
    }
}
Also used : RelationshipManager(org.datanucleus.state.RelationshipManager) LifeCycleState(org.datanucleus.state.LifeCycleState) ObjectProvider(org.datanucleus.state.ObjectProvider) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 45 with ObjectProvider

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

the class L2CachePopulateFieldManager method convertPersistableToCachedPC.

/**
 * Method to convert an embedded/serialised object to a CachedPC object for L2 caching.
 * @param pc The persistable
 * @return The CachedPC that it is stored as
 */
protected CachedPC convertPersistableToCachedPC(Object pc) {
    ObjectProvider valueOP = ec.findObjectProvider(pc);
    CachedPC valueCachedPC = new CachedPC(pc.getClass(), valueOP.getLoadedFields(), null, null);
    int[] loadedFields = valueOP.getLoadedFieldNumbers();
    if (loadedFields != null && loadedFields.length > 0) {
        // Set the values of any fields that are loaded
        valueOP.provideFields(loadedFields, new L2CachePopulateFieldManager(valueOP, valueCachedPC));
    }
    return valueCachedPC;
}
Also used : 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