use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method findExistingVertices.
private Map<Id, AtlasVertex> findExistingVertices(Collection<IReferenceableInstance> instances) throws AtlasException {
VertexLookupContext context = new VertexLookupContext(this);
Map<Id, AtlasVertex> result = new HashMap<>();
for (IReferenceableInstance instance : instances) {
context.addInstance(instance);
}
List<Id> instancesToLoad = new ArrayList<>(context.getInstancesToLoadByGuid());
List<String> guidsToLoad = Lists.transform(instancesToLoad, new Function<Id, String>() {
@Override
public String apply(Id instance) {
Id id = getExistingId(instance);
return id.id;
}
});
Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToLoad);
List<String> missingGuids = new ArrayList<>();
for (int i = 0; i < instancesToLoad.size(); i++) {
String guid = guidsToLoad.get(i);
AtlasVertex instanceVertex = instanceVertices.get(guid);
if (instanceVertex == null) {
missingGuids.add(guid);
continue;
}
Id instance = instancesToLoad.get(i);
if (LOG.isDebugEnabled()) {
LOG.debug("Found vertex {} for instance {}", string(instanceVertex), instance);
}
result.put(instance, instanceVertex);
}
if (missingGuids.size() > 0) {
throw new EntityNotFoundException("Could not find entities in the repository with the following GUIDs: " + missingGuids);
}
for (Map.Entry<ClassType, List<IReferenceableInstance>> entry : context.getInstancesToLoadByUniqueAttribute().entrySet()) {
ClassType type = entry.getKey();
List<IReferenceableInstance> instancesForClass = entry.getValue();
List<AtlasVertex> correspondingVertices = graphHelper.getVerticesForInstancesByUniqueAttribute(type, instancesForClass);
for (int i = 0; i < instancesForClass.size(); i++) {
IReferenceableInstance inst = instancesForClass.get(i);
AtlasVertex vertex = correspondingVertices.get(i);
result.put(getExistingId(inst), vertex);
}
}
return result;
}
use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.
the class VertexLookupContext method addAdditionalInstance.
private void addAdditionalInstance(ITypedReferenceableInstance instance) {
if (instance == null) {
return;
}
Id id = mapper.getExistingId(instance);
if (!id.isAssigned()) {
return;
}
guidsToLookup.add(id);
}
use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.
the class GraphHelper method createVertexWithIdentity.
public AtlasVertex createVertexWithIdentity(ITypedReferenceableInstance typedInstance, Set<String> superTypeNames) {
final String guid = UUID.randomUUID().toString();
final AtlasVertex vertexWithIdentity = createVertexWithoutIdentity(typedInstance.getTypeName(), new Id(guid, 0, typedInstance.getTypeName()), superTypeNames);
// add identity
setProperty(vertexWithIdentity, Constants.GUID_PROPERTY_KEY, guid);
// add version information
setProperty(vertexWithIdentity, Constants.VERSION_PROPERTY_KEY, typedInstance.getId().version);
return vertexWithIdentity;
}
use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.
the class GraphToTypedInstanceMapper method mapGraphToTypedInstance.
public ITypedReferenceableInstance mapGraphToTypedInstance(String guid, AtlasVertex instanceVertex) throws AtlasException {
if (LOG.isDebugEnabled()) {
//place to add a check to see if there are any places that were missed.
if (RequestContext.get().getInstanceV1(guid) != null) {
LOG.warn("Looking up previously cached guid at: ", new Exception());
}
LOG.debug("Mapping graph root vertex {} to typed instance for guid {}", instanceVertex, guid);
}
String typeName = GraphHelper.getSingleValuedProperty(instanceVertex, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class);
List<String> traits = GraphHelper.getTraitNames(instanceVertex);
String state = GraphHelper.getStateAsString(instanceVertex);
String createdBy = GraphHelper.getCreatedByAsString(instanceVertex);
String modifiedBy = GraphHelper.getModifiedByAsString(instanceVertex);
Date createdTime = new Date(GraphHelper.getCreatedTime(instanceVertex));
Date modifiedTime = new Date(GraphHelper.getModifiedTime(instanceVertex));
AtlasSystemAttributes systemAttributes = new AtlasSystemAttributes(createdBy, modifiedBy, createdTime, modifiedTime);
if (LOG.isDebugEnabled()) {
LOG.debug("Found createdBy : {} modifiedBy : {} createdTime: {} modifedTime: {}", createdBy, modifiedBy, createdTime, modifiedTime);
}
Id id = new Id(guid, Integer.parseInt(String.valueOf(GraphHelper.getProperty(instanceVertex, Constants.VERSION_PROPERTY_KEY))), typeName, state);
if (LOG.isDebugEnabled()) {
LOG.debug("Created id {} for instance type {}", id, typeName);
}
ClassType classType = typeSystem.getDataType(ClassType.class, typeName);
ITypedReferenceableInstance typedInstance = classType.createInstance(id, systemAttributes, traits.toArray(new String[traits.size()]));
mapVertexToInstance(instanceVertex, typedInstance, classType.fieldMapping().fields);
mapVertexToInstanceTraits(instanceVertex, typedInstance, traits);
RequestContext.get().cache(typedInstance);
return typedInstance;
}
use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.
the class TypedInstanceToGraphMapper method addOrUpdateAttributesAndTraits.
private String addOrUpdateAttributesAndTraits(Operation operation, ITypedReferenceableInstance typedInstance) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding/Updating typed instance {}", typedInstance.toShortString());
}
Id id = typedInstance.getId();
if (id == null) {
// oops
throw new RepositoryException("id cannot be null");
}
AtlasVertex instanceVertex = idToVertexMap.get(id);
// add the attributes for the instance
ClassType classType = typeSystem.getDataType(ClassType.class, typedInstance.getTypeName());
final Map<String, AttributeInfo> fields = classType.fieldMapping().fields;
mapInstanceToVertex(typedInstance, instanceVertex, fields, false, operation);
if (Operation.CREATE.equals(operation)) {
//TODO - Handle Trait updates
addTraits(typedInstance, instanceVertex, classType);
}
return getId(typedInstance)._getId();
}
Aggregations