Search in sources :

Example 1 with StructType

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

the class GraphBackedSearchIndexer method addIndexForType.

private void addIndexForType(AtlasGraphManagement management, IDataType dataType) {
    switch(dataType.getTypeCategory()) {
        case PRIMITIVE:
        case ENUM:
        case ARRAY:
        case MAP:
            // and not types like structs, traits or classes
            break;
        case STRUCT:
            StructType structType = (StructType) dataType;
            createIndexForFields(management, structType, structType.fieldMapping().fields);
            break;
        case TRAIT:
            TraitType traitType = (TraitType) dataType;
            createIndexForFields(management, traitType, traitType.fieldMapping().fields);
            break;
        case CLASS:
            ClassType classType = (ClassType) dataType;
            createIndexForFields(management, classType, classType.fieldMapping().fields);
            break;
        default:
            throw new IllegalArgumentException("bad data type" + dataType);
    }
}
Also used : StructType(org.apache.atlas.typesystem.types.StructType) AtlasStructType(org.apache.atlas.type.AtlasStructType) TraitType(org.apache.atlas.typesystem.types.TraitType) ClassType(org.apache.atlas.typesystem.types.ClassType)

Example 2 with StructType

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

the class GraphToTypedInstanceMapper method getReferredEntity.

public ITypedInstance getReferredEntity(String edgeId, IDataType<?> referredType) throws AtlasException {
    final AtlasEdge edge = getGraph().getEdge(edgeId);
    if (edge != null) {
        final AtlasVertex referredVertex = edge.getInVertex();
        if (referredVertex != null) {
            switch(referredType.getTypeCategory()) {
                case STRUCT:
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Found struct instance vertex {}, mapping to instance {} ", referredVertex, referredType.getName());
                    }
                    StructType structType = (StructType) referredType;
                    ITypedStruct instance = structType.createInstance();
                    Map<String, AttributeInfo> fields = structType.fieldMapping().fields;
                    mapVertexToInstance(referredVertex, instance, fields);
                    return instance;
                case CLASS:
                    //TODO isComposite handling for class loads
                    return GraphHelper.getIdFromVertex(referredType.getName(), referredVertex);
                default:
                    throw new UnsupportedOperationException("Loading " + referredType.getTypeCategory() + " is not supported");
            }
        }
    }
    return null;
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) StructType(org.apache.atlas.typesystem.types.StructType) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 3 with StructType

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

the class DefaultGraphPersistenceStrategy method constructInstance.

