Search in sources :

Example 26 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class EntityDiscoveryService method searchWithParameters.

@Override
@GraphTransaction
public AtlasSearchResult searchWithParameters(SearchParameters searchParameters) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(searchParameters);
    final QueryParams params = QueryParams.getNormalizedParams(searchParameters.getLimit(), searchParameters.getOffset());
    searchParameters.setLimit(params.limit());
    searchParameters.setOffset(params.offset());
    SearchContext context = new SearchContext(searchParameters, typeRegistry, graph, indexer.getVertexIndexKeys());
    // For future cancellations
    String searchID = searchTracker.add(context);
    try {
        List<AtlasVertex> resultList = context.getSearchProcessor().execute();
        // By default any attribute that shows up in the search parameter should be sent back in the response
        // If additional values are requested then the entityAttributes will be a superset of the all search attributes
        // and the explicitly requested attribute(s)
        Set<String> resultAttributes = new HashSet<>();
        Set<String> entityAttributes = new HashSet<>();
        if (CollectionUtils.isNotEmpty(searchParameters.getAttributes())) {
            resultAttributes.addAll(searchParameters.getAttributes());
        }
        if (CollectionUtils.isNotEmpty(context.getEntityAttributes())) {
            resultAttributes.addAll(context.getEntityAttributes());
        }
        AtlasEntityType entityType = context.getEntityType();
        if (entityType != null) {
            for (String resultAttribute : resultAttributes) {
                AtlasAttribute attribute = entityType.getAttribute(resultAttribute);
                if (attribute != null) {
                    AtlasType attributeType = attribute.getAttributeType();
                    if (attributeType instanceof AtlasArrayType) {
                        attributeType = ((AtlasArrayType) attributeType).getElementType();
                    }
                    if (attributeType instanceof AtlasEntityType || attributeType instanceof AtlasObjectIdType) {
                        entityAttributes.add(resultAttribute);
                    }
                }
            }
        }
        for (AtlasVertex atlasVertex : resultList) {
            AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(atlasVertex, resultAttributes);
            if (searchParameters.getIncludeClassificationAttributes()) {
                entity.setClassifications(entityRetriever.getAllClassifications(atlasVertex));
            }
            ret.addEntity(entity);
            // populate ret.referredEntities
            for (String entityAttribute : entityAttributes) {
                Object attrValue = entity.getAttribute(entityAttribute);
                if (attrValue instanceof AtlasObjectId) {
                    AtlasObjectId objId = (AtlasObjectId) attrValue;
                    if (ret.getReferredEntities() == null) {
                        ret.setReferredEntities(new HashMap<String, AtlasEntityHeader>());
                    }
                    if (!ret.getReferredEntities().containsKey(objId.getGuid())) {
                        ret.getReferredEntities().put(objId.getGuid(), entityRetriever.toAtlasEntityHeader(objId.getGuid()));
                    }
                } else if (attrValue instanceof Collection) {
                    Collection objIds = (Collection) attrValue;
                    for (Object obj : objIds) {
                        if (obj instanceof AtlasObjectId) {
                            AtlasObjectId objId = (AtlasObjectId) obj;
                            if (ret.getReferredEntities() == null) {
                                ret.setReferredEntities(new HashMap<String, AtlasEntityHeader>());
                            }
                            if (!ret.getReferredEntities().containsKey(objId.getGuid())) {
                                ret.getReferredEntities().put(objId.getGuid(), entityRetriever.toAtlasEntityHeader(objId.getGuid()));
                            }
                        }
                    }
                }
            }
        }
    } finally {
        searchTracker.remove(searchID);
    }
    return ret;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasObjectIdType(org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 27 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class SearchProcessor method isIndexSearchable.

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;
}
Also used : AtlasType(org.apache.atlas.type.AtlasType)

Example 28 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class SearchProcessor method toInMemoryPredicate.

