use of org.apache.atlas.repository.RepositoryException in project incubator-atlas by apache.
the class GraphBackedMetadataRepository method getEntityDefinitions.
@Override
@GraphTransaction
public List<ITypedReferenceableInstance> getEntityDefinitions(String... guids) throws RepositoryException, EntityNotFoundException {
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving entities with guids={}", Arrays.toString(guids));
}
RequestContext context = RequestContext.get();
ITypedReferenceableInstance[] result = new ITypedReferenceableInstance[guids.length];
// Map of the guids of instances not in the cache to their index(es) in the result.
// This is used to put the loaded instances into the location(s) corresponding
// to their guid in the result. Note that a set is needed since guids can
// appear more than once in the list.
Map<String, Set<Integer>> uncachedGuids = new HashMap<>();
for (int i = 0; i < guids.length; i++) {
String guid = guids[i];
// First, check the cache.
ITypedReferenceableInstance cached = context.getInstanceV1(guid);
if (cached != null) {
result[i] = cached;
} else {
Set<Integer> indices = uncachedGuids.get(guid);
if (indices == null) {
indices = new HashSet<>(1);
uncachedGuids.put(guid, indices);
}
indices.add(i);
}
}
List<String> guidsToFetch = new ArrayList<>(uncachedGuids.keySet());
Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToFetch);
// search for missing entities
if (instanceVertices.size() != guidsToFetch.size()) {
Set<String> missingGuids = new HashSet<String>(guidsToFetch);
missingGuids.removeAll(instanceVertices.keySet());
if (!missingGuids.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to find guids={}", missingGuids);
}
throw new EntityNotFoundException("Could not find entities in the repository with guids: " + missingGuids.toString());
}
}
for (String guid : guidsToFetch) {
try {
ITypedReferenceableInstance entity = graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertices.get(guid));
for (int index : uncachedGuids.get(guid)) {
result[index] = entity;
}
} catch (AtlasException e) {
throw new RepositoryException(e);
}
}
return Arrays.asList(result);
}
use of org.apache.atlas.repository.RepositoryException 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;
}
use of org.apache.atlas.repository.RepositoryException 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);
}
}
use of org.apache.atlas.repository.RepositoryException 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;
}
use of org.apache.atlas.repository.RepositoryException 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