You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by su...@apache.org on 2016/10/10 22:37:53 UTC
[5/8] incubator-atlas git commit: ATLAS-694 Update Atlas code to use
graph abstraction layer (jnhagelb via sumasai)
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/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 f2e40f9..8038815 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
@@ -18,14 +18,16 @@
package org.apache.atlas.repository.graph;
-import com.thinkaurelius.titan.core.Cardinality;
-import com.thinkaurelius.titan.core.PropertyKey;
-import com.thinkaurelius.titan.core.TitanGraph;
-import com.thinkaurelius.titan.core.schema.Mapping;
-import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
-import com.thinkaurelius.titan.core.schema.TitanManagement;
-import com.tinkerpop.blueprints.Edge;
-import com.tinkerpop.blueprints.Vertex;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.inject.Inject;
+
import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.AtlasException;
import org.apache.atlas.discovery.SearchIndexer;
@@ -35,6 +37,11 @@ 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;
+import org.apache.atlas.repository.graphdb.AtlasGraph;
+import org.apache.atlas.repository.graphdb.AtlasGraphIndex;
+import org.apache.atlas.repository.graphdb.AtlasGraphManagement;
+import org.apache.atlas.repository.graphdb.AtlasPropertyKey;
import org.apache.atlas.typesystem.types.AttributeInfo;
import org.apache.atlas.typesystem.types.ClassType;
import org.apache.atlas.typesystem.types.DataTypes;
@@ -46,13 +53,8 @@ import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import javax.inject.Inject;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
+import com.google.common.annotations.VisibleForTesting;
+
/**
* Adds index for properties of a given type when its added before any instances are added.
@@ -60,33 +62,46 @@ import java.util.Map;
public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChangeHandler {
private static final Logger LOG = LoggerFactory.getLogger(GraphBackedSearchIndexer.class);
-
- private final TitanGraph titanGraph;
-
- List<Class> MIXED_INDEX_EXCLUSIONS = new ArrayList() {{
+
+ private static final List<Class> VERTEX_INDEX_EXCLUSIONS = new ArrayList() {
+ {
add(Boolean.class);
add(BigDecimal.class);
add(BigInteger.class);
- }};
-
+ }
+ };
+
+ //allows injection of a dummy graph for testing
+ private IAtlasGraphProvider provider;
+
@Inject
- public GraphBackedSearchIndexer(GraphProvider<TitanGraph> graphProvider) throws AtlasException {
- this(graphProvider, ApplicationProperties.get());
+ public GraphBackedSearchIndexer() throws RepositoryException, AtlasException {
+ this(new AtlasGraphProvider(), ApplicationProperties.get());
}
- GraphBackedSearchIndexer(GraphProvider<TitanGraph> graphProvider, Configuration configuration)
+ @VisibleForTesting
+ GraphBackedSearchIndexer( IAtlasGraphProvider provider, Configuration configuration)
throws IndexException, RepositoryException {
- this.titanGraph = graphProvider.get();
+ this.provider = provider;
if (!HAConfiguration.isHAEnabled(configuration)) {
- initialize();
+ initialize(provider.get());
}
}
/**
- * Initializes the indices for the graph - create indices for Global Vertex Keys
+ * Initializes the indices for the graph - create indices for Global AtlasVertex Keys
*/
private void initialize() throws RepositoryException, IndexException {
- TitanManagement management = titanGraph.getManagementSystem();
+
+ initialize(provider.get());
+ }
+
+ /**
+ * Initializes the indices for the graph - create indices for Global AtlasVertex Keys
+ */
+ private void initialize(AtlasGraph graph) throws RepositoryException, IndexException {
+ AtlasGraphManagement management = graph.getManagementSystem();
+
try {
if (management.containsPropertyKey(Constants.VERTEX_TYPE_PROPERTY_KEY)) {
LOG.info("Global indexes already exist for graph");
@@ -94,38 +109,40 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
return;
}
- /* This is called only once, which is the first time Atlas types are made indexable .*/
- LOG.info("Indexes do not exist, Creating indexes for titanGraph.");
- management.buildIndex(Constants.VERTEX_INDEX, Vertex.class).buildMixedIndex(Constants.BACKING_INDEX);
- management.buildIndex(Constants.EDGE_INDEX, Edge.class).buildMixedIndex(Constants.BACKING_INDEX);
+ /* This is called only once, which is the first time Atlas types are made indexable .*/
+ LOG.info("Indexes do not exist, Creating indexes for graph.");
+
+
+ management.createVertexIndex(Constants.VERTEX_INDEX, Constants.BACKING_INDEX, Collections.<AtlasPropertyKey>emptyList());
+ management.createEdgeIndex(Constants.EDGE_INDEX, Constants.BACKING_INDEX);
// create a composite index for guid as its unique
createIndexes(management, Constants.GUID_PROPERTY_KEY, String.class, true,
- Cardinality.SINGLE, true, true);
+ AtlasCardinality.SINGLE, true, true);
- // create a composite index for entity state
- createIndexes(management, Constants.TIMESTAMP_PROPERTY_KEY, Long.class, false, Cardinality.SINGLE, true, true);
+ // create a composite index for entity creation timestamp
+ createIndexes(management, Constants.TIMESTAMP_PROPERTY_KEY, Long.class, false, AtlasCardinality.SINGLE, true, true);
// create a mixed index for entity state. Set systemProperty flag deliberately to false
// so that it doesnt create a composite index which has issues with
// titan 0.5.4 - Refer https://groups.google.com/forum/#!searchin/aureliusgraphs/hemanth/aureliusgraphs/bx7T843mzXU/fjAsclx7GAAJ
- createStateMixedIndex(management);
+ createIndexes(management, Constants.STATE_PROPERTY_KEY, String.class, false, AtlasCardinality.SINGLE, false, false);
- // create a composite index for entity state
+ // create a composite index for entity modification timestamp
createIndexes(management, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class, false,
- Cardinality.SINGLE, false, false);
+ AtlasCardinality.SINGLE, false, false);
// create a composite and mixed index for type since it can be combined with other keys
- createIndexes(management, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class, false, Cardinality.SINGLE,
+ createIndexes(management, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class, false, AtlasCardinality.SINGLE,
true, true);
// create a composite and mixed index for type since it can be combined with other keys
- createIndexes(management, Constants.SUPER_TYPES_PROPERTY_KEY, String.class, false, Cardinality.SET,
+ createIndexes(management, Constants.SUPER_TYPES_PROPERTY_KEY, String.class, false, AtlasCardinality.SET,
true, true);
// create a composite and mixed index for traitNames since it can be combined with other
// keys. Traits must be a set and not a list.
- createIndexes(management, Constants.TRAIT_NAMES_PROPERTY_KEY, String.class, false, Cardinality.SET,
+ createIndexes(management, Constants.TRAIT_NAMES_PROPERTY_KEY, String.class, false, AtlasCardinality.SET,
true, true);
// Index for full text search
@@ -133,7 +150,8 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
//Indexes for graph backed type system store
createTypeStoreIndexes(management);
-
+
+
commit(management);
LOG.info("Index creation for global keys complete.");
} catch (Throwable t) {
@@ -141,34 +159,23 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
throw new RepositoryException(t);
}
}
+
- private void createStateMixedIndex(TitanManagement management) {
- PropertyKey propertyKey = management.getPropertyKey(Constants.STATE_PROPERTY_KEY);
- if (propertyKey == null) {
- propertyKey = management.makePropertyKey(Constants.STATE_PROPERTY_KEY).dataType(String.class).cardinality(Cardinality.SINGLE)
- .make();
- }
- enhanceMixedIndex(management, Constants.STATE_PROPERTY_KEY, String.class, Cardinality.SINGLE, propertyKey);
- }
+ private void createFullTextIndex(AtlasGraphManagement management) {
+ AtlasPropertyKey fullText =
+ management.makePropertyKey(Constants.ENTITY_TEXT_PROPERTY_KEY, String.class, AtlasCardinality.SINGLE);
+ management.createFullTextIndex(Constants.FULLTEXT_INDEX, fullText, Constants.BACKING_INDEX);
- private void createFullTextIndex(TitanManagement management) {
- PropertyKey fullText =
- management.makePropertyKey(Constants.ENTITY_TEXT_PROPERTY_KEY).dataType(String.class).make();
-
- management.buildIndex(Constants.FULLTEXT_INDEX, Vertex.class)
- .addKey(fullText, com.thinkaurelius.titan.core.schema.Parameter.of("mapping", Mapping.TEXT))
- .buildMixedIndex(Constants.BACKING_INDEX);
- LOG.info("Created mixed index for {}", Constants.ENTITY_TEXT_PROPERTY_KEY);
}
- private void createTypeStoreIndexes(TitanManagement management) {
+ private void createTypeStoreIndexes(AtlasGraphManagement management) {
//Create unique index on typeName
- createIndexes(management, Constants.TYPENAME_PROPERTY_KEY, String.class, true, Cardinality.SINGLE,
+ createIndexes(management, Constants.TYPENAME_PROPERTY_KEY, String.class, true, AtlasCardinality.SINGLE,
true, true);
//create index on vertex type
- createIndexes(management, Constants.VERTEX_TYPE_PROPERTY_KEY, String.class, false, Cardinality.SINGLE,
+ createIndexes(management, Constants.VERTEX_TYPE_PROPERTY_KEY, String.class, false, AtlasCardinality.SINGLE,
true, true);
}
@@ -180,7 +187,8 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
*/
@Override
public void onAdd(Collection<? extends IDataType> dataTypes) throws AtlasException {
- TitanManagement management = titanGraph.getManagementSystem();
+ AtlasGraphManagement management = provider.get().getManagementSystem();
+
for (IDataType dataType : dataTypes) {
LOG.debug("Creating indexes for type name={}, definition={}", dataType.getName(), dataType.getClass());
try {
@@ -203,7 +211,7 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
onAdd(dataTypes);
}
- private void addIndexForType(TitanManagement management, IDataType dataType) {
+ private void addIndexForType(AtlasGraphManagement management, IDataType dataType) {
switch (dataType.getTypeCategory()) {
case PRIMITIVE:
case ENUM:
@@ -233,17 +241,17 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
}
}
- private void createIndexForFields(TitanManagement management, IDataType dataType, Map<String, AttributeInfo> fields) {
+ private void createIndexForFields(AtlasGraphManagement management, IDataType dataType, Map<String, AttributeInfo> fields) {
for (AttributeInfo field : fields.values()) {
createIndexForAttribute(management, dataType.getName(), field);
}
}
- private void createIndexForAttribute(TitanManagement management, String typeName, AttributeInfo field) {
+ private void createIndexForAttribute(AtlasGraphManagement management, String typeName, AttributeInfo field) {
final String propertyName = GraphHelper.encodePropertyKey(typeName + "." + field.name);
switch (field.dataType().getTypeCategory()) {
case PRIMITIVE:
- Cardinality cardinality = getCardinality(field.multiplicity);
+ AtlasCardinality cardinality = getCardinality(field.multiplicity);
createIndexes(management, propertyName, getPrimitiveClass(field.dataType()), field.isUnique,
cardinality, false, field.isIndexable);
break;
@@ -308,111 +316,119 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
throw new IllegalArgumentException("unknown data type " + dataType);
}
+
-
- private Cardinality getCardinality(Multiplicity multiplicity) {
+ private AtlasCardinality getCardinality(Multiplicity multiplicity) {
if (multiplicity == Multiplicity.OPTIONAL || multiplicity == Multiplicity.REQUIRED) {
- return Cardinality.SINGLE;
+ return AtlasCardinality.SINGLE;
} else if (multiplicity == Multiplicity.COLLECTION) {
- return Cardinality.LIST;
+ return AtlasCardinality.LIST;
} else if (multiplicity == Multiplicity.SET) {
- return Cardinality.SET;
+ return AtlasCardinality.SET;
}
// todo - default to LIST as this is the most forgiving
- return Cardinality.LIST;
+ return AtlasCardinality.LIST;
}
+
+ private AtlasPropertyKey createIndexes(AtlasGraphManagement management, String propertyName, Class propertyClass,
+ boolean isUnique, AtlasCardinality cardinality, boolean createCompositeForAttribute,
+ boolean createCompositeWithTypeandSuperTypes) {
- private PropertyKey createIndexes(TitanManagement management, String propertyName,
- Class propertyClass, boolean isUnique, Cardinality cardinality,
- boolean createCompositeForAttribute, boolean createCompositeWithTypeandSuperTypes) {
- PropertyKey propertyKey = management.getPropertyKey(propertyName);
+ AtlasPropertyKey propertyKey = management.getPropertyKey(propertyName);
if (propertyKey == null) {
- propertyKey = management.makePropertyKey(propertyName).dataType(propertyClass).cardinality(cardinality)
- .make();
+ propertyKey = management.makePropertyKey(propertyName, propertyClass, cardinality);
- enhanceMixedIndex(management, propertyName, propertyClass, cardinality, propertyKey);
+ updateVertexIndex(management, propertyName, propertyClass, cardinality, propertyKey);
}
if (createCompositeForAttribute) {
- createCompositeIndex(management, propertyName, propertyClass, propertyKey, isUnique);
+ createExactMatchIndex(management, propertyClass, propertyKey, isUnique);
} else if (createCompositeWithTypeandSuperTypes) {
- //Index with typename since typename+property key queries need to speed up
- createCompositeIndexWithTypeName(management, propertyName, propertyClass, propertyKey);
- createCompositeIndexWithSuperTypeName(management, propertyName, propertyClass, propertyKey);
+ // Index with typename since typename+property key queries need to
+ // speed up
+ createExactMatchIndexWithTypeName(management, propertyClass, propertyKey);
+ createExactMatchIndexWithSuperTypeName(management, propertyClass, propertyKey);
}
return propertyKey;
}
-
- private void createCompositeIndex(TitanManagement management, String propertyName, Class propertyClass,
- PropertyKey propertyKey, boolean enforceUniqueness) {
- LOG.debug("Creating composite index for property {} of type {} ", propertyName,
- propertyClass.getName());
-
- TitanGraphIndex existingIndex = management.getGraphIndex(propertyName);
- if ( existingIndex == null) {
- TitanManagement.IndexBuilder indexBuilder =
- management.buildIndex(propertyName, Vertex.class).addKey(propertyKey);
+
+ private void createExactMatchIndex(AtlasGraphManagement management, Class propertyClass,
+ AtlasPropertyKey propertyKey, boolean enforceUniqueness) {
+
+ String propertyName = propertyKey.getName();
+ LOG.debug("Creating composite index for property {} of type {} ", propertyName, propertyClass.getName());
+
+ AtlasGraphIndex existingIndex = management.getGraphIndex(propertyName);
+ if (existingIndex == null) {
if (enforceUniqueness) {
- LOG.debug("Enabling unique index for property {} of type {} ", propertyName,
- propertyClass.getName());
- indexBuilder.unique();
+ LOG.debug("Enabling unique index for property {} of type {} ", propertyName, propertyClass.getName());
}
- indexBuilder.buildCompositeIndex();
+ management.createExactMatchIndex(propertyName, enforceUniqueness, Collections.singletonList(propertyKey));
+
}
LOG.info("Created composite index for property {} of type {} ", propertyName, propertyClass.getName());
}
+
- private void createCompositeIndexWithTypeName(TitanManagement management, String propertyName, Class propertyClass,
- PropertyKey propertyKey) {
- createCompositeIndexWithSystemProperty(management, propertyName, propertyClass, propertyKey, Constants.ENTITY_TYPE_PROPERTY_KEY, Cardinality.SINGLE);
+ private void createExactMatchIndexWithTypeName(AtlasGraphManagement management,
+ Class propertyClass, AtlasPropertyKey propertyKey) {
+ createExactMatchIndexWithSystemProperty(management, propertyClass, propertyKey,
+ Constants.ENTITY_TYPE_PROPERTY_KEY, AtlasCardinality.SINGLE);
}
- private void createCompositeIndexWithSuperTypeName(TitanManagement management, String propertyName, Class propertyClass,
- PropertyKey propertyKey) {
- createCompositeIndexWithSystemProperty(management, propertyName, propertyClass, propertyKey, Constants.SUPER_TYPES_PROPERTY_KEY, Cardinality.SET);
+ private void createExactMatchIndexWithSuperTypeName(AtlasGraphManagement management,
+ Class propertyClass, AtlasPropertyKey propertyKey) {
+ createExactMatchIndexWithSystemProperty(management, propertyClass, propertyKey,
+ Constants.SUPER_TYPES_PROPERTY_KEY, AtlasCardinality.SET);
}
- private void createCompositeIndexWithSystemProperty(TitanManagement management, String propertyName, Class propertyClass,
- PropertyKey propertyKey, final String systemPropertyKey, Cardinality cardinality) {
- LOG.debug("Creating composite index for property {} of type {} and {}", propertyName,
- propertyClass.getName(), systemPropertyKey);
- PropertyKey typePropertyKey = management.getPropertyKey(systemPropertyKey);
+ private void createExactMatchIndexWithSystemProperty(AtlasGraphManagement management,
+ Class propertyClass, AtlasPropertyKey propertyKey, final String systemPropertyKey,
+ AtlasCardinality cardinality) {
+
+ LOG.debug("Creating composite index for property {} of type {} and {}", propertyKey.getName(), propertyClass.getName(),
+ systemPropertyKey);
+
+ AtlasPropertyKey typePropertyKey = management.getPropertyKey(systemPropertyKey);
if (typePropertyKey == null) {
- typePropertyKey = management.makePropertyKey(systemPropertyKey).
- dataType(String.class).cardinality(cardinality)
- .make();
+ typePropertyKey = management.makePropertyKey(systemPropertyKey, String.class, cardinality);
}
- final String indexName = propertyName + systemPropertyKey;
- TitanGraphIndex existingIndex = management.getGraphIndex(indexName);
-
- if ( existingIndex == null) {
- TitanManagement.IndexBuilder indexBuilder =
- management.buildIndex(indexName, Vertex.class).
- addKey(propertyKey).addKey(typePropertyKey);
- indexBuilder.buildCompositeIndex();
- LOG.info("Created composite index for property {} of type {} and {}", propertyName, propertyClass.getName(), systemPropertyKey);
+
+ final String indexName = propertyKey.getName() + systemPropertyKey;
+ AtlasGraphIndex existingIndex = management.getGraphIndex(indexName);
+
+ if (existingIndex == null) {
+
+ List<AtlasPropertyKey> keys = new ArrayList<AtlasPropertyKey>(2);
+ keys.add(propertyKey);
+ keys.add(typePropertyKey);
+ management.createExactMatchIndex(indexName, false, keys);
+
+ LOG.info("Created composite index for property {} of type {} and {}", propertyKey.getName(), propertyClass.getName(),
+ systemPropertyKey);
}
}
- private void enhanceMixedIndex(TitanManagement management, String propertyName, Class propertyClass,
- Cardinality cardinality, PropertyKey propertyKey) {
- if (checkIfMixedIndexApplicable(propertyClass, cardinality)) {
- //Use backing index
+ private void updateVertexIndex(AtlasGraphManagement management, String propertyName, Class propertyClass,
+ AtlasCardinality cardinality, AtlasPropertyKey propertyKey) {
+ if (checkIfVertexIndexApplicable(propertyClass, cardinality)) {
+ // Use backing index
+ management.addVertexIndexKey(Constants.VERTEX_INDEX, propertyKey);
LOG.debug("Creating backing index for property {} of type {} ", propertyName, propertyClass.getName());
- TitanGraphIndex vertexIndex = management.getGraphIndex(Constants.VERTEX_INDEX);
- management.addIndexKey(vertexIndex, propertyKey);
+
LOG.info("Created backing index for property {} of type {} ", propertyName, propertyClass.getName());
}
}
- private boolean checkIfMixedIndexApplicable(Class propertyClass, Cardinality cardinality) {
- return !(MIXED_INDEX_EXCLUSIONS.contains(propertyClass) || cardinality == Cardinality.LIST || cardinality ==
- Cardinality.SET);
+ private boolean checkIfVertexIndexApplicable(Class propertyClass, AtlasCardinality cardinality) {
+ return !(VERTEX_INDEX_EXCLUSIONS.contains(propertyClass) || cardinality.isMany());
}
+
- public void commit(TitanManagement management) throws IndexException {
+
+ private void commit(AtlasGraphManagement management) throws IndexException {
try {
management.commit();
} catch (Exception e) {
@@ -421,7 +437,7 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
}
}
- public void rollback(TitanManagement management) throws IndexException {
+ private void rollback(AtlasGraphManagement management) throws IndexException {
try {
management.rollback();
} catch (Exception e) {
@@ -451,7 +467,7 @@ public class GraphBackedSearchIndexer implements SearchIndexer, ActiveStateChang
public void instanceIsPassive() {
LOG.info("Reacting to passive state: No action right now.");
}
-
+
/* Commenting this out since we do not need an index for edge label here
private void createEdgeMixedIndex(String propertyName) {
EdgeLabel edgeLabel = management.getEdgeLabel(propertyName);
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/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 1ce87c9..7e47d30 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
@@ -18,49 +18,55 @@
package org.apache.atlas.repository.graph;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-import com.thinkaurelius.titan.core.TitanGraph;
-import com.thinkaurelius.titan.core.TitanProperty;
-import com.thinkaurelius.titan.core.TitanVertex;
-import com.tinkerpop.blueprints.Direction;
-import com.tinkerpop.blueprints.Edge;
-import com.tinkerpop.blueprints.Element;
-import com.tinkerpop.blueprints.Graph;
-import com.tinkerpop.blueprints.GraphQuery;
-import com.tinkerpop.blueprints.Vertex;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.Stack;
+import java.util.UUID;
+
import org.apache.atlas.ApplicationProperties;
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.AtlasElement;
+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.IReferenceableInstance;
import org.apache.atlas.typesystem.ITypedInstance;
import org.apache.atlas.typesystem.ITypedReferenceableInstance;
+import org.apache.atlas.typesystem.Referenceable;
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.utils.ParamChecker;
import org.apache.commons.lang.StringUtils;
+import org.codehaus.jettison.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.Stack;
-import java.util.UUID;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
/**
* Utility class for graph operations.
@@ -77,13 +83,13 @@ public final class GraphHelper {
private static volatile GraphHelper INSTANCE;
- private TitanGraph titanGraph;
+ private AtlasGraph graph;
private static int maxRetries;
public static long retrySleepTimeMillis;
@VisibleForTesting
- GraphHelper(TitanGraph titanGraph) {
- this.titanGraph = titanGraph;
+ GraphHelper(AtlasGraph graph) {
+ this.graph = graph;
try {
maxRetries = ApplicationProperties.get().getInt(RETRY_COUNT, 3);
retrySleepTimeMillis = ApplicationProperties.get().getLong(RETRY_DELAY, 1000);
@@ -96,7 +102,7 @@ public final class GraphHelper {
if ( INSTANCE == null) {
synchronized (GraphHelper.class) {
if (INSTANCE == null) {
- INSTANCE = new GraphHelper(TitanGraphProvider.getGraphInstance());
+ INSTANCE = new GraphHelper(AtlasGraphProvider.getGraphInstance());
}
}
}
@@ -104,7 +110,7 @@ public final class GraphHelper {
}
@VisibleForTesting
- static GraphHelper getInstance(TitanGraph graph) {
+ static GraphHelper getInstance(AtlasGraph graph) {
if ( INSTANCE == null) {
synchronized (GraphHelper.class) {
if (INSTANCE == null) {
@@ -116,10 +122,10 @@ public final class GraphHelper {
}
- public Vertex createVertexWithIdentity(ITypedReferenceableInstance typedInstance, Set<String> superTypeNames) {
+ public AtlasVertex createVertexWithIdentity(ITypedReferenceableInstance typedInstance, Set<String> superTypeNames) {
final String guid = UUID.randomUUID().toString();
- final Vertex vertexWithIdentity = createVertexWithoutIdentity(typedInstance.getTypeName(),
+ final AtlasVertex vertexWithIdentity = createVertexWithoutIdentity(typedInstance.getTypeName(),
new Id(guid, 0, typedInstance.getTypeName()), superTypeNames);
// add identity
@@ -131,10 +137,10 @@ public final class GraphHelper {
return vertexWithIdentity;
}
- public Vertex createVertexWithoutIdentity(String typeName, Id typedInstanceId, Set<String> superTypeNames) {
- LOG.debug("Creating vertex for type {} id {}", typeName,
+ public AtlasVertex createVertexWithoutIdentity(String typeName, Id typedInstanceId, Set<String> superTypeNames) {
+ LOG.debug("Creating AtlasVertex for type {} id {}", typeName,
typedInstanceId != null ? typedInstanceId._getId() : null);
- final Vertex vertexWithoutIdentity = titanGraph.addVertex(null);
+ final AtlasVertex vertexWithoutIdentity = graph.addVertex();
// add type information
setProperty(vertexWithoutIdentity, Constants.ENTITY_TYPE_PROPERTY_KEY, typeName);
@@ -156,9 +162,9 @@ public final class GraphHelper {
return vertexWithoutIdentity;
}
- private Edge addEdge(Vertex fromVertex, Vertex toVertex, String edgeLabel) {
+ private AtlasEdge addEdge(AtlasVertex fromVertex, AtlasVertex toVertex, String edgeLabel) {
LOG.debug("Adding edge for {} -> label {} -> {}", string(fromVertex), edgeLabel, string(toVertex));
- Edge edge = titanGraph.addEdge(null, fromVertex, toVertex, edgeLabel);
+ 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());
@@ -168,15 +174,15 @@ public final class GraphHelper {
return edge;
}
- public Edge getOrCreateEdge(Vertex outVertex, Vertex inVertex, String edgeLabel) throws RepositoryException {
+ public AtlasEdge getOrCreateEdge(AtlasVertex outVertex, AtlasVertex inVertex, String edgeLabel) throws RepositoryException {
for (int numRetries = 0; numRetries < maxRetries; numRetries++) {
try {
LOG.debug("Running edge creation attempt {}", numRetries);
- Iterator<Edge> edges = getAdjacentEdgesByLabel(inVertex, Direction.IN, edgeLabel);
+ Iterator<AtlasEdge> edges = getAdjacentEdgesByLabel(inVertex, AtlasEdgeDirection.IN, edgeLabel);
while (edges.hasNext()) {
- Edge edge = edges.next();
- if (edge.getVertex(Direction.OUT).getId().toString().equals(outVertex.getId().toString())) {
+ AtlasEdge edge = edges.next();
+ if (edge.getOutVertex().equals(outVertex)) {
Id.EntityState edgeState = getState(edge);
if (edgeState == null || edgeState == Id.EntityState.ACTIVE) {
return edge;
@@ -206,16 +212,16 @@ public final class GraphHelper {
}
- public Edge getEdgeByEdgeId(Vertex outVertex, String edgeLabel, String edgeId) {
+ public AtlasEdge getEdgeByEdgeId(AtlasVertex outVertex, String edgeLabel, String edgeId) {
if (edgeId == null) {
return null;
}
- return titanGraph.getEdge(edgeId);
+ return graph.getEdge(edgeId);
//TODO get edge id is expensive. Use this logic. But doesn't work for now
/**
- Iterable<Edge> edges = outVertex.getEdges(Direction.OUT, edgeLabel);
- for (Edge edge : edges) {
+ Iterable<AtlasEdge> edges = outVertex.getEdges(Direction.OUT, edgeLabel);
+ for (AtlasEdge edge : edges) {
if (edge.getId().toString().equals(edgeId)) {
return edge;
}
@@ -226,24 +232,24 @@ public final class GraphHelper {
/**
* Args of the format prop1, key1, prop2, key2...
- * Searches for a vertex with prop1=key1 && prop2=key2
+ * Searches for a AtlasVertex with prop1=key1 && prop2=key2
* @param args
- * @return vertex with the given property keys
+ * @return AtlasVertex with the given property keys
* @throws EntityNotFoundException
*/
- public Vertex findVertex(Object... args) throws EntityNotFoundException {
+ public AtlasVertex findVertex(Object... args) throws EntityNotFoundException {
StringBuilder condition = new StringBuilder();
- GraphQuery query = titanGraph.query();
+ AtlasGraphQuery query = graph.query();
for (int i = 0 ; i < args.length; i+=2) {
query = query.has((String) args[i], args[i+1]);
condition.append(args[i]).append(" = ").append(args[i+1]).append(", ");
}
String conditionStr = condition.toString();
- LOG.debug("Finding vertex with {}", conditionStr);
+ LOG.debug("Finding AtlasVertex with {}", conditionStr);
- Iterator<Vertex> results = query.vertices().iterator();
+ Iterator<AtlasVertex> results = query.vertices().iterator();
// returning one since entityType, qualifiedName should be unique
- Vertex vertex = results.hasNext() ? results.next() : null;
+ AtlasVertex vertex = results.hasNext() ? results.next() : null;
if (vertex == null) {
LOG.debug("Could not find a vertex with {}", condition.toString());
@@ -257,17 +263,17 @@ public final class GraphHelper {
//In some cases of parallel APIs, the edge is added, but get edge by label doesn't return the edge. ATLAS-1104
//So traversing all the edges
- public Iterator<Edge> getAdjacentEdgesByLabel(Vertex instanceVertex, Direction direction, final String edgeLabel) {
+ public Iterator<AtlasEdge> getAdjacentEdgesByLabel(AtlasVertex instanceVertex, AtlasEdgeDirection direction, final String edgeLabel) {
LOG.debug("Finding edges for {} with label {}", string(instanceVertex), edgeLabel);
if(instanceVertex != null && edgeLabel != null) {
- final Iterator<Edge> iterator = instanceVertex.getEdges(direction).iterator();
- return new Iterator<Edge>() {
- private Edge edge = null;
+ final Iterator<AtlasEdge> iterator = instanceVertex.getEdges(direction).iterator();
+ return new Iterator<AtlasEdge>() {
+ private AtlasEdge edge = null;
@Override
public boolean hasNext() {
while (edge == null && iterator.hasNext()) {
- Edge localEdge = iterator.next();
+ AtlasEdge localEdge = iterator.next();
if (localEdge.getLabel().equals(edgeLabel)) {
edge = localEdge;
}
@@ -276,9 +282,9 @@ public final class GraphHelper {
}
@Override
- public Edge next() {
+ public AtlasEdge next() {
if (hasNext()) {
- Edge localEdge = edge;
+ AtlasEdge localEdge = edge;
edge = null;
return localEdge;
}
@@ -294,8 +300,8 @@ public final class GraphHelper {
return null;
}
- public Iterator<Edge> getOutGoingEdgesByLabel(Vertex instanceVertex, String edgeLabel) {
- return getAdjacentEdgesByLabel(instanceVertex, Direction.OUT, edgeLabel);
+ public Iterator<AtlasEdge> getOutGoingEdgesByLabel(AtlasVertex instanceVertex, String edgeLabel) {
+ return getAdjacentEdgesByLabel(instanceVertex, AtlasEdgeDirection.OUT, edgeLabel);
}
/**
@@ -305,19 +311,19 @@ public final class GraphHelper {
* @param edgeLabel
* @return
*/
- public Edge getEdgeForLabel(Vertex vertex, String edgeLabel) {
- Iterator<Edge> iterator = getAdjacentEdgesByLabel(vertex, Direction.OUT, edgeLabel);
- Edge latestDeletedEdge = null;
+ public AtlasEdge getEdgeForLabel(AtlasVertex vertex, String edgeLabel) {
+ Iterator<AtlasEdge> iterator = getAdjacentEdgesByLabel(vertex, AtlasEdgeDirection.OUT, edgeLabel);
+ AtlasEdge latestDeletedEdge = null;
long latestDeletedEdgeTime = Long.MIN_VALUE;
while (iterator != null && iterator.hasNext()) {
- Edge edge = iterator.next();
+ AtlasEdge edge = iterator.next();
Id.EntityState edgeState = getState(edge);
if (edgeState == null || edgeState == Id.EntityState.ACTIVE) {
LOG.debug("Found {}", string(edge));
return edge;
} else {
- Long modificationTime = getProperty(edge, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY);
+ Long modificationTime = edge.getProperty(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class);
if (modificationTime != null && modificationTime >= latestDeletedEdgeTime) {
latestDeletedEdgeTime = modificationTime;
latestDeletedEdge = edge;
@@ -329,25 +335,26 @@ public final class GraphHelper {
return latestDeletedEdge;
}
- public static String vertexString(final Vertex vertex) {
+ public static String vertexString(final AtlasVertex vertex) {
StringBuilder properties = new StringBuilder();
for (String propertyKey : vertex.getPropertyKeys()) {
- properties.append(propertyKey).append("=").append(vertex.getProperty(propertyKey).toString()).append(", ");
+ Collection<?> propertyValues = vertex.getPropertyValues(propertyKey, Object.class);
+ properties.append(propertyKey).append("=").append(propertyValues.toString()).append(", ");
}
- return "v[" + vertex.getId() + "], Properties[" + properties + "]";
+ return "v[" + vertex.getIdForDisplay() + "], Properties[" + properties + "]";
}
- public static String edgeString(final Edge edge) {
- return "e[" + edge.getLabel() + "], [" + edge.getVertex(Direction.OUT) + " -> " + edge.getLabel() + " -> "
- + edge.getVertex(Direction.IN) + "]";
+ public static String edgeString(final AtlasEdge edge) {
+ return "e[" + edge.getLabel() + "], [" + edge.getOutVertex() + " -> " + edge.getLabel() + " -> "
+ + edge.getInVertex() + "]";
}
- public static <T extends Element> void setProperty(T element, String propertyName, Object value) {
+ public static <T extends AtlasElement> void setProperty(T element, String propertyName, Object value) {
String elementStr = string(element);
String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
LOG.debug("Setting property {} = \"{}\" to {}", actualPropertyName, value, elementStr);
- Object existValue = element.getProperty(actualPropertyName);
+ Object existValue = element.getProperty(actualPropertyName, Object.class);
if(value == null || (value instanceof Collection && ((Collection) value).isEmpty())) {
if(existValue != null) {
LOG.info("Removing property - {} value from {}", actualPropertyName, elementStr);
@@ -361,32 +368,66 @@ public final class GraphHelper {
}
}
- public static <T extends Element, O> O getProperty(T element, String propertyName) {
+ /**
+ * Gets the value of a property that is stored in the graph as a single property value. If
+ * a multi-property such as {@link Constants#TRAIT_NAMES_PROPERTY_KEY} or {@link Constants#SUPER_TYPES_PROPERTY_KEY}
+ * is used, an exception will be thrown.
+ *
+ * @param element
+ * @param propertyName
+ * @param clazz
+ * @return
+ */
+ public static <T> T getSingleValuedProperty(AtlasElement element, String propertyName, Class<T> clazz) {
String elementStr = string(element);
String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
- LOG.debug("Reading property {} from {}", actualPropertyName, elementStr);
- return element.getProperty(actualPropertyName);
+ LOG.debug("Reading property {} from {}", actualPropertyName, elementStr);
+
+ return (T)element.getProperty(actualPropertyName, clazz);
}
-
- public static Iterable<TitanProperty> getProperties(TitanVertex vertex, String propertyName) {
+
+
+ public static Object getProperty(AtlasVertex<?,?> vertex, String propertyName) {
String elementStr = string(vertex);
String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
- LOG.debug("Reading property {} from {}", actualPropertyName, elementStr);
- return vertex.getProperties(actualPropertyName);
- }
+ LOG.debug("Reading property {} from {}", actualPropertyName, elementStr);
- private static <T extends Element> String string(T element) {
- if (element instanceof Vertex) {
- return string((Vertex) element);
- } else if (element instanceof Edge) {
- return string((Edge)element);
+ if(AtlasGraphProvider.getGraphInstance().isMultiProperty(actualPropertyName)) {
+ return vertex.getPropertyValues(actualPropertyName, String.class);
+ }
+ else {
+ return vertex.getProperty(actualPropertyName, Object.class);
+ }
+
+ }
+
+ public static Object getProperty(AtlasEdge<?,?> edge, String propertyName) {
+ String elementStr = string(edge);
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ LOG.debug("Reading property {} from {}", actualPropertyName, elementStr);
+ return edge.getProperty(actualPropertyName, Object.class);
+ }
+
+ private static <T extends AtlasElement> String string(T element) {
+ if (element instanceof AtlasVertex) {
+ return string((AtlasVertex) element);
+ } else if (element instanceof AtlasEdge) {
+ return string((AtlasEdge)element);
}
return element.toString();
}
-
- public static void addProperty(Vertex vertex, String propertyName, Object value) {
- LOG.debug("Adding property {} = \"{}\" to vertex {}", propertyName, value, string(vertex));
- ((TitanVertex)vertex).addProperty(propertyName, value);
+
+ /**
+ * Adds an additional value to a multi-property.
+ *
+ * @param vertex
+ * @param propertyName
+ * @param value
+ */
+ public static void addProperty(AtlasVertex vertex, String propertyName, Object value) {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ LOG.debug("Adding property {} = \"{}\" to vertex {}", actualPropertyName, value, string(vertex));
+ vertex.addProperty(actualPropertyName, value);
}
/**
@@ -394,26 +435,26 @@ public final class GraphHelper {
*
* @param edge
*/
- public void removeEdge(Edge edge) {
+ public void removeEdge(AtlasEdge edge) {
String edgeString = string(edge);
LOG.debug("Removing {}", edgeString);
- titanGraph.removeEdge(edge);
+ graph.removeEdge(edge);
LOG.info("Removed {}", edgeString);
}
/**
- * Remove the specified vertex from the graph.
+ * Remove the specified AtlasVertex from the graph.
*
- * @param vertex
+ * @param AtlasVertex
*/
- public void removeVertex(Vertex vertex) {
+ public void removeVertex(AtlasVertex vertex) {
String vertexString = string(vertex);
LOG.debug("Removing {}", vertexString);
- titanGraph.removeVertex(vertex);
+ graph.removeVertex(vertex);
LOG.info("Removed {}", vertexString);
}
- public Vertex getVertexForGUID(String guid) throws EntityNotFoundException {
+ public AtlasVertex getVertexForGUID(String guid) throws EntityNotFoundException {
return findVertex(Constants.GUID_PROPERTY_KEY, guid);
}
@@ -436,12 +477,12 @@ public final class GraphHelper {
return attrName;
}
- public static List<String> getTraitNames(Vertex entityVertex) {
+ public static List<String> getTraitNames(AtlasVertex<?,?> entityVertex) {
ArrayList<String> traits = new ArrayList<>();
- for (TitanProperty property : ((TitanVertex) entityVertex).getProperties(Constants.TRAIT_NAMES_PROPERTY_KEY)) {
- traits.add((String) property.getValue());
+ Collection<String> propertyValues = entityVertex.getPropertyValues(Constants.TRAIT_NAMES_PROPERTY_KEY, String.class);
+ for(String value : propertyValues) {
+ traits.add(value);
}
-
return traits;
}
@@ -454,26 +495,26 @@ public final class GraphHelper {
return GraphHelper.EDGE_LABEL_PREFIX + getQualifiedFieldName(dataType, aInfo.name);
}
- public static Id getIdFromVertex(String dataTypeName, Vertex vertex) {
+ public static Id getIdFromVertex(String dataTypeName, AtlasVertex vertex) {
return new Id(getIdFromVertex(vertex),
- vertex.<Integer>getProperty(Constants.VERSION_PROPERTY_KEY), dataTypeName, getStateAsString(vertex));
+ vertex.getProperty(Constants.VERSION_PROPERTY_KEY, Integer.class), dataTypeName, getStateAsString(vertex));
}
- public static String getIdFromVertex(Vertex vertex) {
- return vertex.<String>getProperty(Constants.GUID_PROPERTY_KEY);
+ public static String getIdFromVertex(AtlasVertex vertex) {
+ return vertex.<String>getProperty(Constants.GUID_PROPERTY_KEY, String.class);
}
- public static String getTypeName(Vertex instanceVertex) {
- return instanceVertex.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
+ public static String getTypeName(AtlasVertex instanceVertex) {
+ return instanceVertex.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY, String.class);
}
- public static Id.EntityState getState(Element element) {
+ public static Id.EntityState getState(AtlasElement element) {
String state = getStateAsString(element);
return state == null ? null : Id.EntityState.valueOf(state);
}
- public static String getStateAsString(Element element) {
- return element.getProperty(Constants.STATE_PROPERTY_KEY);
+ public static String getStateAsString(AtlasElement element) {
+ return element.getProperty(Constants.STATE_PROPERTY_KEY, String.class);
}
/**
@@ -485,10 +526,10 @@ public final class GraphHelper {
* @return
* @throws AtlasException
*/
- public Vertex getVertexForInstanceByUniqueAttribute(ClassType classType, IReferenceableInstance instance)
+ public AtlasVertex getVertexForInstanceByUniqueAttribute(ClassType classType, IReferenceableInstance instance)
throws AtlasException {
LOG.debug("Checking if there is an instance with the same unique attributes for instance {}", instance.toShortString());
- Vertex result = null;
+ AtlasVertex result = null;
for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) {
if (attributeInfo.isUnique) {
String propertyKey = getQualifiedFieldName(classType, attributeInfo.name);
@@ -507,14 +548,14 @@ public final class GraphHelper {
}
/**
- * Guid and Vertex combo
+ * Guid and AtlasVertex combo
*/
public static class VertexInfo {
private String guid;
- private Vertex vertex;
+ private AtlasVertex vertex;
private String typeName;
- public VertexInfo(String guid, Vertex vertex, String typeName) {
+ public VertexInfo(String guid, AtlasVertex vertex, String typeName) {
this.guid = guid;
this.vertex = vertex;
this.typeName = typeName;
@@ -523,7 +564,7 @@ public final class GraphHelper {
public String getGuid() {
return guid;
}
- public Vertex getVertex() {
+ public AtlasVertex getVertex() {
return vertex;
}
public String getTypeName() {
@@ -560,19 +601,19 @@ public final class GraphHelper {
}
/**
- * Get the GUIDs and vertices for all composite entities owned/contained by the specified root entity vertex.
+ * Get the GUIDs and vertices for all composite entities owned/contained by the specified root entity AtlasVertex.
* The graph is traversed from the root entity through to the leaf nodes of the containment graph.
*
* @param entityVertex the root entity vertex
* @return set of VertexInfo for all composite entities
* @throws AtlasException
*/
- public Set<VertexInfo> getCompositeVertices(Vertex entityVertex) throws AtlasException {
+ public Set<VertexInfo> getCompositeVertices(AtlasVertex entityVertex) throws AtlasException {
Set<VertexInfo> result = new HashSet<>();
- Stack<Vertex> vertices = new Stack<>();
+ Stack<AtlasVertex> vertices = new Stack<>();
vertices.push(entityVertex);
while (vertices.size() > 0) {
- Vertex vertex = vertices.pop();
+ AtlasVertex vertex = vertices.pop();
String typeName = GraphHelper.getTypeName(vertex);
String guid = GraphHelper.getIdFromVertex(vertex);
Id.EntityState state = GraphHelper.getState(vertex);
@@ -589,9 +630,9 @@ public final class GraphHelper {
String edgeLabel = GraphHelper.getEdgeLabel(classType, attributeInfo);
switch (attributeInfo.dataType().getTypeCategory()) {
case CLASS:
- Edge edge = getEdgeForLabel(vertex, edgeLabel);
+ AtlasEdge edge = getEdgeForLabel(vertex, edgeLabel);
if (edge != null && GraphHelper.getState(edge) == Id.EntityState.ACTIVE) {
- Vertex compositeVertex = edge.getVertex(Direction.IN);
+ AtlasVertex compositeVertex = edge.getInVertex();
vertices.push(compositeVertex);
}
break;
@@ -601,12 +642,12 @@ public final class GraphHelper {
if (elementTypeCategory != TypeCategory.CLASS) {
continue;
}
- Iterator<Edge> edges = getOutGoingEdgesByLabel(vertex, edgeLabel);
+ Iterator<AtlasEdge> edges = getOutGoingEdgesByLabel(vertex, edgeLabel);
if (edges != null) {
while (edges.hasNext()) {
edge = edges.next();
if (edge != null && GraphHelper.getState(edge) == Id.EntityState.ACTIVE) {
- Vertex compositeVertex = edge.getVertex(Direction.IN);
+ AtlasVertex compositeVertex = edge.getInVertex();
vertices.push(compositeVertex);
}
}
@@ -619,13 +660,13 @@ public final class GraphHelper {
continue;
}
String propertyName = GraphHelper.getQualifiedFieldName(classType, attributeInfo.name);
- List<String> keys = vertex.getProperty(propertyName);
+ List<String> keys = vertex.getProperty(propertyName, List.class);
if (keys != null) {
for (String key : keys) {
String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, key);
edge = getEdgeForLabel(vertex, mapEdgeLabel);
if (edge != null && GraphHelper.getState(edge) == Id.EntityState.ACTIVE) {
- Vertex compositeVertex = edge.getVertex(Direction.IN);
+ AtlasVertex compositeVertex = edge.getInVertex();
vertices.push(compositeVertex);
}
}
@@ -639,15 +680,99 @@ public final class GraphHelper {
return result;
}
- public static void dumpToLog(final Graph graph) {
+ public static ITypedReferenceableInstance[] deserializeClassInstances(TypeSystem typeSystem, String entityInstanceDefinition)
+ throws AtlasException {
+ try {
+ JSONArray referableInstances = new JSONArray(entityInstanceDefinition);
+ ITypedReferenceableInstance[] instances = new ITypedReferenceableInstance[referableInstances.length()];
+ for (int index = 0; index < referableInstances.length(); index++) {
+ Referenceable entityInstance =
+ InstanceSerialization.fromJsonReferenceable(referableInstances.getString(index), true);
+ ITypedReferenceableInstance typedInstrance = getTypedReferenceableInstance(typeSystem, entityInstance);
+ instances[index] = typedInstrance;
+ }
+ return instances;
+ } catch(ValueConversionException | TypeNotFoundException e) {
+ throw e;
+ } catch (Exception e) { // exception from deserializer
+ LOG.error("Unable to deserialize json={}", entityInstanceDefinition, e);
+ throw new IllegalArgumentException("Unable to deserialize json", e);
+ }
+ }
+
+ public static ITypedReferenceableInstance getTypedReferenceableInstance(TypeSystem typeSystem, Referenceable entityInstance)
+ throws AtlasException {
+ final String entityTypeName = ParamChecker.notEmpty(entityInstance.getTypeName(), "Entity type cannot be null");
+
+ ClassType entityType = typeSystem.getDataType(ClassType.class, 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);
+ return typedInstrance;
+ }
+
+ public static boolean isReference(IDataType type) {
+
+ return type.getTypeCategory() == DataTypes.TypeCategory.STRUCT ||
+ type.getTypeCategory() == DataTypes.TypeCategory.CLASS;
+
+ }
+
+ public static void setArrayElementsProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName, List<Object> values) {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ if(GraphHelper.isReference(elementType)) {
+ setListPropertyFromElementIds(instanceVertex, actualPropertyName, (List)values);
+ }
+ else {
+ setProperty(instanceVertex, actualPropertyName, values);
+ }
+ }
+
+ public static void setMapValueProperty(IDataType elementType, AtlasVertex instanceVertex, String propertyName, Object value) {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ if(GraphHelper.isReference(elementType)) {
+ instanceVertex.setPropertyFromElementId(actualPropertyName, (AtlasEdge)value);
+ }
+ else {
+ instanceVertex.setProperty(actualPropertyName, value);
+ }
+ }
+
+ 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, String.class).toString();
+ }
+ }
+
+ 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);
- for (Vertex vertex : graph.getVertices()) {
+ for (AtlasVertex vertex : graph.getVertices()) {
LOG.debug(vertexString(vertex));
}
LOG.debug("Edges of {}", graph);
- for (Edge edge : graph.getEdges()) {
+ for (AtlasEdge edge : graph.getEdges()) {
LOG.debug(edgeString(edge));
}
LOG.debug("*******************Graph Dump****************************");
@@ -657,41 +782,41 @@ public final class GraphHelper {
return String.format("entity[type=%s guid=%]", instance.getTypeName(), instance.getId()._getId());
}
- public static String string(Vertex vertex) {
+ public static String string(AtlasVertex<?,?> vertex) {
if(vertex == null) {
return "vertex[null]";
} else {
if (LOG.isDebugEnabled()) {
return getVertexDetails(vertex);
} else {
- return String.format("vertex[id=%s]", vertex.getId().toString());
+ return String.format("vertex[id=%s]", vertex.getIdForDisplay());
}
}
}
- public static String getVertexDetails(Vertex vertex) {
+ public static String getVertexDetails(AtlasVertex<?,?> vertex) {
- return String.format("vertex[id=%s type=%s guid=%s]", vertex.getId().toString(), getTypeName(vertex),
+ return String.format("vertex[id=%s type=%s guid=%s]", vertex.getIdForDisplay(), getTypeName(vertex),
getIdFromVertex(vertex));
}
- public static String string(Edge edge) {
+ public static String string(AtlasEdge<?,?> edge) {
if(edge == null) {
return "edge[null]";
} else {
if (LOG.isDebugEnabled()) {
return getEdgeDetails(edge);
} else {
- return String.format("edge[id=%s]", edge.getId().toString());
+ return String.format("edge[id=%s]", edge.getIdForDisplay());
}
}
}
- public static String getEdgeDetails(Edge edge) {
+ public static String getEdgeDetails(AtlasEdge<?,?> edge) {
- return String.format("edge[id=%s label=%s from %s -> to %s]", edge.getId().toString(), edge.getLabel(),
- string(edge.getVertex(Direction.OUT)), string(edge.getVertex(Direction.IN)));
+ return String.format("edge[id=%s label=%s from %s -> to %s]", edge.getIdForDisplay(), edge.getLabel(),
+ string(edge.getOutVertex()), string(edge.getInVertex()));
}
@VisibleForTesting
@@ -740,5 +865,35 @@ public final class GraphHelper {
}
return null;
}
+
+ public static boolean elementExists(AtlasElement v) {
+ return v != null && v.exists();
+ }
+
+ public static void setListPropertyFromElementIds(AtlasVertex<?, ?> instanceVertex, String propertyName,
+ List<AtlasElement> elements) {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ instanceVertex.setPropertyFromElementsIds(actualPropertyName, elements);
+
+ }
+
+ public static void setPropertyFromElementId(AtlasVertex<?, ?> instanceVertex, String propertyName,
+ AtlasElement value) {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ instanceVertex.setPropertyFromElementId(actualPropertyName, value);
+
+ }
+
+ public static void setListProperty(AtlasVertex instanceVertex, String propertyName, ArrayList<String> value) throws AtlasException {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ instanceVertex.setListProperty(actualPropertyName, value);
+ }
+
+ public static List<String> getListProperty(AtlasVertex instanceVertex, String propertyName) throws AtlasException {
+ String actualPropertyName = GraphHelper.encodePropertyKey(propertyName);
+ return instanceVertex.getListProperty(actualPropertyName);
+ }
+
+
}
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/repository/src/main/java/org/apache/atlas/repository/graph/GraphProvider.java
----------------------------------------------------------------------
diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphProvider.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphProvider.java
deleted file mode 100755
index f89bdf5..0000000
--- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphProvider.java
+++ /dev/null
@@ -1,27 +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.inject.throwingproviders.CheckedProvider;
-import com.tinkerpop.blueprints.Graph;
-
-public interface GraphProvider<T extends Graph> extends CheckedProvider<T> {
- @Override
- T get();
-}
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/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
index 6141927..51d5928 100644
--- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java
+++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphSchemaInitializer.java
@@ -18,7 +18,7 @@
package org.apache.atlas.repository.graph;
-import com.thinkaurelius.titan.core.TitanGraph;
+import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.setup.SetupException;
import org.apache.atlas.setup.SetupStep;
@@ -41,7 +41,7 @@ public class GraphSchemaInitializer implements SetupStep {
LOG.info("Initializing graph schema backend.");
try {
// The implementation of this method internally creates the schema.
- TitanGraphProvider.getGraphInstance();
+ 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);
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/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
index 5c7cb2e..ceb6011 100644
--- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java
+++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphToTypedInstanceMapper.java
@@ -17,13 +17,22 @@
*/
package org.apache.atlas.repository.graph;
-import com.google.inject.Singleton;
-import com.thinkaurelius.titan.core.TitanGraph;
-import com.tinkerpop.blueprints.Direction;
-import com.tinkerpop.blueprints.Edge;
-import com.tinkerpop.blueprints.Vertex;
+import static org.apache.atlas.repository.graph.GraphHelper.string;
+
+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 org.apache.atlas.AtlasException;
import org.apache.atlas.repository.Constants;
+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;
@@ -39,17 +48,7 @@ import org.apache.atlas.typesystem.types.TypeSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import static org.apache.atlas.repository.graph.GraphHelper.getIdFromVertex;
-import static org.apache.atlas.repository.graph.GraphHelper.string;
+import com.google.inject.Singleton;
@Singleton
public final class GraphToTypedInstanceMapper {
@@ -58,17 +57,17 @@ public final class GraphToTypedInstanceMapper {
private static TypeSystem typeSystem = TypeSystem.getInstance();
private static final GraphHelper graphHelper = GraphHelper.getInstance();
- private TitanGraph titanGraph;
+ private AtlasGraph graph;
- public GraphToTypedInstanceMapper(TitanGraph titanGraph) {
- this.titanGraph = titanGraph;
+ public GraphToTypedInstanceMapper(AtlasGraph graph) {
+ this.graph = graph;
}
- public ITypedReferenceableInstance mapGraphToTypedInstance(String guid, Vertex instanceVertex)
+ public ITypedReferenceableInstance mapGraphToTypedInstance(String guid, AtlasVertex instanceVertex)
throws AtlasException {
LOG.debug("Mapping graph root vertex {} to typed instance for guid {}", instanceVertex, guid);
- String typeName = GraphHelper.getProperty(instanceVertex, Constants.ENTITY_TYPE_PROPERTY_KEY);
+ String typeName = GraphHelper.getSingleValuedProperty(instanceVertex, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class);
List<String> traits = GraphHelper.getTraitNames(instanceVertex);
String state = GraphHelper.getStateAsString(instanceVertex);
@@ -86,7 +85,7 @@ public final class GraphToTypedInstanceMapper {
return typedInstance;
}
- private void mapVertexToInstanceTraits(Vertex instanceVertex, ITypedReferenceableInstance typedInstance,
+ private void mapVertexToInstanceTraits(AtlasVertex instanceVertex, ITypedReferenceableInstance typedInstance,
List<String> traits) throws AtlasException {
for (String traitName : traits) {
LOG.debug("mapping trait {} to instance", traitName);
@@ -95,7 +94,7 @@ public final class GraphToTypedInstanceMapper {
}
}
- public void mapVertexToInstance(Vertex instanceVertex, ITypedInstance typedInstance,
+ public void mapVertexToInstance(AtlasVertex instanceVertex, ITypedInstance typedInstance,
Map<String, AttributeInfo> fields) throws AtlasException {
LOG.debug("Mapping vertex {} to instance {} for fields", instanceVertex, typedInstance.getTypeName(),
@@ -105,7 +104,7 @@ public final class GraphToTypedInstanceMapper {
}
}
- private void mapVertexToAttribute(Vertex instanceVertex, ITypedInstance typedInstance,
+ private void mapVertexToAttribute(AtlasVertex instanceVertex, ITypedInstance typedInstance,
AttributeInfo attributeInfo) throws AtlasException {
LOG.debug("Mapping attributeInfo {}", attributeInfo.name);
final IDataType dataType = attributeInfo.dataType();
@@ -145,8 +144,9 @@ public final class GraphToTypedInstanceMapper {
break;
case CLASS:
+ AtlasEdge nullEdge = null;
Object idOrInstance = mapVertexToClassReference(instanceVertex, attributeInfo, relationshipLabel,
- attributeInfo.dataType(), null);
+ attributeInfo.dataType(), nullEdge);
if (idOrInstance != null) {
typedInstance.set(attributeInfo.name, idOrInstance);
}
@@ -157,27 +157,30 @@ public final class GraphToTypedInstanceMapper {
}
}
- private Object mapVertexToClassReference(Vertex instanceVertex, AttributeInfo attributeInfo,
- String relationshipLabel, IDataType dataType, String edgeId) throws AtlasException {
+ private Object mapVertexToClassReference(AtlasVertex instanceVertex, AttributeInfo attributeInfo,
+ String relationshipLabel, IDataType dataType, AtlasEdge optionalEdge) throws AtlasException {
LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel);
- Edge edge;
- if (edgeId == null) {
+ AtlasEdge edge = null;
+ if (optionalEdge == null) {
edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel);
} else {
- edge = graphHelper.getEdgeByEdgeId(instanceVertex, relationshipLabel, edgeId);
+ edge = optionalEdge;
}
- if (edge != null) {
- final Vertex referenceVertex = edge.getVertex(Direction.IN);
- final String guid = GraphHelper.getIdFromVertex(referenceVertex);
+ if (GraphHelper.elementExists(edge)) {
+ final AtlasVertex referenceVertex = edge.getInVertex();
+ final String guid = GraphHelper.getSingleValuedProperty(referenceVertex, Constants.GUID_PROPERTY_KEY, String.class);
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");
return mapGraphToTypedInstance(guid, referenceVertex);
} else {
- Id referenceId = getIdFromVertex(dataType.getName(), referenceVertex);
+ String state = GraphHelper.getStateAsString(referenceVertex);
+ Id referenceId =
+ new Id(guid, GraphHelper.getSingleValuedProperty(referenceVertex, Constants.VERSION_PROPERTY_KEY, Integer.class),
+ dataType.getName(), state);
LOG.debug("Found non-composite, adding id {} ", referenceId);
return referenceId;
}
@@ -187,21 +190,24 @@ public final class GraphToTypedInstanceMapper {
}
@SuppressWarnings("unchecked")
- private void mapVertexToArrayInstance(Vertex instanceVertex, ITypedInstance typedInstance,
- AttributeInfo attributeInfo, String propertyName) throws AtlasException {
+ private void mapVertexToArrayInstance(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance,
+ AttributeInfo attributeInfo, String propertyName) throws AtlasException {
LOG.debug("mapping vertex {} to array {}", instanceVertex, attributeInfo.name);
- List list = GraphHelper.getProperty(instanceVertex, propertyName);
+
+ 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;
}
- DataTypes.ArrayType arrayType = (DataTypes.ArrayType) attributeInfo.dataType();
- final IDataType elementType = arrayType.getElemType();
String edgeLabel = GraphHelper.EDGE_LABEL_PREFIX + propertyName;
ArrayList values = new ArrayList();
for (int index = 0; index < list.size(); index++) {
values.add(mapVertexToCollectionEntry(instanceVertex, attributeInfo, elementType, list.get(index),
- edgeLabel));
+ edgeLabel));
}
if (values.size() > 0) {
@@ -209,7 +215,7 @@ public final class GraphToTypedInstanceMapper {
}
}
- private Object mapVertexToCollectionEntry(Vertex instanceVertex, AttributeInfo attributeInfo,
+ private Object mapVertexToCollectionEntry(AtlasVertex instanceVertex, AttributeInfo attributeInfo,
IDataType elementType, Object value, String edgeLabel) throws AtlasException {
switch (elementType.getTypeCategory()) {
case PRIMITIVE:
@@ -223,10 +229,10 @@ public final class GraphToTypedInstanceMapper {
break;
case STRUCT:
- return mapVertexToStructInstance(instanceVertex, (StructType) elementType, edgeLabel, (String) value);
+ return mapVertexToStructInstance(instanceVertex, (StructType) elementType, edgeLabel, (AtlasEdge) value);
case CLASS:
- return mapVertexToClassReference(instanceVertex, attributeInfo, edgeLabel, elementType, (String) value);
+ return mapVertexToClassReference(instanceVertex, attributeInfo, edgeLabel, elementType, (AtlasEdge) value);
default:
break;
@@ -236,21 +242,21 @@ public final class GraphToTypedInstanceMapper {
}
@SuppressWarnings("unchecked")
- private void mapVertexToMapInstance(Vertex instanceVertex, ITypedInstance typedInstance,
- AttributeInfo attributeInfo, final String propertyName) throws AtlasException {
+ private void mapVertexToMapInstance(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance,
+ AttributeInfo attributeInfo, final String propertyName) throws AtlasException {
LOG.debug("mapping vertex {} to array {}", instanceVertex, attributeInfo.name);
- List<String> keys = GraphHelper.getProperty(instanceVertex, propertyName);
+ 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 values = new HashMap();
+ 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.getProperty(instanceVertex, keyPropertyName);
+ final Object keyValue = GraphHelper.getMapValueProperty(valueType, instanceVertex, keyPropertyName);
Object mapValue = mapVertexToCollectionEntry(instanceVertex, attributeInfo, valueType, keyValue, edgeLabel);
if (mapValue != null) {
values.put(key, mapValue);
@@ -262,21 +268,21 @@ public final class GraphToTypedInstanceMapper {
}
}
- private ITypedStruct mapVertexToStructInstance(Vertex instanceVertex, StructType structType,
- String relationshipLabel, String edgeId) throws AtlasException {
+ private ITypedStruct mapVertexToStructInstance(AtlasVertex instanceVertex, StructType structType,
+ String relationshipLabel, AtlasEdge optionalEdge) throws AtlasException {
LOG.debug("mapping {} to struct {}", string(instanceVertex), relationshipLabel);
ITypedStruct structInstance = null;
- Edge edge;
- if (edgeId == null) {
+ AtlasEdge edge;
+ if (optionalEdge == null) {
edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel);
} else {
- edge = graphHelper.getEdgeByEdgeId(instanceVertex, relationshipLabel, edgeId);
+ edge = optionalEdge;
}
- if (edge != null) {
+ if (GraphHelper.elementExists(edge)) {
structInstance = structType.createInstance();
- Vertex structInstanceVertex = edge.getVertex(Direction.IN);
+ AtlasVertex structInstanceVertex = edge.getInVertex();
LOG.debug("Found struct instance {}, mapping to instance {} ", string(structInstanceVertex),
structInstance.getTypeName());
mapVertexToInstance(structInstanceVertex, structInstance, structType.fieldMapping().fields);
@@ -285,69 +291,68 @@ public final class GraphToTypedInstanceMapper {
return structInstance;
}
- private void mapVertexToTraitInstance(Vertex instanceVertex, ITypedReferenceableInstance typedInstance,
+ 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(Vertex instanceVertex, String typedInstanceTypeName, String traitName,
- TraitType traitType, ITypedStruct traitInstance) throws AtlasException {
+ private void mapVertexToTraitInstance(AtlasVertex<?,?> instanceVertex, String typedInstanceTypeName, String traitName,
+ TraitType traitType, ITypedStruct traitInstance) throws AtlasException {
String relationshipLabel = GraphHelper.getTraitLabel(typedInstanceTypeName, traitName);
LOG.debug("Finding edge for {} -> label {} ", instanceVertex, relationshipLabel);
- Iterator<Edge> edgeIterator = graphHelper.getOutGoingEdgesByLabel(instanceVertex, relationshipLabel);
- while (edgeIterator.hasNext()) {
- Edge edge = edgeIterator.next();
- final Vertex traitInstanceVertex = edge.getVertex(Direction.IN);
+ for (AtlasEdge<?,?> edge : instanceVertex.getEdges(AtlasEdgeDirection.OUT, relationshipLabel)) {
+ final AtlasVertex<?,?> traitInstanceVertex = edge.getInVertex();
if (traitInstanceVertex != null) {
LOG.debug("Found trait instance vertex {}, mapping to instance {} ", traitInstanceVertex,
- traitInstance.getTypeName());
+ traitInstance.getTypeName());
mapVertexToInstance(traitInstanceVertex, traitInstance, traitType.fieldMapping().fields);
break;
}
}
}
- private void mapVertexToPrimitive(Vertex instanceVertex, ITypedInstance typedInstance,
- AttributeInfo attributeInfo) throws AtlasException {
+ private void mapVertexToPrimitive(AtlasVertex<?,?> instanceVertex, ITypedInstance typedInstance,
+ AttributeInfo attributeInfo) throws AtlasException {
LOG.debug("Adding primitive {} from vertex {}", attributeInfo, instanceVertex);
final String vertexPropertyName = GraphHelper.getQualifiedFieldName(typedInstance, attributeInfo);
- Object propertyValue = GraphHelper.getProperty(instanceVertex, vertexPropertyName);
- if (propertyValue == null) {
+ if (GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Object.class) == null) {
return;
}
if (attributeInfo.dataType() == DataTypes.STRING_TYPE) {
- typedInstance.setString(attributeInfo.name, (String) propertyValue);
+ typedInstance.setString(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, String.class));
} else if (attributeInfo.dataType() == DataTypes.SHORT_TYPE) {
- typedInstance.setShort(attributeInfo.name, (Short) propertyValue);
+ typedInstance.setShort(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Short.class));
} else if (attributeInfo.dataType() == DataTypes.INT_TYPE) {
- typedInstance.setInt(attributeInfo.name, (Integer) propertyValue);
+ typedInstance.setInt(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Integer.class));
} else if (attributeInfo.dataType() == DataTypes.BIGINTEGER_TYPE) {
- typedInstance.setBigInt(attributeInfo.name, (BigInteger) propertyValue);
+ typedInstance.setBigInt(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, BigInteger.class));
} else if (attributeInfo.dataType() == DataTypes.BOOLEAN_TYPE) {
- typedInstance.setBoolean(attributeInfo.name, (Boolean) propertyValue);
+ typedInstance.setBoolean(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Boolean.class));
} else if (attributeInfo.dataType() == DataTypes.BYTE_TYPE) {
- typedInstance.setByte(attributeInfo.name, (Byte) propertyValue);
+ typedInstance.setByte(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Byte.class));
} else if (attributeInfo.dataType() == DataTypes.LONG_TYPE) {
- typedInstance.setLong(attributeInfo.name, (Long) propertyValue);
+ typedInstance.setLong(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Long.class));
} else if (attributeInfo.dataType() == DataTypes.FLOAT_TYPE) {
- typedInstance.setFloat(attributeInfo.name, (Float) propertyValue);
+ typedInstance.setFloat(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Float.class));
} else if (attributeInfo.dataType() == DataTypes.DOUBLE_TYPE) {
- typedInstance.setDouble(attributeInfo.name, (Double) propertyValue);
+ typedInstance.setDouble(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, Double.class));
} else if (attributeInfo.dataType() == DataTypes.BIGDECIMAL_TYPE) {
- typedInstance.setBigDecimal(attributeInfo.name, (BigDecimal) propertyValue);
+ typedInstance
+ .setBigDecimal(attributeInfo.name, GraphHelper.getSingleValuedProperty(instanceVertex, vertexPropertyName, BigDecimal.class));
} else if (attributeInfo.dataType() == DataTypes.DATE_TYPE) {
- final Long dateVal = (Long) propertyValue;
+ 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 Edge edge = titanGraph.getEdge(edgeId);
+ final AtlasEdge edge = graph.getEdge(edgeId);
if (edge != null) {
- final Vertex referredVertex = edge.getVertex(Direction.IN);
+ final AtlasVertex referredVertex = edge.getInVertex();
if (referredVertex != null) {
switch (referredType.getTypeCategory()) {
case STRUCT:
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/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
index 3636791..78cf946 100644
--- a/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java
+++ b/repository/src/main/java/org/apache/atlas/repository/graph/HardDeleteHandler.java
@@ -19,8 +19,8 @@
package org.apache.atlas.repository.graph;
import com.google.inject.Inject;
-import com.tinkerpop.blueprints.Edge;
-import com.tinkerpop.blueprints.Vertex;
+import org.apache.atlas.repository.graphdb.AtlasEdge;
+import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.AtlasException;
import org.apache.atlas.typesystem.types.TypeSystem;
@@ -32,12 +32,12 @@ public class HardDeleteHandler extends DeleteHandler {
}
@Override
- protected void _deleteVertex(Vertex instanceVertex, boolean force) {
+ protected void _deleteVertex(AtlasVertex instanceVertex, boolean force) {
graphHelper.removeVertex(instanceVertex);
}
@Override
- protected void deleteEdge(Edge edge, boolean force) throws AtlasException {
+ protected void deleteEdge(AtlasEdge edge, boolean force) throws AtlasException {
graphHelper.removeEdge(edge);
}
}