Search in sources :

Example 11 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class AtlasStructType method resolveReferences.

@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);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Cardinality(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality) HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 12 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class EntityGraphRetriever method mapVertexToAtlasEntityHeader.

private AtlasEntityHeader mapVertexToAtlasEntityHeader(AtlasVertex entityVertex) throws AtlasBaseException {
    AtlasEntityHeader ret = new AtlasEntityHeader();
    String typeName = entityVertex.getProperty(Constants.TYPE_NAME_PROPERTY_KEY, String.class);
    String guid = entityVertex.getProperty(Constants.GUID_PROPERTY_KEY, String.class);
    ret.setTypeName(typeName);
    ret.setGuid(guid);
    ret.setStatus(GraphHelper.getStatus(entityVertex));
    ret.setClassificationNames(GraphHelper.getTraitNames(entityVertex));
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
    if (entityType != null) {
        for (AtlasAttribute uniqueAttribute : entityType.getUniqAttributes().values()) {
            Object attrValue = getVertexAttribute(entityVertex, uniqueAttribute);
            if (attrValue != null) {
                ret.setAttribute(uniqueAttribute.getName(), attrValue);
            }
        }
        Object name = getVertexAttribute(entityVertex, entityType.getAttribute(AtlasClient.NAME));
        Object description = getVertexAttribute(entityVertex, entityType.getAttribute(AtlasClient.DESCRIPTION));
        Object owner = getVertexAttribute(entityVertex, entityType.getAttribute(AtlasClient.OWNER));
        Object displayText = name != null ? name : ret.getAttribute(AtlasClient.QUALIFIED_NAME);
        ret.setAttribute(AtlasClient.NAME, name);
        ret.setAttribute(AtlasClient.DESCRIPTION, description);
        ret.setAttribute(AtlasClient.OWNER, owner);
        if (displayText != null) {
            ret.setDisplayText(displayText.toString());
        }
    }
    return ret;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader)

Example 13 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class EntityDiscoveryService method searchUsingBasicQuery.

@Override
public AtlasSearchResult searchUsingBasicQuery(String query, String typeName, String classification, String attrName, String attrValuePrefix, boolean excludeDeletedEntities, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(AtlasQueryType.BASIC);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing basic search query: {} with type: {} and classification: {}", query, typeName, classification);
    }
    final QueryParams params = validateSearchParams(limit, offset);
    Set<String> typeNames = null;
    Set<String> classificationNames = null;
    String attrQualifiedName = null;
    if (StringUtils.isNotEmpty(typeName)) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            throw new AtlasBaseException(UNKNOWN_TYPENAME, typeName);
        }
        typeNames = entityType.getTypeAndAllSubTypes();
        ret.setType(typeName);
    }
    if (StringUtils.isNotEmpty(classification)) {
        AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classification);
        if (classificationType == null) {
            throw new AtlasBaseException(CLASSIFICATION_NOT_FOUND, classification);
        }
        classificationNames = classificationType.getTypeAndAllSubTypes();
        ret.setClassification(classification);
    }
    boolean isAttributeSearch = StringUtils.isNotEmpty(attrName) || StringUtils.isNotEmpty(attrValuePrefix);
    boolean isGuidPrefixSearch = false;
    if (isAttributeSearch) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        ret.setQueryType(AtlasQueryType.ATTRIBUTE);
        if (entityType != null) {
            AtlasAttribute attribute = null;
            if (StringUtils.isNotEmpty(attrName)) {
                attribute = entityType.getAttribute(attrName);
                if (attribute == null) {
                    throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attrName, typeName);
                }
            } else {
                // if attrName is null|empty iterate defaultAttrNames to get attribute value
                final List<String> defaultAttrNames = new ArrayList<>(Arrays.asList("qualifiedName", "name"));
                Iterator<String> iter = defaultAttrNames.iterator();
                while (iter.hasNext() && attribute == null) {
                    attrName = iter.next();
                    attribute = entityType.getAttribute(attrName);
                }
            }
            if (attribute == null) {
                // for guid prefix search use gremlin and nullify query to avoid using fulltext
                // (guids cannot be searched in fulltext)
                isGuidPrefixSearch = true;
                query = null;
            } else {
                attrQualifiedName = attribute.getQualifiedName();
                String attrQuery = String.format("%s AND (%s *)", attrName, attrValuePrefix.replaceAll("\\.", " "));
                query = StringUtils.isEmpty(query) ? attrQuery : String.format("(%s) AND (%s)", query, attrQuery);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing attribute search attrName: {} and attrValue: {}", attrName, attrValuePrefix);
        }
    }
    // results in a faster and accurate results than using CONTAINS/CONTAINS_PREFIX filter on entityText property
    if (StringUtils.isNotEmpty(query)) {
        final String idxQuery = getQueryForFullTextSearch(query, typeName, classification);
        final int startIdx = params.offset();
        final int resultSize = params.limit();
        int resultIdx = 0;
        for (int indexQueryOffset = 0; ; indexQueryOffset += getMaxResultSetSize()) {
            final Iterator<Result<?, ?>> qryResult = graph.indexQuery(Constants.FULLTEXT_INDEX, idxQuery, indexQueryOffset).vertices();
            if (LOG.isDebugEnabled()) {
                LOG.debug("indexQuery: query=" + idxQuery + "; offset=" + indexQueryOffset);
            }
            if (!qryResult.hasNext()) {
                break;
            }
            while (qryResult.hasNext()) {
                AtlasVertex<?, ?> vertex = qryResult.next().getVertex();
                String vertexTypeName = GraphHelper.getTypeName(vertex);
                // skip non-entity vertices
                if (StringUtils.isEmpty(vertexTypeName) || StringUtils.isEmpty(GraphHelper.getGuid(vertex))) {
                    continue;
                }
                if (typeNames != null && !typeNames.contains(vertexTypeName)) {
                    continue;
                }
                if (classificationNames != null) {
                    List<String> traitNames = GraphHelper.getTraitNames(vertex);
                    if (CollectionUtils.isEmpty(traitNames) || !CollectionUtils.containsAny(classificationNames, traitNames)) {
                        continue;
                    }
                }
                if (isAttributeSearch) {
                    String vertexAttrValue = vertex.getProperty(attrQualifiedName, String.class);
                    if (StringUtils.isNotEmpty(vertexAttrValue) && !vertexAttrValue.startsWith(attrValuePrefix)) {
                        continue;
                    }
                }
                if (skipDeletedEntities(excludeDeletedEntities, vertex)) {
                    continue;
                }
                resultIdx++;
                if (resultIdx <= startIdx) {
                    continue;
                }
                AtlasEntityHeader header = entityRetriever.toAtlasEntityHeader(vertex);
                ret.addEntity(header);
                if (ret.getEntities().size() == resultSize) {
                    break;
                }
            }
            if (ret.getEntities() != null && ret.getEntities().size() == resultSize) {
                break;
            }
        }
    } else {
        final Map<String, Object> bindings = new HashMap<>();
        String basicQuery = "g.V()";
        if (classificationNames != null) {
            bindings.put("traitNames", classificationNames);
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER);
        }
        if (typeNames != null) {
            bindings.put("typeNames", typeNames);
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER);
        }
        if (excludeDeletedEntities) {
            bindings.put("state", Status.ACTIVE.toString());
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_STATE_FILTER);
        }
        if (isGuidPrefixSearch) {
            bindings.put("guid", attrValuePrefix + ".*");
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.GUID_PREFIX_FILTER);
        }
        bindings.put("startIdx", params.offset());
        bindings.put("endIdx", params.offset() + params.limit());
        basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.TO_RANGE_LIST);
        ScriptEngine scriptEngine = graph.getGremlinScriptEngine();
        try {
            Object result = graph.executeGremlinScript(scriptEngine, bindings, basicQuery, false);
            if (result instanceof List && CollectionUtils.isNotEmpty((List) result)) {
                List queryResult = (List) result;
                Object firstElement = queryResult.get(0);
                if (firstElement instanceof AtlasVertex) {
                    for (Object element : queryResult) {
                        if (element instanceof AtlasVertex) {
                            ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) element));
                        } else {
                            LOG.warn("searchUsingBasicQuery({}): expected an AtlasVertex; found unexpected entry in result {}", basicQuery, element);
                        }
                    }
                }
            }
        } catch (ScriptException e) {
            throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, basicQuery);
        } finally {
            graph.releaseGremlinScriptEngine(scriptEngine);
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) ScriptEngine(javax.script.ScriptEngine) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasFullTextResult(org.apache.atlas.model.discovery.AtlasSearchResult.AtlasFullTextResult) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Result(org.apache.atlas.repository.graphdb.AtlasIndexQuery.Result) AttributeSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult.AttributeSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) ScriptException(javax.script.ScriptException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) List(java.util.List) ArrayList(java.util.ArrayList) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 14 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class EntityGraphMapper method mapArrayValue.

