Skip to content

Commit 3b914a9

Browse files
authored
Merge pull request #2355 from atlanhq/plt-1891-struct-diff
[Staging] PLT-1891 -Diff calculation on entity diff improvement
2 parents 93c17b4 + 45c1708 commit 3b914a9

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

intg/src/main/java/org/apache/atlas/model/instance/AtlasStruct.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Objects;
34+
import java.util.ArrayList;
3435

3536
import javax.xml.bind.annotation.XmlAccessType;
3637
import javax.xml.bind.annotation.XmlAccessorType;
@@ -39,7 +40,9 @@
3940

4041
import org.apache.atlas.model.PList;
4142
import org.apache.atlas.model.SearchFilter.SortType;
43+
import org.apache.atlas.model.TypeCategory;
4244
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
45+
import org.apache.atlas.type.AtlasStructType;
4346
import org.apache.commons.collections.CollectionUtils;
4447
import org.apache.commons.collections.MapUtils;
4548

@@ -65,6 +68,14 @@ public class AtlasStruct implements Serializable {
6568
@Deprecated
6669
public static final DateFormat DATE_FORMATTER = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR);
6770

71+
private static final List<String> ALLOWED_DATATYPES_FOR_DEFAULT_NULL = new ArrayList() {
72+
{
73+
add("int");
74+
add("long");
75+
add("float");
76+
}
77+
};
78+
6879
private String typeName;
6980
private Map<String, Object> attributes;
7081

@@ -99,6 +110,22 @@ public AtlasStruct(Map map) {
99110
}
100111
}
101112

113+
public AtlasStruct(Map map, Map<String, AtlasStructType.AtlasAttribute> attributeTypes, boolean setDefaultValues) {
114+
if (map != null) {
115+
Object typeName = map.get(KEY_TYPENAME);
116+
Map attributes = (map.get(KEY_ATTRIBUTES) instanceof Map) ? (Map) map.get(KEY_ATTRIBUTES) : map;
117+
118+
if (typeName != null) {
119+
setTypeName(typeName.toString());
120+
}
121+
if (setDefaultValues) {
122+
setAttributes(new HashMap<>(attributes), attributeTypes);
123+
} else {
124+
setAttributes(new HashMap<>(attributes));
125+
}
126+
}
127+
}
128+
102129
public AtlasStruct(AtlasStruct other) {
103130
if (other != null) {
104131
setTypeName(other.getTypeName());
@@ -122,6 +149,35 @@ public void setAttributes(Map<String, Object> attributes) {
122149
this.attributes = attributes;
123150
}
124151

152+
public void setAttributes(Map<String, Object> attributes,
153+
Map<String, AtlasStructType.AtlasAttribute> attributeTypes) {
154+
if (MapUtils.isNotEmpty(attributeTypes) && MapUtils.isNotEmpty(attributes)) {
155+
this.attributes = new HashMap<>();
156+
for (Map.Entry<String, AtlasStructType.AtlasAttribute> entry : attributeTypes.entrySet()) {
157+
String attrName = entry.getKey();
158+
AtlasStructType.AtlasAttribute attrType = entry.getValue();
159+
Object attrValue = attributes.get(attrName);
160+
161+
if (attrType.getAttributeType().getTypeCategory() == TypeCategory.PRIMITIVE) {
162+
if (attrValue == null) {
163+
if (attrType.getAttributeDef().getDefaultValue() != null) {
164+
attrValue = attrType.getAttributeType().createDefaultValue();
165+
} else if (attrType.getAttributeDef().getIsDefaultValueNull()
166+
&& ALLOWED_DATATYPES_FOR_DEFAULT_NULL.contains(attrType.getAttributeType().getTypeName())) {
167+
attrValue = null;
168+
} else if (attrType.getAttributeDef().getIsOptional()) {
169+
attrValue = attrType.getAttributeType().createOptionalDefaultValue();
170+
} else {
171+
attrValue = attrType.getAttributeType().createDefaultValue();
172+
}
173+
}
174+
}
175+
176+
this.attributes.put(attrName, attrValue);
177+
}
178+
}
179+
}
180+
125181
public boolean hasAttribute(String name) {
126182
Map<String, Object> a = this.attributes;
127183

intg/src/main/java/org/apache/atlas/type/AtlasStructType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,12 @@ private AtlasStruct getStructFromValue(Object val) {
711711
if (val instanceof AtlasStruct) {
712712
ret = (AtlasStruct) val;
713713
} else if (val instanceof Map) {
714-
ret = new AtlasStruct((Map) val);
714+
ret = new AtlasStruct((Map) val, this.allAttributes, true);
715+
716+
if (StringUtils.isEmpty(ret.getTypeName()) && !StringUtils.isEmpty(this.getTypeName())) {
717+
ret.setTypeName(this.getTypeName());
718+
}
719+
715720
} else if (val instanceof String) {
716721
Map map = AtlasType.fromJson(val.toString(), Map.class);
717722

repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasEntityComparator.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.atlas.repository.store.graph.v2;
2020

2121
import org.apache.atlas.exception.AtlasBaseException;
22+
import org.apache.atlas.model.TypeCategory;
2223
import org.apache.atlas.model.instance.AtlasClassification;
2324
import org.apache.atlas.model.instance.AtlasEntity;
2425
import org.apache.atlas.repository.graphdb.AtlasVertex;
@@ -28,6 +29,7 @@
2829
import org.apache.atlas.utils.AtlasEntityUtil;
2930
import org.apache.commons.collections.MapUtils;
3031

32+
import java.util.ArrayList;
3133
import java.util.List;
3234
import java.util.Map;
3335
import java.util.Objects;
@@ -81,7 +83,28 @@ private AtlasEntityDiffResult getDiffResult(AtlasEntity updatedEntity, AtlasEnti
8183
continue;
8284
}
8385

84-
Object newVal = entry.getValue();
86+
TypeCategory category = attribute.getAttributeType().getTypeCategory();
87+
boolean isDefaultValueNotNull = !attribute.getAttributeDef().getIsDefaultValueNull();
88+
Object newVal;
89+
90+
if (entry.getValue() == null && isDefaultValueNotNull) {
91+
switch (category) {
92+
case PRIMITIVE:
93+
newVal = attribute.getAttributeType().createDefaultValue();
94+
break;
95+
case ARRAY:
96+
newVal = new ArrayList<>();
97+
break;
98+
case MAP:
99+
newVal = MapUtils.EMPTY_MAP;
100+
break;
101+
default:
102+
newVal = entry.getValue();
103+
}
104+
} else {
105+
newVal = entry.getValue();
106+
}
107+
85108
Object currVal = (storedEntity != null) ? storedEntity.getAttribute(attrName) : entityRetriever.getEntityAttribute(storedVertex, attribute);
86109

87110
if (!attribute.getAttributeType().areEqualValues(currVal, newVal, guidRefMap)) {

0 commit comments

Comments
 (0)