private void addStructType(AtlasStructType structType, ExportContext context) { if (!context.structTypes.contains(structType.getTypeName())) { context.structTypes.add(structType.getTypeName()); addAttributeTypes(structType, context); } }
@Test public void testStructTypeDefaultValue() { AtlasStruct defValue = structType.createDefaultValue(); assertNotNull(defValue); assertEquals(defValue.getTypeName(), structType.getTypeName()); }
static Map<String, List<String>> getVertexPropertiesForCollectionAttributes(AtlasStructType type) { try { Map<String, List<String>> collectionProperties = new HashMap<>(); for (AtlasAttribute attr : type.getAllAttributes().values()) { addIfCollectionAttribute(attr, collectionProperties); } return collectionProperties; } catch (Exception e) { LOG.error("addVertexPropertiesForCollectionAttributes", e); } return null; }
@Override public Object getNormalizedValue(Object obj) { Object ret = null; if (obj != null) { if (isValidValue(obj)) { if (obj instanceof AtlasStruct) { normalizeAttributeValues((AtlasStruct) obj); ret = obj; } else if (obj instanceof Map) { normalizeAttributeValues((Map) obj); ret = obj; } } } return ret; }
public void normalizeAttributeValues(Map<String, Object> obj) { if (obj != null) { for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { String attributeName = attributeDef.getName(); if (obj.containsKey(attributeName)) { Object attributeValue = getNormalizedValue(obj.get(attributeName), attributeDef); obj.put(attributeName, attributeValue); } else if (!attributeDef.getIsOptional()) { obj.put(attributeName, createDefaultValue(attributeDef)); } } } }
protected Map<String, Object> fromV2ToV1(AtlasStructType structType, Map<String, Object> attributes, ConverterContext context) throws AtlasBaseException { Map<String, Object> ret = null; if (MapUtils.isNotEmpty(attributes)) { ret = new HashMap<>(); // Only process the requested/set attributes for (String attrName : attributes.keySet()) { AtlasAttribute attr = structType.getAttribute(attrName); if (attr == null) { LOG.warn("ignored unknown attribute {}.{}", structType.getTypeName(), attrName); continue; } AtlasType attrType = attr.getAttributeType(); Object v2Value = attributes.get(attr.getName()); Object v1Value; AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); v1Value = attrConverter.fromV2ToV1(v2Value, attrType, context); ret.put(attr.getName(), v1Value); } } return ret; }
private void resolveConstraints(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { for (AtlasAttributeDef attributeDef : getStructDef().getAttributeDefs()) { if (CollectionUtils.isEmpty(attributeDef.getConstraints())) { continue; AtlasEntityType attrType = getReferencedEntityType(typeRegistry.getType(attributeDef.getTypeName())); getTypeName(), attributeDef.getName(), CONSTRAINT_TYPE_OWNED_REF, attributeDef.getTypeName()); AtlasEntityType attrType = getReferencedEntityType(typeRegistry.getType(attributeDef.getTypeName())); getTypeName(), attributeDef.getName(), CONSTRAINT_TYPE_INVERSE_REF, attributeDef.getTypeName()); getTypeName(), attributeDef.getName(), CONSTRAINT_PARAM_ATTRIBUTE, CONSTRAINT_TYPE_INVERSE_REF, String.valueOf(constraint.getParams())); getTypeName(), attributeDef.getName(), CONSTRAINT_TYPE_INVERSE_REF, attrType.getTypeName(), inverseRefAttrName); AtlasEntityType inverseRefAttrType = getReferencedEntityType(typeRegistry.getType(inverseRefAttrDef.getTypeName())); getTypeName(), attributeDef.getName(), CONSTRAINT_TYPE_INVERSE_REF, attrType.getTypeName(), inverseRefAttrName);
@Override public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { Map<String, AtlasAttribute> a = new HashMap<>(); for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { AtlasType attrType = typeRegistry.getType(attributeDef.getTypeName()); AtlasAttribute attribute = new AtlasAttribute(this, attributeDef, attrType); Cardinality cardinality = attributeDef.getCardinality(); if (cardinality == Cardinality.LIST || cardinality == Cardinality.SET) { if (!(attrType instanceof AtlasArrayType)) { throw new AtlasBaseException(AtlasErrorCode.INVALID_ATTRIBUTE_TYPE_FOR_CARDINALITY, getTypeName(), attributeDef.getName()); } AtlasArrayType arrayType = (AtlasArrayType)attrType; arrayType.setMinCount(attributeDef.getValuesMinCount()); arrayType.setMaxCount(attributeDef.getValuesMaxCount()); } a.put(attributeDef.getName(), attribute); } resolveConstraints(typeRegistry); this.allAttributes = Collections.unmodifiableMap(a); this.uniqAttributes = getUniqueAttributes(this.allAttributes); }
private void validateAttributes(final AtlasStructType structType, final FilterCriteria filterCriteria) throws AtlasBaseException { if (filterCriteria != null) { FilterCriteria.Condition condition = filterCriteria.getCondition(); if (condition != null && CollectionUtils.isNotEmpty(filterCriteria.getCriterion())) { for (FilterCriteria criteria : filterCriteria.getCriterion()) { validateAttributes(structType, criteria); } } else { String attributeName = filterCriteria.getAttributeName(); if (StringUtils.isNotEmpty(attributeName) && structType.getAttributeType(attributeName) == null) { throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attributeName, structType.getTypeName()); } } } }
public AtlasStructDef getDefinedInDef() { return definedInType.getStructDef(); }
private static AttributeDefinition[] getAttributes(AtlasStructType structType, AtlasTypeRegistry registry) throws AtlasBaseException { List<AttributeDefinition> ret = new ArrayList<>(); List<AtlasAttributeDef> attrDefs = structType.getStructDef().getAttributeDefs(); if (CollectionUtils.isNotEmpty(attrDefs)) { for (AtlasAttributeDef attrDef : attrDefs) { AtlasAttribute attribute = structType.getAttribute(attrDef.getName()); ret.add(AtlasStructDefStoreV1.toAttributeDefintion(attribute)); } } return ret.toArray(new AttributeDefinition[ret.size()]); } }
private static AtlasStructType getStructType(AtlasStructDef structDef) { try { return new AtlasStructType(structDef, ModelTestUtil.getTypesRegistry()); } catch (AtlasBaseException excp) { return null; } } }
@Override public boolean isValidValue(Object obj) { if (obj != null) { for (AtlasEntityType superType : superTypes) { if (!superType.isValidValue(obj)) { return false; } } return super.isValidValue(obj); } return true; }
private void mapAttributes(AtlasStruct struct, AtlasVertex vertex, EntityOperation op, EntityMutationContext context) throws AtlasBaseException { if (LOG.isDebugEnabled()) { LOG.debug("==> mapAttributes({}, {})", op, struct.getTypeName()); } if (MapUtils.isNotEmpty(struct.getAttributes())) { AtlasStructType structType = getStructType(struct.getTypeName()); if (op.equals(CREATE)) { for (AtlasAttribute attribute : structType.getAllAttributes().values()) { Object attrValue = struct.getAttribute(attribute.getName()); mapAttribute(attribute, attrValue, vertex, op, context); } } else if (op.equals(UPDATE)) { for (String attrName : struct.getAttributes().keySet()) { AtlasAttribute attribute = structType.getAttribute(attrName); if (attribute != null) { Object attrValue = struct.getAttribute(attrName); mapAttribute(attribute, attrValue, vertex, op, context); } else { LOG.warn("mapAttributes(): invalid attribute {}.{}. Ignored..", struct.getTypeName(), attrName); } } } updateModificationMetadata(vertex); } if (LOG.isDebugEnabled()) { LOG.debug("<== mapAttributes({}, {})", op, struct.getTypeName()); } }
private static List<AttributeDefinition> getAttributes(AtlasStructType structType, AtlasTypeRegistry registry) { List<AttributeDefinition> ret = new ArrayList<>(); List<AtlasAttributeDef> attrDefs = structType.getStructDef().getAttributeDefs(); if (CollectionUtils.isNotEmpty(attrDefs)) { for (AtlasAttributeDef attrDef : attrDefs) { AtlasAttribute attribute = structType.getAttribute(attrDef.getName()); AttributeDefinition oldAttrDef = AtlasStructDefStoreV2.toAttributeDefinition(attribute); ret.add(new AttributeDefinition(oldAttrDef.getName(), oldAttrDef.getDataTypeName(), new Multiplicity(oldAttrDef.getMultiplicity()), oldAttrDef.getIsComposite(), oldAttrDef.getIsUnique(), oldAttrDef.getIsIndexable(), oldAttrDef.getReverseAttributeName())); } } return ret; } }
@Override public boolean validateValue(Object obj, String objName, List<String> messages) { boolean ret = true; if (obj != null) { for (AtlasClassificationType superType : superTypes) { ret = superType.validateValue(obj, objName, messages) && ret; } ret = super.validateValue(obj, objName, messages) && ret; } return ret; }
ret = false; } else { AtlasStruct structVal1 = getStructFromValue(val1); AtlasStruct structVal2 = getStructFromValue(val2); for (Map.Entry<String, Object> entry : structVal1.getAttributes().entrySet()) { String attrName = entry.getKey(); AtlasAttribute attribute = getAttribute(attrName);
public void populateDefaultValues(AtlasStruct obj) { if (obj != null) { Map<String, Object> attributes = obj.getAttributes(); if (attributes == null) { attributes = new HashMap<>(); } for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { if (!attributeDef.getIsOptional()) { attributes.put(attributeDef.getName(), createDefaultValue(attributeDef)); } } obj.setAttributes(attributes); } }
private boolean isIndexSearchable(FilterCriteria filterCriteria, AtlasStructType structType) throws AtlasBaseException { String qualifiedName = structType.getQualifiedAttributeName(filterCriteria.getAttributeName()); Set<String> indexedKeys = context.getIndexedKeys(); boolean ret = indexedKeys != null && indexedKeys.contains(qualifiedName); if (ret) { // index exists // for string type attributes, don't use index query in the following cases: // - operation is NEQ, as it might return fewer entries due to tokenization of vertex property value // - value-to-compare has special characters AtlasType attributeType = structType.getAttributeType(filterCriteria.getAttributeName()); if (AtlasBaseTypeDef.ATLAS_TYPE_STRING.equals(attributeType.getTypeName())) { if (filterCriteria.getOperator() == SearchParameters.Operator.NEQ) { if (LOG.isDebugEnabled()) { LOG.debug("NEQ operator found for string attribute {}, deferring to in-memory or graph query (might cause poor performance)", qualifiedName); } ret = false; } else if (hasIndexQuerySpecialChar(filterCriteria.getAttributeValue())) { if (LOG.isDebugEnabled()) { LOG.debug("special characters found in filter value {}, deferring to in-memory or graph query (might cause poor performance)", filterCriteria.getAttributeValue()); } ret = false; } } } if (LOG.isDebugEnabled()) { if (!ret) { LOG.debug("Not using index query for: attribute='{}', operator='{}', value='{}'", qualifiedName, filterCriteria.getOperator(), filterCriteria.getAttributeValue()); } } return ret; }
@Test public void testStructTypeGetNormalizedValue() { assertNull(structType.getNormalizedValue(null), "value=" + null); for (Object value : validValues) { if (value == null) { continue; } Object normalizedValue = structType.getNormalizedValue(value); assertNotNull(normalizedValue, "value=" + value); } for (Object value : invalidValues) { assertNull(structType.getNormalizedValue(value), "value=" + value); } }