Search in sources :

Example 31 with AttributeInfo

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

the class FullTextMapper method forInstance.

private String forInstance(ITypedInstance typedInstance, boolean followReferences) throws AtlasException {
    StringBuilder fullText = new StringBuilder();
    for (AttributeInfo attributeInfo : typedInstance.fieldMapping().fields.values()) {
        Object attrValue = typedInstance.get(attributeInfo.name);
        if (attrValue == null) {
            continue;
        }
        String attrFullText = forAttribute(attributeInfo.dataType(), attrValue, followReferences);
        if (StringUtils.isNotEmpty(attrFullText)) {
            fullText = fullText.append(FULL_TEXT_DELIMITER).append(attributeInfo.name).append(FULL_TEXT_DELIMITER).append(attrFullText);
        }
    }
    return fullText.toString();
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo)

Example 32 with AttributeInfo

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

the class GraphHelper method getVerticesForInstancesByUniqueAttribute.

/**
     * Finds vertices that match at least one unique attribute of the instances specified.  The AtlasVertex at a given index in the result corresponds
     * to the IReferencableInstance at that same index that was passed in.  The number of elements in the resultant list is guaranteed to match the
     * number of instances that were passed in.  If no vertex is found for a given instance, that entry will be null in the resultant list.
     *
     *
     * @param classType
     * @param instancesForClass
     * @return
     * @throws AtlasException
     */
public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(ClassType classType, List<? extends IReferenceableInstance> instancesForClass) throws AtlasException {
    //For each attribute, need to figure out what values to search for and which instance(s)
    //those values correspond to.
    Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>();
    for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) {
        if (attributeInfo.isUnique) {
            String propertyKey = getQualifiedFieldName(classType, attributeInfo.name);
            AttributeValueMap mapForAttribute = new AttributeValueMap();
            for (int idx = 0; idx < instancesForClass.size(); idx++) {
                IReferenceableInstance instance = instancesForClass.get(idx);
                Object value = instance.get(attributeInfo.name);
                mapForAttribute.put(value, instance, idx);
            }
            map.put(propertyKey, mapForAttribute);
        }
    }
    AtlasVertex[] result = new AtlasVertex[instancesForClass.size()];
    if (map.isEmpty()) {
        //no unique attributes
        return Arrays.asList(result);
    }
    //construct gremlin query
    AtlasGraphQuery query = graph.query();
    query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName());
    query.has(Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
    List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>();
    //that matches the value in some instance.
    for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
        AtlasGraphQuery orChild = query.createChildQuery();
        String propertyName = entry.getKey();
        AttributeValueMap valueMap = entry.getValue();
        Set<Object> values = valueMap.getAttributeValues();
        if (values.size() == 1) {
            orChild.has(propertyName, values.iterator().next());
        } else if (values.size() > 1) {
            orChild.in(propertyName, values);
        }
        orChildren.add(orChild);
    }
    if (orChildren.size() == 1) {
        AtlasGraphQuery child = orChildren.get(0);
        query.addConditionsFrom(child);
    } else if (orChildren.size() > 1) {
        query.or(orChildren);
    }
    Iterable<AtlasVertex> queryResult = query.vertices();
    for (AtlasVertex matchingVertex : queryResult) {
        Collection<IndexedInstance> matches = getInstancesForVertex(map, matchingVertex);
        for (IndexedInstance wrapper : matches) {
            result[wrapper.getIndex()] = matchingVertex;
        }
    }
    return Arrays.asList(result);
}
Also used : HashMap(java.util.HashMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap) ArrayList(java.util.ArrayList) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) IndexedInstance(org.apache.atlas.util.IndexedInstance) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap)

Example 33 with AttributeInfo

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

the class DeleteHandler method deleteVertex.