public List mapArrayValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapArrayValue({})", ctx);
    }
    AtlasAttribute attribute = ctx.getAttribute();
    List newElements = (List) ctx.getValue();
    AtlasArrayType arrType = (AtlasArrayType) attribute.getAttributeType();
    AtlasType elementType = arrType.getElementType();
    List<Object> currentElements = getArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty());
    boolean isReference = AtlasGraphUtilsV1.isReference(elementType);
    AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
    List<Object> newElementsCreated = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(newElements)) {
        for (int index = 0; index < newElements.size(); index++) {
            AtlasEdge existingEdge = getEdgeAt(currentElements, index, elementType);
            AttributeMutationContext arrCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), ctx.getAttribute(), newElements.get(index), ctx.getVertexProperty(), elementType, existingEdge);
            Object newEntry = mapCollectionElementsToVertex(arrCtx, context);
            if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                // Update the inverse reference value.
                AtlasEdge newEdge = (AtlasEdge) newEntry;
                addInverseReference(arrCtx, inverseRefAttribute, newEdge);
            }
            newElementsCreated.add(newEntry);
        }
    }
    if (isReference) {
        List<AtlasEdge> additionalEdges = removeUnusedArrayEntries(attribute, (List) currentElements, (List) newElementsCreated);
        newElementsCreated.addAll(additionalEdges);
    }
    // for dereference on way out
    setArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty(), newElementsCreated);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapArrayValue({})", ctx);
    }
    return newElementsCreated;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 15 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class EntityGraphRetriever method mapAttributes.

private void mapAttributes(AtlasVertex entityVertex, AtlasStruct struct, AtlasEntityExtInfo entityExtInfo) throws AtlasBaseException {
    AtlasType objType = typeRegistry.getType(struct.getTypeName());
    if (!(objType instanceof AtlasStructType)) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, struct.getTypeName());
    }
    AtlasStructType structType = (AtlasStructType) objType;
    for (AtlasAttribute attribute : structType.getAllAttributes().values()) {
        Object attrValue = mapVertexToAttribute(entityVertex, attribute, entityExtInfo);
        struct.setAttribute(attribute.getName(), attrValue);
    }
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Aggregations

AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)21 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)6 AtlasType (org.apache.atlas.type.AtlasType)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)3 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)3 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)3 List (java.util.List)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasTypeDefGraphStoreV1 (org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1)2 AtlasStructType (org.apache.atlas.type.AtlasStructType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptException (javax.script.ScriptException)1 AtlasException (org.apache.atlas.AtlasException)1