/** * For the given type, finds an unique attribute and checks if there is an existing instance with the same * unique value * * @param classType * @param instance * @return * @throws AtlasException */ public AtlasVertex getVertexForInstanceByUniqueAttribute(AtlasEntityType classType, Referenceable instance) throws AtlasException { if (LOG.isDebugEnabled()) { LOG.debug("Checking if there is an instance with the same unique attributes for instance {}", instance.toShortString()); } AtlasVertex result = null; for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) { String propertyKey = attributeInfo.getQualifiedName(); try { result = findVertex(propertyKey, instance.get(attributeInfo.getName()), ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName(), STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); if (LOG.isDebugEnabled()) { LOG.debug("Found vertex by unique attribute : {}={}", propertyKey, instance.get(attributeInfo.getName())); } } catch (EntityNotFoundException e) { //Its ok if there is no entity with the same unique value } } return result; }
private static String getEncodedTypePropertyKey(String defaultKey) { try { Configuration configuration = ApplicationProperties.get(); if (configuration.containsKey("atlas.graph.index.search.backend") && configuration.getString("atlas.graph.index.search.backend").equals("elasticsearch")) { return defaultKey.replaceAll("\\.", "_"); } return encodePropertyKey(defaultKey); } catch (AtlasException e) { return encodePropertyKey(defaultKey); } } }
@VisibleForTesting public static String toJsonFromAttribute(AtlasAttribute attribute) { AtlasAttributeDef attributeDef = attribute.getAttributeDef(); Map<String, Object> attribInfo = new HashMap<>(); attribInfo.put("isIndexable", attributeDef.getIsIndexable()); attribInfo.put("includeInNotification", attributeDef.getIncludeInNotification()); attribInfo.put("isComposite", attribute.isOwnedRef()); attribInfo.put("reverseAttributeName", attribute.getInverseRefAttributeName()); attribInfo.put("defaultValue", attributeDef.getDefaultValue()); attribInfo.put("description", attributeDef.getDescription());
AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection(); String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel(); if (ctx.getAttribute().getInverseRefAttribute() != null) { addInverseReference(context, ctx.getAttribute().getInverseRefAttribute(), newEdge, getRelationshipAttributes(ctx.getValue())); deleteHandler.deleteEdgeReference(currentEdge, ctx.getAttrType().getTypeCategory(), ctx.getAttribute().isOwnedRef(), true, ctx.getAttribute().getRelationshipEdgeDirection(), ctx.getReferringVertex());
AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection(); String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel(); if (ctx.getAttribute().getInverseRefAttribute() != null) { addInverseReference(context, ctx.getAttribute().getInverseRefAttribute(), newEdge, getRelationshipAttributes(ctx.getValue())); deleteDelegate.getHandler().deleteEdgeReference(currentEdge, ctx.getAttrType().getTypeCategory(), ctx.getAttribute().isOwnedRef(), true, ctx.getAttribute().getRelationshipEdgeDirection(), ctx.getReferringVertex());
AtlasArrayType arrType = (AtlasArrayType) attribute.getAttributeType(); AtlasType elementType = arrType.getElementType(); boolean isReference = isReference(elementType); boolean isSoftReference = ctx.getAttribute().getAttributeDef().isSoftReferenced(); AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute(); Cardinality cardinality = attribute.getAttributeDef().getCardinality(); List<Object> newElementsCreated = new ArrayList<>(); List<Object> currentElements;
AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
Map<String, Object> currentMap = getMapElementsProperty(mapType, ctx.getReferringVertex(), ctx.getVertexProperty(), attribute); boolean isReference = isReference(mapType.getValueType()); boolean isSoftReference = ctx.getAttribute().getAttributeDef().isSoftReferenced(); AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
String encodedStructDefPropertyKey = encodePropertyKey(structDefPropertyKey); List<String> currAttrNames = vertex.getProperty(encodedStructDefPropertyKey, List.class);
AtlasArrayType arrType = (AtlasArrayType) attribute.getAttributeType(); AtlasType elementType = arrType.getElementType(); boolean isReference = isReference(elementType); AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute(); Cardinality cardinality = attribute.getAttributeDef().getCardinality(); List<Object> newElementsCreated = new ArrayList<>(); List<Object> currentElements;
private Object mapVertexToAttribute(AtlasVertex entityVertex, AtlasAttribute attribute, AtlasEntityExtInfo entityExtInfo, final boolean isMinExtInfo) throws AtlasBaseException { Object ret = null; AtlasType attrType = attribute.getAttributeType(); String edgeLabel = EDGE_LABEL_PREFIX + attribute.getQualifiedName(); boolean isOwnedAttribute = attribute.isOwnedRef(); AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection(); LOG.debug("Mapping vertex {} to atlas entity {}.{}", entityVertex, attribute.getDefinedInDef().getName(), attribute.getName()); ret = mapVertexToPrimitive(entityVertex, attribute.getVertexPropertyName(), attribute.getAttributeDef()); break; case ENUM: ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, attribute.getVertexPropertyName(), Object.class); break; case STRUCT: break; case OBJECT_ID_TYPE: if(attribute.getAttributeDef().isSoftReferenced()) { ret = mapVertexToObjectIdForSoftRef(entityVertex, attribute, entityExtInfo, isMinExtInfo); } else { if(attribute.getAttributeDef().isSoftReferenced()) { ret = mapVertexToArrayForSoftRef(entityVertex, attribute, entityExtInfo, isMinExtInfo); } else { if(attribute.getAttributeDef().isSoftReferenced()) { ret = mapVertexToMapForSoftRef(entityVertex, attribute, entityExtInfo, isMinExtInfo); } else {
LOG.warn("Length of indexed attribute {} is {} characters, longer than safe-limit {}; trimming to {} - attempt #{}", ctx.getAttribute().getQualifiedName(), value.length(), INDEXED_STR_SAFE_LEN, trimmedLength, requestContext.getAttemptCount()); LOG.warn("Length of indexed attribute {} is {} characters, longer than safe-limit {}", ctx.getAttribute().getQualifiedName(), value.length(), INDEXED_STR_SAFE_LEN);
@VisibleForTesting public static String toJsonFromAttribute(AtlasAttribute attribute) { AtlasAttributeDef attributeDef = attribute.getAttributeDef(); Map<String, Object> attribInfo = new HashMap<>(); attribInfo.put("isIndexable", attributeDef.getIsIndexable()); attribInfo.put("includeInNotification", attributeDef.getIncludeInNotification()); attribInfo.put("isComposite", attribute.isOwnedRef()); attribInfo.put("reverseAttributeName", attribute.getInverseRefAttributeName()); attribInfo.put("defaultValue", attributeDef.getDefaultValue()); attribInfo.put("description", attributeDef.getDescription());
private Object mapVertexToAttribute(AtlasVertex entityVertex, AtlasAttribute attribute, AtlasEntityExtInfo entityExtInfo, final boolean isMinExtInfo) throws AtlasBaseException { Object ret = null; AtlasType attrType = attribute.getAttributeType(); String edgeLabel = EDGE_LABEL_PREFIX + attribute.getQualifiedName(); boolean isOwnedAttribute = attribute.isOwnedRef(); AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection(); LOG.debug("Mapping vertex {} to atlas entity {}.{}", entityVertex, attribute.getDefinedInDef().getName(), attribute.getName()); ret = mapVertexToPrimitive(entityVertex, attribute.getVertexPropertyName(), attribute.getAttributeDef()); break; case ENUM: ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, attribute.getVertexPropertyName(), Object.class); break; case STRUCT:
public static AttributeDefinition toV1AttributeDefinition(AtlasStructType.AtlasAttribute attribute) { AtlasAttributeDef attributeDef = attribute.getAttributeDef(); AttributeDefinition ret = new AttributeDefinition(); ret.setIsUnique(attributeDef.getIsUnique()); ret.setIsIndexable(attributeDef.getIsIndexable()); ret.setIsComposite(attribute.isOwnedRef()); ret.setReverseAttributeName(attribute.getInverseRefAttributeName()); ret.setDefaultValue(attributeDef.getDefaultValue()); ret.setDescription(attributeDef.getDescription());
@Test public void testValidConstraints() { AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry(); AtlasTransientTypeRegistry ttr = null; boolean commit = false; List<AtlasEntityDef> entityDefs = new ArrayList<>(); String failureMsg = null; entityDefs.add(createTableEntityDef()); entityDefs.add(createColumnEntityDef()); try { ttr = typeRegistry.lockTypeRegistryForUpdate(); ttr.addTypes(entityDefs); AtlasEntityType typeTable = ttr.getEntityTypeByName(TYPE_TABLE); AtlasEntityType typeColumn = ttr.getEntityTypeByName(TYPE_COLUMN); assertTrue(typeTable.getAttribute(ATTR_COLUMNS).isOwnedRef()); assertNull(typeTable.getAttribute(ATTR_COLUMNS).getInverseRefAttributeName()); assertFalse(typeColumn.getAttribute(ATTR_TABLE).isOwnedRef()); assertEquals(typeColumn.getAttribute(ATTR_TABLE).getInverseRefAttributeName(), ATTR_COLUMNS); assertEquals(typeColumn.getAttribute(ATTR_TABLE).getInverseRefAttribute(), typeTable.getAttribute(ATTR_COLUMNS)); commit = true; } catch (AtlasBaseException excp) { failureMsg = excp.getMessage(); } finally { typeRegistry.releaseTypeRegistryForUpdate(ttr, commit); } assertNull(failureMsg, "failed to create types " + TYPE_TABLE + " and " + TYPE_COLUMN); }
@Test public void testValidConstraints() { AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry(); AtlasTransientTypeRegistry ttr = null; boolean commit = false; List<AtlasEntityDef> entityDefs = new ArrayList<>(); String failureMsg = null; entityDefs.add(createTableEntityDef()); entityDefs.add(createColumnEntityDef()); try { ttr = typeRegistry.lockTypeRegistryForUpdate(); ttr.addTypes(entityDefs); AtlasEntityType typeTable = ttr.getEntityTypeByName(TYPE_TABLE); AtlasEntityType typeColumn = ttr.getEntityTypeByName(TYPE_COLUMN); assertTrue(typeTable.getAttribute(ATTR_COLUMNS).isOwnedRef()); assertNull(typeTable.getAttribute(ATTR_COLUMNS).getInverseRefAttributeName()); assertFalse(typeColumn.getAttribute(ATTR_TABLE).isOwnedRef()); assertEquals(typeColumn.getAttribute(ATTR_TABLE).getInverseRefAttributeName(), ATTR_COLUMNS); assertEquals(typeColumn.getAttribute(ATTR_TABLE).getInverseRefAttribute(), typeTable.getAttribute(ATTR_COLUMNS)); commit = true; } catch (AtlasBaseException excp) { failureMsg = excp.getMessage(); } finally { typeRegistry.releaseTypeRegistryForUpdate(ttr, commit); } assertNull(failureMsg, "failed to create types " + TYPE_TABLE + " and " + TYPE_COLUMN); }
public AtlasAttribute(AtlasStructType definedInType, AtlasAttributeDef attrDef, AtlasType attributeType, String relationshipLabel) { this.definedInType = definedInType; this.attributeDef = attrDef; this.attributeType = attributeType.getTypeForAttribute(); this.qualifiedName = getQualifiedAttributeName(definedInType.getStructDef(), attributeDef.getName()); this.vertexPropertyName = encodePropertyKey(this.qualifiedName); this.relationshipEdgeLabel = getRelationshipEdgeLabel(relationshipLabel); boolean isOwnedRef = false; String inverseRefAttribute = null; if (CollectionUtils.isNotEmpty(attributeDef.getConstraints())) { for (AtlasConstraintDef constraint : attributeDef.getConstraints()) { if (constraint.isConstraintType(CONSTRAINT_TYPE_OWNED_REF)) { isOwnedRef = true; } if (constraint.isConstraintType(CONSTRAINT_TYPE_INVERSE_REF)) { Object val = constraint.getParam(CONSTRAINT_PARAM_ATTRIBUTE); if (val != null) { inverseRefAttribute = val.toString(); } } } } this.isOwnedRef = isOwnedRef; this.inverseRefAttributeName = inverseRefAttribute; this.relationshipEdgeDirection = AtlasRelationshipEdgeDirection.OUT; }
public AtlasAttribute(AtlasStructType definedInType, AtlasAttributeDef attrDef, AtlasType attributeType, String relationshipLabel) { this.definedInType = definedInType; this.attributeDef = attrDef; this.attributeType = attributeType.getTypeForAttribute(); this.qualifiedName = getQualifiedAttributeName(definedInType.getStructDef(), attributeDef.getName()); this.vertexPropertyName = encodePropertyKey(this.qualifiedName); this.relationshipEdgeLabel = getRelationshipEdgeLabel(relationshipLabel); boolean isOwnedRef = false; String inverseRefAttribute = null; if (CollectionUtils.isNotEmpty(attributeDef.getConstraints())) { for (AtlasConstraintDef constraint : attributeDef.getConstraints()) { if (constraint.isConstraintType(CONSTRAINT_TYPE_OWNED_REF)) { isOwnedRef = true; } if (constraint.isConstraintType(CONSTRAINT_TYPE_INVERSE_REF)) { Object val = constraint.getParam(CONSTRAINT_PARAM_ATTRIBUTE); if (val != null) { inverseRefAttribute = val.toString(); } } } } this.isOwnedRef = isOwnedRef; this.inverseRefAttributeName = inverseRefAttribute; this.relationshipEdgeDirection = AtlasRelationshipEdgeDirection.OUT; }
private void restoreEntityAttributes(Referenceable entity, Map<String, Object> prunedAttributes) throws AtlasException { if (MapUtils.isEmpty(prunedAttributes)) { return; } AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName()); if (entityType != null && MapUtils.isNotEmpty(entityType.getAllAttributes())) { Map<String, Object> entityAttributes = entity.getValuesMap(); for (AtlasStructType.AtlasAttribute attribute : entityType.getAllAttributes().values()) { String attrName = attribute.getName(); Object attrValue = entityAttributes.get(attrName); if (prunedAttributes.containsKey(attrName)) { entity.set(attrName, prunedAttributes.get(attrName)); } else if (attribute.isOwnedRef()) { if (attrValue instanceof Collection) { for (Object arrElem : (Collection) attrValue) { if (arrElem instanceof Referenceable) { restoreAttributes(prunedAttributes, (Referenceable) arrElem); } } } else if (attrValue instanceof Referenceable) { restoreAttributes(prunedAttributes, (Referenceable) attrValue); } } } } }