Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync staging with master #2365

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ArrayList;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand All @@ -39,7 +40,9 @@

import org.apache.atlas.model.PList;
import org.apache.atlas.model.SearchFilter.SortType;
import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.type.AtlasStructType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

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

private static final List<String> ALLOWED_DATATYPES_FOR_DEFAULT_NULL = new ArrayList() {
{
add("int");
add("long");
add("float");
}
};

private String typeName;
private Map<String, Object> attributes;

Expand Down Expand Up @@ -99,6 +110,22 @@ public AtlasStruct(Map map) {
}
}

public AtlasStruct(Map map, Map<String, AtlasStructType.AtlasAttribute> attributeTypes, boolean setDefaultValues) {
if (map != null) {
Object typeName = map.get(KEY_TYPENAME);
Map attributes = (map.get(KEY_ATTRIBUTES) instanceof Map) ? (Map) map.get(KEY_ATTRIBUTES) : map;

if (typeName != null) {
setTypeName(typeName.toString());
}
if (setDefaultValues) {
setAttributes(new HashMap<>(attributes), attributeTypes);
} else {
setAttributes(new HashMap<>(attributes));
}
}
}

public AtlasStruct(AtlasStruct other) {
if (other != null) {
setTypeName(other.getTypeName());
Expand All @@ -122,6 +149,35 @@ public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}

public void setAttributes(Map<String, Object> attributes,
Map<String, AtlasStructType.AtlasAttribute> attributeTypes) {
if (MapUtils.isNotEmpty(attributeTypes) && MapUtils.isNotEmpty(attributes)) {
this.attributes = new HashMap<>();
for (Map.Entry<String, AtlasStructType.AtlasAttribute> entry : attributeTypes.entrySet()) {
String attrName = entry.getKey();
AtlasStructType.AtlasAttribute attrType = entry.getValue();
Object attrValue = attributes.get(attrName);

if (attrType.getAttributeType().getTypeCategory() == TypeCategory.PRIMITIVE) {
if (attrValue == null) {
if (attrType.getAttributeDef().getDefaultValue() != null) {
attrValue = attrType.getAttributeType().createDefaultValue();
} else if (attrType.getAttributeDef().getIsDefaultValueNull()
&& ALLOWED_DATATYPES_FOR_DEFAULT_NULL.contains(attrType.getAttributeType().getTypeName())) {
attrValue = null;
} else if (attrType.getAttributeDef().getIsOptional()) {
attrValue = attrType.getAttributeType().createOptionalDefaultValue();
} else {
attrValue = attrType.getAttributeType().createDefaultValue();
}
}
}

this.attributes.put(attrName, attrValue);
}
}
}

public boolean hasAttribute(String name) {
Map<String, Object> a = this.attributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,12 @@ private AtlasStruct getStructFromValue(Object val) {
if (val instanceof AtlasStruct) {
ret = (AtlasStruct) val;
} else if (val instanceof Map) {
ret = new AtlasStruct((Map) val);
ret = new AtlasStruct((Map) val, this.allAttributes, true);

if (StringUtils.isEmpty(ret.getTypeName()) && !StringUtils.isEmpty(this.getTypeName())) {
ret.setTypeName(this.getTypeName());
}

} else if (val instanceof String) {
Map map = AtlasType.fromJson(val.toString(), Map.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.atlas.repository.store.graph.v2;

import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.repository.graphdb.AtlasVertex;
Expand All @@ -28,6 +29,7 @@
import org.apache.atlas.utils.AtlasEntityUtil;
import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -81,7 +83,28 @@ private AtlasEntityDiffResult getDiffResult(AtlasEntity updatedEntity, AtlasEnti
continue;
}

Object newVal = entry.getValue();
TypeCategory category = attribute.getAttributeType().getTypeCategory();
boolean isDefaultValueNotNull = !attribute.getAttributeDef().getIsDefaultValueNull();
Object newVal;

if (entry.getValue() == null && isDefaultValueNotNull) {
switch (category) {
case PRIMITIVE:
newVal = attribute.getAttributeType().createDefaultValue();
break;
case ARRAY:
newVal = new ArrayList<>();
break;
case MAP:
newVal = MapUtils.EMPTY_MAP;
break;
default:
newVal = entry.getValue();
}
} else {
newVal = entry.getValue();
}

Object currVal = (storedEntity != null) ? storedEntity.getAttribute(attrName) : entityRetriever.getEntityAttribute(storedVertex, attribute);

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