Commit f5cd728d by Madhan Neethiraj

ATLAS-3700: added option to append value to array, map type attributes

parent 521118c8
...@@ -209,6 +209,11 @@ public class EntityMutationResponse { ...@@ -209,6 +209,11 @@ public class EntityMutationResponse {
} }
@JsonIgnore @JsonIgnore
public AtlasEntityHeader getFirstPartialUpdatedEntityByTypeName(String typeName) {
return getFirstEntityByType(getEntitiesByOperation(EntityOperation.PARTIAL_UPDATE), typeName);
}
@JsonIgnore
public void addEntity(EntityOperation op, AtlasEntityHeader header) { public void addEntity(EntityOperation op, AtlasEntityHeader header) {
// if an entity is already included in CREATE, ignore subsequent UPDATE, PARTIAL_UPDATE // if an entity is already included in CREATE, ignore subsequent UPDATE, PARTIAL_UPDATE
if (op == EntityOperation.UPDATE || op == EntityOperation.PARTIAL_UPDATE) { if (op == EntityOperation.UPDATE || op == EntityOperation.PARTIAL_UPDATE) {
......
...@@ -263,10 +263,11 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable { ...@@ -263,10 +263,11 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable {
public static final int DEFAULT_SEARCHWEIGHT = -1; public static final int DEFAULT_SEARCHWEIGHT = -1;
public static final String SEARCH_WEIGHT_ATTR_NAME = "searchWeight"; public static final String SEARCH_WEIGHT_ATTR_NAME = "searchWeight";
public static final String INDEX_TYPE_ATTR_NAME = "indexType"; public static final String INDEX_TYPE_ATTR_NAME = "indexType";
public static final String ATTRDEF_OPTION_SOFT_REFERENCE = "isSoftReference"; public static final String ATTRDEF_OPTION_SOFT_REFERENCE = "isSoftReference";
private final String STRING_TRUE = "true"; public static final String ATTRDEF_OPTION_APPEND_ON_PARTIAL_UPDATE = "isAppendOnPartialUpdate";
private final String STRING_TRUE = "true";
/** /**
* single-valued attribute or multi-valued attribute. * single-valued attribute or multi-valued attribute.
...@@ -520,6 +521,13 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable { ...@@ -520,6 +521,13 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable {
} }
@JsonIgnore @JsonIgnore
public boolean isAppendOnPartialUpdate() {
String val = getOption(AtlasAttributeDef.ATTRDEF_OPTION_APPEND_ON_PARTIAL_UPDATE);
return val != null && Boolean.valueOf(val);
}
@JsonIgnore
public void setOption(String name, String value) { public void setOption(String name, String value) {
if (this.options == null) { if (this.options == null) {
this.options = new HashMap<>(); this.options = new HashMap<>();
......
...@@ -627,6 +627,13 @@ public final class TestUtilsV2 { ...@@ -627,6 +627,13 @@ public final class TestUtilsV2 {
} }
public static AtlasTypesDef defineSimpleAttrType() { public static AtlasTypesDef defineSimpleAttrType() {
AtlasAttributeDef attrPuArray = new AtlasAttributeDef("puArray", "array<string>", true, SINGLE, 1, 1, false, false, false, null);
AtlasAttributeDef attrPuMap = new AtlasAttributeDef("puMap", "map<string,string>", true, SINGLE, 1,1, false, false, false, null);
attrPuArray.setOption(AtlasAttributeDef.ATTRDEF_OPTION_APPEND_ON_PARTIAL_UPDATE, "true");
attrPuMap.setOption(AtlasAttributeDef.ATTRDEF_OPTION_APPEND_ON_PARTIAL_UPDATE, "true");
AtlasEntityDef simpleAttributesEntityType = AtlasEntityDef simpleAttributesEntityType =
createClassTypeDef(ENTITY_TYPE_WITH_SIMPLE_ATTR, ENTITY_TYPE_WITH_SIMPLE_ATTR + "_description", null, createClassTypeDef(ENTITY_TYPE_WITH_SIMPLE_ATTR, ENTITY_TYPE_WITH_SIMPLE_ATTR + "_description", null,
createUniqueRequiredAttrDef("name", "string"), createUniqueRequiredAttrDef("name", "string"),
...@@ -641,7 +648,11 @@ public final class TestUtilsV2 { ...@@ -641,7 +648,11 @@ public final class TestUtilsV2 {
false, false, false, null), false, false, false, null),
new AtlasAttributeDef("mapOfStrings", "map<string,string>", new AtlasAttributeDef("mapOfStrings", "map<string,string>",
true, SINGLE, 1,1, false, false, false, null) true, SINGLE, 1,1, false, false, false, null),
attrPuArray,
attrPuMap
); );
AtlasTypesDef ret = AtlasTypeUtil.getTypesDef(Collections.<AtlasEnumDef>emptyList(), AtlasTypesDef ret = AtlasTypeUtil.getTypesDef(Collections.<AtlasEnumDef>emptyList(),
...@@ -659,6 +670,8 @@ public final class TestUtilsV2 { ...@@ -659,6 +670,8 @@ public final class TestUtilsV2 {
entity.setAttribute("stringAtrr", "DummyThree"); entity.setAttribute("stringAtrr", "DummyThree");
entity.setAttribute("arrayOfStrings", Arrays.asList("DummyOne", "DummyTwo")); entity.setAttribute("arrayOfStrings", Arrays.asList("DummyOne", "DummyTwo"));
entity.setAttribute("mapOfStrings", Collections.singletonMap("one", "DummyString")); entity.setAttribute("mapOfStrings", Collections.singletonMap("one", "DummyString"));
entity.setAttribute("puArray", Arrays.asList("DummyOne", "DummyTwo"));
entity.setAttribute("puMap", Collections.singletonMap("one", "DummyString"));
return new AtlasEntityWithExtInfo(entity); return new AtlasEntityWithExtInfo(entity);
} }
......
...@@ -284,6 +284,8 @@ public class EntityGraphMapper { ...@@ -284,6 +284,8 @@ public class EntityGraphMapper {
} }
} }
EntityOperation updateType = isPartialUpdate ? PARTIAL_UPDATE : UPDATE;
if (CollectionUtils.isNotEmpty(updatedEntities)) { if (CollectionUtils.isNotEmpty(updatedEntities)) {
for (AtlasEntity updatedEntity : updatedEntities) { for (AtlasEntity updatedEntity : updatedEntities) {
String guid = updatedEntity.getGuid(); String guid = updatedEntity.getGuid();
...@@ -292,14 +294,10 @@ public class EntityGraphMapper { ...@@ -292,14 +294,10 @@ public class EntityGraphMapper {
mapRelationshipAttributes(updatedEntity, entityType, vertex, UPDATE, context); mapRelationshipAttributes(updatedEntity, entityType, vertex, UPDATE, context);
mapAttributes(updatedEntity, entityType, vertex, UPDATE, context); mapAttributes(updatedEntity, entityType, vertex, updateType, context);
setCustomAttributes(vertex,updatedEntity); setCustomAttributes(vertex,updatedEntity);
if (isPartialUpdate) { resp.addEntity(updateType, constructHeader(updatedEntity, entityType, vertex));
resp.addEntity(PARTIAL_UPDATE, constructHeader(updatedEntity, entityType, vertex));
} else {
resp.addEntity(UPDATE, constructHeader(updatedEntity, entityType, vertex));
}
if (replaceClassifications) { if (replaceClassifications) {
deleteClassifications(guid); deleteClassifications(guid);
...@@ -325,12 +323,7 @@ public class EntityGraphMapper { ...@@ -325,12 +323,7 @@ public class EntityGraphMapper {
} }
for (AtlasEntityHeader entity : req.getUpdatedEntities()) { for (AtlasEntityHeader entity : req.getUpdatedEntities()) {
if (isPartialUpdate) { resp.addEntity(updateType, entity);
resp.addEntity(PARTIAL_UPDATE, entity);
}
else {
resp.addEntity(UPDATE, entity);
}
} }
RequestContext.get().endMetricRecord(metric); RequestContext.get().endMetricRecord(metric);
...@@ -623,7 +616,7 @@ public class EntityGraphMapper { ...@@ -623,7 +616,7 @@ public class EntityGraphMapper {
mapAttribute(attribute, attrValue, vertex, op, context); mapAttribute(attribute, attrValue, vertex, op, context);
} }
} else if (op.equals(UPDATE)) { } else if (op.equals(UPDATE) || op.equals(PARTIAL_UPDATE)) {
for (String attrName : struct.getAttributes().keySet()) { for (String attrName : struct.getAttributes().keySet()) {
AtlasAttribute attribute = structType.getAttribute(attrName); AtlasAttribute attribute = structType.getAttribute(attrName);
...@@ -665,7 +658,7 @@ public class EntityGraphMapper { ...@@ -665,7 +658,7 @@ public class EntityGraphMapper {
mapAttribute(attribute, attrValue, vertex, op, context); mapAttribute(attribute, attrValue, vertex, op, context);
} }
} else if (op.equals(UPDATE)) { } else if (op.equals(UPDATE) || op.equals(PARTIAL_UPDATE)) {
// relationship attributes mapping // relationship attributes mapping
for (String attrName : entityType.getRelationshipAttributes().keySet()) { for (String attrName : entityType.getRelationshipAttributes().keySet()) {
if (entity.hasRelationshipAttribute(attrName)) { if (entity.hasRelationshipAttribute(attrName)) {
...@@ -1251,6 +1244,22 @@ public class EntityGraphMapper { ...@@ -1251,6 +1244,22 @@ public class EntityGraphMapper {
boolean isReference = isReference(mapType.getValueType()); boolean isReference = isReference(mapType.getValueType());
boolean isSoftReference = ctx.getAttribute().getAttributeDef().isSoftReferenced(); boolean isSoftReference = ctx.getAttribute().getAttributeDef().isSoftReferenced();
if (PARTIAL_UPDATE.equals(ctx.getOp()) && attribute.getAttributeDef().isAppendOnPartialUpdate() && MapUtils.isNotEmpty(currentMap)) {
if (MapUtils.isEmpty(newVal)) {
newVal = new HashMap<>(currentMap);
} else {
Map<Object, Object> mergedVal = new HashMap<>(currentMap);
for (Map.Entry<Object, Object> entry : newVal.entrySet()) {
String newKey = entry.getKey().toString();
mergedVal.put(newKey, entry.getValue());
}
newVal = mergedVal;
}
}
boolean isNewValNull = newVal == null; boolean isNewValNull = newVal == null;
if (isNewValNull) { if (isNewValNull) {
...@@ -1334,7 +1343,6 @@ public class EntityGraphMapper { ...@@ -1334,7 +1343,6 @@ public class EntityGraphMapper {
Cardinality cardinality = attribute.getAttributeDef().getCardinality(); Cardinality cardinality = attribute.getAttributeDef().getCardinality();
List<Object> newElementsCreated = new ArrayList<>(); List<Object> newElementsCreated = new ArrayList<>();
List<Object> currentElements; List<Object> currentElements;
boolean isNewElementsNull = newElements == null;
if (isReference && !isSoftReference) { if (isReference && !isSoftReference) {
currentElements = (List) getCollectionElementsUsingRelationship(ctx.getReferringVertex(), attribute); currentElements = (List) getCollectionElementsUsingRelationship(ctx.getReferringVertex(), attribute);
...@@ -1342,6 +1350,20 @@ public class EntityGraphMapper { ...@@ -1342,6 +1350,20 @@ public class EntityGraphMapper {
currentElements = (List) getArrayElementsProperty(elementType, isSoftReference, ctx.getReferringVertex(), ctx.getVertexProperty()); currentElements = (List) getArrayElementsProperty(elementType, isSoftReference, ctx.getReferringVertex(), ctx.getVertexProperty());
} }
if (PARTIAL_UPDATE.equals(ctx.getOp()) && attribute.getAttributeDef().isAppendOnPartialUpdate() && CollectionUtils.isNotEmpty(currentElements)) {
if (CollectionUtils.isEmpty(newElements)) {
newElements = new ArrayList<>(currentElements);
} else {
List<Object> mergedVal = new ArrayList<>(currentElements);
mergedVal.addAll(newElements);
newElements = mergedVal;
}
}
boolean isNewElementsNull = newElements == null;
if (isNewElementsNull) { if (isNewElementsNull) {
newElements = new ArrayList(); newElements = new ArrayList();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment