Search in sources :

Example 46 with ITypedReferenceableInstance

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

the class GraphHelperTest method testGetVerticesForGUIDSWithDuplicates.

@Test
public void testGetVerticesForGUIDSWithDuplicates() throws Exception {
    ITypedReferenceableInstance hrDept = TestUtils.createDeptEg1(TypeSystem.getInstance());
    List<String> result = repositoryService.createEntities(hrDept).getCreatedEntities();
    String guid = result.get(0);
    Map<String, AtlasVertex> verticesForGUIDs = GraphHelper.getInstance().getVerticesForGUIDs(Arrays.asList(guid, guid));
    Assert.assertEquals(verticesForGUIDs.size(), 1);
    Assert.assertTrue(verticesForGUIDs.containsKey(guid));
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Test(org.testng.annotations.Test)

Example 47 with ITypedReferenceableInstance

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

the class ReverseReferenceUpdateTestBase method testOneToOneReference.

@Test
public void testOneToOneReference() throws Exception {
    ITypedReferenceableInstance a = typeA.createInstance();
    a.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b1 = typeB.createInstance();
    b1.setString("name", TestUtils.randomString());
    a.set("b", b1);
    // Create a.  This should also create b1 and set the reverse b1->a reference.
    repositoryService.createEntities(a);
    a = repositoryService.getEntityDefinition("A", "name", a.getString("name"));
    b1 = repositoryService.getEntityDefinition("B", "name", b1.getString("name"));
    Object object = a.get("b");
    Assert.assertTrue(object instanceof ITypedReferenceableInstance);
    ITypedReferenceableInstance refValue = (ITypedReferenceableInstance) object;
    Assert.assertEquals(refValue.getId()._getId(), b1.getId()._getId());
    object = b1.get("a");
    Assert.assertTrue(object instanceof ITypedReferenceableInstance);
    refValue = (ITypedReferenceableInstance) object;
    Assert.assertEquals(refValue.getId()._getId(), a.getId()._getId());
    ITypedReferenceableInstance b2 = typeB.createInstance();
    b2.setString("name", TestUtils.randomString());
    b2.set("a", a.getId());
    // Create b2.  This should set the reverse a->b2 reference
    // and disconnect b1->a.
    repositoryService.createEntities(b2);
    a = repositoryService.getEntityDefinition(a.getId()._getId());
    b2 = repositoryService.getEntityDefinition("B", "name", b2.getString("name"));
    object = a.get("b");
    Assert.assertTrue(object instanceof ITypedReferenceableInstance);
    refValue = (ITypedReferenceableInstance) object;
    Assert.assertEquals(refValue.getId()._getId(), b2.getId()._getId());
    object = b2.get("a");
    Assert.assertTrue(object instanceof ITypedReferenceableInstance);
    refValue = (ITypedReferenceableInstance) object;
    Assert.assertEquals(refValue.getId()._getId(), a.getId()._getId());
    // Verify b1->a was disconnected.
    b1 = repositoryService.getEntityDefinition("B", "name", b1.getString("name"));
    object = b1.get("a");
    assertTestOneToOneReference(object, a, b1);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Test(org.testng.annotations.Test)

Example 48 with ITypedReferenceableInstance

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

the class MemRepository method get.

public ITypedReferenceableInstance get(Id id) throws RepositoryException {
    try {
        ReplaceIdWithInstance replacer = new ReplaceIdWithInstance(this);
        ObjectGraphWalker walker = new ObjectGraphWalker(typeSystem, replacer);
        replacer.setWalker(walker);
        ITypedReferenceableInstance r = getDuringWalk(id, walker);
        walker.walk();
        return r;
    } catch (AtlasException me) {
        throw new RepositoryException("TypeSystem error when walking the ObjectGraph", me);
    }
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ObjectGraphWalker(org.apache.atlas.typesystem.types.ObjectGraphWalker) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasException(org.apache.atlas.AtlasException)

Example 49 with ITypedReferenceableInstance

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

the class MemRepository method getDuringWalk.

/*
     * - Id must be valid; Class must be valid.
     * - Ask ClassStore to createInstance.
     * - Ask ClassStore to load instance.
     * - load instance traits
     * - add to GraphWalker
     */
ITypedReferenceableInstance getDuringWalk(Id id, ObjectGraphWalker walker) throws RepositoryException {
    ClassStore cS = getClassStore(id.getTypeName());
    if (cS == null) {
        throw new RepositoryException(String.format("Unknown Class %s", id.getTypeName()));
    }
    cS.validate(this, id);
    ReferenceableInstance r = cS.createInstance(this, id);
    cS.load(r);
    for (String traitName : r.getTraits()) {
        HierarchicalTypeStore tt = typeStores.get(traitName);
        tt.load(r);
    }
    walker.addRoot(r);
    return r;
}
Also used : RepositoryException(org.apache.atlas.repository.RepositoryException) ReferenceableInstance(org.apache.atlas.typesystem.persistence.ReferenceableInstance) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance)

Example 50 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance 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

ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)142 Test (org.testng.annotations.Test)54 List (java.util.List)34 ArrayList (java.util.ArrayList)29 Id (org.apache.atlas.typesystem.persistence.Id)28 Referenceable (org.apache.atlas.typesystem.Referenceable)22 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)21 ImmutableList (com.google.common.collect.ImmutableList)20 ClassType (org.apache.atlas.typesystem.types.ClassType)19 AtlasException (org.apache.atlas.AtlasException)16 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)16 HashMap (java.util.HashMap)15 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)14 EntityResult (org.apache.atlas.model.legacy.EntityResult)12 IStruct (org.apache.atlas.typesystem.IStruct)10 Map (java.util.Map)9 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 RepositoryException (org.apache.atlas.repository.RepositoryException)9 BeforeTest (org.testng.annotations.BeforeTest)9