@Override
public <U> U constructInstance(IDataType<U> dataType, Object value) {
    try {
        switch(dataType.getTypeCategory()) {
            case PRIMITIVE:
            case ENUM:
                return dataType.convert(value, Multiplicity.OPTIONAL);
            case ARRAY:
                DataTypes.ArrayType arrType = (DataTypes.ArrayType) dataType;
                IDataType<?> elemType = arrType.getElemType();
                ImmutableCollection.Builder result = ImmutableList.builder();
                List list = (List) value;
                for (Object listElement : list) {
                    Object collectionEntry = constructCollectionEntry(elemType, listElement);
                    if (collectionEntry != null) {
                        result.add(collectionEntry);
                    }
                }
                return (U) result.build();
            case MAP:
                // todo
                break;
            case STRUCT:
                AtlasVertex structVertex = (AtlasVertex) value;
                StructType structType = (StructType) dataType;
                ITypedStruct structInstance = structType.createInstance();
                TypeSystem.IdType idType = TypeSystem.getInstance().getIdType();
                if (dataType.getName().equals(idType.getName())) {
                    structInstance.set(idType.typeNameAttrName(), GraphHelper.getSingleValuedProperty(structVertex, typeAttributeName(), String.class));
                    structInstance.set(idType.idAttrName(), GraphHelper.getSingleValuedProperty(structVertex, idAttributeName(), String.class));
                    String stateValue = GraphHelper.getSingleValuedProperty(structVertex, stateAttributeName(), String.class);
                    if (stateValue != null) {
                        structInstance.set(idType.stateAttrName(), stateValue);
                    }
                    structInstance.set(idType.versionAttrName(), structVertex.getProperty(versionAttributeName(), Integer.class));
                } else {
                    metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(structVertex, structInstance, structType.fieldMapping().fields);
                }
                return dataType.convert(structInstance, Multiplicity.OPTIONAL);
            case TRAIT:
                AtlasVertex traitVertex = (AtlasVertex) value;
                TraitType traitType = (TraitType) dataType;
                ITypedStruct traitInstance = traitType.createInstance();
                // todo - this is not right, we should load the Instance associated with this
                // trait. for now just loading the trait struct.
                // metadataRepository.getGraphToInstanceMapper().mapVertexToTraitInstance(
                //        traitVertex, dataType.getName(), , traitType, traitInstance);
                metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(traitVertex, traitInstance, traitType.fieldMapping().fields);
                break;
            case CLASS:
                AtlasVertex classVertex = (AtlasVertex) value;
                String guid = classVertex.getProperty(Constants.GUID_PROPERTY_KEY, String.class);
                // Check if the instance we need was previously loaded.
                ITypedReferenceableInstance classInstance = RequestContext.get().getInstanceV1(guid);
                if (classInstance == null) {
                    classInstance = metadataRepository.getGraphToInstanceMapper().mapGraphToTypedInstance(guid, classVertex);
                }
                return dataType.convert(classInstance, Multiplicity.OPTIONAL);
            default:
                throw new UnsupportedOperationException("Load for type " + dataType + "is not supported");
        }
    } catch (AtlasException e) {
        LOG.error("error while constructing an instance", e);
    }
    return null;
}
Also used : ImmutableCollection(com.google.common.collect.ImmutableCollection) TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) StructType(org.apache.atlas.typesystem.types.StructType) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasException(org.apache.atlas.AtlasException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 4 with StructType

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

the class GraphBackedSearchIndexer method createIndexForAttribute.

private void createIndexForAttribute(AtlasGraphManagement management, String typeName, AttributeInfo field) {
    final String propertyName = GraphHelper.encodePropertyKey(typeName + "." + field.name);
    switch(field.dataType().getTypeCategory()) {
        case PRIMITIVE:
            AtlasCardinality cardinality = getCardinality(field.multiplicity);
            createIndexes(management, propertyName, getPrimitiveClass(field.dataType()), field.isUnique, cardinality, false, field.isIndexable);
            break;
        case ENUM:
            cardinality = getCardinality(field.multiplicity);
            createIndexes(management, propertyName, String.class, field.isUnique, cardinality, false, field.isIndexable);
            break;
        case ARRAY:
        case MAP:
            // IGNORE: Can only index single-valued property keys on vertices in Mixed Index
            break;
        case STRUCT:
            StructType structType = (StructType) field.dataType();
            createIndexForFields(management, structType, structType.fieldMapping().fields);
            break;
        case TRAIT:
            // do nothing since this is NOT contained in other types
            break;
        case CLASS:
            //createEdgeMixedIndex(propertyName);
            break;
        default:
            throw new IllegalArgumentException("bad data type" + field.dataType().getName());
    }
}
Also used : AtlasCardinality(org.apache.atlas.repository.graphdb.AtlasCardinality) StructType(org.apache.atlas.typesystem.types.StructType) AtlasStructType(org.apache.atlas.type.AtlasStructType)

Example 5 with StructType

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

the class StructInstance method getSignatureHash.

@Override
public String getSignatureHash(MessageDigest digester) throws AtlasException {
    StructType structType = TypeSystem.getInstance().getDataType(StructType.class, getTypeName());
    structType.updateSignatureHash(digester, this);
    byte[] digest = digester.digest();
    return MD5Utils.toString(digest);
}
Also used : StructType(org.apache.atlas.typesystem.types.StructType)

Aggregations

StructType (org.apache.atlas.typesystem.types.StructType)5 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 AtlasStructType (org.apache.atlas.type.AtlasStructType)2 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)2 TraitType (org.apache.atlas.typesystem.types.TraitType)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 AtlasException (org.apache.atlas.AtlasException)1 AtlasCardinality (org.apache.atlas.repository.graphdb.AtlasCardinality)1 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)1 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)1 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)1 ClassType (org.apache.atlas.typesystem.types.ClassType)1 DataTypes (org.apache.atlas.typesystem.types.DataTypes)1 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)1