Search in sources :

Example 76 with ITypedReferenceableInstance

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

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

the class ReverseReferenceUpdateTestBase method testOneToManyReference.

@Test
public void testOneToManyReference() throws Exception {
    ITypedReferenceableInstance a1 = typeA.createInstance();
    a1.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance a2 = typeA.createInstance();
    a2.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b1 = typeB.createInstance();
    b1.setString("name", TestUtils.randomString());
    a1.set("oneB", b1);
    ITypedReferenceableInstance b2 = typeB.createInstance();
    b2.setString("name", TestUtils.randomString());
    repositoryService.createEntities(a1, a2, b2);
    a1 = repositoryService.getEntityDefinition("A", "name", a1.getString("name"));
    a2 = repositoryService.getEntityDefinition("A", "name", a2.getString("name"));
    b1 = repositoryService.getEntityDefinition("B", "name", b1.getString("name"));
    b2 = repositoryService.getEntityDefinition("B", "name", b2.getString("name"));
    Object object = b1.get("manyA");
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refValues = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refValues.size(), 1);
    Assert.assertTrue(refValues.contains(a1.getId()));
    a2.set("oneB", b1.getId());
    repositoryService.updateEntities(a2);
    b1 = repositoryService.getEntityDefinition(b1.getId()._getId());
    object = b1.get("manyA");
    Assert.assertTrue(object instanceof List);
    refValues = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refValues.size(), 2);
    Assert.assertTrue(refValues.containsAll(Arrays.asList(a1.getId(), a2.getId())));
    b2.set("manyA", Collections.singletonList(a2));
    repositoryService.updateEntities(b2);
    a2 = repositoryService.getEntityDefinition("A", "name", a2.getString("name"));
    // Verify reverse a2.oneB reference was set to b2.
    object = a2.get("oneB");
    Assert.assertTrue(object instanceof ITypedReferenceableInstance);
    ITypedReferenceableInstance refValue = (ITypedReferenceableInstance) object;
    Assert.assertEquals(refValue.getId()._getId(), b2.getId()._getId());
    // Verify a2 was removed from b1.manyA reference list.
    b1 = repositoryService.getEntityDefinition(b1.getId()._getId());
    object = b1.get("manyA");
    assertTestOneToManyReference(object, b1);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 78 with ITypedReferenceableInstance

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

the class ReverseReferenceUpdateTestBase method testCallerHasSetBothEnds.

/**
     * Verify that explicitly setting both ends of a reference
     * does not cause duplicate entries due to auto-update of
     * reverse reference.
     */
@Test
public void testCallerHasSetBothEnds() throws Exception {
    ITypedReferenceableInstance a = typeA.createInstance();
    a.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b1 = typeB.createInstance();
    b1.setString("name", TestUtils.randomString());
    // Set both sides of the reference.
    a.set("oneB", b1);
    b1.set("manyA", Collections.singletonList(a));
    CreateUpdateEntitiesResult result = repositoryService.createEntities(a);
    Map<String, String> guidAssignments = result.getGuidMapping().getGuidAssignments();
    String aGuid = a.getId()._getId();
    String b1Guid = guidAssignments.get(b1.getId()._getId());
    a = repositoryService.getEntityDefinition(aGuid);
    Object object = a.get("oneB");
    Assert.assertTrue(object instanceof ITypedReferenceableInstance);
    Assert.assertEquals(((ITypedReferenceableInstance) object).getId()._getId(), b1Guid);
    b1 = repositoryService.getEntityDefinition(b1Guid);
    object = b1.get("manyA");
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refValues = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refValues.size(), 1);
    Assert.assertEquals(refValues.get(0).getId()._getId(), aGuid);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 79 with ITypedReferenceableInstance

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

the class ReverseReferenceUpdateTestBase method testManyToManyReference.

@Test
public void testManyToManyReference() throws Exception {
    ITypedReferenceableInstance a1 = typeA.createInstance();
    a1.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance a2 = typeA.createInstance();
    a2.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b1 = typeB.createInstance();
    b1.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b2 = typeB.createInstance();
    b2.setString("name", TestUtils.randomString());
    repositoryService.createEntities(a1, a2, b1, b2);
    a1 = repositoryService.getEntityDefinition("A", "name", a1.getString("name"));
    a2 = repositoryService.getEntityDefinition("A", "name", a2.getString("name"));
    b1 = repositoryService.getEntityDefinition("B", "name", b1.getString("name"));
    b2 = repositoryService.getEntityDefinition("B", "name", b2.getString("name"));
    // Update a1 to add b1 to its manyB reference.
    // This should update b1.manyToManyA.
    a1.set("manyB", Arrays.asList(b1.getId()));
    repositoryService.updateEntities(a1);
    // Verify reverse b1.manyToManyA reference was updated.
    b1 = repositoryService.getEntityDefinition(b1.getId()._getId());
    Object object = b1.get("manyToManyA");
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refValues = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refValues.size(), 1);
    Assert.assertTrue(refValues.contains(a1.getId()));
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 80 with ITypedReferenceableInstance

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

the class ReverseReferenceUpdateTestBase method testMapReference.

/**
     * Auto-update of bi-directional references where one end is a map reference is
     * not currently supported.  Verify that the auto-update is not applied in this case.
     */
@Test
public void testMapReference() throws Exception {
    ITypedReferenceableInstance a1 = typeA.createInstance();
    a1.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance a2 = typeA.createInstance();
    a2.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b1 = typeB.createInstance();
    b1.setString("name", TestUtils.randomString());
    ITypedReferenceableInstance b2 = typeB.createInstance();
    b2.setString("name", TestUtils.randomString());
    repositoryService.createEntities(a1, a2, b1, b2);
    a1 = repositoryService.getEntityDefinition("A", "name", a1.getString("name"));
    a2 = repositoryService.getEntityDefinition("A", "name", a2.getString("name"));
    b1 = repositoryService.getEntityDefinition("B", "name", b1.getString("name"));
    b2 = repositoryService.getEntityDefinition("B", "name", b2.getString("name"));
    a1.set("map", Collections.singletonMap("b1", b1));
    repositoryService.updateEntities(a1);
    // Verify reverse b1.manyToManyA reference was not updated.
    b1 = repositoryService.getEntityDefinition(b1.getId()._getId());
    Object object = b1.get("backToMap");
    Assert.assertNull(object);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Test(org.testng.annotations.Test)

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