protected void deleteVertex(AtlasVertex instanceVertex, boolean force) throws AtlasException {
    //Update external references(incoming edges) to this vertex
    if (LOG.isDebugEnabled()) {
        LOG.debug("Setting the external references to {} to null(removing edges)", string(instanceVertex));
    }
    for (AtlasEdge edge : (Iterable<AtlasEdge>) instanceVertex.getEdges(AtlasEdgeDirection.IN)) {
        Id.EntityState edgeState = GraphHelper.getState(edge);
        if (edgeState == Id.EntityState.ACTIVE) {
            //Delete only the active edge references
            AttributeInfo attribute = getAttributeForEdge(edge.getLabel());
            //TODO use delete edge instead??
            deleteEdgeBetweenVertices(edge.getOutVertex(), edge.getInVertex(), attribute.name);
        }
    }
    _deleteVertex(instanceVertex, force);
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) Id(org.apache.atlas.typesystem.persistence.Id) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 34 with AttributeInfo

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

the class StructInstance method setByte.

public void setByte(String attrName, byte val) throws AtlasException {
    AttributeInfo i = fieldMapping.fields.get(attrName);
    if (i == null) {
        throw new AtlasException(String.format("Unknown field %s for Struct %s", attrName, getTypeName()));
    }
    if (i.dataType() != DataTypes.BYTE_TYPE) {
        throw new AtlasException(String.format("Field %s for Struct %s is not a %s, call generic set method", attrName, getTypeName(), DataTypes.BYTE_TYPE.getName()));
    }
    int pos = fieldMapping.fieldPos.get(attrName);
    int nullPos = fieldMapping.fieldNullPos.get(attrName);
    nullFlags[nullPos] = false;
    bytes[pos] = val;
    explicitSets[nullPos] = true;
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasException(org.apache.atlas.AtlasException)

Example 35 with AttributeInfo

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

the class StructInstance method get.

public Object get(String attrName) throws AtlasException {
    AttributeInfo i = fieldMapping.fields.get(attrName);
    if (i == null) {
        throw new AtlasException(String.format("Unknown field %s for Struct %s", attrName, getTypeName()));
    }
    int pos = fieldMapping.fieldPos.get(attrName);
    int nullPos = fieldMapping.fieldNullPos.get(attrName);
    if (nullFlags[nullPos]) {
        if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.PRIMITIVE) {
            return ((DataTypes.PrimitiveType) i.dataType()).nullValue();
        } else {
            return null;
        }
    }
    if (i.dataType() == DataTypes.BOOLEAN_TYPE) {
        return bools[pos];
    } else if (i.dataType() == DataTypes.BYTE_TYPE) {
        return bytes[pos];
    } else if (i.dataType() == DataTypes.SHORT_TYPE) {
        return shorts[pos];
    } else if (i.dataType() == DataTypes.INT_TYPE) {
        return ints[pos];
    } else if (i.dataType() == DataTypes.LONG_TYPE) {
        return longs[pos];
    } else if (i.dataType() == DataTypes.FLOAT_TYPE) {
        return floats[pos];
    } else if (i.dataType() == DataTypes.DOUBLE_TYPE) {
        return doubles[pos];
    } else if (i.dataType() == DataTypes.BIGINTEGER_TYPE) {
        return bigIntegers[pos];
    } else if (i.dataType() == DataTypes.BIGDECIMAL_TYPE) {
        return bigDecimals[pos];
    } else if (i.dataType() == DataTypes.DATE_TYPE) {
        return dates[pos];
    } else if (i.dataType() == DataTypes.STRING_TYPE) {
        return strings[pos];
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ENUM) {
        return ((EnumType) i.dataType()).fromOrdinal(ints[pos]);
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
        return arrays[pos];
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) {
        return maps[pos];
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT || i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT) {
        return structs[pos];
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
        if (ids[pos] != null) {
            return ids[pos];
        } else {
            return referenceables[pos];
        }
    } else {
        throw new AtlasException(String.format("Unknown datatype %s", i.dataType()));
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasException(org.apache.atlas.AtlasException)

Aggregations

AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)47 AtlasException (org.apache.atlas.AtlasException)26 HashMap (java.util.HashMap)6 IDataType (org.apache.atlas.typesystem.types.IDataType)6 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)5 FieldMapping (org.apache.atlas.typesystem.types.FieldMapping)5 Map (java.util.Map)4 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)4 HierarchicalType (org.apache.atlas.typesystem.types.HierarchicalType)4 Test (org.testng.annotations.Test)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)3 Id (org.apache.atlas.typesystem.persistence.Id)3 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)2 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)2 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)2 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)2