private Predicate toInMemoryPredicate(AtlasStructType type, String attrName, SearchParameters.Operator op, String attrVal) {
    Predicate ret = null;
    AtlasAttribute attribute = type.getAttribute(attrName);
    VertexAttributePredicateGenerator predicate = OPERATOR_PREDICATE_MAP.get(op);
    if (attribute != null && predicate != null) {
        final AtlasType attrType = attribute.getAttributeType();
        final Class attrClass;
        final Object attrValue;
        // Some operators support null comparison, thus the parsing has to be conditional
        switch(attrType.getTypeName()) {
            case AtlasBaseTypeDef.ATLAS_TYPE_STRING:
                attrClass = String.class;
                attrValue = attrVal;
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_SHORT:
                attrClass = Short.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Short.parseShort(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_INT:
                attrClass = Integer.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Integer.parseInt(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_BIGINTEGER:
                attrClass = BigInteger.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : new BigInteger(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_BOOLEAN:
                attrClass = Boolean.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Boolean.parseBoolean(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_BYTE:
                attrClass = Byte.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Byte.parseByte(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_LONG:
            case AtlasBaseTypeDef.ATLAS_TYPE_DATE:
                attrClass = Long.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Long.parseLong(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_FLOAT:
                attrClass = Float.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Float.parseFloat(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_DOUBLE:
                attrClass = Double.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : Double.parseDouble(attrVal);
                break;
            case AtlasBaseTypeDef.ATLAS_TYPE_BIGDECIMAL:
                attrClass = BigDecimal.class;
                attrValue = StringUtils.isEmpty(attrVal) ? null : new BigDecimal(attrVal);
                break;
            default:
                if (attrType instanceof AtlasEnumType) {
                    attrClass = String.class;
                } else if (attrType instanceof AtlasArrayType) {
                    attrClass = List.class;
                } else {
                    attrClass = Object.class;
                }
                attrValue = attrVal;
                break;
        }
        ret = predicate.generatePredicate(attribute.getQualifiedName(), attrValue, attrClass);
    }
    return ret;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasEnumType(org.apache.atlas.type.AtlasEnumType) AtlasType(org.apache.atlas.type.AtlasType) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) BigDecimal(java.math.BigDecimal) Predicate(org.apache.commons.collections.Predicate)

Example 29 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class ExportService method addType.

private void addType(String typeName, ExportContext context) {
    AtlasType type = null;
    try {
        type = typeRegistry.getType(typeName);
        addType(type, context);
    } catch (AtlasBaseException excp) {
        LOG.error("unknown type {}", typeName);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType)

Example 30 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class AtlasMapFormatConverter method fromV1ToV2.

@Override
public Map fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    Map ret = null;
    if (v1Obj != null) {
        if (v1Obj instanceof Map) {
            AtlasMapType mapType = (AtlasMapType) type;
            AtlasType keyType = mapType.getKeyType();
            AtlasType valueType = mapType.getValueType();
            AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
            AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
            Map v1Map = (Map) v1Obj;
            ret = new HashMap<>();
            for (Object key : v1Map.keySet()) {
                Object value = v1Map.get(key);
                Object v2Key = keyConverter.fromV1ToV2(key, keyType, ctx);
                Object v2Value = valueConverter.fromV1ToV2(value, valueType, ctx);
                ret.put(v2Key, v2Value);
            }
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v1Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Aggregations

AtlasType (org.apache.atlas.type.AtlasType)95 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)51 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)33 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)23 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)17 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)17 AtlasStructType (org.apache.atlas.type.AtlasStructType)16 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)15 AtlasMapType (org.apache.atlas.type.AtlasMapType)13 ArrayList (java.util.ArrayList)11 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 HashMap (java.util.HashMap)8 AtlasTypeAccessRequest (org.apache.atlas.authorize.AtlasTypeAccessRequest)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)8 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)8 Map (java.util.Map)7 List (java.util.List)6 Collection (java.util.Collection)5 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)4