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);
}
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;
}
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;
}
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);
}
}
}
}
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;
}
Aggregations