Search in sources :

Example 46 with Id

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

the class GraphBackedRepositorySoftDeleteTest method assertMaxForTestDisconnectBidirectionalReferences.

@Override
protected void assertMaxForTestDisconnectBidirectionalReferences(Map<String, String> nameGuidMap) throws Exception {
    // Verify that the Department.employees reference to the deleted employee
    // was disconnected.
    ITypedReferenceableInstance hrDept = repositoryService.getEntityDefinition(nameGuidMap.get("hr"));
    List<ITypedReferenceableInstance> employees = (List<ITypedReferenceableInstance>) hrDept.get("employees");
    Assert.assertEquals(employees.size(), 4);
    String maxGuid = nameGuidMap.get("Max");
    for (ITypedReferenceableInstance employee : employees) {
        if (employee.getId()._getId().equals(maxGuid)) {
            assertEquals(employee.getId().getState(), Id.EntityState.DELETED);
        }
    }
    // Verify that the Manager.subordinates still references deleted employee
    ITypedReferenceableInstance jane = repositoryService.getEntityDefinition(nameGuidMap.get("Jane"));
    List<ITypedReferenceableInstance> subordinates = (List<ITypedReferenceableInstance>) jane.get("subordinates");
    assertEquals(subordinates.size(), 2);
    for (ITypedReferenceableInstance subordinate : subordinates) {
        if (subordinate.getId()._getId().equals(maxGuid)) {
            assertEquals(subordinate.getId().getState(), Id.EntityState.DELETED);
        }
    }
    // Verify that max's Person.mentor unidirectional reference to john was disconnected.
    ITypedReferenceableInstance john = repositoryService.getEntityDefinition(nameGuidMap.get("John"));
    Id mentor = (Id) john.get("mentor");
    assertEquals(mentor._getId(), maxGuid);
    assertEquals(mentor.getState(), Id.EntityState.DELETED);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id)

Example 47 with Id

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

the class GraphBackedRepositorySoftDeleteTest method assertJohnForTestDisconnectBidirectionalReferences.

@Override
protected void assertJohnForTestDisconnectBidirectionalReferences(ITypedReferenceableInstance john, String janeGuid) throws Exception {
    Id mgr = (Id) john.get("manager");
    assertNotNull(mgr);
    assertEquals(mgr._getId(), janeGuid);
    assertEquals(mgr.getState(), Id.EntityState.DELETED);
}
Also used : Id(org.apache.atlas.typesystem.persistence.Id)

Example 48 with Id

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

the class EntityNotificationIT method testDeleteEntity.

@Test
public void testDeleteEntity() throws Exception {
    final String tableName = "table-" + randomString();
    final String dbName = "db-" + randomString();
    Referenceable HiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbId = createInstance(HiveDBInstance);
    Referenceable tableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    final Id tableId = createInstance(tableInstance);
    final String guid = tableId._getId();
    waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.ENTITY_CREATE, HIVE_TABLE_TYPE_BUILTIN, guid));
    final String name = (String) tableInstance.get(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME);
    atlasClientV1.deleteEntity(HIVE_TABLE_TYPE_BUILTIN, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, name);
    waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.ENTITY_DELETE, HIVE_TABLE_TYPE_BUILTIN, guid));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) Id(org.apache.atlas.typesystem.persistence.Id) Test(org.testng.annotations.Test)

Example 49 with Id

use of org.apache.atlas.typesystem.persistence.Id 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)

Example 50 with Id

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

the class ReplaceIdWithInstance method convertToInstances.

ImmutableMap<?, ?> convertToInstances(ImmutableMap val, Multiplicity m, DataTypes.MapType mapType) throws AtlasException {
    if (val == null || (mapType.getKeyType().getTypeCategory() != DataTypes.TypeCategory.CLASS && mapType.getValueType().getTypeCategory() != DataTypes.TypeCategory.CLASS)) {
        return val;
    }
    ImmutableMap.Builder b = ImmutableMap.builder();
    for (Map.Entry elem : (Iterable<Map.Entry>) val.entrySet()) {
        Object oldKey = elem.getKey();
        Object oldValue = elem.getValue();
        Object newKey = oldKey;
        Object newValue = oldValue;
        if (oldKey instanceof Id) {
            Id id = (Id) elem;
            ITypedReferenceableInstance r = getInstance(id);
        }
        if (oldValue instanceof Id) {
            Id id = (Id) elem;
            ITypedReferenceableInstance r = getInstance(id);
        }
        b.put(newKey, newValue);
    }
    return b.build();
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Id (org.apache.atlas.typesystem.persistence.Id)94 Referenceable (org.apache.atlas.typesystem.Referenceable)50 Test (org.testng.annotations.Test)37 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)28 List (java.util.List)17 ArrayList (java.util.ArrayList)12 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)12 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)12 ImmutableList (com.google.common.collect.ImmutableList)10 TraitType (org.apache.atlas.typesystem.types.TraitType)10 JSONObject (org.codehaus.jettison.json.JSONObject)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AtlasServiceException (org.apache.atlas.AtlasServiceException)7 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)7 Struct (org.apache.atlas.typesystem.Struct)7 ClassType (org.apache.atlas.typesystem.types.ClassType)7 AtlasException (org.apache.atlas.AtlasException)6 EntityResult (org.apache.atlas.model.legacy.EntityResult)6 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5