Search in sources :

Example 1 with TraitType

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

the class StoreBackedTypeCacheTest method testGetTraitType.

@Test
public void testGetTraitType() throws Exception {
    ImmutableList<String> traitNames = ts.getTypeNamesByCategory(TypeCategory.TRAIT);
    for (String traitTypeName : traitNames) {
        // Not cached yet
        Assert.assertFalse(typeCache.isCachedInMemory(traitTypeName));
        IDataType dataType = typeCache.get(traitTypeName);
        // Verify the type is now cached.
        Assert.assertTrue(typeCache.isCachedInMemory(traitTypeName));
        Assert.assertTrue(dataType instanceof TraitType);
        TraitType cachedType = (TraitType) dataType;
        // Verify that get() also loaded and cached any dependencies of this type from the type store.
        verifyHierarchicalType(cachedType, ts.getDataType(TraitType.class, traitTypeName));
    }
}
Also used : TraitType(org.apache.atlas.typesystem.types.TraitType) IDataType(org.apache.atlas.typesystem.types.IDataType) Test(org.testng.annotations.Test)

Example 2 with TraitType

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

the class EntityNotificationImpl method getSuperTraits.

private static List<IStruct> getSuperTraits(String typeName, Map<String, Object> values, TypeSystem typeSystem) throws AtlasException {
    List<IStruct> superTypes = new LinkedList<>();
    TraitType traitDef = typeSystem.getDataType(TraitType.class, typeName);
    Set<String> superTypeNames = traitDef.getAllSuperTypeNames();
    for (String superTypeName : superTypeNames) {
        TraitType superTraitDef = typeSystem.getDataType(TraitType.class, superTypeName);
        Map<String, Object> superTypeValues = new HashMap<>();
        FieldMapping fieldMapping = superTraitDef.fieldMapping();
        if (fieldMapping != null) {
            Set<String> superTypeAttributeNames = fieldMapping.fields.keySet();
            for (String superTypeAttributeName : superTypeAttributeNames) {
                if (values.containsKey(superTypeAttributeName)) {
                    superTypeValues.put(superTypeAttributeName, values.get(superTypeAttributeName));
                }
            }
        }
        IStruct superTrait = new Struct(superTypeName, superTypeValues);
        superTypes.add(superTrait);
        superTypes.addAll(getSuperTraits(superTypeName, values, typeSystem));
    }
    return superTypes;
}
Also used : HashMap(java.util.HashMap) TraitType(org.apache.atlas.typesystem.types.TraitType) FieldMapping(org.apache.atlas.typesystem.types.FieldMapping) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 3 with TraitType

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

the class GraphBackedMetadataRepositoryTest method testAddTraitWithAttribute.

@Test(dependsOnMethods = "testAddTrait")
public void testAddTraitWithAttribute() throws Exception {
    final String aGUID = getGUID();
    final String traitName = "P_I_I";
    HierarchicalTypeDefinition<TraitType> piiTrait = TypesUtil.createTraitTypeDef(traitName, ImmutableSet.<String>of(), TypesUtil.createRequiredAttrDef("type", DataTypes.STRING_TYPE));
    TraitType traitType = typeSystem.defineTraitType(piiTrait);
    ITypedStruct traitInstance = traitType.createInstance();
    traitInstance.set("type", "SSN");
    repositoryService.addTrait(aGUID, traitInstance);
    TestUtils.dumpGraph(TestUtils.getGraph());
    // refresh trait names
    List<String> traitNames = repositoryService.getTraitNames(aGUID);
    Assert.assertEquals(traitNames.size(), 3);
    Assert.assertTrue(traitNames.contains(traitName));
    ITypedReferenceableInstance instance = repositoryService.getEntityDefinition(aGUID);
    IStruct traitInstanceRef = instance.getTrait(traitName);
    String type = (String) traitInstanceRef.get("type");
    Assert.assertEquals(type, "SSN");
}
Also used : TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 4 with TraitType

use of org.apache.atlas.typesystem.types.TraitType 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 5 with TraitType

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

the class MemRepository method create.

/**
 * 1. traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id],
 *    also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
 *   - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
 *   - traverse Structs
 *   - traverse Traits.
 * 1b. Ensure that every newId has an associated Instance.
 * 2. Traverse oldIdToInstance map create newInstances : List[ITypedReferenceableInstance]
 *    - create a ITypedReferenceableInstance.
 *      replace any old References ( ids or object references) with new Ids.
 * 3. Traverse over newInstances
 *    - ask ClassStore to assign a position to the Id.
 *      - for Instances with Traits, assign a position for each Trait
 *    - invoke store on the nwInstance.
 *
 * Recovery:
 * - on each newInstance, invoke releaseId and delete on its ClassStore and Traits' Stores.
 *
 * @param i
 * @return
 * @throws org.apache.atlas.repository.RepositoryException
 */
public ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException {
    DiscoverInstances discoverInstances = new DiscoverInstances(this);
    /*
         * Step 1: traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id],
         *    also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
         *   - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
         *   - traverse Structs
         *   - traverse Traits.
         */
    try {
        new ObjectGraphWalker(typeSystem, discoverInstances, i).walk();
    } catch (AtlasException me) {
        throw new RepositoryException("TypeSystem error when walking the ObjectGraph", me);
    }
    /*
         * Step 1b: Ensure that every newId has an associated Instance.
         */
    for (Id oldId : discoverInstances.idToNewIdMap.keySet()) {
        if (!discoverInstances.idToInstanceMap.containsKey(oldId)) {
            throw new RepositoryException(String.format("Invalid Object Graph: " + "Encountered an unassignedId %s that is not associated with an Instance", oldId));
        }
    }
    /* Step 2: Traverse oldIdToInstance map create newInstances :
        List[ITypedReferenceableInstance]
         * - create a ITypedReferenceableInstance.
         *   replace any old References ( ids or object references) with new Ids.
        */
    List<ITypedReferenceableInstance> newInstances = new ArrayList<>();
    ITypedReferenceableInstance retInstance = null;
    Set<ClassType> classTypes = new TreeSet<>();
    Set<TraitType> traitTypes = new TreeSet<>();
    for (IReferenceableInstance transientInstance : discoverInstances.idToInstanceMap.values()) {
        try {
            ClassType cT = typeSystem.getDataType(ClassType.class, transientInstance.getTypeName());
            ITypedReferenceableInstance newInstance = cT.convert(transientInstance, Multiplicity.REQUIRED);
            newInstances.add(newInstance);
            classTypes.add(cT);
            for (String traitName : newInstance.getTraits()) {
                TraitType tT = typeSystem.getDataType(TraitType.class, traitName);
                traitTypes.add(tT);
            }
            if (newInstance.getId() == i.getId()) {
                retInstance = newInstance;
            }
            /*
                 * Now replace old references with new Ids
                 */
            MapIds mapIds = new MapIds(discoverInstances.idToNewIdMap);
            new ObjectGraphWalker(typeSystem, mapIds, newInstances).walk();
        } catch (AtlasException me) {
            throw new RepositoryException(String.format("Failed to create Instance(id = %s", transientInstance.getId()), me);
        }
    }
    /*
         * 3. Acquire Class and Trait Storage locks.
         * - acquire them in a stable order (super before subclass, classes before traits
         */
    for (ClassType cT : classTypes) {
        HierarchicalTypeStore st = typeStores.get(cT.getName());
        st.acquireWriteLock();
    }
    for (TraitType tT : traitTypes) {
        HierarchicalTypeStore st = typeStores.get(tT.getName());
        st.acquireWriteLock();
    }
    /*
         * 4. Traverse over newInstances
         *    - ask ClassStore to assign a position to the Id.
         *      - for Instances with Traits, assign a position for each Trait
         *    - invoke store on the nwInstance.
         */
    try {
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.assignPosition(instance.getId());
            for (String traitName : instance.getTraits()) {
                HierarchicalTypeStore tt = typeStores.get(traitName);
                tt.assignPosition(instance.getId());
            }
        }
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.store((ReferenceableInstance) instance);
            for (String traitName : instance.getTraits()) {
                HierarchicalTypeStore tt = typeStores.get(traitName);
                tt.store((ReferenceableInstance) instance);
            }
        }
    } catch (RepositoryException re) {
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.releaseId(instance.getId());
        }
        throw re;
    } finally {
        for (ClassType cT : classTypes) {
            HierarchicalTypeStore st = typeStores.get(cT.getName());
            st.releaseWriteLock();
        }
        for (TraitType tT : traitTypes) {
            HierarchicalTypeStore st = typeStores.get(tT.getName());
            st.releaseWriteLock();
        }
    }
    return retInstance;
}
Also used : DiscoverInstances(org.apache.atlas.repository.DiscoverInstances) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TraitType(org.apache.atlas.typesystem.types.TraitType) ObjectGraphWalker(org.apache.atlas.typesystem.types.ObjectGraphWalker) ArrayList(java.util.ArrayList) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasException(org.apache.atlas.AtlasException) ClassType(org.apache.atlas.typesystem.types.ClassType) MapIds(org.apache.atlas.typesystem.persistence.MapIds) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) TreeSet(java.util.TreeSet) Id(org.apache.atlas.typesystem.persistence.Id)

Aggregations

TraitType (org.apache.atlas.typesystem.types.TraitType)40 Test (org.testng.annotations.Test)22 ClassType (org.apache.atlas.typesystem.types.ClassType)19 TypesDef (org.apache.atlas.typesystem.TypesDef)13 Referenceable (org.apache.atlas.typesystem.Referenceable)12 Struct (org.apache.atlas.typesystem.Struct)12 Id (org.apache.atlas.typesystem.persistence.Id)10 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)10 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)7 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)7 IStruct (org.apache.atlas.typesystem.IStruct)6 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)6 ArrayList (java.util.ArrayList)5 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)5 AtlasServiceException (org.apache.atlas.AtlasServiceException)4 HierarchicalTypeDefinition (org.apache.atlas.typesystem.types.HierarchicalTypeDefinition)3 BeforeTest (org.testng.annotations.BeforeTest)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HashMap (java.util.HashMap)2