private boolean isValidRelationshipType(AtlasType attributeType) { boolean ret = false; if (attributeType != null) { if (attributeType instanceof AtlasArrayType) { attributeType = ((AtlasArrayType) attributeType).getElementType(); } if (attributeType instanceof AtlasObjectIdType || attributeType instanceof AtlasEntityType) { ret = true; } } return ret; }
AtlasEntityType getReferencedEntityType(AtlasType type) { if (type instanceof AtlasArrayType) { type = ((AtlasArrayType)type).getElementType(); } if (type instanceof AtlasMapType) { type = ((AtlasMapType)type).getValueType(); } return type instanceof AtlasEntityType ? (AtlasEntityType)type : null; }
AtlasEntityType getReferencedEntityType(AtlasType type) { if (type instanceof AtlasArrayType) { type = ((AtlasArrayType)type).getElementType(); } if (type instanceof AtlasMapType) { type = ((AtlasMapType)type).getValueType(); } return type instanceof AtlasEntityType ? (AtlasEntityType)type : null; }
@Override public Collection fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException { Collection ret = null; if (v2Obj != null) { if (v2Obj instanceof List) { ret = new ArrayList(); } else if (v2Obj instanceof Set) { ret = new LinkedHashSet(); } else { throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "List or Set", v2Obj.getClass().getCanonicalName()); } AtlasArrayType arrType = (AtlasArrayType) type; AtlasType elemType = arrType.getElementType(); AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory()); Collection v2List = (Collection) v2Obj; for (Object v2Elem : v2List) { Object convertedVal = elemConverter.fromV2ToV1(v2Elem, elemType, ctx); ret.add(convertedVal); } } return ret; } }
@Override public Collection fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException { Collection ret = null; if (v1Obj != null) { if (v1Obj instanceof Set) { ret = new LinkedHashSet(); } else { ret = new ArrayList(); } AtlasArrayType arrType = (AtlasArrayType) type; AtlasType elemType = arrType.getElementType(); AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory()); if (v1Obj instanceof Collection) { Collection v1List = (Collection) v1Obj; for (Object v1Elem : v1List) { Object convertedVal = elemConverter.fromV1ToV2(v1Elem, elemType, ctx); ret.add(convertedVal); } } else { Object convertedVal = elemConverter.fromV1ToV2(v1Obj, elemType, ctx); ret.add(convertedVal); } } return ret; }
@Override public Collection fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException { Collection ret = null; if (v1Obj != null) { if (v1Obj instanceof Set) { ret = new LinkedHashSet(); } else { ret = new ArrayList(); } AtlasArrayType arrType = (AtlasArrayType) type; AtlasType elemType = arrType.getElementType(); AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory()); if (v1Obj instanceof Collection) { Collection v1List = (Collection) v1Obj; for (Object v1Elem : v1List) { Object convertedVal = elemConverter.fromV1ToV2(v1Elem, elemType, ctx); ret.add(convertedVal); } } else { Object convertedVal = elemConverter.fromV1ToV2(v1Obj, elemType, ctx); ret.add(convertedVal); } } return ret; }
private List<Object> mapVertexToArray(AtlasVertex entityVertex, AtlasEntityExtInfo entityExtInfo, boolean isOwnedAttribute, AtlasAttribute attribute, final boolean isMinExtInfo) throws AtlasBaseException { AtlasArrayType arrayType = (AtlasArrayType) attribute.getAttributeType(); AtlasType arrayElementType = arrayType.getElementType(); List<Object> arrayElements = getArrayElementsProperty(arrayElementType, entityVertex, attribute); if (CollectionUtils.isEmpty(arrayElements)) { return null; } if (LOG.isDebugEnabled()) { LOG.debug("Mapping array attribute {} for vertex {}", arrayElementType.getTypeName(), entityVertex); } List arrValues = new ArrayList(arrayElements.size()); String edgeLabel = attribute.getRelationshipEdgeLabel(); AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection(); for (Object element : arrayElements) { // When internal types are deleted, sometimes the collection type attribute will contain a null value // Graph layer does erroneous mapping of the null element, hence avoiding the processing of the null element if (element == null) { LOG.debug("Skipping null arrayElement"); continue; } Object arrValue = mapVertexToCollectionEntry(entityVertex, arrayElementType, element, edgeLabel, entityExtInfo, isOwnedAttribute, edgeDirection, isMinExtInfo); if (arrValue != null) { arrValues.add(arrValue); } } return arrValues; }
private List<Object> mapVertexToArray(AtlasVertex entityVertex, AtlasEntityExtInfo entityExtInfo, boolean isOwnedAttribute, AtlasAttribute attribute, final boolean isMinExtInfo) throws AtlasBaseException { AtlasArrayType arrayType = (AtlasArrayType) attribute.getAttributeType(); AtlasType arrayElementType = arrayType.getElementType(); List<Object> arrayElements = getArrayElementsProperty(arrayElementType, entityVertex, attribute); if (CollectionUtils.isEmpty(arrayElements)) { return null; } if (LOG.isDebugEnabled()) { LOG.debug("Mapping array attribute {} for vertex {}", arrayElementType.getTypeName(), entityVertex); } List arrValues = new ArrayList(arrayElements.size()); String edgeLabel = attribute.getRelationshipEdgeLabel(); AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection(); for (Object element : arrayElements) { // When internal types are deleted, sometimes the collection type attribute will contain a null value // Graph layer does erroneous mapping of the null element, hence avoiding the processing of the null element if (element == null) { LOG.debug("Skipping null arrayElement"); continue; } Object arrValue = mapVertexToCollectionEntry(entityVertex, arrayElementType, element, edgeLabel, entityExtInfo, isOwnedAttribute, edgeDirection, isMinExtInfo); if (arrValue != null) { arrValues.add(arrValue); } } return arrValues; }
private List<AtlasEdge> removeUnusedArrayEntries(AtlasAttribute attribute, List<AtlasEdge> currentEntries, List<AtlasEdge> newEntries, AtlasVertex entityVertex) throws AtlasBaseException { if (CollectionUtils.isNotEmpty(currentEntries)) { AtlasType entryType = ((AtlasArrayType) attribute.getAttributeType()).getElementType(); if (isReference(entryType)) { Collection<AtlasEdge> edgesToRemove = CollectionUtils.subtract(currentEntries, newEntries); if (CollectionUtils.isNotEmpty(edgesToRemove)) { List<AtlasEdge> additionalElements = new ArrayList<>(); for (AtlasEdge edge : edgesToRemove) { boolean deleted = deleteHandler.deleteEdgeReference(edge, entryType.getTypeCategory(), attribute.isOwnedRef(), true, attribute.getRelationshipEdgeDirection(), entityVertex); if (!deleted) { additionalElements.add(edge); } } return additionalElements; } } } return Collections.emptyList(); } private void setArrayElementsProperty(AtlasType elementType, AtlasVertex vertex, String vertexPropertyName, List<Object> values) {
private List<AtlasEdge> removeUnusedArrayEntries(AtlasAttribute attribute, List<AtlasEdge> currentEntries, List<AtlasEdge> newEntries, AtlasVertex entityVertex) throws AtlasBaseException { if (CollectionUtils.isNotEmpty(currentEntries)) { AtlasType entryType = ((AtlasArrayType) attribute.getAttributeType()).getElementType(); if (isReference(entryType)) { Collection<AtlasEdge> edgesToRemove = CollectionUtils.subtract(currentEntries, newEntries); if (CollectionUtils.isNotEmpty(edgesToRemove)) { List<AtlasEdge> additionalElements = new ArrayList<>(); for (AtlasEdge edge : edgesToRemove) { boolean deleted = deleteDelegate.getHandler().deleteEdgeReference(edge, entryType.getTypeCategory(), attribute.isOwnedRef(), true, attribute.getRelationshipEdgeDirection(), entityVertex); if (!deleted) { additionalElements.add(edge); } } return additionalElements; } } } return Collections.emptyList(); } private void setArrayElementsProperty(AtlasType elementType, boolean isSoftReference, AtlasVertex vertex, String vertexPropertyName, List<Object> values) {
private boolean isRelationshipAttribute(AtlasAttribute attribute) throws AtlasBaseException { boolean ret = true; AtlasType attrType = attribute.getAttributeType(); if (attrType.getTypeCategory() == ARRAY) { attrType = ((AtlasArrayType) attrType).getElementType(); } else if (attrType.getTypeCategory() == MAP) { attrType = ((AtlasMapType) attrType).getValueType(); } if (attrType.getTypeCategory() != OBJECT_ID_TYPE) { ret = false; } return ret; }
private static void addIfCollectionAttribute(AtlasAttribute attr, Map<String, List<String>> collectionProperties) { AtlasType attrType = attr.getAttributeType(); TypeCategory attrTypeCategory = attrType.getTypeCategory(); switch (attrTypeCategory) { case ARRAY: { TypeCategory arrayElementType = ((AtlasArrayType) attrType).getElementType().getTypeCategory(); if (nonPrimitives.contains(arrayElementType)) { addVertexProperty(attrTypeCategory.toString(), attr.getVertexPropertyName(), collectionProperties); } } break; case MAP: { TypeCategory mapValueType = ((AtlasMapType) attrType).getValueType().getTypeCategory(); if (nonPrimitives.contains(mapValueType)) { addVertexProperty(attrTypeCategory.toString(), attr.getVertexPropertyName(), collectionProperties); } else { addVertexProperty(attrTypeCategory.toString() + "_PRIMITIVE", attr.getVertexPropertyName(), collectionProperties); } } break; } }
private void addType(AtlasType type, ExportContext context) { if (type.getTypeCategory() == TypeCategory.PRIMITIVE) { return; } if (type instanceof AtlasArrayType) { AtlasArrayType arrayType = (AtlasArrayType)type; addType(arrayType.getElementType(), context); } else if (type instanceof AtlasMapType) { AtlasMapType mapType = (AtlasMapType)type; addType(mapType.getKeyType(), context); addType(mapType.getValueType(), context); } else if (type instanceof AtlasEntityType) { addEntityType((AtlasEntityType)type, context); } else if (type instanceof AtlasClassificationType) { addClassificationType((AtlasClassificationType)type, context); } else if (type instanceof AtlasStructType) { addStructType((AtlasStructType)type, context); } else if (type instanceof AtlasEnumType) { addEnumType((AtlasEnumType)type, context); } }
@Override public String getTypeFromEdge(GremlinQueryComposer.Context context, String item) { AtlasEntityType et = context.getActiveEntityType(); if(et == null) { return ""; } AtlasStructType.AtlasAttribute attr = et.getAttribute(item); if(attr == null) { return null; } AtlasType at = attr.getAttributeType(); switch (at.getTypeCategory()) { case ARRAY: AtlasArrayType arrType = ((AtlasArrayType)at); return getCollectionElementType(arrType.getElementType()); case MAP: AtlasMapType mapType = ((AtlasMapType)at); return getCollectionElementType(mapType.getValueType()); } return context.getActiveEntityType().getAttribute(item).getTypeName(); }
@Override public String getTypeFromEdge(GremlinQueryComposer.Context context, String item) { AtlasEntityType et = context.getActiveEntityType(); if(et == null) { return ""; } AtlasStructType.AtlasAttribute attr = et.getAttribute(item); if(attr == null) { return null; } AtlasType at = attr.getAttributeType(); switch (at.getTypeCategory()) { case ARRAY: AtlasArrayType arrType = ((AtlasArrayType)at); return getCollectionElementType(arrType.getElementType()); case MAP: AtlasMapType mapType = ((AtlasMapType)at); return getCollectionElementType(mapType.getValueType()); } return context.getActiveEntityType().getAttribute(item).getTypeName(); }
@Override public boolean isPrimitive(GremlinQueryComposer.Context context, String attributeName) { AtlasEntityType et = context.getActiveEntityType(); if(et == null) { return false; } if(isSystemAttribute(attributeName)) { return true; } AtlasType at = et.getAttributeType(attributeName); if(at == null) { return false; } TypeCategory tc = at.getTypeCategory(); if (isPrimitiveUsingTypeCategory(tc)) return true; if ((tc != null) && (tc == TypeCategory.ARRAY)) { AtlasArrayType ct = ((AtlasArrayType)at); return isPrimitiveUsingTypeCategory(ct.getElementType().getTypeCategory()); } if ((tc != null) && (tc == TypeCategory.MAP)) { AtlasMapType ct = ((AtlasMapType)at); return isPrimitiveUsingTypeCategory(ct.getValueType().getTypeCategory()); } return false; }
@Override public boolean isPrimitive(GremlinQueryComposer.Context context, String attributeName) { AtlasEntityType et = context.getActiveEntityType(); if(et == null) { return false; } if(isSystemAttribute(attributeName)) { return true; } AtlasType at = et.getAttributeType(attributeName); if(at == null) { return false; } TypeCategory tc = at.getTypeCategory(); if (isPrimitiveUsingTypeCategory(tc)) return true; if ((tc != null) && (tc == TypeCategory.ARRAY)) { AtlasArrayType ct = ((AtlasArrayType)at); return isPrimitiveUsingTypeCategory(ct.getElementType().getTypeCategory()); } if ((tc != null) && (tc == TypeCategory.MAP)) { AtlasMapType ct = ((AtlasMapType)at); return isPrimitiveUsingTypeCategory(ct.getValueType().getTypeCategory()); } return false; }
void visitAttribute(AtlasType attrType, Object val) throws AtlasBaseException { if (attrType == null || val == null) { return; } switch (attrType.getTypeCategory()) { case PRIMITIVE: case ENUM: return; case ARRAY: { AtlasArrayType arrayType = (AtlasArrayType) attrType; AtlasType elemType = arrayType.getElementType(); visitCollectionReferences(elemType, val); } break; case MAP: { AtlasType keyType = ((AtlasMapType) attrType).getKeyType(); AtlasType valueType = ((AtlasMapType) attrType).getValueType(); visitMapReferences(keyType, valueType, val); } break; case STRUCT: visitStruct((AtlasStructType)attrType, val); break; case OBJECT_ID_TYPE: visitReference((AtlasObjectIdType) attrType, val); break; default: throw new AtlasBaseException(AtlasErrorCode.TYPE_CATEGORY_INVALID, attrType.getTypeCategory().name()); } }
private void addType(AtlasType type, ExportService.ExportContext context) { if (type.getTypeCategory() == TypeCategory.PRIMITIVE) { return; } if (type instanceof AtlasArrayType) { AtlasArrayType arrayType = (AtlasArrayType)type; addType(arrayType.getElementType(), context); } else if (type instanceof AtlasMapType) { AtlasMapType mapType = (AtlasMapType)type; addType(mapType.getKeyType(), context); addType(mapType.getValueType(), context); } else if (type instanceof AtlasEntityType) { addEntityType((AtlasEntityType)type, context); } else if (type instanceof AtlasClassificationType) { addClassificationType((AtlasClassificationType)type, context); } else if (type instanceof AtlasStructType) { addStructType((AtlasStructType)type, context); } else if (type instanceof AtlasEnumType) { addEnumType((AtlasEnumType)type, context); } else if (type instanceof AtlasRelationshipType) { addRelationshipType((AtlasRelationshipType)type, context); } }
void visitAttribute(AtlasType attrType, Object val) throws AtlasBaseException { if (attrType == null || val == null) { return; } switch (attrType.getTypeCategory()) { case PRIMITIVE: case ENUM: return; case ARRAY: { AtlasArrayType arrayType = (AtlasArrayType) attrType; AtlasType elemType = arrayType.getElementType(); visitCollectionReferences(elemType, val); } break; case MAP: { AtlasType keyType = ((AtlasMapType) attrType).getKeyType(); AtlasType valueType = ((AtlasMapType) attrType).getValueType(); visitMapReferences(keyType, valueType, val); } break; case STRUCT: visitStruct((AtlasStructType)attrType, val); break; case OBJECT_ID_TYPE: visitReference((AtlasObjectIdType) attrType, val); break; default: throw new AtlasBaseException(AtlasErrorCode.TYPE_CATEGORY_INVALID, attrType.getTypeCategory().name()); } }