Improved: General refactoring and code improvements, package

org.apache.ofbiz.entity.cache.
(OFBIZ-9949)

Thanks Dennis Balkir for reporting and providing the patches.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1818478 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-17 11:34:39 +00:00
parent e752662174
commit f3a43c5c9e
4 changed files with 54 additions and 32 deletions
@@ -26,7 +26,6 @@ import java.util.concurrent.ConcurrentMap;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.cache.UtilCache;
import org.apache.ofbiz.entity.GenericEntity;
import org.apache.ofbiz.entity.GenericPK;
@@ -44,7 +43,9 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
protected V get(String entityName, EntityCondition condition, K key) {
ConcurrentMap<K, V> conditionCache = getConditionCache(entityName, condition);
if (conditionCache == null) return null;
if (conditionCache == null) {
return null;
}
return conditionCache.get(key);
}
@@ -74,13 +75,17 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
public void remove(String entityName, EntityCondition condition) {
UtilCache<EntityCondition, ConcurrentMap<K, V>> cache = getCache(entityName);
if (cache == null) return;
if (cache == null) {
return;
}
cache.remove(condition);
}
protected V remove(String entityName, EntityCondition condition, K key) {
ConcurrentMap<K, V> conditionCache = getConditionCache(entityName, condition);
if (conditionCache == null) return null;
if (conditionCache == null) {
return null;
}
return conditionCache.remove(key);
}
@@ -90,19 +95,14 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
public static final EntityCondition getFrozenConditionKey(EntityCondition condition) {
EntityCondition frozenCondition = condition != null ? condition.freeze() : null;
// This is no longer needed, fixed issue with unequal conditions after freezing
//if (condition != null) {
// if (!condition.equals(frozenCondition)) {
// Debug.logWarning("Frozen condition does not equal condition:\n -=-=-=-Original=" + condition + "\n -=-=-=-Frozen=" + frozenCondition, module);
// Debug.logWarning("Frozen condition not equal info: condition class=" + condition.getClass().getName() + "; frozenCondition class=" + frozenCondition.getClass().getName(), module);
// }
//}
return frozenCondition;
}
protected ConcurrentMap<K, V> getConditionCache(String entityName, EntityCondition condition) {
UtilCache<EntityCondition, ConcurrentMap<K, V>> cache = getCache(entityName);
if (cache == null) return null;
if (cache == null) {
return null;
}
return cache.get(getConditionKey(condition));
}
@@ -156,7 +156,9 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
}
protected List<? extends Map<String, Object>> convert(String targetEntityName, GenericEntity entity) {
if (isNull(entity)) return null;
if (isNull(entity)) {
return null;
}
return entity.getModelEntity().convertToViewValues(targetEntityName, entity);
}
@@ -179,7 +181,6 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
return;
}
for (EntityCondition condition: entityCache.getCacheLineKeys()) {
//Debug.logInfo("In storeHook entityName [" + entityName + "] checking against condition: " + condition, module);
boolean shouldRemove = false;
if (condition == null) {
shouldRemove = true;
@@ -196,13 +197,11 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
T1 oldValue = oldValueIter.next();
if (condition.mapMatches(getDelegator(), oldValue)) {
oldMatched = true;
//Debug.logInfo("In storeHook, oldMatched for entityName [" + entityName + "]; shouldRemove is false", module);
if (newValues != null) {
Iterator<T2> newValueIter = newValues.iterator();
while (newValueIter.hasNext() && !shouldRemove) {
T2 newValue = newValueIter.next();
shouldRemove |= isNull(newValue) || condition.mapMatches(getDelegator(), newValue);
//Debug.logInfo("In storeHook, for entityName [" + entityName + "] shouldRemove is now " + shouldRemove, module);
}
} else {
shouldRemove = true;
@@ -211,12 +210,13 @@ public abstract class AbstractEntityConditionCache<K, V> extends AbstractCache<E
}
// QUESTION: what is this? why would we do this?
if (!oldMatched && isPK) {
//Debug.logInfo("In storeHook, for entityName [" + entityName + "] oldMatched is false and isPK is true, so setting shouldRemove to true (will remove from cache)", module);
shouldRemove = true;
}
}
if (shouldRemove) {
if (Debug.verboseOn()) Debug.logVerbose("In storeHook, matched condition, removing from cache for entityName [" + entityName + "] in cache with name [" + entityCache.getName() + "] entry with condition: " + condition, module);
if (Debug.verboseOn()) {
Debug.logVerbose("In storeHook, matched condition, removing from cache for entityName [" + entityName + "] in cache with name [" + entityCache.getName() + "] entry with condition: " + condition, module);
}
// doesn't work anymore since this is a copy of the cache keySet, can call remove directly though with a concurrent mod exception: cacheKeyIter.remove();
entityCache.remove(condition);
}
@@ -110,18 +110,20 @@ public class Cache {
}
public GenericValue remove(GenericEntity entity) {
if (Debug.verboseOn()) Debug.logVerbose("Cache remove GenericEntity: " + entity, module);
if (Debug.verboseOn()) {
Debug.logVerbose("Cache remove GenericEntity: " + entity, module);
}
GenericValue oldEntity = entityCache.remove(entity.getPrimaryKey());
// Workaround because AbstractEntityConditionCache.storeHook doesn't work.
entityListCache.remove(entity);
entityObjectCache.remove(entity);
// entityListCache.storeHook(entity, null);
// entityObjectCache.storeHook(entity, null);
return oldEntity;
}
public GenericValue remove(GenericPK pk) {
if (Debug.verboseOn()) Debug.logVerbose("Cache remove GenericPK: " + pk, module);
if (Debug.verboseOn()) {
Debug.logVerbose("Cache remove GenericPK: " + pk, module);
}
GenericValue oldEntity = entityCache.remove(pk);
// Workaround because AbstractEntityConditionCache.storeHook doesn't work.
entityListCache.remove(pk);
@@ -36,12 +36,16 @@ public class EntityCache extends AbstractCache<GenericPK, GenericValue> {
public GenericValue get(GenericPK pk) {
UtilCache<GenericPK, GenericValue> entityCache = getCache(pk.getEntityName());
if (entityCache == null) return null;
if (entityCache == null) {
return null;
}
return entityCache.get(pk);
}
public GenericValue put(GenericValue entity) {
if (entity == null) return null;
if (entity == null) {
return null;
}
return put(entity.getPrimaryKey(), entity);
}
@@ -63,11 +67,17 @@ public class EntityCache extends AbstractCache<GenericPK, GenericValue> {
public void remove(String entityName, EntityCondition condition) {
UtilCache<GenericPK, GenericValue> entityCache = getCache(entityName);
if (entityCache == null) return;
if (entityCache == null) {
return;
}
for (GenericPK pk: entityCache.getCacheLineKeys()) {
GenericValue entity = entityCache.get(pk);
if (entity == null) continue;
if (condition.entityMatches(entity)) entityCache.remove(pk);
if (entity == null) {
continue;
}
if (condition.entityMatches(entity)) {
entityCache.remove(pk);
}
}
}
@@ -77,8 +87,12 @@ public class EntityCache extends AbstractCache<GenericPK, GenericValue> {
public GenericValue remove(GenericPK pk) {
UtilCache<GenericPK, GenericValue> entityCache = getCache(pk.getEntityName());
if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], will remove from this cache: " + (entityCache == null ? "[No cache found to remove from]" : entityCache.getName()), module);
if (entityCache == null) return null;
if (Debug.verboseOn()) {
Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], will remove from this cache: " + (entityCache == null ? "[No cache found to remove from]" : entityCache.getName()), module);
}
if (entityCache == null) {
return null;
}
GenericValue retVal = entityCache.remove(pk);
ModelEntity model = pk.getModelEntity();
if (model != null) {
@@ -88,7 +102,9 @@ public class EntityCache extends AbstractCache<GenericPK, GenericValue> {
UtilCache.clearCache(getCacheName(targetEntityName));
}
}
if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], found this in the cache: " + retVal, module);
if (Debug.verboseOn()) {
Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], found this in the cache: " + retVal, module);
}
return retVal;
}
}
@@ -44,13 +44,17 @@ public class EntityListCache extends AbstractEntityConditionCache<Object, List<G
public List<GenericValue> get(String entityName, EntityCondition condition, List<String> orderBy) {
ConcurrentMap<Object, List<GenericValue>> conditionCache = getConditionCache(entityName, condition);
if (conditionCache == null) return null;
if (conditionCache == null) {
return null;
}
Object orderByKey = getOrderByKey(orderBy);
List<GenericValue> valueList = conditionCache.get(orderByKey);
if (valueList == null) {
// the valueList was not found for the given ordering, so grab the first one and order it in memory
Iterator<List<GenericValue>> it = conditionCache.values().iterator();
if (it.hasNext()) valueList = it.next();
if (it.hasNext()) {
valueList = it.next();
}
if (valueList != null) {
// Does not need to be synchronized; if 2 threads do the same ordering,