You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2018/02/17 10:13:22 UTC
incubator-unomi git commit: UNOMI-160 Optimize ItemType retrieval
from classes
Repository: incubator-unomi
Updated Branches:
refs/heads/master 681384714 -> 4218ccafb
UNOMI-160 Optimize ItemType retrieval from classes
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/4218ccaf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/4218ccaf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/4218ccaf
Branch: refs/heads/master
Commit: 4218ccafbb89e1067bf85c8921c8298a39d5b448
Parents: 6813847
Author: Serge Huber <sh...@apache.org>
Authored: Sat Feb 17 11:13:19 2018 +0100
Committer: Serge Huber <sh...@apache.org>
Committed: Sat Feb 17 11:13:19 2018 +0100
----------------------------------------------------------------------
.../main/java/org/apache/unomi/api/Item.java | 29 +++++++++--
.../ElasticSearchPersistenceServiceImpl.java | 51 +++++---------------
2 files changed, 38 insertions(+), 42 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/4218ccaf/api/src/main/java/org/apache/unomi/api/Item.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/unomi/api/Item.java b/api/src/main/java/org/apache/unomi/api/Item.java
index 2d9099d..72f0ae6 100644
--- a/api/src/main/java/org/apache/unomi/api/Item.java
+++ b/api/src/main/java/org/apache/unomi/api/Item.java
@@ -21,6 +21,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* A context server tracked entity. All tracked entities need to extend this class so as to provide the minimal information the context server needs to be able to track such
@@ -37,16 +39,35 @@ public abstract class Item implements Serializable {
private static final Logger logger = LoggerFactory.getLogger(Item.class.getName());
private static final long serialVersionUID = 7446061538573517071L;
+
+ private static final Map<Class,String> itemTypeCache = new ConcurrentHashMap<>();
+
+ public static String getItemType(Class clazz) {
+ String itemType = itemTypeCache.get(clazz);
+ if (itemType != null) {
+ return itemType;
+ }
+ try {
+ itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ itemTypeCache.put(clazz, itemType);
+ return itemType;
+ } catch (NoSuchFieldException e) {
+ logger.error("Class " + clazz.getName() + " doesn't define a publicly accessible ITEM_TYPE field", e);
+ } catch (IllegalAccessException e) {
+ logger.error("Error resolving itemType for class " + clazz.getName(), e);
+ }
+ return null;
+ }
+
protected String itemId;
protected String itemType;
protected String scope;
protected Long version;
public Item() {
- try {
- this.itemType = (String) this.getClass().getField("ITEM_TYPE").get(null);
- } catch (IllegalAccessException | NoSuchFieldException e) {
- logger.error("Item implementations must provide a public String constant named ITEM_TYPE to uniquely identify this Item for the persistence service.", e);
+ this.itemType = getItemType(this.getClass());
+ if (itemType == null) {
+ logger.error("Item implementations must provide a public String constant named ITEM_TYPE to uniquely identify this Item for the persistence service.");
}
}
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/4218ccaf/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
----------------------------------------------------------------------
diff --git a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
index 8a20f95..2f55843 100644
--- a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
+++ b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
@@ -566,7 +566,11 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
return new InClassLoaderExecute<T>() {
protected T execute(Object... args) throws Exception {
try {
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
+ T itemFromCache = getFromCache(itemId, clazz);
+ if (itemFromCache != null) {
+ return itemFromCache;
+ }
if (itemsMonthlyIndexed.contains(itemType) && dateHint == null) {
PartialList<T> r = query(QueryBuilders.idsQuery(itemType).addIds(itemId), null, clazz, 0, 1, null, null);
@@ -653,7 +657,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
Boolean result = new InClassLoaderExecute<Boolean>() {
protected Boolean execute(Object... args) throws Exception {
try {
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
String index = indexNames.containsKey(itemType) ? indexNames.get(itemType) :
(itemsMonthlyIndexed.contains(itemType) && dateHint != null ? getMonthlyIndexName(dateHint) : indexName);
@@ -669,10 +673,6 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
return true;
} catch (IndexNotFoundException e) {
throw new Exception("No index found for itemType=" + clazz.getName() + "itemId=" + itemId, e);
- } catch (NoSuchFieldException e) {
- throw new Exception("Error updating item " + itemId, e);
- } catch (IllegalAccessException e) {
- throw new Exception("Error updating item " + itemId, e);
}
}
}.catchingExecuteInClassLoader(true);
@@ -688,7 +688,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
Boolean result = new InClassLoaderExecute<Boolean>() {
protected Boolean execute(Object... args) throws Exception {
try {
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
String index = indexNames.containsKey(itemType) ? indexNames.get(itemType) :
(itemsMonthlyIndexed.contains(itemType) && dateHint != null ? getMonthlyIndexName(dateHint) : indexName);
@@ -723,10 +723,6 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
return true;
} catch (IndexNotFoundException e) {
throw new Exception("No index found for itemType=" + clazz.getName(), e);
- } catch (NoSuchFieldException e) {
- throw new Exception("Error updating item ", e);
- } catch (IllegalAccessException e) {
- throw new Exception("Error updating item ", e);
} catch (ScriptException e) {
logger.error("Error in the update script : {}\n{}\n{}", e.getScript(), e.getDetailedMessage(), e.getScriptStack());
throw new Exception("Error in the update script");
@@ -747,7 +743,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
Boolean result = new InClassLoaderExecute<Boolean>() {
protected Boolean execute(Object... args) throws Exception {
try {
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
String index = indexNames.containsKey(itemType) ? indexNames.get(itemType) :
(itemsMonthlyIndexed.contains(itemType) && dateHint != null ? getMonthlyIndexName(dateHint) : indexName);
@@ -765,10 +761,6 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
return true;
} catch (IndexNotFoundException e) {
throw new Exception("No index found for itemType=" + clazz.getName() + "itemId=" + itemId, e);
- } catch (NoSuchFieldException e) {
- throw new Exception("Error updating item " + itemId, e);
- } catch (IllegalAccessException e) {
- throw new Exception("Error updating item " + itemId, e);
}
}
}.catchingExecuteInClassLoader(true);
@@ -783,9 +775,8 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
public <T extends Item> boolean remove(final String itemId, final Class<T> clazz) {
Boolean result = new InClassLoaderExecute<Boolean>() {
protected Boolean execute(Object... args) throws Exception {
- //Index the query = register it in the percolator
try {
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
client.prepareDelete(getIndexNameForQuery(itemType), itemType, itemId)
.execute().actionGet();
@@ -806,7 +797,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
Boolean result = new InClassLoaderExecute<Boolean>() {
protected Boolean execute(Object... args) throws Exception {
try {
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
BulkRequestBuilder deleteByScope = client.prepareBulk();
@@ -1160,18 +1151,13 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
}
try {
final Class<? extends Item> clazz = item.getClass();
- String itemType = (String) clazz.getField("ITEM_TYPE").get(null);
+ String itemType = Item.getItemType(clazz);
QueryBuilder builder = QueryBuilders.boolQuery()
.must(QueryBuilders.idsQuery(itemType).addIds(item.getItemId()))
.must(conditionESQueryBuilderDispatcher.buildFilter(query));
return queryCount(builder, itemType) > 0;
- } catch (IllegalAccessException e) {
- logger.error("Error getting query for item=" + item, e);
- } catch (NoSuchFieldException e) {
- logger.error("Error getting query for item=" + item, e);
}
- return false;
}
@Override
@@ -1257,7 +1243,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
String scrollIdentifier = null;
long totalHits = 0;
try {
- String itemType = getItemType(clazz);
+ String itemType = Item.getItemType(clazz);
TimeValue keepAlive = TimeValue.timeValueHours(1);
SearchRequestBuilder requestBuilder = null;
if (scrollTimeValidity != null) {
@@ -1533,19 +1519,8 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService,
}.catchingExecuteInClassLoader(true);
}
- private <T extends Item> String getItemType(Class<T> clazz) {
- try {
- return (String) clazz.getField("ITEM_TYPE").get(null);
- } catch (NoSuchFieldException e) {
- logger.error("Class " + clazz.getName() + " doesn't define a publicly accessible ITEM_TYPE field", e);
- } catch (IllegalAccessException e) {
- logger.error("Error loading itemType=" + clazz.getName(), e);
- }
- return null;
- }
-
private <T extends Item> String[] getRouting(String fieldName, String[] fieldValues, Class<T> clazz) {
- String itemType = getItemType(clazz);
+ String itemType = Item.getItemType(clazz);
String[] routing = null;
if (routingByType.containsKey(itemType) && routingByType.get(itemType).equals(fieldName)) {
routing = fieldValues;