Search in sources :

Example 1 with DatastoreUniqueLongId

use of org.datanucleus.identity.DatastoreUniqueLongId in project datanucleus-core by datanucleus.

the class PersistenceNucleusContextImpl method isClassWithIdentityCacheable.

/* (non-Javadoc)
     * @see org.datanucleus.NucleusContext#isClassWithIdentityCacheable(java.lang.Object)
     */
@Override
public boolean isClassWithIdentityCacheable(Object id) {
    if (id == null) {
        return false;
    }
    if (id instanceof SCOID) {
        return false;
    } else if (id instanceof DatastoreUniqueLongId) {
        // This doesn't have the class name so can't get metadata
        return false;
    }
    AbstractClassMetaData cmd = null;
    String className = IdentityUtils.getTargetClassNameForIdentity(id);
    if (className != null) {
        // "Identity" defines the class name
        cmd = getMetaDataManager().getMetaDataForClass(className, getClassLoaderResolver(id.getClass().getClassLoader()));
    } else {
        // Application identity with user PK class, so find all using this PK and take first one
        Collection<AbstractClassMetaData> cmds = getMetaDataManager().getClassMetaDataWithApplicationId(id.getClass().getName());
        if (cmds != null && !cmds.isEmpty()) {
            cmd = cmds.iterator().next();
        }
    }
    return isClassCacheable(cmd);
}
Also used : SCOID(org.datanucleus.identity.SCOID) DatastoreUniqueLongId(org.datanucleus.identity.DatastoreUniqueLongId) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 2 with DatastoreUniqueLongId

use of org.datanucleus.identity.DatastoreUniqueLongId in project datanucleus-core by datanucleus.

the class ExecutionContextImpl method getClassDetailsForId.

/**
 * Convenience method that takes an id, an optional class name for the object it represents, and whether
 * to check for inheritance, and returns class details of the object being represented.
 * Used by the findObject process.
 * @param id The identity
 * @param objectClassName Class name for the object (if known, otherwise is derived)
 * @param checkInheritance Whether to check the inheritance level for this id
 * @return The details for the class
 */
private ClassDetailsForId getClassDetailsForId(Object id, String objectClassName, boolean checkInheritance) {
    // 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 DatastoreUniqueLongId) {
        // Should have been found using "persistenceHandler.findObject()"
        throw new NucleusObjectNotFoundException(Localiser.msg("010026"), id);
    } else if (objectClassName != null) {
        // Object class name specified so use that directly
        originalClassName = objectClassName;
    } else {
        originalClassName = getStoreManager().manageClassForIdentity(id, clr);
    }
    if (originalClassName == null) {
        // We dont know the object class so try to deduce it from what is known by the StoreManager
        originalClassName = getClassNameForObjectId(id);
        checkedClassName = true;
    }
    Object pc = null;
    if (checkInheritance) {
        // Validate the inheritance level
        className = checkedClassName ? originalClassName : getClassNameForObjectId(id);
        if (className == null) {
            throw new NucleusObjectNotFoundException(Localiser.msg("010026"), id);
        }
        if (!checkedClassName) {
            // Check if this id for any known subclasses is in the cache to save searching
            if (IdentityUtils.isDatastoreIdentity(id) || IdentityUtils.isSingleFieldIdentity(id)) {
                String[] subclasses = getMetaDataManager().getSubclassesForClass(className, true);
                if (subclasses != null) {
                    for (int i = 0; i < subclasses.length; i++) {
                        Object oid = null;
                        if (IdentityUtils.isDatastoreIdentity(id)) {
                            oid = nucCtx.getIdentityManager().getDatastoreId(subclasses[i], IdentityUtils.getTargetKeyForDatastoreIdentity(id));
                        } else if (IdentityUtils.isSingleFieldIdentity(id)) {
                            oid = nucCtx.getIdentityManager().getSingleFieldId(id.getClass(), getClassLoaderResolver().classForName(subclasses[i]), IdentityUtils.getTargetKeyForSingleFieldIdentity(id));
                        }
                        pc = getObjectFromCache(oid);
                        if (pc != null) {
                            className = subclasses[i];
                            break;
                        }
                    }
                }
            }
        }
        if (pc == null && originalClassName != null && !originalClassName.equals(className)) {
            // Inheritance check implies different inheritance level, so retry
            if (IdentityUtils.isDatastoreIdentity(id)) {
                // Create new OID using correct target class, and recheck cache
                id = nucCtx.getIdentityManager().getDatastoreId(className, ((DatastoreId) id).getKeyAsObject());
                pc = getObjectFromCache(id);
            } else if (IdentityUtils.isSingleFieldIdentity(id)) {
                // Create new SingleFieldIdentity using correct targetClass, and recheck cache
                id = nucCtx.getIdentityManager().getSingleFieldId(id.getClass(), clr.classForName(className), IdentityUtils.getTargetKeyForSingleFieldIdentity(id));
                pc = getObjectFromCache(id);
            }
        }
    } else {
        className = originalClassName;
    }
    return new ClassDetailsForId(id, className, pc);
}
Also used : DatastoreId(org.datanucleus.identity.DatastoreId) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SCOID(org.datanucleus.identity.SCOID) DatastoreUniqueLongId(org.datanucleus.identity.DatastoreUniqueLongId) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException)

Aggregations

DatastoreUniqueLongId (org.datanucleus.identity.DatastoreUniqueLongId)2 SCOID (org.datanucleus.identity.SCOID)2 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 DatastoreId (org.datanucleus.identity.DatastoreId)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1