Repository: incubator-unomi Updated Branches: refs/heads/feature-UNOMI-117 3f362a027 -> de1ae00ad
UNOMI-117 add ES document version to Item.java so we know the version of a document Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/de1ae00a Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/de1ae00a Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/de1ae00a Branch: refs/heads/feature-UNOMI-117 Commit: de1ae00ad352877d18df998e98edc1250b17c42c Parents: 3f362a0 Author: dgaillard <[email protected]> Authored: Fri Sep 15 17:58:17 2017 +0200 Committer: dgaillard <[email protected]> Committed: Fri Sep 15 17:58:17 2017 +0200 ---------------------------------------------------------------------- .../main/java/org/apache/unomi/api/Item.java | 9 +++++ .../elasticsearch/ESCustomObjectMapper.java | 42 ++++++++++++++++++++ .../persistence/elasticsearch/ESItemMixIn.java | 29 ++++++++++++++ .../ElasticSearchPersistenceServiceImpl.java | 22 +++++----- .../apache/unomi/rest/LocalizationHelper.java | 2 + .../org/apache/unomi/rest/RESTActionType.java | 9 +++++ .../apache/unomi/rest/RESTConditionType.java | 9 +++++ 7 files changed, 112 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/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 2af5c00..2d9099d 100644 --- a/api/src/main/java/org/apache/unomi/api/Item.java +++ b/api/src/main/java/org/apache/unomi/api/Item.java @@ -40,6 +40,7 @@ public abstract class Item implements Serializable { protected String itemId; protected String itemType; protected String scope; + protected Long version; public Item() { try { @@ -110,4 +111,12 @@ public abstract class Item implements Serializable { public int hashCode() { return itemId != null ? itemId.hashCode() : 0; } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESCustomObjectMapper.java ---------------------------------------------------------------------- diff --git a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESCustomObjectMapper.java b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESCustomObjectMapper.java new file mode 100644 index 0000000..119d1b2 --- /dev/null +++ b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESCustomObjectMapper.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.persistence.elasticsearch; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.unomi.api.Item; +import org.apache.unomi.persistence.spi.CustomObjectMapper; + +/** + * @author dgaillard + */ +public class ESCustomObjectMapper extends CustomObjectMapper { + + private static final long serialVersionUID = -5017620674440085575L; + + public ESCustomObjectMapper() { + super(); + this.addMixIn(Item.class, ESItemMixIn.class); + } + + public static ObjectMapper getObjectMapper() { + return ESCustomObjectMapper.Holder.INSTANCE; + } + + private static class Holder { + static final ESCustomObjectMapper INSTANCE = new ESCustomObjectMapper(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESItemMixIn.java ---------------------------------------------------------------------- diff --git a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESItemMixIn.java b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESItemMixIn.java new file mode 100644 index 0000000..40ea3c3 --- /dev/null +++ b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ESItemMixIn.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.persistence.elasticsearch; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +/** + * @author dgaillard + */ +public abstract class ESItemMixIn { + + public ESItemMixIn() { } + + @JsonIgnore abstract Long getVersion(); +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/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 bb84d81..f6d570e 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 @@ -26,7 +26,6 @@ import org.apache.unomi.api.query.DateRange; import org.apache.unomi.api.query.IpRange; import org.apache.unomi.api.query.NumericRange; import org.apache.unomi.persistence.elasticsearch.conditions.*; -import org.apache.unomi.persistence.spi.CustomObjectMapper; import org.apache.unomi.persistence.spi.PersistenceService; import org.apache.unomi.persistence.spi.aggregate.*; import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; @@ -654,8 +653,9 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, .actionGet(); if (response.isExists()) { String sourceAsString = response.getSourceAsString(); - final T value = CustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); + final T value = ESCustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); value.setItemId(response.getId()); + value.setVersion(response.getVersion()); return value; } else { return null; @@ -663,10 +663,8 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, } } catch (IndexNotFoundException e) { throw new Exception("No index found for itemType=" + clazz.getName() + " itemId=" + itemId, e); - } catch (IllegalAccessException e) { - throw new Exception("Error loading itemType=" + clazz.getName() + " itemId=" + itemId, e); - } catch (Exception t) { - throw new Exception("Error loading itemType=" + clazz.getName() + " itemId=" + itemId, t); + } catch (Exception ex) { + throw new Exception("Error loading itemType=" + clazz.getName() + " itemId=" + itemId, ex); } } }.catchingExecuteInClassLoader(true); @@ -683,7 +681,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, Boolean result = new InClassLoaderExecute<Boolean>() { protected Boolean execute(Object... args) throws Exception { try { - String source = CustomObjectMapper.getObjectMapper().writeValueAsString(item); + String source = ESCustomObjectMapper.getObjectMapper().writeValueAsString(item); String itemType = item.getItemType(); String index = indexNames.containsKey(itemType) ? indexNames.get(itemType) : (itemsMonthlyIndexed.contains(itemType) ? getMonthlyIndex(((TimestampedItem) item).getTimeStamp()) : indexName); @@ -1341,6 +1339,7 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, } } SearchResponse response = requestBuilder + .setVersion(true) .execute() .actionGet(); if (size == -1) { @@ -1350,8 +1349,9 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, for (SearchHit searchHit : response.getHits().getHits()) { // add hit to results String sourceAsString = searchHit.getSourceAsString(); - final T value = CustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); + final T value = ESCustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); value.setItemId(searchHit.getId()); + value.setVersion(searchHit.getVersion()); results.add(value); } @@ -1369,8 +1369,9 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, totalHits = searchHits.getTotalHits(); for (SearchHit searchHit : searchHits) { String sourceAsString = searchHit.getSourceAsString(); - final T value = CustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); + final T value = ESCustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); value.setItemId(searchHit.getId()); + value.setVersion(searchHit.getVersion()); results.add(value); } } @@ -1406,8 +1407,9 @@ public class ElasticSearchPersistenceServiceImpl implements PersistenceService, for (SearchHit searchHit : response.getHits().getHits()) { // add hit to results String sourceAsString = searchHit.getSourceAsString(); - final T value = CustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); + final T value = ESCustomObjectMapper.getObjectMapper().readValue(sourceAsString, clazz); value.setItemId(searchHit.getId()); + value.setVersion(searchHit.getVersion()); results.add(value); } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/rest/src/main/java/org/apache/unomi/rest/LocalizationHelper.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/LocalizationHelper.java b/rest/src/main/java/org/apache/unomi/rest/LocalizationHelper.java index 1899f2a..880e16a 100644 --- a/rest/src/main/java/org/apache/unomi/rest/LocalizationHelper.java +++ b/rest/src/main/java/org/apache/unomi/rest/LocalizationHelper.java @@ -86,6 +86,7 @@ public class LocalizationHelper { public RESTConditionType generateCondition(ConditionType conditionType, String language) { RESTConditionType result = new RESTConditionType(); result.setId(conditionType.getItemId()); + result.setVersion(conditionType.getVersion()); result.setName(conditionType.getMetadata().getName()); result.setDescription(conditionType.getMetadata().getDescription()); @@ -109,6 +110,7 @@ public class LocalizationHelper { public RESTActionType generateAction(ActionType actionType, String language) { RESTActionType result = new RESTActionType(); result.setId(actionType.getItemId()); + result.setVersion(actionType.getVersion()); result.setName(actionType.getMetadata().getName()); result.setDescription(actionType.getMetadata().getDescription()); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java b/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java index b135adc..ef8a0f7 100644 --- a/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java +++ b/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java @@ -31,6 +31,7 @@ public class RESTActionType { private String description; private Set<String> tags; private List<RESTParameter> parameters; + protected Long version; public String getId() { return id; @@ -71,4 +72,12 @@ public class RESTActionType { public void setParameters(List<RESTParameter> parameters) { this.parameters = parameters; } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/de1ae00a/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java b/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java index 6ffce83..30aa497 100644 --- a/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java +++ b/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java @@ -30,6 +30,7 @@ public class RESTConditionType { private String description; private Set<String> tags = new LinkedHashSet<>(); private List<RESTParameter> parameters = new ArrayList<RESTParameter>(); + protected Long version; public RESTConditionType() { } @@ -73,4 +74,12 @@ public class RESTConditionType { public void setParameters(List<RESTParameter> parameters) { this.parameters = parameters; } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } }
