Search in sources :

Example 1 with Pair

use of org.apache.atlas.typesystem.types.TypeUtils.Pair in project incubator-atlas by apache.

the class HierarchicalType method constructFieldMapping.

protected Pair<FieldMapping, ImmutableMap<String, String>> constructFieldMapping(ImmutableSet<String> superTypes, AttributeInfo... fields) throws AtlasException {
    Map<String, AttributeInfo> fieldsMap = new LinkedHashMap();
    Map<String, Integer> fieldPos = new HashMap();
    Map<String, Integer> fieldNullPos = new HashMap();
    Map<String, String> attributeNameToType = new HashMap<>();
    int numBools = 0;
    int numBytes = 0;
    int numShorts = 0;
    int numInts = 0;
    int numLongs = 0;
    int numFloats = 0;
    int numDoubles = 0;
    int numBigInts = 0;
    int numBigDecimals = 0;
    int numDates = 0;
    int numStrings = 0;
    int numArrays = 0;
    int numMaps = 0;
    int numStructs = 0;
    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);
        ImmutableList<AttributeInfo> superTypeFields = superType == this ? ImmutableList.copyOf(fields) : superType.immediateAttrs;
        Set<String> immediateFields = new HashSet<>();
        for (AttributeInfo i : superTypeFields) {
            if (superType == this) {
                if (immediateFields.contains(i.name)) {
                    throw new AtlasException(String.format("Struct defintion cannot contain multiple fields with the" + " same name %s", i.name));
                }
                immediateFields.add(i.name);
            }
            String attrName = i.name;
            if (fieldsMap.containsKey(attrName)) {
                attrName = currentPath.addOverrideAttr(attrName);
            }
            attributeNameToType.put(attrName, superType.getName());
            fieldsMap.put(attrName, i);
            fieldNullPos.put(attrName, fieldNullPos.size());
            if (i.dataType() == DataTypes.BOOLEAN_TYPE) {
                fieldPos.put(attrName, numBools);
                numBools++;
            } else if (i.dataType() == DataTypes.BYTE_TYPE) {
                fieldPos.put(attrName, numBytes);
                numBytes++;
            } else if (i.dataType() == DataTypes.SHORT_TYPE) {
                fieldPos.put(attrName, numShorts);
                numShorts++;
            } else if (i.dataType() == DataTypes.INT_TYPE) {
                fieldPos.put(attrName, numInts);
                numInts++;
            } else if (i.dataType() == DataTypes.LONG_TYPE) {
                fieldPos.put(attrName, numLongs);
                numLongs++;
            } else if (i.dataType() == DataTypes.FLOAT_TYPE) {
                fieldPos.put(attrName, numFloats);
                numFloats++;
            } else if (i.dataType() == DataTypes.DOUBLE_TYPE) {
                fieldPos.put(attrName, numDoubles);
                numDoubles++;
            } else if (i.dataType() == DataTypes.BIGINTEGER_TYPE) {
                fieldPos.put(attrName, numBigInts);
                numBigInts++;
            } else if (i.dataType() == DataTypes.BIGDECIMAL_TYPE) {
                fieldPos.put(attrName, numBigDecimals);
                numBigDecimals++;
            } else if (i.dataType() == DataTypes.DATE_TYPE) {
                fieldPos.put(attrName, numDates);
                numDates++;
            } else if (i.dataType() == DataTypes.STRING_TYPE) {
                fieldPos.put(attrName, numStrings);
                numStrings++;
            } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ENUM) {
                fieldPos.put(i.name, numInts);
                numInts++;
            } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
                fieldPos.put(attrName, numArrays);
                numArrays++;
            } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) {
                fieldPos.put(attrName, numMaps);
                numMaps++;
            } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT || i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT) {
                fieldPos.put(attrName, numStructs);
                numStructs++;
            } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
                fieldPos.put(attrName, numReferenceables);
                numReferenceables++;
            } else {
                throw new AtlasException(String.format("Unknown datatype %s", i.dataType()));
            }
        }
    }
    this.superTypePaths = ImmutableMap.copyOf(superTypePaths);
    this.pathNameToPathMap = ImmutableMap.copyOf(pathNameToPathMap);
    FieldMapping fm = new FieldMapping(fieldsMap, fieldPos, fieldNullPos, numBools, numBytes, numShorts, numInts, numLongs, numFloats, numDoubles, numBigInts, numBigDecimals, numDates, numStrings, numArrays, numMaps, numStructs, numReferenceables);
    return new Pair(fm, ImmutableMap.copyOf(attributeNameToType));
}
Also used : AtlasException(org.apache.atlas.AtlasException) Pair(org.apache.atlas.typesystem.types.TypeUtils.Pair)

Aggregations

AtlasException (org.apache.atlas.AtlasException)1 Pair (org.apache.atlas.typesystem.types.TypeUtils.Pair)1