HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, String description, String version, ImmutableSet<String> superTypes, AttributeInfo... fields) throws AtlasException { super(name, description, version); this.typeSystem = typeSystem; this.superTypeClass = superTypeClass; Pair<FieldMapping, ImmutableMap<String, String>> p = constructFieldMapping(superTypes, fields); this.fieldMapping = p.left; this.attributeNameToType = p.right; this.numFields = this.fieldMapping.fields.size(); this.superTypes = superTypes == null ? ImmutableSet.<String>of() : superTypes; this.immediateAttrs = ImmutableList.copyOf(fields); }
public String getDefinedTypeName(String attrName) throws AtlasException { return getDefinedType(attrName).getName(); }
public String getQualifiedName(String attrName) throws AtlasException { String attrTypeName = getDefinedTypeName(attrName); return attrName.contains(".") ? attrName : String.format("%s.%s", attrTypeName, attrName); }
int numReferenceables = 0; setupSuperTypesGraph(superTypes); Iterator<Path> pathItr = pathIterator(); while (pathItr.hasNext()) { Path currentPath = pathItr.next(); ST superType = Objects.equals(currentPath.typeName, getName()) ? (ST) this : typeSystem.getDataType(superTypeClass, currentPath.typeName); attrName = currentPath.addOverrideAttr(attrName); attributeNameToType.put(attrName, superType.getName());
@Override public String toFullyQualifiedName(String propName, String type) { HierarchicalType dataType = getDataType(type); String replacement = m_cleanToQualifiedMap.get(propName); if (replacement == null && dataType != null) { FieldMapping fieldMap = dataType.fieldMapping(); if (fieldMap.fields.containsKey(propName)) { try { replacement = dataType.getQualifiedName(propName); } catch (AtlasException e) { throw new CatalogRuntimeException(String.format( "Unable to resolve fully qualified property name for type '%s': %s", type, e), e); } } } if (replacement == null) { replacement = propName; } return replacement; }
@Override public String toCleanName(String propName, String type) { HierarchicalType dataType = getDataType(type); String replacement = m_qualifiedToCleanMap.get(propName); if (replacement == null && dataType != null) { FieldMapping fieldMap = dataType.fieldMapping(); if (! fieldMap.fields.containsKey(propName) && propName.contains(".")) { String cleanName = propName.substring(propName.lastIndexOf('.') + 1); if (fieldMap.fields.containsKey(cleanName)) { replacement = cleanName; } } } if (replacement == null) { replacement = propName; } return replacement; }
public IStruct castAs(IStruct s, String superTypeName) throws AtlasException { if (!superTypePaths.containsKey(superTypeName)) { throw new AtlasException(String.format("Cannot downcast to %s from type %s", superTypeName, getName())); } if (s != null) { if (!Objects.equals(s.getTypeName(), getName())) { throw new AtlasException( String.format("Downcast called on wrong type %s, instance type is %s", getName(), s.getTypeName())); } List<Path> pathToSuper = superTypePaths.get(superTypeName); if (pathToSuper.size() > 1) { throw new AtlasException(String.format( "Cannot downcast called to %s, from %s: there are multiple paths " + "to SuperType", superTypeName, getName())); } ST superType = typeSystem.getDataType(superTypeClass, superTypeName); Map<String, String> downCastMap = superType.constructDowncastFieldMap(this, pathToSuper.get(0)); return new DownCastStructInstance(superTypeName, new DownCastFieldMapping(ImmutableMap.copyOf(downCastMap)), s); } return null; }
/** * Given type must be a SubType of this type. * @param typeName * @throws AtlasException */ public boolean isSubType(String typeName) throws AtlasException { HierarchicalType cType = typeSystem.getDataType(HierarchicalType.class, typeName); return (cType == this || cType.superTypePaths.containsKey(getName())); }
@Override public String toString() { StringBuilder buf = new StringBuilder(); try { output(buf, new HashSet<String>()); } catch (AtlasException e) { throw new RuntimeException(e); } return buf.toString(); }
protected Map<String, String> constructDowncastFieldMap(ST subType, Path pathToSubType) { String pathToSubTypeName = pathToSubType.pathAfterThis; /* * the downcastMap; */ Map<String, String> dCMap = new HashMap<>(); Iterator<Path> itr = pathIterator(); while (itr.hasNext()) { Path p = itr.next(); Path pInSubType = (Path) subType.pathNameToPathMap.get(p.pathName + "." + pathToSubTypeName); if (pInSubType.hiddenAttributeMap != null) { for (Map.Entry<String, String> e : pInSubType.hiddenAttributeMap.entrySet()) { String mappedInThisType = p.hiddenAttributeMap != null ? p.hiddenAttributeMap.get(e.getKey()) : null; if (mappedInThisType == null) { dCMap.put(e.getKey(), e.getValue()); } else { dCMap.put(mappedInThisType, e.getValue()); } } } } return dCMap; }
private boolean shouldIncludeType(IDataType type, Map<TYPE_FILTER, String> filterMap) { if (filterMap == null) { return true; } for (Entry<TYPE_FILTER, String> filterEntry : filterMap.entrySet()) { switch (filterEntry.getKey()) { case CATEGORY: if (!filterEntry.getValue().equals(type.getTypeCategory().name())) { return false; } break; case SUPERTYPE: if (!validSupertypeFilterCategories.contains(type.getTypeCategory()) || !((HierarchicalType) type).getAllSuperTypeNames().contains(filterEntry.getValue())) { return false; } break; case NOT_SUPERTYPE: if (!validSupertypeFilterCategories.contains(type.getTypeCategory()) || type.getName().equals(filterEntry.getValue()) || ((HierarchicalType) type).getAllSuperTypeNames().contains(filterEntry.getValue())) { return false; } break; } } return true; }
int numReferenceables = 0; setupSuperTypesGraph(superTypes); Iterator<Path> pathItr = pathIterator(); while (pathItr.hasNext()) { Path currentPath = pathItr.next(); ST superType = Objects.equals(currentPath.typeName, getName()) ? (ST) this : typeSystem.getDataType(superTypeClass, currentPath.typeName); attrName = currentPath.addOverrideAttr(attrName); attributeNameToType.put(attrName, superType.getName());
/** * Get the field mappings for the specified data type. * Field mappings are only relevant for CLASS, TRAIT, and STRUCT types. * * @param type * @return {@link FieldMapping} for the specified type * @throws IllegalArgumentException if type is not a CLASS, TRAIT, or STRUCT type. */ public static FieldMapping getFieldMapping(IDataType type) { switch (type.getTypeCategory()) { case CLASS: case TRAIT: return ((HierarchicalType)type).fieldMapping(); case STRUCT: return ((StructType)type).fieldMapping(); default: throw new IllegalArgumentException("Type " + type + " doesn't have any fields!"); } } }
public IStruct castAs(IStruct s, String superTypeName) throws AtlasException { if (!superTypePaths.containsKey(superTypeName)) { throw new AtlasException(String.format("Cannot downcast to %s from type %s", superTypeName, getName())); } if (s != null) { if (!Objects.equals(s.getTypeName(), getName())) { throw new AtlasException( String.format("Downcast called on wrong type %s, instance type is %s", getName(), s.getTypeName())); } List<Path> pathToSuper = superTypePaths.get(superTypeName); if (pathToSuper.size() > 1) { throw new AtlasException(String.format( "Cannot downcast called to %s, from %s: there are multiple paths " + "to SuperType", superTypeName, getName())); } ST superType = typeSystem.getDataType(superTypeClass, superTypeName); Map<String, String> downCastMap = superType.constructDowncastFieldMap(this, pathToSuper.get(0)); return new DownCastStructInstance(superTypeName, new DownCastFieldMapping(ImmutableMap.copyOf(downCastMap)), s); } return null; }
/** * Given type must be a SubType of this type. * @param typeName * @throws AtlasException */ public boolean isSubType(String typeName) throws AtlasException { HierarchicalType cType = typeSystem.getDataType(HierarchicalType.class, typeName); return (cType == this || cType.superTypePaths.containsKey(getName())); }
@Override public String toString() { StringBuilder buf = new StringBuilder(); try { output(buf, new HashSet<String>()); } catch (AtlasException e) { throw new RuntimeException(e); } return buf.toString(); }
protected Map<String, String> constructDowncastFieldMap(ST subType, Path pathToSubType) { String pathToSubTypeName = pathToSubType.pathAfterThis; /* * the downcastMap; */ Map<String, String> dCMap = new HashMap<>(); Iterator<Path> itr = pathIterator(); while (itr.hasNext()) { Path p = itr.next(); Path pInSubType = (Path) subType.pathNameToPathMap.get(p.pathName + "." + pathToSubTypeName); if (pInSubType.hiddenAttributeMap != null) { for (Map.Entry<String, String> e : pInSubType.hiddenAttributeMap.entrySet()) { String mappedInThisType = p.hiddenAttributeMap != null ? p.hiddenAttributeMap.get(e.getKey()) : null; if (mappedInThisType == null) { dCMap.put(e.getKey(), e.getValue()); } else { dCMap.put(mappedInThisType, e.getValue()); } } } } return dCMap; }
private boolean shouldIncludeType(IDataType type, Map<TYPE_FILTER, String> filterMap) { if (filterMap == null) { return true; } for (Entry<TYPE_FILTER, String> filterEntry : filterMap.entrySet()) { switch (filterEntry.getKey()) { case CATEGORY: if (!filterEntry.getValue().equals(type.getTypeCategory().name())) { return false; } break; case SUPERTYPE: if (!validSupertypeFilterCategories.contains(type.getTypeCategory()) || !((HierarchicalType) type).getAllSuperTypeNames().contains(filterEntry.getValue())) { return false; } break; case NOT_SUPERTYPE: if (!validSupertypeFilterCategories.contains(type.getTypeCategory()) || type.getName().equals(filterEntry.getValue()) || ((HierarchicalType) type).getAllSuperTypeNames().contains(filterEntry.getValue())) { return false; } break; } } return true; }
/** * Get the field mappings for the specified data type. * Field mappings are only relevant for CLASS, TRAIT, and STRUCT types. * * @param type * @return {@link FieldMapping} for the specified type * @throws IllegalArgumentException if type is not a CLASS, TRAIT, or STRUCT type. */ public static FieldMapping getFieldMapping(IDataType type) { switch (type.getTypeCategory()) { case CLASS: case TRAIT: return ((HierarchicalType)type).fieldMapping(); case STRUCT: return ((StructType)type).fieldMapping(); default: throw new IllegalArgumentException("Type " + type + " doesn't have any fields!"); } } }
public String getDefinedTypeName(String attrName) throws AtlasException { return getDefinedType(attrName).getName(); }