Search in sources :

Example 1 with CachedEntity

use of org.activiti.engine.impl.persistence.cache.CachedEntity in project Activiti by Activiti.

the class AbstractDataManager method getList.

/**
 * Gets a list by querying the database and the cache using {@link CachedEntityMatcher}.
 * First, the entities are fetched from the database using the provided query.
 * The cache is then queried for the entities of the same type. If an entity matches
 * the {@link CachedEntityMatcher} condition, it replaces the entity from the database (as it is newer).
 *
 * @param dbQueryName The query name that needs to be executed.
 * @param parameter The parameters for the query.
 * @param entityMatcher The matcher used to determine which entities from the cache needs to be retained
 * @param checkCache If false, no cache check will be done, and the returned list will simply be the list from the database.
 */
@SuppressWarnings("unchecked")
protected List<EntityImpl> getList(String dbQueryName, Object parameter, CachedEntityMatcher<EntityImpl> cachedEntityMatcher, boolean checkCache) {
    Collection<EntityImpl> result = getDbSqlSession().selectList(dbQueryName, parameter);
    if (checkCache) {
        Collection<CachedEntity> cachedObjects = getEntityCache().findInCacheAsCachedObjects(getManagedEntityClass());
        if ((cachedObjects != null && cachedObjects.size() > 0) || getManagedEntitySubClasses() != null) {
            HashMap<String, EntityImpl> entityMap = new HashMap<String, EntityImpl>(result.size());
            // Database entities
            for (EntityImpl entity : result) {
                entityMap.put(entity.getId(), entity);
            }
            // Cache entities
            if (cachedObjects != null && cachedEntityMatcher != null) {
                for (CachedEntity cachedObject : cachedObjects) {
                    EntityImpl cachedEntity = (EntityImpl) cachedObject.getEntity();
                    if (cachedEntityMatcher.isRetained(result, cachedObjects, cachedEntity, parameter)) {
                        // will overwite db version with newer version
                        entityMap.put(cachedEntity.getId(), cachedEntity);
                    }
                }
            }
            if (getManagedEntitySubClasses() != null && cachedEntityMatcher != null) {
                for (Class<? extends EntityImpl> entitySubClass : getManagedEntitySubClasses()) {
                    Collection<CachedEntity> subclassCachedObjects = getEntityCache().findInCacheAsCachedObjects(entitySubClass);
                    if (subclassCachedObjects != null) {
                        for (CachedEntity subclassCachedObject : subclassCachedObjects) {
                            EntityImpl cachedSubclassEntity = (EntityImpl) subclassCachedObject.getEntity();
                            if (cachedEntityMatcher.isRetained(result, cachedObjects, cachedSubclassEntity, parameter)) {
                                // will overwite db version with newer version
                                entityMap.put(cachedSubclassEntity.getId(), cachedSubclassEntity);
                            }
                        }
                    }
                }
            }
            result = entityMap.values();
        }
    }
    // Remove entries which are already deleted
    if (result.size() > 0) {
        Iterator<EntityImpl> resultIterator = result.iterator();
        while (resultIterator.hasNext()) {
            if (getDbSqlSession().isEntityToBeDeleted(resultIterator.next())) {
                resultIterator.remove();
            }
        }
    }
    return new ArrayList<EntityImpl>(result);
}
Also used : HashMap(java.util.HashMap) CachedEntity(org.activiti.engine.impl.persistence.cache.CachedEntity) ArrayList(java.util.ArrayList)

Example 2 with CachedEntity

use of org.activiti.engine.impl.persistence.cache.CachedEntity in project Activiti by Activiti.

the class DbSqlSession method cacheLoadOrStore.

// internal session cache
// ///////////////////////////////////////////////////
@SuppressWarnings("rawtypes")
protected List cacheLoadOrStore(List<Object> loadedObjects) {
    if (loadedObjects.isEmpty()) {
        return loadedObjects;
    }
    if (!(loadedObjects.get(0) instanceof Entity)) {
        return loadedObjects;
    }
    List<Entity> filteredObjects = new ArrayList<Entity>(loadedObjects.size());
    for (Object loadedObject : loadedObjects) {
        Entity cachedEntity = cacheLoadOrStore((Entity) loadedObject);
        filteredObjects.add(cachedEntity);
    }
    return filteredObjects;
}
Also used : CachedEntity(org.activiti.engine.impl.persistence.cache.CachedEntity) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Entity(org.activiti.engine.impl.persistence.entity.Entity) PropertyEntity(org.activiti.engine.impl.persistence.entity.PropertyEntity) ArrayList(java.util.ArrayList)

