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));
}
}
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;
}
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");
}
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);
}
}
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;
}
Aggregations