http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java deleted file mode 100755 index 74886b5..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java +++ /dev/null @@ -1,505 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.atlas.repository.graph; - -import com.google.common.base.Preconditions; -import org.apache.atlas.AtlasException; -import org.apache.atlas.CreateUpdateEntitiesResult; -import org.apache.atlas.GraphTransactionInterceptor; -import org.apache.atlas.RequestContext; -import org.apache.atlas.annotation.GraphTransaction; -import org.apache.atlas.model.instance.GuidMapping; -import org.apache.atlas.model.legacy.EntityResult; -import org.apache.atlas.repository.Constants; -import org.apache.atlas.repository.MetadataRepository; -import org.apache.atlas.repository.RepositoryException; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasGraph; -import org.apache.atlas.repository.graphdb.AtlasGraphQuery; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.exception.EntityExistsException; -import org.apache.atlas.typesystem.exception.EntityNotFoundException; -import org.apache.atlas.typesystem.exception.TraitNotFoundException; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; -import javax.inject.Singleton; -import java.util.*; - -/** - * An implementation backed by a Graph database provided - * as a Graph Service. - */ -@Singleton -@Component -@Deprecated -public class GraphBackedMetadataRepository implements MetadataRepository { - - private static final Logger LOG = LoggerFactory.getLogger(GraphBackedMetadataRepository.class); - - private static TypeSystem typeSystem = TypeSystem.getInstance(); - - private static final GraphHelper graphHelper = GraphHelper.getInstance(); - - private DeleteHandler deleteHandler; - - private final AtlasGraph atlasGraph; - private final GraphToTypedInstanceMapper graphToInstanceMapper; - - @Inject - public GraphBackedMetadataRepository(DeleteHandler deleteHandler, AtlasGraph atlasGraph) { - this.atlasGraph = atlasGraph; - this.graphToInstanceMapper = new GraphToTypedInstanceMapper(atlasGraph); - this.deleteHandler = deleteHandler; - } - - public GraphToTypedInstanceMapper getGraphToInstanceMapper() { - return graphToInstanceMapper; - } - - @Override - public String getTypeAttributeName() { - return Constants.ENTITY_TYPE_PROPERTY_KEY; - } - - @Override - public String getStateAttributeName() { - return Constants.STATE_PROPERTY_KEY; - } - - /** - * Returns the property key used to store super type names. - * - * @return property key used to store super type names. - */ - @Override - public String getSuperTypeAttributeName() { - return Constants.SUPER_TYPES_PROPERTY_KEY; - } - - public String getIdAttributeName() { - return Constants.GUID_PROPERTY_KEY; - } - - @Override - public String getVersionAttributeName() { - return Constants.VERSION_PROPERTY_KEY; - } - - @Override - public String getTraitLabel(IDataType<?> dataType, String traitName) { - return GraphHelper.getTraitLabel(dataType.getName(), traitName); - } - - @Override - public String getFieldNameInVertex(IDataType<?> dataType, AttributeInfo aInfo) throws AtlasException { - if (aInfo.name.startsWith(Constants.INTERNAL_PROPERTY_KEY_PREFIX)) { - return aInfo.name; - } - return GraphHelper.encodePropertyKey(GraphHelper.getQualifiedFieldName(dataType, aInfo.name)); - } - - public String getFieldNameInVertex(IDataType<?> dataType, String attrName) throws AtlasException { - return GraphHelper.getQualifiedFieldName(dataType, attrName); - } - - @Override - public String getEdgeLabel(IDataType<?> dataType, AttributeInfo aInfo) throws AtlasException { - return GraphHelper.getEdgeLabel(dataType, aInfo); - } - - @Override - @GraphTransaction - public CreateUpdateEntitiesResult createEntities(ITypedReferenceableInstance... entities) throws RepositoryException, - EntityExistsException { - if (LOG.isDebugEnabled()) { - LOG.debug("adding entities={}", entities); - } - - try { - TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler); - instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.CREATE, entities); - List<String> createdGuids = RequestContext.get().getCreatedEntityIds(); - CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult(); - EntityResult entityResult = new EntityResult(createdGuids, null, null); - GuidMapping mapping = instanceToGraphMapper.createGuidMapping(); - result.setEntityResult(entityResult); - result.setGuidMapping(mapping); - return result; - } catch (EntityExistsException e) { - throw e; - } catch (AtlasException e) { - throw new RepositoryException(e); - } - } - - @Override - @GraphTransaction - public ITypedReferenceableInstance getEntityDefinition(String guid) throws RepositoryException, EntityNotFoundException { - return getEntityDefinitions(guid).get(0); - } - - @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); - } - - @Override - @GraphTransaction - public ITypedReferenceableInstance getEntityDefinition(String entityType, String attribute, Object value) - throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Retrieving entity with type={} and {}={}", entityType, attribute, value); - } - - IDataType type = typeSystem.getDataType(IDataType.class, entityType); - String propertyKey = getFieldNameInVertex(type, attribute); - AtlasVertex instanceVertex = graphHelper.findVertex(propertyKey, value, - Constants.ENTITY_TYPE_PROPERTY_KEY, entityType, - Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); - - String guid = GraphHelper.getGuid(instanceVertex); - ITypedReferenceableInstance cached = RequestContext.get().getInstanceV1(guid); - if(cached != null) { - return cached; - } - return graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertex); - } - - @Override - @GraphTransaction - public List<String> getEntityList(String entityType) throws RepositoryException { - if (LOG.isDebugEnabled()) { - LOG.debug("Retrieving entity list for type={}", entityType); - } - - AtlasGraphQuery query = getGraph().query().has(Constants.ENTITY_TYPE_PROPERTY_KEY, entityType); - Iterator<AtlasVertex> results = query.vertices().iterator(); - if (!results.hasNext()) { - return Collections.emptyList(); - } - - ArrayList<String> entityList = new ArrayList<>(); - while (results.hasNext()) { - AtlasVertex vertex = results.next(); - entityList.add(GraphHelper.getGuid(vertex)); - } - - return entityList; - } - - /** - * Gets the list of trait names for a given entity represented by a guid. - * - * @param guid globally unique identifier for the entity - * @return a list of trait names for the given entity guid - * @throws RepositoryException - */ - @Override - @GraphTransaction - public List<String> getTraitNames(String guid) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Retrieving trait names for entity={}", guid); - } - - AtlasVertex instanceVertex = graphHelper.getVertexForGUID(guid); - return GraphHelper.getTraitNames(instanceVertex); - } - - /** - * Adds a new trait to the list of entities represented by their respective guids - * @param entityGuids list of globally unique identifier for the entities - * @param traitInstance trait instance that needs to be added to entities - * @throws RepositoryException - */ - @Override - @GraphTransaction - public void addTrait(List<String> entityGuids, ITypedStruct traitInstance) throws RepositoryException { - Preconditions.checkNotNull(entityGuids, "entityGuids list cannot be null"); - Preconditions.checkNotNull(traitInstance, "Trait instance cannot be null"); - - if (LOG.isDebugEnabled()) { - LOG.debug("Adding a new trait={} for entities={}", traitInstance.getTypeName(), entityGuids); - } - - GraphTransactionInterceptor.lockObjectAndReleasePostCommit(entityGuids); - for (String entityGuid : entityGuids) { - addTraitImpl(entityGuid, traitInstance); - } - } - - /** - * Adds a new trait to an existing entity represented by a guid. - * - * @param guid globally unique identifier for the entity - * @param traitInstance trait instance that needs to be added to entity - * @throws RepositoryException - */ - @Override - @GraphTransaction - public void addTrait(String guid, ITypedStruct traitInstance) throws RepositoryException { - Preconditions.checkNotNull(guid, "guid cannot be null"); - Preconditions.checkNotNull(traitInstance, "Trait instance cannot be null"); - - GraphTransactionInterceptor.lockObjectAndReleasePostCommit(guid); - addTraitImpl(guid, traitInstance); - } - - private void addTraitImpl(String guid, ITypedStruct traitInstance) throws RepositoryException { - final String traitName = traitInstance.getTypeName(); - if (LOG.isDebugEnabled()) { - LOG.debug("Adding a new trait={} for entity={}", traitName, guid); - } - - try { - AtlasVertex instanceVertex = graphHelper.getVertexForGUID(guid); - - // add the trait instance as a new vertex - final String typeName = GraphHelper.getTypeName(instanceVertex); - - TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler); - instanceToGraphMapper.mapTraitInstanceToVertex(traitInstance, - typeSystem.getDataType(ClassType.class, typeName), instanceVertex); - - - // update the traits in entity once adding trait instance is successful - GraphHelper.addProperty(instanceVertex, Constants.TRAIT_NAMES_PROPERTY_KEY, traitName); - GraphHelper.setProperty(instanceVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, - RequestContext.get().getRequestTime()); - GraphHelper.setProperty(instanceVertex, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser()); - - } catch (RepositoryException e) { - throw e; - } catch (Exception e) { - throw new RepositoryException(e); - } - } - - /** - * Deletes a given trait from an existing entity represented by a guid. - * - * @param guid globally unique identifier for the entity - * @param traitNameToBeDeleted name of the trait - * @throws RepositoryException - */ - @Override - @GraphTransaction - public void deleteTrait(String guid, String traitNameToBeDeleted) throws TraitNotFoundException, EntityNotFoundException, RepositoryException { - LOG.debug("Deleting trait={} from entity={}", traitNameToBeDeleted, guid); - GraphTransactionInterceptor.lockObjectAndReleasePostCommit(guid); - - AtlasVertex instanceVertex = graphHelper.getVertexForGUID(guid); - - List<String> traitNames = GraphHelper.getTraitNames(instanceVertex); - if (!traitNames.contains(traitNameToBeDeleted)) { - throw new TraitNotFoundException( - "Could not find trait=" + traitNameToBeDeleted + " in the repository for entity: " + guid); - } - - try { - final String entityTypeName = GraphHelper.getTypeName(instanceVertex); - String relationshipLabel = GraphHelper.getTraitLabel(entityTypeName, traitNameToBeDeleted); - AtlasEdge edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel); - if(edge != null) { - deleteHandler.deleteEdgeReference(edge, DataTypes.TypeCategory.TRAIT, false, true); - } - - // update the traits in entity once trait removal is successful - traitNames.remove(traitNameToBeDeleted); - updateTraits(instanceVertex, traitNames); - } catch (Exception e) { - throw new RepositoryException(e); - } - } - - - private void updateTraits(AtlasVertex instanceVertex, List<String> traitNames) { - // remove the key - instanceVertex.removeProperty(Constants.TRAIT_NAMES_PROPERTY_KEY); - - // add it back again - for (String traitName : traitNames) { - GraphHelper.addProperty(instanceVertex, Constants.TRAIT_NAMES_PROPERTY_KEY, traitName); - } - GraphHelper.setProperty(instanceVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, - RequestContext.get().getRequestTime()); - GraphHelper.setProperty(instanceVertex, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser()); - } - - @Override - @GraphTransaction - public CreateUpdateEntitiesResult updateEntities(ITypedReferenceableInstance... entitiesUpdated) throws RepositoryException { - if (LOG.isDebugEnabled()) { - LOG.debug("updating entity {}", entitiesUpdated); - } - - try { - TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler); - instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.UPDATE_FULL, - entitiesUpdated); - CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult(); - RequestContext requestContext = RequestContext.get(); - result.setEntityResult(createEntityResultFromContext(requestContext)); - GuidMapping mapping = instanceToGraphMapper.createGuidMapping(); - result.setGuidMapping(mapping); - return result; - } catch (AtlasException e) { - throw new RepositoryException(e); - } - } - - @Override - @GraphTransaction - public CreateUpdateEntitiesResult updatePartial(ITypedReferenceableInstance entity) throws RepositoryException { - if (LOG.isDebugEnabled()) { - LOG.debug("updating entity {}", entity); - } - - try { - TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler); - instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.UPDATE_PARTIAL, entity); - RequestContext requestContext = RequestContext.get(); - CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult(); - GuidMapping mapping = instanceToGraphMapper.createGuidMapping(); - result.setEntityResult(createEntityResultFromContext(requestContext)); - result.setGuidMapping(mapping); - return result; - } catch (AtlasException e) { - throw new RepositoryException(e); - } - } - - - - @Override - @GraphTransaction - public EntityResult deleteEntities(List<String> guids) throws RepositoryException { - - if (guids == null || guids.size() == 0) { - throw new IllegalArgumentException("guids must be non-null and non-empty"); - } - - // Retrieve vertices for requested guids. - Map<String, AtlasVertex> vertices = graphHelper.getVerticesForGUIDs(guids); - Collection<AtlasVertex> deletionCandidates = vertices.values(); - - if(LOG.isDebugEnabled()) { - for(String guid : guids) { - if(! vertices.containsKey(guid)) { - // Entity does not exist - treat as non-error, since the caller - // wanted to delete the entity and it's already gone. - LOG.debug("Deletion request ignored for non-existent entity with guid " + guid); - } - } - } - - if (deletionCandidates.isEmpty()) { - LOG.info("No deletion candidate entities were found for guids %s", guids); - return new EntityResult(Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList()); - } - - try { - deleteHandler.deleteEntities(deletionCandidates); - } - catch (AtlasException e) { - throw new RepositoryException(e); - } - - RequestContext requestContext = RequestContext.get(); - return createEntityResultFromContext(requestContext); - } - - private EntityResult createEntityResultFromContext(RequestContext requestContext) { - return new EntityResult( - requestContext.getCreatedEntityIds(), - requestContext.getUpdatedEntityIds(), - requestContext.getDeletedEntityIds()); - } - - public AtlasGraph getGraph() throws RepositoryException { - return atlasGraph; - } -}
http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedSearchIndexer.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedSearchIndexer.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedSearchIndexer.java index 6eee24b..9f1206c 100755 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedSearchIndexer.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedSearchIndexer.java @@ -34,7 +34,6 @@ import org.apache.atlas.model.typedef.AtlasEnumDef; import org.apache.atlas.model.typedef.AtlasStructDef; import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; import org.apache.atlas.repository.Constants; -import org.apache.atlas.repository.IndexCreationException; import org.apache.atlas.repository.IndexException; import org.apache.atlas.repository.RepositoryException; import org.apache.atlas.repository.graphdb.AtlasCardinality; @@ -49,13 +48,6 @@ import org.apache.atlas.type.AtlasStructType; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeUtil; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.StructType; -import org.apache.atlas.typesystem.types.TraitType; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.configuration.Configuration; import org.slf4j.Logger; @@ -66,11 +58,9 @@ import javax.inject.Inject; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.*; @@ -80,8 +70,7 @@ import static org.apache.atlas.model.typedef.AtlasBaseTypeDef.*; * Adds index for properties of a given type when its added before any instances are added. */ @Component -public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChangeHandler, - TypeDefChangeListener { +public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChangeHandler, TypeDefChangeListener { private static final Logger LOG = LoggerFactory.getLogger(GraphBackedSearchIndexer.class); @@ -118,6 +107,110 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang } /** + * Initialize global indices for Titan graph on server activation. + * + * Since the indices are shared state, we need to do this only from an active instance. + */ + @Override + public void instanceIsActive() throws AtlasException { + LOG.info("Reacting to active: initializing index"); + try { + initialize(); + } catch (RepositoryException | IndexException e) { + throw new AtlasException("Error in reacting to active on initialization", e); + } + } + + @Override + public void instanceIsPassive() { + LOG.info("Reacting to passive state: No action right now."); + } + + @Override + public int getHandlerOrder() { + return HandlerOrder.GRAPH_BACKED_SEARCH_INDEXER.getOrder(); + } + + @Override + public void onChange(ChangedTypeDefs changedTypeDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("Processing changed typedefs {}", changedTypeDefs); + } + AtlasGraphManagement management = null; + try { + management = provider.get().getManagementSystem(); + + // Update index for newly created types + if (CollectionUtils.isNotEmpty(changedTypeDefs.getCreateTypeDefs())) { + for (AtlasBaseTypeDef typeDef : changedTypeDefs.getCreateTypeDefs()) { + updateIndexForTypeDef(management, typeDef); + } + } + + // Update index for updated types + if (CollectionUtils.isNotEmpty(changedTypeDefs.getUpdatedTypeDefs())) { + for (AtlasBaseTypeDef typeDef : changedTypeDefs.getUpdatedTypeDefs()) { + updateIndexForTypeDef(management, typeDef); + } + } + + // Invalidate the property key for deleted types + if (CollectionUtils.isNotEmpty(changedTypeDefs.getDeletedTypeDefs())) { + for (AtlasBaseTypeDef typeDef : changedTypeDefs.getDeletedTypeDefs()) { + cleanupIndices(management, typeDef); + } + } + + //Commit indexes + commit(management); + } catch (RepositoryException | IndexException e) { + LOG.error("Failed to update indexes for changed typedefs", e); + attemptRollback(changedTypeDefs, management); + } + + } + + public Set<String> getVertexIndexKeys() { + if (recomputeIndexedKeys) { + AtlasGraphManagement management = null; + + try { + management = provider.get().getManagementSystem(); + + if (management != null) { + AtlasGraphIndex vertexIndex = management.getGraphIndex(Constants.VERTEX_INDEX); + + if (vertexIndex != null) { + recomputeIndexedKeys = false; + + Set<String> indexKeys = new HashSet<>(); + + for (AtlasPropertyKey fieldKey : vertexIndex.getFieldKeys()) { + indexKeys.add(fieldKey.getName()); + } + + vertexIndexKeys = indexKeys; + } + + management.commit(); + } + } catch (Exception excp) { + LOG.error("getVertexIndexKeys(): failed to get indexedKeys from graph", excp); + + if (management != null) { + try { + management.rollback(); + } catch (Exception e) { + LOG.error("getVertexIndexKeys(): rollback failed", e); + } + } + } + } + + return vertexIndexKeys; + } + + /** * Initializes the indices for the graph - create indices for Global AtlasVertex Keys */ private void initialize() throws RepositoryException, IndexException { @@ -220,81 +313,6 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang true, true); } - /** - * This is upon adding a new type to Store. - * - * @param dataTypes data type - * @throws AtlasException - */ - @Override - public void onAdd(Collection<? extends IDataType> dataTypes) throws AtlasException { - AtlasGraphManagement management = provider.get().getManagementSystem(); - - for (IDataType dataType : dataTypes) { - if (LOG.isDebugEnabled()) { - LOG.debug("Creating indexes for type name={}, definition={}", dataType.getName(), dataType.getClass()); - } - - try { - addIndexForType(management, dataType); - LOG.info("Index creation for type {} complete", dataType.getName()); - } catch (Throwable throwable) { - LOG.error("Error creating index for type {}", dataType, throwable); - //Rollback indexes if any failure - rollback(management); - throw new IndexCreationException("Error while creating index for type " + dataType, throwable); - } - } - - //Commit indexes - commit(management); - } - - @Override - public void onChange(Collection<? extends IDataType> dataTypes) throws AtlasException { - onAdd(dataTypes); - } - - public Set<String> getVertexIndexKeys() { - if (recomputeIndexedKeys) { - AtlasGraphManagement management = null; - - try { - management = provider.get().getManagementSystem(); - - if (management != null) { - AtlasGraphIndex vertexIndex = management.getGraphIndex(Constants.VERTEX_INDEX); - - if (vertexIndex != null) { - recomputeIndexedKeys = false; - - Set<String> indexKeys = new HashSet<>(); - - for (AtlasPropertyKey fieldKey : vertexIndex.getFieldKeys()) { - indexKeys.add(fieldKey.getName()); - } - - vertexIndexKeys = indexKeys; - } - - management.commit(); - } - } catch (Exception excp) { - LOG.error("getVertexIndexKeys(): failed to get indexedKeys from graph", excp); - - if (management != null) { - try { - management.rollback(); - } catch (Exception e) { - LOG.error("getVertexIndexKeys(): rollback failed", e); - } - } - } - } - - return vertexIndexKeys; - } - private void addIndexForType(AtlasGraphManagement management, AtlasBaseTypeDef typeDef) { if (typeDef instanceof AtlasEnumDef) { // Only handle complex types like Struct, Classification and Entity @@ -414,82 +432,6 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang throw new IllegalArgumentException(String.format("Bad cardinality %s", cardinality)); } - private void addIndexForType(AtlasGraphManagement management, IDataType dataType) { - switch (dataType.getTypeCategory()) { - case PRIMITIVE: - case ENUM: - case ARRAY: - case MAP: - // do nothing since these are only attributes - // 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); - } - } - - private void createIndexForFields(AtlasGraphManagement management, IDataType dataType, Map<String, AttributeInfo> fields) { - for (AttributeInfo field : fields.values()) { - createIndexForAttribute(management, dataType.getName(), field); - } - } - - private void createIndexForAttribute(AtlasGraphManagement management, String typeName, AttributeInfo field) { - final String propertyName = GraphHelper.encodePropertyKey(typeName + "." + field.name); - switch (field.dataType().getTypeCategory()) { - case PRIMITIVE: - AtlasCardinality cardinality = getCardinality(field.multiplicity); - createIndexes(management, propertyName, getPrimitiveClass(field.dataType()), field.isUnique, - cardinality, false, field.isIndexable); - break; - - case ENUM: - cardinality = getCardinality(field.multiplicity); - createIndexes(management, propertyName, String.class, field.isUnique, cardinality, false, field.isIndexable); - break; - - case ARRAY: - createLabelIfNeeded(management, propertyName, field.dataType().getName()); - break; - case MAP: - // todo - how do we overcome this limitation? - // IGNORE: Can only index single-valued property keys on vertices in Mixed Index - break; - - case STRUCT: - StructType structType = (StructType) field.dataType(); - createIndexForFields(management, structType, structType.fieldMapping().fields); - break; - - case TRAIT: - // do nothing since this is NOT contained in other types - break; - - case CLASS: - createEdgeLabel(management, propertyName); - break; - - default: - throw new IllegalArgumentException("bad data type" + field.dataType().getName()); - } - } - private void createEdgeLabel(final AtlasGraphManagement management, final String propertyName) { // Create the edge label upfront to avoid running into concurrent call issue (ATLAS-2092) // ATLAS-2092 addresses this problem by creating the edge label upfront while type creation @@ -506,50 +448,6 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang } } - private Class getPrimitiveClass(IDataType dataType) { - if (dataType == DataTypes.STRING_TYPE) { - return String.class; - } else if (dataType == DataTypes.SHORT_TYPE) { - return Short.class; - } else if (dataType == DataTypes.INT_TYPE) { - return Integer.class; - } else if (dataType == DataTypes.BIGINTEGER_TYPE) { - return BigInteger.class; - } else if (dataType == DataTypes.BOOLEAN_TYPE) { - return Boolean.class; - } else if (dataType == DataTypes.BYTE_TYPE) { - return Byte.class; - } else if (dataType == DataTypes.LONG_TYPE) { - return Long.class; - } else if (dataType == DataTypes.FLOAT_TYPE) { - return Float.class; - } else if (dataType == DataTypes.DOUBLE_TYPE) { - return Double.class; - } else if (dataType == DataTypes.BIGDECIMAL_TYPE) { - return BigDecimal.class; - } else if (dataType == DataTypes.DATE_TYPE) { - //Indexing with date converted to long as of now since Titan is yet to add support for Date type with mixed indexes - return Long.class; - } - - - throw new IllegalArgumentException("unknown data type " + dataType); - } - - - private AtlasCardinality getCardinality(Multiplicity multiplicity) { - if (multiplicity == Multiplicity.OPTIONAL || multiplicity == Multiplicity.REQUIRED) { - return AtlasCardinality.SINGLE; - } else if (multiplicity == Multiplicity.COLLECTION) { - return AtlasCardinality.LIST; - } else if (multiplicity == Multiplicity.SET) { - return AtlasCardinality.SET; - } - - // todo - default to LIST as this is the most forgiving - return AtlasCardinality.LIST; - } - private AtlasPropertyKey createIndexes(AtlasGraphManagement management, String propertyName, Class propertyClass, boolean isUnique, AtlasCardinality cardinality, boolean createCompositeForAttribute, boolean createCompositeWithTypeandSuperTypes) { @@ -677,70 +575,6 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang } } - /** - * Initialize global indices for Titan graph on server activation. - * - * Since the indices are shared state, we need to do this only from an active instance. - */ - @Override - public void instanceIsActive() throws AtlasException { - LOG.info("Reacting to active: initializing index"); - try { - initialize(); - } catch (RepositoryException | IndexException e) { - throw new AtlasException("Error in reacting to active on initialization", e); - } - } - - @Override - public void instanceIsPassive() { - LOG.info("Reacting to passive state: No action right now."); - } - - @Override - public int getHandlerOrder() { - return HandlerOrder.GRAPH_BACKED_SEARCH_INDEXER.getOrder(); - } - - @Override - public void onChange(ChangedTypeDefs changedTypeDefs) throws AtlasBaseException { - if (LOG.isDebugEnabled()) { - LOG.debug("Processing changed typedefs {}", changedTypeDefs); - } - AtlasGraphManagement management = null; - try { - management = provider.get().getManagementSystem(); - - // Update index for newly created types - if (CollectionUtils.isNotEmpty(changedTypeDefs.getCreateTypeDefs())) { - for (AtlasBaseTypeDef typeDef : changedTypeDefs.getCreateTypeDefs()) { - updateIndexForTypeDef(management, typeDef); - } - } - - // Update index for updated types - if (CollectionUtils.isNotEmpty(changedTypeDefs.getUpdatedTypeDefs())) { - for (AtlasBaseTypeDef typeDef : changedTypeDefs.getUpdatedTypeDefs()) { - updateIndexForTypeDef(management, typeDef); - } - } - - // Invalidate the property key for deleted types - if (CollectionUtils.isNotEmpty(changedTypeDefs.getDeletedTypeDefs())) { - for (AtlasBaseTypeDef typeDef : changedTypeDefs.getDeletedTypeDefs()) { - cleanupIndices(management, typeDef); - } - } - - //Commit indexes - commit(management); - } catch (RepositoryException | IndexException e) { - LOG.error("Failed to update indexes for changed typedefs", e); - attemptRollback(changedTypeDefs, management); - } - - } - private void cleanupIndices(AtlasGraphManagement management, AtlasBaseTypeDef typeDef) { Preconditions.checkNotNull(typeDef, "Cannot process null typedef"); if (LOG.isDebugEnabled()) { @@ -816,14 +650,4 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang addIndexForType(management, typeDef); LOG.info("Index creation for type {} complete", typeDef.getName()); } - - /* Commenting this out since we do not need an index for edge label here - private void createEdgeMixedIndex(String propertyName) { - EdgeLabel edgeLabel = management.getEdgeLabel(propertyName); - if (edgeLabel == null) { - edgeLabel = management.makeEdgeLabel(propertyName).make(); - management.buildEdgeIndex(edgeLabel, propertyName, Direction.BOTH, Order.DEFAULT); - LOG.info("Created index for edge label {}", propertyName); - } - }*/ } http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java index ffe859b..fab8ffc 100755 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java @@ -24,12 +24,15 @@ import com.google.common.collect.HashBiMap; import org.apache.atlas.ApplicationProperties; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasException; -import org.apache.atlas.RequestContext; +import org.apache.atlas.RequestContextV1; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.instance.AtlasEntity.Status; import org.apache.atlas.model.instance.AtlasObjectId; import org.apache.atlas.model.instance.AtlasRelationship; import org.apache.atlas.model.typedef.AtlasRelationshipDef; +import org.apache.atlas.model.v1.instance.Id; +import org.apache.atlas.model.v1.instance.Referenceable; +import org.apache.atlas.type.AtlasStructType.AtlasAttribute; import org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection; import org.apache.atlas.repository.Constants; import org.apache.atlas.repository.RepositoryException; @@ -43,24 +46,11 @@ import org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1; import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasRelationshipType; import org.apache.atlas.type.AtlasType; -import org.apache.atlas.typesystem.IReferenceableInstance; -import org.apache.atlas.typesystem.ITypedInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.Referenceable; +import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.typesystem.exception.EntityNotFoundException; import org.apache.atlas.typesystem.exception.TypeNotFoundException; -import org.apache.atlas.typesystem.json.InstanceSerialization; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.persistence.ReferenceableInstance; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; import org.apache.atlas.typesystem.types.DataTypes; import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; -import org.apache.atlas.typesystem.types.HierarchicalType; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.apache.atlas.typesystem.types.ValueConversionException; import org.apache.atlas.typesystem.types.utils.TypesUtil; import org.apache.atlas.util.AttributeValueMap; import org.apache.atlas.util.IndexedInstance; @@ -97,8 +87,6 @@ public final class GraphHelper { private static final Logger LOG = LoggerFactory.getLogger(GraphHelper.class); public static final String EDGE_LABEL_PREFIX = "__"; - private static final TypeSystem typeSystem = TypeSystem.getInstance(); - public static final String RETRY_COUNT = "atlas.graph.storage.num.retries"; public static final String RETRY_DELAY = "atlas.graph.storage.retry.sleeptime.ms"; @@ -143,7 +131,7 @@ public final class GraphHelper { } - public AtlasVertex createVertexWithIdentity(ITypedReferenceableInstance typedInstance, Set<String> superTypeNames) { + public AtlasVertex createVertexWithIdentity(Referenceable typedInstance, Set<String> superTypeNames) { final String guid = UUID.randomUUID().toString(); final AtlasVertex vertexWithIdentity = createVertexWithoutIdentity(typedInstance.getTypeName(), @@ -153,7 +141,7 @@ public final class GraphHelper { setProperty(vertexWithIdentity, Constants.GUID_PROPERTY_KEY, guid); // add version information - setProperty(vertexWithIdentity, Constants.VERSION_PROPERTY_KEY, Long.valueOf(typedInstance.getId().version)); + setProperty(vertexWithIdentity, Constants.VERSION_PROPERTY_KEY, Long.valueOf(typedInstance.getId().getVersion())); return vertexWithIdentity; } @@ -179,12 +167,12 @@ public final class GraphHelper { setProperty(vertexWithoutIdentity, Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); // add timestamp information - setProperty(vertexWithoutIdentity, Constants.TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); + setProperty(vertexWithoutIdentity, Constants.TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime()); setProperty(vertexWithoutIdentity, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, - RequestContext.get().getRequestTime()); + RequestContextV1.get().getRequestTime()); - setProperty(vertexWithoutIdentity, Constants.CREATED_BY_KEY, RequestContext.get().getUser()); - setProperty(vertexWithoutIdentity, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser()); + setProperty(vertexWithoutIdentity, Constants.CREATED_BY_KEY, RequestContextV1.get().getUser()); + setProperty(vertexWithoutIdentity, Constants.MODIFIED_BY_KEY, RequestContextV1.get().getUser()); return vertexWithoutIdentity; } @@ -196,10 +184,10 @@ public final class GraphHelper { AtlasEdge edge = graph.addEdge(fromVertex, toVertex, edgeLabel); setProperty(edge, Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); - setProperty(edge, Constants.TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); - setProperty(edge, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); - setProperty(edge, Constants.CREATED_BY_KEY, RequestContext.get().getUser()); - setProperty(edge, Constants.MODIFIED_BY_KEY, RequestContext.get().getUser()); + setProperty(edge, Constants.TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime()); + setProperty(edge, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime()); + setProperty(edge, Constants.CREATED_BY_KEY, RequestContextV1.get().getUser()); + setProperty(edge, Constants.MODIFIED_BY_KEY, RequestContextV1.get().getUser()); if (LOG.isDebugEnabled()) { LOG.debug("Added {}", string(edge)); @@ -673,17 +661,6 @@ public final class GraphHelper { return prefix + "." + key; } - public static String getQualifiedFieldName(ITypedInstance typedInstance, AttributeInfo attributeInfo) throws AtlasException { - IDataType dataType = typeSystem.getDataType(IDataType.class, typedInstance.getTypeName()); - return getQualifiedFieldName(dataType, attributeInfo.name); - } - - public static String getQualifiedFieldName(IDataType dataType, String attributeName) throws AtlasException { - return dataType.getTypeCategory() == DataTypes.TypeCategory.STRUCT ? dataType.getName() + "." + attributeName - // else class or trait - : ((HierarchicalType) dataType).getQualifiedName(attributeName); - } - public static String getTraitLabel(String typeName, String attrName) { return attrName; } @@ -710,13 +687,8 @@ public final class GraphHelper { return superTypes; } - public static String getEdgeLabel(ITypedInstance typedInstance, AttributeInfo aInfo) throws AtlasException { - IDataType dataType = typeSystem.getDataType(IDataType.class, typedInstance.getTypeName()); - return getEdgeLabel(dataType, aInfo); - } - - public static String getEdgeLabel(IDataType dataType, AttributeInfo aInfo) throws AtlasException { - return GraphHelper.EDGE_LABEL_PREFIX + getQualifiedFieldName(dataType, aInfo.name); + public static String getEdgeLabel(AtlasAttribute aInfo) throws AtlasException { + return GraphHelper.EDGE_LABEL_PREFIX + aInfo.getQualifiedName(); } public static Id getIdFromVertex(String dataTypeName, AtlasVertex vertex) { @@ -784,26 +756,24 @@ public final class GraphHelper { * @return * @throws AtlasException */ - public AtlasVertex getVertexForInstanceByUniqueAttribute(ClassType classType, IReferenceableInstance instance) + public AtlasVertex getVertexForInstanceByUniqueAttribute(AtlasEntityType classType, Referenceable instance) throws AtlasException { if (LOG.isDebugEnabled()) { LOG.debug("Checking if there is an instance with the same unique attributes for instance {}", instance.toShortString()); } AtlasVertex result = null; - for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) { - if (attributeInfo.isUnique) { - String propertyKey = getQualifiedFieldName(classType, attributeInfo.name); - try { - result = findVertex(propertyKey, instance.get(attributeInfo.name), - Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName(), - Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); - if (LOG.isDebugEnabled()) { - LOG.debug("Found vertex by unique attribute : {}={}", propertyKey, instance.get(attributeInfo.name)); - } - } catch (EntityNotFoundException e) { - //Its ok if there is no entity with the same unique value + for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) { + String propertyKey = attributeInfo.getQualifiedName(); + try { + result = findVertex(propertyKey, instance.get(attributeInfo.getName()), + Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName(), + Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); + if (LOG.isDebugEnabled()) { + LOG.debug("Found vertex by unique attribute : {}={}", propertyKey, instance.get(attributeInfo.getName())); } + } catch (EntityNotFoundException e) { + //Its ok if there is no entity with the same unique value } } @@ -821,23 +791,21 @@ public final class GraphHelper { * @return * @throws AtlasException */ - public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(ClassType classType, List<? extends IReferenceableInstance> instancesForClass) throws AtlasException { + public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(AtlasEntityType classType, List<? extends Referenceable> instancesForClass) throws AtlasException { //For each attribute, need to figure out what values to search for and which instance(s) //those values correspond to. Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>(); - for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) { - if (attributeInfo.isUnique) { - String propertyKey = getQualifiedFieldName(classType, attributeInfo.name); - AttributeValueMap mapForAttribute = new AttributeValueMap(); - for(int idx = 0; idx < instancesForClass.size(); idx++) { - IReferenceableInstance instance = instancesForClass.get(idx); - Object value = instance.get(attributeInfo.name); - mapForAttribute.put(value, instance, idx); - } - map.put(propertyKey, mapForAttribute); + for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) { + String propertyKey = attributeInfo.getQualifiedName(); + AttributeValueMap mapForAttribute = new AttributeValueMap(); + for(int idx = 0; idx < instancesForClass.size(); idx++) { + Referenceable instance = instancesForClass.get(idx); + Object value = instance.get(attributeInfo.getName()); + mapForAttribute.put(value, instance, idx); } + map.put(propertyKey, mapForAttribute); } AtlasVertex[] result = new AtlasVertex[instancesForClass.size()]; @@ -849,7 +817,7 @@ public final class GraphHelper { //construct gremlin query AtlasGraphQuery query = graph.query(); - query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName()); + query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName()); query.has(Constants.STATE_PROPERTY_KEY,Id.EntityState.ACTIVE.name()); List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>(); @@ -965,6 +933,7 @@ public final class GraphHelper { * @return set of VertexInfo for all composite entities * @throws AtlasException */ + /* public Set<VertexInfo> getCompositeVertices(AtlasVertex entityVertex) throws AtlasException { Set<VertexInfo> result = new HashSet<>(); Stack<AtlasVertex> vertices = new Stack<>(); @@ -1035,20 +1004,22 @@ public final class GraphHelper { } return result; } + */ - public static ITypedReferenceableInstance[] deserializeClassInstances(TypeSystem typeSystem, String entityInstanceDefinition) + /* + public static Referenceable[] deserializeClassInstances(AtlasTypeRegistry typeRegistry, String entityInstanceDefinition) throws AtlasException { try { JSONArray referableInstances = new JSONArray(entityInstanceDefinition); - ITypedReferenceableInstance[] instances = new ITypedReferenceableInstance[referableInstances.length()]; + Referenceable[] instances = new Referenceable[referableInstances.length()]; for (int index = 0; index < referableInstances.length(); index++) { Referenceable entityInstance = - InstanceSerialization.fromJsonReferenceable(referableInstances.getString(index), true); - ITypedReferenceableInstance typedInstrance = getTypedReferenceableInstance(typeSystem, entityInstance); + AtlasType.fromV1Json(referableInstances.getString(index), Referenceable.class); + Referenceable typedInstrance = getTypedReferenceableInstance(typeRegistry, entityInstance); instances[index] = typedInstrance; } return instances; - } catch(ValueConversionException | TypeNotFoundException e) { + } catch(TypeNotFoundException e) { throw e; } catch (Exception e) { // exception from deserializer LOG.error("Unable to deserialize json={}", entityInstanceDefinition, e); @@ -1056,28 +1027,22 @@ public final class GraphHelper { } } - public static ITypedReferenceableInstance getTypedReferenceableInstance(TypeSystem typeSystem, Referenceable entityInstance) + public static Referenceable getTypedReferenceableInstance(AtlasTypeRegistry typeRegistry, Referenceable entityInstance) throws AtlasException { final String entityTypeName = ParamChecker.notEmpty(entityInstance.getTypeName(), "Entity type cannot be null"); - ClassType entityType = typeSystem.getDataType(ClassType.class, entityTypeName); + AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName); //Both assigned id and values are required for full update //classtype.convert() will remove values if id is assigned. So, set temp id, convert and // then replace with original id Id origId = entityInstance.getId(); - entityInstance.replaceWithNewId(new Id(entityInstance.getTypeName())); - ITypedReferenceableInstance typedInstrance = entityType.convert(entityInstance, Multiplicity.REQUIRED); - ((ReferenceableInstance)typedInstrance).replaceWithNewId(origId); + entityInstance.setId(new Id(entityInstance.getTypeName())); + Referenceable typedInstrance = new Referenceable(entityInstance); + typedInstrance.setId(origId); return typedInstrance; } - - public static boolean isReference(IDataType type) { - - return type.getTypeCategory() == DataTypes.TypeCategory.STRUCT || - type.getTypeCategory() == DataTypes.TypeCategory.CLASS; - - } + */ public static boolean isInternalType(AtlasVertex vertex) { return vertex != null && isInternalType(getTypeName(vertex)); @@ -1087,9 +1052,9 @@ public final class GraphHelper { return typeName != null && typeName.startsWith(Constants.INTERNAL_PROPERTY_KEY_PREFIX); } - public static void setArrayElementsProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName, List<Object> values) { + public static void setArrayElementsProperty(AtlasType elementType, AtlasVertex instanceVertex, String propertyName, List<Object> values) { String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { + if(AtlasGraphUtilsV1.isReference(elementType)) { setListPropertyFromElementIds(instanceVertex, actualPropertyName, (List)values); } else { @@ -1097,9 +1062,9 @@ public final class GraphHelper { } } - public static void setMapValueProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName, Object value) { + public static void setMapValueProperty(AtlasType elementType, AtlasVertex instanceVertex, String propertyName, Object value) { String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { + if(AtlasGraphUtilsV1.isReference(elementType)) { instanceVertex.setPropertyFromElementId(actualPropertyName, (AtlasEdge)value); } else { @@ -1107,16 +1072,6 @@ public final class GraphHelper { } } - public static Object getMapValueProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName) { - String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { - return instanceVertex.getProperty(actualPropertyName, AtlasEdge.class); - } - else { - return instanceVertex.getProperty(actualPropertyName, Object.class).toString(); - } - } - public static Object getMapValueProperty(AtlasType elementType, AtlasVertex instanceVertex, String propertyName) { String vertexPropertyName = GraphHelper.encodePropertyKey(propertyName); @@ -1138,16 +1093,6 @@ public final class GraphHelper { } } - public static List<Object> getArrayElementsProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName) { - String actualPropertyName = GraphHelper.encodePropertyKey(propertyName); - if(GraphHelper.isReference(elementType)) { - return (List)instanceVertex.getListProperty(actualPropertyName, AtlasEdge.class); - } - else { - return (List)instanceVertex.getListProperty(actualPropertyName); - } - } - public static void dumpToLog(final AtlasGraph<?,?> graph) { LOG.debug("*******************Graph Dump****************************"); LOG.debug("Vertices of {}", graph); @@ -1162,7 +1107,7 @@ public final class GraphHelper { LOG.debug("*******************Graph Dump****************************"); } - public static String string(ITypedReferenceableInstance instance) { + public static String string(Referenceable instance) { return String.format("entity[type=%s guid=%s]", instance.getTypeName(), instance.getId()._getId()); } @@ -1245,6 +1190,7 @@ public final class GraphHelper { return instanceVertexId; } + /* public static AttributeInfo getAttributeInfoForSystemAttributes(String field) { switch (field) { case Constants.STATE_PROPERTY_KEY: @@ -1259,6 +1205,7 @@ public final class GraphHelper { } return null; } + */ public static boolean elementExists(AtlasElement v) { return v != null && v.exists(); http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java deleted file mode 100644 index e877680..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.atlas.repository.graph; - -import org.apache.atlas.setup.SetupException; -import org.apache.atlas.setup.SetupStep; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -/** - * A {@link SetupStep} that initializes the Graph backend for Atlas. - * - * This class will initialize the specific backend implementation specified in - * the Atlas configuration for the key atlas.graph.storage.backend. - */ -@Component -public class GraphSchemaInitializer implements SetupStep { - - private static final Logger LOG = LoggerFactory.getLogger(GraphSchemaInitializer.class); - - @Override - public void run() throws SetupException { - LOG.info("Initializing graph schema backend."); - try { - // The implementation of this method internally creates the schema. - AtlasGraphProvider.getGraphInstance(); - LOG.info("Completed initializing graph schema backend."); - } catch (Exception e) { - LOG.error("Could not initialize graph schema backend due to exception, {}", e.getMessage(), e); - throw new SetupException("Could not initialize graph schema due to exception", e); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java deleted file mode 100644 index d7a8fa9..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java +++ /dev/null @@ -1,452 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.repository.graph; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.RequestContext; -import org.apache.atlas.repository.Constants; -import org.apache.atlas.repository.RepositoryException; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; -import org.apache.atlas.repository.graphdb.AtlasGraph; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.ITypedInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.persistence.AtlasSystemAttributes; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.StructType; -import org.apache.atlas.typesystem.types.TraitType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.apache.atlas.repository.graph.GraphHelper.string; - -@Component -@Deprecated -public final class GraphToTypedInstanceMapper { - - private static final Logger LOG = LoggerFactory.getLogger(GraphToTypedInstanceMapper.class); - private static TypeSystem typeSystem = TypeSystem.getInstance(); - private static final GraphHelper graphHelper = GraphHelper.getInstance(); - - private final AtlasGraph atlasGraph; - - @Inject - public GraphToTypedInstanceMapper(AtlasGraph atlasGraph) { - this.atlasGraph = atlasGraph; - } - - public ITypedReferenceableInstance mapGraphToTypedInstance(String guid, AtlasVertex instanceVertex) - throws AtlasException { - - if(LOG.isDebugEnabled()) { - //We don't do a cache check here since we want that to be at a higher level - //where the vertex lookup can also be avoided. However, this is a convenient - //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; - } - - private void mapVertexToInstanceTraits(AtlasVertex instanceVertex, ITypedReferenceableInstance typedInstance, - List<String> traits) throws AtlasException { - for (String traitName : traits) { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping trait {} to instance", traitName); - } - - TraitType traitType = typeSystem.getDataType(TraitType.class, traitName); - mapVertexToTraitInstance(instanceVertex, typedInstance, traitName, traitType); - } - } - - public void mapVertexToInstance(AtlasVertex instanceVertex, ITypedInstance typedInstance, - Map<String, AttributeInfo> fields) throws AtlasException { - - if (LOG.isDebugEnabled()) { - LOG.debug("Mapping vertex {} to instance {} for fields", instanceVertex, typedInstance.getTypeName(), - fields); - } - - for (AttributeInfo attributeInfo : fields.values()) { - mapVertexToAttribute(instanceVertex, typedInstance, attributeInfo); - } - } - - private void mapVertexToAttribute(AtlasVertex instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo) throws AtlasException { - - if (LOG.isDebugEnabled()) { - LOG.debug("Mapping attributeInfo {}", attributeInfo.name); - } - - final IDataType dataType = attributeInfo.dataType(); - final String vertexPropertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo); - String relationshipLabel = GraphHelper.getEdgeLabel(typedInstance, attributeInfo); - - switch (dataType.getTypeCategory()) { - case PRIMITIVE: - mapVertexToPrimitive(instanceVertex, typedInstance, attributeInfo); - break; // add only if vertex has this attribute - - case ENUM: - Object propertyValue = GraphHelper.getProperty(instanceVertex, vertexPropertyName); - if (propertyValue == null) { - return; - } - - typedInstance.set(attributeInfo.name, dataType.convert(propertyValue, Multiplicity.REQUIRED)); - break; - - case ARRAY: - mapVertexToArrayInstance(instanceVertex, typedInstance, attributeInfo, vertexPropertyName); - break; - - case MAP: - mapVertexToMapInstance(instanceVertex, typedInstance, attributeInfo, vertexPropertyName); - break; - - case STRUCT: - ITypedStruct structInstance = mapVertexToStructInstance(instanceVertex, - (StructType) attributeInfo.dataType(), relationshipLabel, null); - typedInstance.set(attributeInfo.name, structInstance); - break; - - case TRAIT: - // do NOTHING - handled in class - break; - - case CLASS: - AtlasEdge nullEdge = null; - Object idOrInstance = mapVertexToClassReference(instanceVertex, attributeInfo, relationshipLabel, - attributeInfo.dataType(), nullEdge); - if (idOrInstance != null) { - typedInstance.set(attributeInfo.name, idOrInstance); - } - break; - - default: - break; - } - } - - private Object mapVertexToClassReference(AtlasVertex instanceVertex, AttributeInfo attributeInfo, - String relationshipLabel, IDataType dataType, AtlasEdge optionalEdge) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel); - } - - AtlasEdge edge = null; - if (optionalEdge == null) { - edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel); - } else { - edge = optionalEdge; - } - - if (GraphHelper.elementExists(edge)) { - final AtlasVertex referenceVertex = edge.getInVertex(); - final String guid = GraphHelper.getSingleValuedProperty(referenceVertex, Constants.GUID_PROPERTY_KEY, String.class); - - if (LOG.isDebugEnabled()) { - LOG.debug("Found vertex {} for label {} with guid {}", referenceVertex, relationshipLabel, guid); - } - - if (attributeInfo.isComposite) { - //Also, when you retrieve a type's instance, you get the complete object graph of the composites - LOG.debug("Found composite, mapping vertex to instance"); - ITypedReferenceableInstance cached = RequestContext.get().getInstanceV1(guid); - if(cached != null) { - return cached; - } - return mapGraphToTypedInstance(guid, referenceVertex); - } else { - String state = GraphHelper.getStateAsString(referenceVertex); - Id referenceId = new Id(guid, GraphHelper.getVersion(referenceVertex).intValue(), dataType.getName(), state); - - if (LOG.isDebugEnabled()) { - LOG.debug("Found non-composite, adding id {} ", referenceId); - } - - return referenceId; - } - } - - return null; - } - - @SuppressWarnings("unchecked") - private void mapVertexToArrayInstance(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo, String propertyName) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping vertex {} to array {}", instanceVertex, attributeInfo.name); - } - - final DataTypes.ArrayType arrayType = (DataTypes.ArrayType) attributeInfo.dataType(); - final IDataType elementType = arrayType.getElemType(); - - List<Object> list = GraphHelper.getArrayElementsProperty(elementType, instanceVertex, propertyName); - - if (list == null || list.size() == 0) { - return; - } - - String edgeLabel = GraphHelper.EDGE_LABEL_PREFIX + propertyName; - ArrayList values = new ArrayList(); - for (Object aList : list) { - values.add(mapVertexToCollectionEntry(instanceVertex, attributeInfo, elementType, aList, - edgeLabel)); - } - - if (values.size() > 0) { - typedInstance.set(attributeInfo.name, values); - } - } - - private Object mapVertexToCollectionEntry(AtlasVertex instanceVertex, AttributeInfo attributeInfo, - IDataType elementType, Object value, String edgeLabel) throws AtlasException { - switch (elementType.getTypeCategory()) { - case PRIMITIVE: - case ENUM: - return value; - - case ARRAY: - case MAP: - case TRAIT: - // do nothing - break; - - case STRUCT: - return mapVertexToStructInstance(instanceVertex, (StructType) elementType, edgeLabel, (AtlasEdge) value); - - case CLASS: - return mapVertexToClassReference(instanceVertex, attributeInfo, edgeLabel, elementType, (AtlasEdge) value); - - default: - break; - } - - throw new IllegalArgumentException(); - } - - @SuppressWarnings("unchecked") - private void mapVertexToMapInstance(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo, final String propertyName) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping vertex {} to array {}", instanceVertex, attributeInfo.name); - } - - List<String> keys = GraphHelper.getListProperty(instanceVertex, propertyName); - if (keys == null || keys.size() == 0) { - return; - } - DataTypes.MapType mapType = (DataTypes.MapType) attributeInfo.dataType(); - final IDataType valueType = mapType.getValueType(); - - HashMap<String,Object> values = new HashMap<>(); - for (String key : keys) { - final String keyPropertyName = propertyName + "." + key; - final String edgeLabel = GraphHelper.EDGE_LABEL_PREFIX + keyPropertyName; - final Object keyValue = GraphHelper.getMapValueProperty(valueType, instanceVertex, keyPropertyName); - Object mapValue = mapVertexToCollectionEntry(instanceVertex, attributeInfo, valueType, keyValue, edgeLabel); - if (mapValue != null) { - values.put(key, mapValue); - } - } - - if (!values.isEmpty()) { - typedInstance.set(attributeInfo.name, values); - } - } - - private ITypedStruct mapVertexToStructInstance(AtlasVertex instanceVertex, StructType structType, - String relationshipLabel, AtlasEdge optionalEdge) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("mapping {} to struct {}", string(instanceVertex), relationshipLabel); - } - - ITypedStruct structInstance = null; - - AtlasEdge edge; - if (optionalEdge == null) { - edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel); - } else { - edge = optionalEdge; - } - - if (GraphHelper.elementExists(edge)) { - structInstance = structType.createInstance(); - AtlasVertex structInstanceVertex = edge.getInVertex(); - if (LOG.isDebugEnabled()) { - LOG.debug("Found struct instance {}, mapping to instance {} ", string(structInstanceVertex), - structInstance.getTypeName()); - } - - mapVertexToInstance(structInstanceVertex, structInstance, structType.fieldMapping().fields); - } - return structInstance; - } - - private void mapVertexToTraitInstance(AtlasVertex instanceVertex, ITypedReferenceableInstance typedInstance, - String traitName, TraitType traitType) throws AtlasException { - ITypedStruct traitInstance = (ITypedStruct) typedInstance.getTrait(traitName); - - mapVertexToTraitInstance(instanceVertex, typedInstance.getTypeName(), traitName, traitType, traitInstance); - } - - private void mapVertexToTraitInstance(AtlasVertex<?,?> instanceVertex, String typedInstanceTypeName, String traitName, - TraitType traitType, ITypedStruct traitInstance) throws AtlasException { - String relationshipLabel = GraphHelper.getTraitLabel(typedInstanceTypeName, traitName); - - if (LOG.isDebugEnabled()) { - LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel); - } - - for (AtlasEdge<?,?> edge : instanceVertex.getEdges(AtlasEdgeDirection.OUT, relationshipLabel)) { - final AtlasVertex<?,?> traitInstanceVertex = edge.getInVertex(); - if (traitInstanceVertex != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Found trait instance vertex {}, mapping to instance {} ", traitInstanceVertex, - traitInstance.getTypeName()); - } - - mapVertexToInstance(traitInstanceVertex, traitInstance, traitType.fieldMapping().fields); - break; - } - } - } - - private void mapVertexToPrimitive(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance, - AttributeInfo attributeInfo) throws AtlasException { - if (LOG.isDebugEnabled()) { - LOG.debug("Adding primitive {} from vertex {}", attributeInfo, instanceVertex); - } - - final String vertexPropertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo); - if (GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Object.class) == null) { - return; - } - - if (attributeInfo.dataType() == DataTypes.STRING_TYPE) { - typedInstance.setString(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, String.class)); - } else if (attributeInfo.dataType() == DataTypes.SHORT_TYPE) { - typedInstance.setShort(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Short.class)); - } else if (attributeInfo.dataType() == DataTypes.INT_TYPE) { - typedInstance.setInt(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Integer.class)); - } else if (attributeInfo.dataType() == DataTypes.BIGINTEGER_TYPE) { - typedInstance.setBigInt(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, BigInteger.class)); - } else if (attributeInfo.dataType() == DataTypes.BOOLEAN_TYPE) { - typedInstance.setBoolean(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Boolean.class)); - } else if (attributeInfo.dataType() == DataTypes.BYTE_TYPE) { - typedInstance.setByte(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Byte.class)); - } else if (attributeInfo.dataType() == DataTypes.LONG_TYPE) { - typedInstance.setLong(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Long.class)); - } else if (attributeInfo.dataType() == DataTypes.FLOAT_TYPE) { - typedInstance.setFloat(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Float.class)); - } else if (attributeInfo.dataType() == DataTypes.DOUBLE_TYPE) { - typedInstance.setDouble(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Double.class)); - } else if (attributeInfo.dataType() == DataTypes.BIGDECIMAL_TYPE) { - typedInstance - .setBigDecimal(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, BigDecimal.class)); - } else if (attributeInfo.dataType() == DataTypes.DATE_TYPE) { - final Long dateVal = GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Long.class); - typedInstance.setDate(attributeInfo.name, new Date(dateVal)); - } - } - - - public ITypedInstance getReferredEntity(String edgeId, IDataType<?> referredType) throws AtlasException { - final AtlasEdge edge = getGraph().getEdge(edgeId); - if (edge != null) { - final AtlasVertex referredVertex = edge.getInVertex(); - if (referredVertex != null) { - switch (referredType.getTypeCategory()) { - case STRUCT: - if (LOG.isDebugEnabled()) { - LOG.debug("Found struct instance vertex {}, mapping to instance {} ", referredVertex, - referredType.getName()); - } - - StructType structType = (StructType) referredType; - ITypedStruct instance = structType.createInstance(); - Map<String, AttributeInfo> fields = structType.fieldMapping().fields; - mapVertexToInstance(referredVertex, instance, fields); - return instance; - case CLASS: - //TODO isComposite handling for class loads - return GraphHelper.getIdFromVertex(referredType.getName(), referredVertex); - default: - throw new UnsupportedOperationException("Loading " + referredType.getTypeCategory() + " is not supported"); - } - } - } - return null; - } - - private AtlasGraph getGraph() throws RepositoryException { - return atlasGraph; - } -} - http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java b/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java deleted file mode 100644 index e00ef96..0000000 --- a/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.atlas.repository.graph; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.annotation.ConditionalOnAtlasProperty; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; - -@Component -@ConditionalOnAtlasProperty(property = "atlas.DeleteHandler.impl") -public class HardDeleteHandler extends DeleteHandler { - - @Inject - public HardDeleteHandler(TypeSystem typeSystem) { - super(typeSystem, true, false); - } - - @Override - protected void _deleteVertex(AtlasVertex instanceVertex, boolean force) { - graphHelper.removeVertex(instanceVertex); - } - - @Override - protected void deleteEdge(AtlasEdge edge, boolean force) throws AtlasException { - graphHelper.removeEdge(edge); - } -}