Example 3 with CachedEntity

use of org.activiti.engine.impl.persistence.cache.CachedEntity in project Activiti by Activiti.

the class DbSqlSession method cacheLoadOrStore.

/**
 * Returns the object in the cache. If this object was loaded before, then the original object is returned (the cached version is more recent).
 * If this is the first time this object is loaded, then the loadedObject is added to the cache.
 */
protected Entity cacheLoadOrStore(Entity entity) {
    Entity cachedEntity = entityCache.findInCache(entity.getClass(), entity.getId());
    if (cachedEntity != null) {
        return cachedEntity;
    }
    entityCache.put(entity, true);
    return entity;
}
Also used : CachedEntity(org.activiti.engine.impl.persistence.cache.CachedEntity) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Entity(org.activiti.engine.impl.persistence.entity.Entity) PropertyEntity(org.activiti.engine.impl.persistence.entity.PropertyEntity)

Example 4 with CachedEntity

use of org.activiti.engine.impl.persistence.cache.CachedEntity in project Activiti by Activiti.

the class DbSqlSession method determineUpdatedObjects.

public void determineUpdatedObjects() {
    updatedObjects = new ArrayList<Entity>();
    Map<Class<?>, Map<String, CachedEntity>> cachedObjects = entityCache.getAllCachedEntities();
    for (Class<?> clazz : cachedObjects.keySet()) {
        Map<String, CachedEntity> classCache = cachedObjects.get(clazz);
        for (CachedEntity cachedObject : classCache.values()) {
            Entity cachedEntity = cachedObject.getEntity();
            if (!isEntityInserted(cachedEntity) && (ExecutionEntity.class.isAssignableFrom(cachedEntity.getClass()) || !isEntityToBeDeleted(cachedEntity)) && cachedObject.hasChanged()) {
                updatedObjects.add(cachedEntity);
            }
        }
    }
}
Also used : CachedEntity(org.activiti.engine.impl.persistence.cache.CachedEntity) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Entity(org.activiti.engine.impl.persistence.entity.Entity) PropertyEntity(org.activiti.engine.impl.persistence.entity.PropertyEntity) CachedEntity(org.activiti.engine.impl.persistence.cache.CachedEntity) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with CachedEntity

use of org.activiti.engine.impl.persistence.cache.CachedEntity in project Activiti by Activiti.

the class AbstractDataManager method getListFromCache.

protected List<EntityImpl> getListFromCache(CachedEntityMatcher<EntityImpl> entityMatcher, Object parameter) {
    Collection<CachedEntity> cachedObjects = getEntityCache().findInCacheAsCachedObjects(getManagedEntityClass());
    DbSqlSession dbSqlSession = getDbSqlSession();
    List<EntityImpl> result = new ArrayList<EntityImpl>(cachedObjects.size());
    if (cachedObjects != null && entityMatcher != null) {
        for (CachedEntity cachedObject : cachedObjects) {
            EntityImpl cachedEntity = (EntityImpl) cachedObject.getEntity();
            if (entityMatcher.isRetained(null, cachedObjects, cachedEntity, parameter) && !dbSqlSession.isEntityToBeDeleted(cachedEntity)) {
                result.add(cachedEntity);
            }
        }
    }
    if (getManagedEntitySubClasses() != null && entityMatcher != null) {
        for (Class<? extends EntityImpl> entitySubClass : getManagedEntitySubClasses()) {
            Collection<CachedEntity> subclassCachedObjects = getEntityCache().findInCacheAsCachedObjects(entitySubClass);
            if (subclassCachedObjects != null) {
                for (CachedEntity subclassCachedObject : subclassCachedObjects) {
                    EntityImpl cachedSubclassEntity = (EntityImpl) subclassCachedObject.getEntity();
                    if (entityMatcher.isRetained(null, cachedObjects, cachedSubclassEntity, parameter) && !dbSqlSession.isEntityToBeDeleted(cachedSubclassEntity)) {
                        result.add(cachedSubclassEntity);
                    }
                }
            }
        }
    }
    return result;
}
Also used : CachedEntity(org.activiti.engine.impl.persistence.cache.CachedEntity) ArrayList(java.util.ArrayList) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession)

Aggregations

CachedEntity (org.activiti.engine.impl.persistence.cache.CachedEntity)5 ArrayList (java.util.ArrayList)3 Entity (org.activiti.engine.impl.persistence.entity.Entity)3 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)3 PropertyEntity (org.activiti.engine.impl.persistence.entity.PropertyEntity)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DbSqlSession (org.activiti.engine.impl.db.DbSqlSession)1