http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Database.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Database.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Database.java deleted file mode 100644 index 56b1664..0000000 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Database.java +++ /dev/null @@ -1,204 +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.graphdb.titan0; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.HashMap; -import java.util.Map; - -import org.apache.atlas.ApplicationProperties; -import org.apache.atlas.AtlasException; -import org.apache.atlas.repository.graphdb.AtlasGraph; -import org.apache.atlas.repository.graphdb.GraphDatabase; -import org.apache.commons.configuration.Configuration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.ImmutableMap; -import com.thinkaurelius.titan.core.TitanFactory; -import com.thinkaurelius.titan.core.TitanGraph; -import com.thinkaurelius.titan.core.schema.TitanManagement; -import com.thinkaurelius.titan.core.util.TitanCleanup; -import com.thinkaurelius.titan.diskstorage.StandardIndexProvider; -import com.thinkaurelius.titan.diskstorage.solr.Solr5Index; - -/** - * Titan 0.5.4 implementation of GraphDatabase. - */ -public class Titan0Database implements GraphDatabase<Titan0Vertex, Titan0Edge> { - - private static final Logger LOG = LoggerFactory.getLogger(Titan0Database.class); - - /** - * Constant for the configuration property that indicates the prefix. - */ - public static final String GRAPH_PREFIX = "atlas.graph"; - - public static final String INDEX_BACKEND_CONF = "index.search.backend"; - - public static final String INDEX_BACKEND_LUCENE = "lucene"; - - public static final String INDEX_BACKEND_ES = "elasticsearch"; - - private static volatile TitanGraph graphInstance; - - public static Configuration getConfiguration() throws AtlasException { - Configuration configProperties = ApplicationProperties.get(); - return ApplicationProperties.getSubsetConfiguration(configProperties, GRAPH_PREFIX); - } - - static { - addSolr5Index(); - } - - /** - * Titan loads index backend name to implementation using - * StandardIndexProvider.ALL_MANAGER_CLASSES But - * StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final - * ImmutableMap Only way to inject Solr5Index is to modify this field. So, - * using hacky reflection to add Sol5Index - */ - private static void addSolr5Index() { - try { - Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES"); - field.setAccessible(true); - - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - - Map<String, String> customMap = new HashMap<>(StandardIndexProvider.getAllProviderClasses()); - customMap.put("solr", Solr5Index.class.getName()); // for - // consistency - // with Titan - // 1.0.0 - customMap.put("solr5", Solr5Index.class.getName()); // for backward - // compatibility - ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap); - field.set(null, immap); - - LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public static TitanGraph getGraphInstance() { - if (graphInstance == null) { - synchronized (Titan0Database.class) { - if (graphInstance == null) { - Configuration config; - try { - config = getConfiguration(); - } catch (AtlasException e) { - throw new RuntimeException(e); - } - - graphInstance = TitanFactory.open(config); - validateIndexBackend(config); - } - } - } - return graphInstance; - } - - public static void unload() { - - synchronized (Titan0Database.class) { - if (graphInstance == null) { - return; - } - - graphInstance.commit(); - //shutdown invalidates the graph instance - graphInstance.shutdown(); - graphInstance = null; - } - } - - static void validateIndexBackend(Configuration config) { - String configuredIndexBackend = config.getString(INDEX_BACKEND_CONF); - TitanManagement managementSystem = null; - - try { - managementSystem = getGraphInstance().getManagementSystem(); - String currentIndexBackend = managementSystem.get(INDEX_BACKEND_CONF); - - if (!equals(configuredIndexBackend, currentIndexBackend)) { - throw new RuntimeException("Configured Index Backend " + configuredIndexBackend - + " differs from earlier configured Index Backend " + currentIndexBackend + ". Aborting!"); - } - - } finally { - if (managementSystem != null) { - managementSystem.commit(); - } - } - - - } - - private static boolean equals(Object o1, Object o2) { - if (o1 == null) { - return o2 == null; - } - return o1.equals(o2); - } - - @Override - public AtlasGraph<Titan0Vertex, Titan0Edge> getGraph() { - // force graph loading up front to avoid bootstrapping - // issues - getGraphInstance(); - return new Titan0Graph(); - } - - @Override - public boolean isGraphLoaded() { - return graphInstance != null; - } - - - @Override - public void initializeTestGraph() { - - //nothing to do - } - - @Override - public void removeTestGraph() { - try { - getGraphInstance().shutdown(); - } - catch(Throwable t) { - LOG.warn("Could not shutdown test TitanGraph", t); - t.printStackTrace(); - } - - try { - TitanCleanup.clear(getGraphInstance()); - } - catch(Throwable t) { - LOG.warn("Could not clear test TitanGraph", t); - t.printStackTrace(); - } - } - -}
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseManager.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseManager.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseManager.java deleted file mode 100644 index b4234d7..0000000 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseManager.java +++ /dev/null @@ -1,170 +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.graphdb.titan0; - -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.Multiplicity; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.thinkaurelius.titan.core.Cardinality; -import com.thinkaurelius.titan.core.PropertyKey; -import com.thinkaurelius.titan.core.schema.Mapping; -import com.thinkaurelius.titan.core.schema.PropertyKeyMaker; -import com.thinkaurelius.titan.core.schema.TitanGraphIndex; -import com.thinkaurelius.titan.core.schema.TitanManagement; -import com.tinkerpop.blueprints.Edge; -import com.tinkerpop.blueprints.Element; -import com.tinkerpop.blueprints.Vertex; - -/** - * Titan 0.5.4 implementation of AtlasGraphManagement. - */ -public class Titan0DatabaseManager implements AtlasGraphManagement { - - private static final Logger LOG = LoggerFactory.getLogger(Titan0DatabaseManager.class); - - private TitanManagement management; - - public Titan0DatabaseManager(TitanManagement managementSystem) { - - management = managementSystem; - } - - @Override - public void buildMixedVertexIndex(String index, String backingIndex) { - buildMixedIndex(index, Vertex.class, backingIndex); - } - - @Override - public void buildMixedEdgeIndex(String index, String backingIndex) { - buildMixedIndex(index, Edge.class, backingIndex); - } - - private void buildMixedIndex(String index, Class<? extends Element> titanClass, String backingIndex) { - - management.buildIndex(index, titanClass).buildMixedIndex(backingIndex); - } - - @Override - public void createFullTextIndex(String indexName, AtlasPropertyKey propertyKey, String backingIndex) { - - PropertyKey fullText = TitanObjectFactory.createPropertyKey(propertyKey); - - management.buildIndex(indexName, Vertex.class) - .addKey(fullText, com.thinkaurelius.titan.core.schema.Parameter.of("mapping", Mapping.TEXT)) - .buildMixedIndex(backingIndex); - } - - @Override - public boolean containsPropertyKey(String propertyKey) { - return management.containsPropertyKey(propertyKey); - } - - @Override - public void rollback() { - management.rollback(); - - } - - @Override - public void commit() { - management.commit(); - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.atlas.repository.graphdb.AtlasGraphManagement#makePropertyKey( - * java.lang.String, java.lang.Class, - * org.apache.atlas.typesystem.types.Multiplicity) - */ - @Override - public AtlasPropertyKey makePropertyKey(String propertyName, Class propertyClass, Multiplicity multiplicity) { - - PropertyKeyMaker propertyKeyBuilder = management.makePropertyKey(propertyName).dataType(propertyClass); - - if (multiplicity != null) { - Cardinality cardinality = TitanObjectFactory.createCardinality(multiplicity); - propertyKeyBuilder.cardinality(cardinality); - } - PropertyKey propertyKey = propertyKeyBuilder.make(); - return GraphDbObjectFactory.createPropertyKey(propertyKey); - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.atlas.repository.graphdb.AtlasGraphManagement#getPropertyKey( - * java.lang.String) - */ - @Override - public AtlasPropertyKey getPropertyKey(String propertyName) { - - return GraphDbObjectFactory.createPropertyKey(management.getPropertyKey(propertyName)); - } - - /* - * (non-Javadoc) - * - * @see org.apache.atlas.repository.graphdb.AtlasGraphManagement# - * createCompositeIndex(java.lang.String, - * org.apache.atlas.repository.graphdb.AtlasPropertyKey, boolean) - */ - @Override - public void createCompositeIndex(String propertyName, AtlasPropertyKey propertyKey, boolean enforceUniqueness) { - PropertyKey titanKey = TitanObjectFactory.createPropertyKey(propertyKey); - TitanManagement.IndexBuilder indexBuilder = management.buildIndex(propertyName, Vertex.class).addKey(titanKey); - if (enforceUniqueness) { - indexBuilder.unique(); - } - indexBuilder.buildCompositeIndex(); - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.atlas.repository.graphdb.AtlasGraphManagement#addIndexKey(java - * .lang.String, org.apache.atlas.repository.graphdb.AtlasPropertyKey) - */ - @Override - public void addIndexKey(String indexName, AtlasPropertyKey propertyKey) { - PropertyKey titanKey = TitanObjectFactory.createPropertyKey(propertyKey); - TitanGraphIndex vertexIndex = management.getGraphIndex(indexName); - management.addIndexKey(vertexIndex, titanKey); - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.atlas.repository.graphdb.AtlasGraphManagement#getGraphIndex( - * java.lang.String) - */ - @Override - public AtlasGraphIndex getGraphIndex(String indexName) { - TitanGraphIndex index = management.getGraphIndex(indexName); - return GraphDbObjectFactory.createGraphIndex(index); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Element.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Element.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Element.java index cacbaf8..468c2a7 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Element.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Element.java @@ -72,7 +72,20 @@ public class Titan0Element<T extends Element> implements AtlasElement { @Override public <U> U getProperty(String propertyName, Class<U> clazz) { - return (U)convert(wrappedElement.getProperty(propertyName), clazz); + + Object rawValue = wrappedElement.getProperty(propertyName); + + if (rawValue == null) { + return null; + } + if (AtlasEdge.class == clazz) { + return (U)graph.getEdge(rawValue.toString()); + } + if (AtlasVertex.class == clazz) { + return (U)graph.getVertex(rawValue.toString()); + } + return (U)rawValue; + } /** @@ -136,7 +149,7 @@ public class Titan0Element<T extends Element> implements AtlasElement { @Override public boolean equals(Object other) { - if(other == null) { + if (other == null) { return false; } if (other.getClass() != getClass()) { @@ -154,9 +167,8 @@ public class Titan0Element<T extends Element> implements AtlasElement { @Override public boolean exists() { try { - return ! ((TitanElement)wrappedElement).isRemoved(); - } - catch(IllegalStateException e) { + return !((TitanElement)wrappedElement).isRemoved(); + } catch(IllegalStateException e) { return false; } @@ -192,18 +204,6 @@ public class Titan0Element<T extends Element> implements AtlasElement { return getId().toString(); } - private <T> T convert(Object propertyValue, Class<T> clazz) { - if(propertyValue == null) { - return null; - } - if(AtlasEdge.class.isAssignableFrom(clazz)) { - return (T)graph.getEdge(propertyValue.toString()); - } - if(AtlasVertex.class.isAssignableFrom(clazz)) { - return (T)graph.getVertex(propertyValue.toString()); - } - return (T)propertyValue; - } @Override @@ -211,14 +211,13 @@ public class Titan0Element<T extends Element> implements AtlasElement { List<String> value = getListProperty(propertyName); - if(value == null) { + if (value == null) { return null; } - if(AtlasEdge.class.isAssignableFrom(elementType)) { - + if (AtlasEdge.class == elementType) { - return (List<V>)Lists.transform(value, new Function<String,AtlasEdge>(){ + return (List<V>)Lists.transform(value, new Function<String, AtlasEdge>(){ @Override public AtlasEdge apply(String input) { @@ -227,9 +226,9 @@ public class Titan0Element<T extends Element> implements AtlasElement { }); } - if(AtlasVertex.class.isAssignableFrom(elementType)) { + if (AtlasVertex.class == elementType) { - return (List<V>)Lists.transform(value, new Function<String,AtlasVertex>(){ + return (List<V>)Lists.transform(value, new Function<String, AtlasVertex>(){ @Override public AtlasVertex apply(String input) { http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java index 51531ed..b540788 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java @@ -19,8 +19,14 @@ package org.apache.atlas.repository.graphdb.titan0; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import javax.script.Bindings; @@ -42,9 +48,13 @@ import org.apache.atlas.utils.IteratorToIterableAdapter; import com.google.common.base.Function; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.thinkaurelius.titan.core.Cardinality; +import com.thinkaurelius.titan.core.PropertyKey; import com.thinkaurelius.titan.core.SchemaViolationException; import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.TitanIndexQuery; +import com.thinkaurelius.titan.core.schema.TitanManagement; import com.thinkaurelius.titan.core.util.TitanCleanup; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Element; @@ -58,8 +68,25 @@ import com.tinkerpop.pipes.util.structures.Row; */ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { - public Titan0Graph() { + private final Set<String> multiProperties; + public Titan0Graph() { + //determine multi-properties once at startup + TitanManagement mgmt = null; + try { + mgmt = Titan0GraphDatabase.getGraphInstance().getManagementSystem(); + Iterable<PropertyKey> keys = mgmt.getRelationTypes(PropertyKey.class); + multiProperties = Collections.synchronizedSet(new HashSet<String>()); + for(PropertyKey key : keys) { + if (key.getCardinality() != Cardinality.SINGLE) { + multiProperties.add(key.getName()); + } + } + } finally { + if (mgmt != null) { + mgmt.rollback(); + } + } } @Override @@ -74,6 +101,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { } } + @Override public AtlasGraphQuery<Titan0Vertex, Titan0Edge> query() { @@ -134,7 +162,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { @Override public AtlasGraphManagement getManagementSystem() { - return new Titan0DatabaseManager(getGraph().getManagementSystem()); + return new Titan0GraphManagement(this, getGraph().getManagementSystem()); } @Override @@ -170,20 +198,31 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { return wrapVertices(result); } - @Override - public Object getGremlinColumnValue(Object rowValue, String colName, int idx) { - Row<List> rV = (Row<List>) rowValue; - Object value = rV.getColumn(colName).get(idx); - return convertGremlinValue(value); - } + private Object convertGremlinValue(Object rawValue) { - @Override - public Object convertGremlinValue(Object rawValue) { if (rawValue instanceof Vertex) { return GraphDbObjectFactory.createVertex(this, (Vertex) rawValue); - } - if (rawValue instanceof Edge) { + } else if (rawValue instanceof Edge) { return GraphDbObjectFactory.createEdge(this, (Edge) rawValue); + } else if (rawValue instanceof Row) { + Row rowValue = (Row)rawValue; + Map<String, Object> result = new HashMap<>(rowValue.size()); + List<String> columnNames = rowValue.getColumnNames(); + for(int i = 0; i < rowValue.size(); i++) { + String key = columnNames.get(i); + Object value = convertGremlinValue(rowValue.get(i)); + result.put(key, value); + } + return result; + } else if (rawValue instanceof List) { + return Lists.transform((List)rawValue, new Function<Object, Object>() { + @Override + public Object apply(Object input) { + return convertGremlinValue(input); + } + }); + } else if (rawValue instanceof Collection) { + throw new UnsupportedOperationException("Unhandled collection type: " + rawValue.getClass()); } return rawValue; } @@ -194,11 +233,11 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { return GremlinVersion.TWO; } - @Override - public List<Object> convertPathQueryResultToList(Object rawValue) { + private List<Object> convertPathQueryResultToList(Object rawValue) { return (List<Object>) rawValue; } + @Override public void clear() { TitanGraph graph = getGraph(); @@ -211,7 +250,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { private TitanGraph getGraph() { // return the singleton instance of the graph in the plugin - return Titan0Database.getGraphInstance(); + return Titan0GraphDatabase.getGraphInstance(); } @Override @@ -219,15 +258,24 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { GraphSONWriter.outputGraph(getGraph(), os); } - /* - * (non-Javadoc) - * - * @see - * org.apache.atlas.repository.graphdb.AtlasGraph#executeGremlinScript(java. - * lang.String) - */ @Override - public Object executeGremlinScript(String gremlinQuery) throws ScriptException { + public Object executeGremlinScript(String query, boolean isPath) throws ScriptException { + + Object result = executeGremlinScript(query); + if (isPath) { + List<Object> path = convertPathQueryResultToList(result); + + List<Object> convertedResult = new ArrayList<>(path.size()); + for(Object o : path) { + convertedResult.add(convertGremlinValue(o)); + } + return convertedResult; + } else { + return convertGremlinValue(result); + } + } + + private Object executeGremlinScript(String gremlinQuery) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("gremlin-groovy"); @@ -298,4 +346,14 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { } }); } + + + @Override + public boolean isMultiProperty(String propertyName) { + return multiProperties.contains(propertyName); + } + + public void addMultiProperties(Set<String> names) { + multiProperties.addAll(names); + } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphDatabase.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphDatabase.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphDatabase.java new file mode 100644 index 0000000..5af90d8 --- /dev/null +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphDatabase.java @@ -0,0 +1,204 @@ +/** + * 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.graphdb.titan0; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; + +import org.apache.atlas.ApplicationProperties; +import org.apache.atlas.AtlasException; +import org.apache.atlas.repository.graphdb.AtlasGraph; +import org.apache.atlas.repository.graphdb.GraphDatabase; +import org.apache.commons.configuration.Configuration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableMap; +import com.thinkaurelius.titan.core.TitanFactory; +import com.thinkaurelius.titan.core.TitanGraph; +import com.thinkaurelius.titan.core.schema.TitanManagement; +import com.thinkaurelius.titan.core.util.TitanCleanup; +import com.thinkaurelius.titan.diskstorage.StandardIndexProvider; +import com.thinkaurelius.titan.diskstorage.solr.Solr5Index; + +/** + * Titan 0.5.4 implementation of GraphDatabase. + */ +public class Titan0GraphDatabase implements GraphDatabase<Titan0Vertex, Titan0Edge> { + + private static final Logger LOG = LoggerFactory.getLogger(Titan0GraphDatabase.class); + + /** + * Constant for the configuration property that indicates the prefix. + */ + public static final String GRAPH_PREFIX = "atlas.graph"; + + public static final String INDEX_BACKEND_CONF = "index.search.backend"; + + public static final String INDEX_BACKEND_LUCENE = "lucene"; + + public static final String INDEX_BACKEND_ES = "elasticsearch"; + + private static volatile Titan0Graph atlasGraphInstance = null; + private static volatile TitanGraph graphInstance = null; + + public static Configuration getConfiguration() throws AtlasException { + Configuration configProperties = ApplicationProperties.get(); + return ApplicationProperties.getSubsetConfiguration(configProperties, GRAPH_PREFIX); + } + + static { + addSolr5Index(); + } + + /** + * Titan loads index backend name to implementation using + * StandardIndexProvider.ALL_MANAGER_CLASSES But + * StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final + * ImmutableMap Only way to inject Solr5Index is to modify this field. So, + * using hacky reflection to add Sol5Index + */ + private static void addSolr5Index() { + try { + Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES"); + field.setAccessible(true); + + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + Map<String, String> customMap = new HashMap<>(StandardIndexProvider.getAllProviderClasses()); + customMap.put("solr", Solr5Index.class.getName()); // for + // consistency + // with Titan + // 1.0.0 + customMap.put("solr5", Solr5Index.class.getName()); // for backward + // compatibility + ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap); + field.set(null, immap); + + LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static TitanGraph getGraphInstance() { + if (graphInstance == null) { + synchronized (Titan0GraphDatabase.class) { + if (graphInstance == null) { + Configuration config; + try { + config = getConfiguration(); + } catch (AtlasException e) { + throw new RuntimeException(e); + } + + graphInstance = TitanFactory.open(config); + atlasGraphInstance = new Titan0Graph(); + validateIndexBackend(config); + } + } + } + return graphInstance; + } + + public static void unload() { + + synchronized (Titan0GraphDatabase.class) { + if (graphInstance == null) { + return; + } + + graphInstance.commit(); + //shutdown invalidates the graph instance + graphInstance.shutdown(); + graphInstance = null; + } + } + + static void validateIndexBackend(Configuration config) { + String configuredIndexBackend = config.getString(INDEX_BACKEND_CONF); + TitanManagement managementSystem = null; + + try { + managementSystem = getGraphInstance().getManagementSystem(); + String currentIndexBackend = managementSystem.get(INDEX_BACKEND_CONF); + + if (!equals(configuredIndexBackend, currentIndexBackend)) { + throw new RuntimeException("Configured Index Backend " + configuredIndexBackend + + " differs from earlier configured Index Backend " + currentIndexBackend + ". Aborting!"); + } + + } finally { + if (managementSystem != null) { + managementSystem.commit(); + } + } + + + } + + private static boolean equals(Object o1, Object o2) { + if (o1 == null) { + return o2 == null; + } + return o1.equals(o2); + } + + @Override + public AtlasGraph<Titan0Vertex, Titan0Edge> getGraph() { + // force graph loading up front to avoid bootstrapping + // issues + getGraphInstance(); + return atlasGraphInstance; + } + + @Override + public boolean isGraphLoaded() { + return graphInstance != null; + } + + + @Override + public void initializeTestGraph() { + + //nothing to do + } + + @Override + public void cleanup() { + try { + getGraphInstance().shutdown(); + } catch(Throwable t) { + LOG.warn("Could not shutdown test TitanGraph", t); + t.printStackTrace(); + } + + try { + TitanCleanup.clear(getGraphInstance()); + } catch(Throwable t) { + LOG.warn("Could not clear test TitanGraph", t); + t.printStackTrace(); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphManagement.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphManagement.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphManagement.java new file mode 100644 index 0000000..9a17d9e --- /dev/null +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0GraphManagement.java @@ -0,0 +1,167 @@ +/** + * 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.graphdb.titan0; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.atlas.repository.graphdb.AtlasCardinality; +import org.apache.atlas.repository.graphdb.AtlasGraphIndex; +import org.apache.atlas.repository.graphdb.AtlasGraphManagement; +import org.apache.atlas.repository.graphdb.AtlasPropertyKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.thinkaurelius.titan.core.Cardinality; +import com.thinkaurelius.titan.core.PropertyKey; +import com.thinkaurelius.titan.core.schema.Mapping; +import com.thinkaurelius.titan.core.schema.PropertyKeyMaker; +import com.thinkaurelius.titan.core.schema.TitanGraphIndex; +import com.thinkaurelius.titan.core.schema.TitanManagement; +import com.tinkerpop.blueprints.Edge; +import com.tinkerpop.blueprints.Element; +import com.tinkerpop.blueprints.Vertex; + +/** + * Titan 0.5.4 implementation of AtlasGraphManagement. + */ +public class Titan0GraphManagement implements AtlasGraphManagement { + + private static final Logger LOG = LoggerFactory.getLogger(Titan0GraphManagement.class); + + private final Titan0Graph graph; + private final TitanManagement management; + + private Set<String> newMultProperties = new HashSet<>(); + + public Titan0GraphManagement(Titan0Graph graph, TitanManagement managementSystem) { + + this.graph = graph; + management = managementSystem; + } + + @Override + public void createEdgeIndex(String index, String backingIndex) { + buildMixedIndex(index, Edge.class, backingIndex); + } + + private void buildMixedIndex(String index, Class<? extends Element> titanClass, String backingIndex) { + + management.buildIndex(index, titanClass).buildMixedIndex(backingIndex); + } + + @Override + public void createFullTextIndex(String indexName, AtlasPropertyKey propertyKey, String backingIndex) { + + PropertyKey fullText = TitanObjectFactory.createPropertyKey(propertyKey); + + management.buildIndex(indexName, Vertex.class) + .addKey(fullText, com.thinkaurelius.titan.core.schema.Parameter.of("mapping", Mapping.TEXT)) + .buildMixedIndex(backingIndex); + } + + @Override + public boolean containsPropertyKey(String propertyKey) { + return management.containsPropertyKey(propertyKey); + } + + @Override + public void rollback() { + management.rollback(); + + } + + @Override + public void commit() { + graph.addMultiProperties(newMultProperties); + newMultProperties.clear(); + management.commit(); + } + + @Override + public AtlasPropertyKey makePropertyKey(String propertyName, Class propertyClass, AtlasCardinality cardinality) { + + if (cardinality.isMany()) { + newMultProperties.add(propertyName); + } + + PropertyKeyMaker propertyKeyBuilder = management.makePropertyKey(propertyName).dataType(propertyClass); + + if (cardinality != null) { + Cardinality titanCardinality = TitanObjectFactory.createCardinality(cardinality); + propertyKeyBuilder.cardinality(titanCardinality); + } + PropertyKey propertyKey = propertyKeyBuilder.make(); + return GraphDbObjectFactory.createPropertyKey(propertyKey); + } + + @Override + public AtlasPropertyKey getPropertyKey(String propertyName) { + + return GraphDbObjectFactory.createPropertyKey(management.getPropertyKey(propertyName)); + } + + @Override + public void createExactMatchIndex(String propertyName, boolean enforceUniqueness, + List<AtlasPropertyKey> propertyKeys) { + + TitanManagement.IndexBuilder indexBuilder = management.buildIndex(propertyName, Vertex.class); + for(AtlasPropertyKey key : propertyKeys) { + PropertyKey titanKey = TitanObjectFactory.createPropertyKey(key); + indexBuilder.addKey(titanKey); + } + if (enforceUniqueness) { + indexBuilder.unique(); + } + indexBuilder.buildCompositeIndex(); + } + + @Override + public void createVertexIndex(String propertyName, String backingIndex, List<AtlasPropertyKey> propertyKeys) { + + TitanManagement.IndexBuilder indexBuilder = management.buildIndex(propertyName, Vertex.class); + for(AtlasPropertyKey key : propertyKeys) { + PropertyKey titanKey = TitanObjectFactory.createPropertyKey(key); + indexBuilder.addKey(titanKey); + } + indexBuilder.buildMixedIndex(backingIndex); + } + + + @Override + public void addVertexIndexKey(String indexName, AtlasPropertyKey propertyKey) { + PropertyKey titanKey = TitanObjectFactory.createPropertyKey(propertyKey); + TitanGraphIndex vertexIndex = management.getGraphIndex(indexName); + management.addIndexKey(vertexIndex, titanKey); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.atlas.repository.graphdb.AtlasGraphManagement#getGraphIndex( + * java.lang.String) + */ + @Override + public AtlasGraphIndex getGraphIndex(String indexName) { + TitanGraphIndex index = management.getGraphIndex(indexName); + return GraphDbObjectFactory.createGraphIndex(index); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0IndexQuery.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0IndexQuery.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0IndexQuery.java index 18f0be5..1ed1734 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0IndexQuery.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0IndexQuery.java @@ -59,7 +59,7 @@ public class Titan0IndexQuery implements AtlasIndexQuery<Titan0Vertex, Titan0Edg private final class ResultImpl implements AtlasIndexQuery.Result<Titan0Vertex, Titan0Edge> { private TitanIndexQuery.Result<Vertex> wrappedResult; - public ResultImpl(TitanIndexQuery.Result<Vertex> source) { + ResultImpl(TitanIndexQuery.Result<Vertex> source) { wrappedResult = source; } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0PropertyKey.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0PropertyKey.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0PropertyKey.java index 1f9f6ef..d6707bd 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0PropertyKey.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0PropertyKey.java @@ -18,6 +18,7 @@ package org.apache.atlas.repository.graphdb.titan0; +import org.apache.atlas.repository.graphdb.AtlasCardinality; import org.apache.atlas.repository.graphdb.AtlasPropertyKey; import com.thinkaurelius.titan.core.PropertyKey; @@ -51,6 +52,11 @@ public class Titan0PropertyKey implements AtlasPropertyKey { } @Override + public AtlasCardinality getCardinality() { + return GraphDbObjectFactory.createCardinality(wrappedPropertyKey.getCardinality()); + } + + @Override public boolean equals(Object other) { if (!(other instanceof Titan0PropertyKey)) { return false; @@ -66,4 +72,11 @@ public class Titan0PropertyKey implements AtlasPropertyKey { return result; } + @Override + public String toString() { + return wrappedPropertyKey.getName(); + } + + + } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Vertex.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Vertex.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Vertex.java index b26ff04..9ca0441 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Vertex.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Vertex.java @@ -22,7 +22,6 @@ import java.util.Collection; import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; -import org.apache.atlas.repository.graphdb.AtlasGraphManagement; import org.apache.atlas.repository.graphdb.AtlasSchemaViolationException; import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertexQuery; @@ -62,7 +61,7 @@ public class Titan0Vertex extends Titan0Element<Vertex> implements AtlasVertex<T @Override public <T> T getProperty(String propertyName, Class<T> clazz) { - if (AtlasGraphManagement.MULTIPLICITY_MANY_PROPERTY_KEYS.contains(propertyName)) { + if (graph.isMultiProperty(propertyName)) { // throw exception in this case to be consistent with Titan 1.0.0 // behavior. throw new IllegalStateException(); @@ -78,7 +77,7 @@ public class Titan0Vertex extends Titan0Element<Vertex> implements AtlasVertex<T // For consistency with Titan 1.0.0, treat sets of multiplicity many // properties as adds. Handle this here since this is an uncommon // occurrence. - if (AtlasGraphManagement.MULTIPLICITY_MANY_PROPERTY_KEYS.contains(propertyName)) { + if (graph.isMultiProperty(propertyName)) { addProperty(propertyName, value); } else { throw e; http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/TitanObjectFactory.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/TitanObjectFactory.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/TitanObjectFactory.java index ab0e798..8dff1e4 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/TitanObjectFactory.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/TitanObjectFactory.java @@ -17,9 +17,9 @@ */ package org.apache.atlas.repository.graphdb.titan0; +import org.apache.atlas.repository.graphdb.AtlasCardinality; import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; import org.apache.atlas.repository.graphdb.AtlasPropertyKey; -import org.apache.atlas.typesystem.types.Multiplicity; import com.thinkaurelius.titan.core.Cardinality; import com.thinkaurelius.titan.core.PropertyKey; @@ -63,17 +63,18 @@ public final class TitanObjectFactory { * @param multiplicity * @return */ - public static Cardinality createCardinality(Multiplicity multiplicity) { + public static Cardinality createCardinality(AtlasCardinality cardinality) { + switch(cardinality) { - if (multiplicity == Multiplicity.OPTIONAL || multiplicity == Multiplicity.REQUIRED) { + case SINGLE: return Cardinality.SINGLE; - } else if (multiplicity == Multiplicity.COLLECTION) { + case LIST: return Cardinality.LIST; - } else if (multiplicity == Multiplicity.SET) { + case SET: return Cardinality.SET; + default: + throw new IllegalStateException("Unrecognized cardinality: " + cardinality); } - // default to LIST as this is the most forgiving - return Cardinality.LIST; } public static PropertyKey createPropertyKey(AtlasPropertyKey key) { http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/NativeTitan0GraphQuery.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/NativeTitan0GraphQuery.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/NativeTitan0GraphQuery.java index cfd9905..9f9c8ae 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/NativeTitan0GraphQuery.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/NativeTitan0GraphQuery.java @@ -22,7 +22,7 @@ import java.util.Collection; import org.apache.atlas.repository.graphdb.AtlasGraphQuery.ComparisionOperator; import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.titan.query.NativeTitanGraphQuery; -import org.apache.atlas.repository.graphdb.titan0.Titan0Database; +import org.apache.atlas.repository.graphdb.titan0.Titan0GraphDatabase; import org.apache.atlas.repository.graphdb.titan0.Titan0Edge; import org.apache.atlas.repository.graphdb.titan0.Titan0Graph; import org.apache.atlas.repository.graphdb.titan0.Titan0Vertex; @@ -34,30 +34,30 @@ import com.tinkerpop.blueprints.Compare; /** * Titan 0.5.4 implementation of NativeTitanGraphQuery. - * + * * @author jeff * */ -public class NativeTitan0GraphQuery implements NativeTitanGraphQuery<Titan0Vertex,Titan0Edge> { +public class NativeTitan0GraphQuery implements NativeTitanGraphQuery<Titan0Vertex, Titan0Edge> { - private Titan0Graph graph_; - private TitanGraphQuery<?> query_; + private Titan0Graph graph; + private TitanGraphQuery<?> query; public NativeTitan0GraphQuery(Titan0Graph graph) { - query_ = Titan0Database.getGraphInstance().query(); - graph_ = graph; + query = Titan0GraphDatabase.getGraphInstance().query(); + this.graph = graph; } @Override - public Iterable<AtlasVertex<Titan0Vertex,Titan0Edge>> vertices() { - Iterable it = query_.vertices(); - return graph_.wrapVertices(it); + public Iterable<AtlasVertex<Titan0Vertex, Titan0Edge>> vertices() { + Iterable it = query.vertices(); + return graph.wrapVertices(it); } @Override public void in(String propertyName, Collection<? extends Object> values) { - query_.has(propertyName, Contain.IN, values); + query.has(propertyName, Contain.IN, values); } @@ -66,7 +66,7 @@ public class NativeTitan0GraphQuery implements NativeTitanGraphQuery<Titan0Verte Compare c = getGremlinPredicate(op); TitanPredicate pred = TitanPredicate.Converter.convert(c); - query_.has(propertyName, pred, value); + query.has(propertyName, pred, value); } private Compare getGremlinPredicate(ComparisionOperator op) { http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/Titan0GraphQuery.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/Titan0GraphQuery.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/Titan0GraphQuery.java index 6c12ac2..1b85ada 100644 --- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/Titan0GraphQuery.java +++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/query/Titan0GraphQuery.java @@ -28,7 +28,8 @@ import org.apache.atlas.repository.graphdb.titan0.Titan0Vertex; /** * Titan 0.5.4 implementation of AtlasGraphQuery. */ -public class Titan0GraphQuery extends TitanGraphQuery<Titan0Vertex,Titan0Edge> implements NativeTitanQueryFactory<Titan0Vertex,Titan0Edge> { +public class Titan0GraphQuery extends TitanGraphQuery<Titan0Vertex, Titan0Edge> + implements NativeTitanQueryFactory<Titan0Vertex, Titan0Edge> { public Titan0GraphQuery(Titan0Graph graph, boolean isChildQuery) { super(graph, isChildQuery); @@ -40,17 +41,17 @@ public class Titan0GraphQuery extends TitanGraphQuery<Titan0Vertex,Titan0Edge> i @Override public AtlasGraphQuery<Titan0Vertex, Titan0Edge> createChildQuery() { - return new Titan0GraphQuery((Titan0Graph)graph_, true); + return new Titan0GraphQuery((Titan0Graph)graph, true); } @Override - protected NativeTitanQueryFactory<Titan0Vertex,Titan0Edge> getQueryFactory() { + protected NativeTitanQueryFactory<Titan0Vertex, Titan0Edge> getQueryFactory() { return this; } @Override public NativeTitanGraphQuery<Titan0Vertex, Titan0Edge> createNativeTitanQuery() { - return new NativeTitan0GraphQuery((Titan0Graph)graph_); + return new NativeTitan0GraphQuery((Titan0Graph)graph); } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/AbstractGraphDatabaseTest.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/AbstractGraphDatabaseTest.java b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/AbstractGraphDatabaseTest.java index 35735e3..2dca50e 100644 --- a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/AbstractGraphDatabaseTest.java +++ b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/AbstractGraphDatabaseTest.java @@ -19,14 +19,15 @@ package org.apache.atlas.repository.graphdb.titan0; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasCardinality; import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraphManagement; import org.apache.atlas.repository.graphdb.AtlasPropertyKey; import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.types.Multiplicity; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; @@ -36,75 +37,49 @@ import org.testng.annotations.BeforeClass; */ public abstract class AbstractGraphDatabaseTest { - private static class RunnableWrapper implements Runnable { - private final Runnable r; - private Throwable exceptionThrown_ = null; - - private RunnableWrapper(Runnable r) { - this.r = r; - } - - @Override - public void run() { - try { - r.run(); - } - catch(Throwable e) { - exceptionThrown_ = e; - } - - } - - public Throwable getExceptionThrown() { - return exceptionThrown_; - } - } - protected static final String WEIGHT_PROPERTY = "weight"; protected static final String TRAIT_NAMES = Constants.TRAIT_NAMES_PROPERTY_KEY; - protected static final String typeProperty = "__type"; - protected static final String typeSystem = "typeSystem"; + protected static final String TYPE_PROPERTY_NAME = "__type"; + protected static final String TYPESYSTEM = "TYPESYSTEM"; - /** - * - */ private static final String BACKING_INDEX_NAME = "backing"; - private AtlasGraph<?,?> graph = null; + private AtlasGraph<?, ?> graph = null; - @AfterMethod - public void commitGraph() { - //force any pending actions to be committed so we can be sure they don't cause errors. - pushChangesAndFlushCache(); - graph.commit(); - } - protected <V, E> void pushChangesAndFlushCache() { - AtlasGraph<V, E> graph = getGraph(); - graph.commit(); - } + public AbstractGraphDatabaseTest() { + super(); + } @BeforeClass public static void createIndices() { - Titan0Database db = new Titan0Database(); + Titan0GraphDatabase db = new Titan0GraphDatabase(); AtlasGraphManagement mgmt = db.getGraph().getManagementSystem(); - if(mgmt.getGraphIndex(BACKING_INDEX_NAME) == null) { - mgmt.buildMixedVertexIndex(BACKING_INDEX_NAME, Constants.BACKING_INDEX); + if (mgmt.getGraphIndex(BACKING_INDEX_NAME) == null) { + mgmt.createVertexIndex(BACKING_INDEX_NAME, Constants.BACKING_INDEX, + Collections.<AtlasPropertyKey>emptyList()); } - mgmt.makePropertyKey("age13",Integer.class, Multiplicity.OPTIONAL); - - createIndices(mgmt, "name", String.class, false, Multiplicity.REQUIRED); - createIndices(mgmt, WEIGHT_PROPERTY, Integer.class, false, Multiplicity.OPTIONAL); - createIndices(mgmt, "size15", String.class, false, Multiplicity.REQUIRED); - createIndices(mgmt, "typeName", String.class, false, Multiplicity.REQUIRED); - createIndices(mgmt, "__type", String.class, false, Multiplicity.REQUIRED); - createIndices(mgmt, Constants.GUID_PROPERTY_KEY, String.class, true, Multiplicity.REQUIRED); - createIndices(mgmt, Constants.TRAIT_NAMES_PROPERTY_KEY, String.class, false, Multiplicity.SET); - createIndices(mgmt, Constants.SUPER_TYPES_PROPERTY_KEY, String.class, false, Multiplicity.SET); + mgmt.makePropertyKey("age13", Integer.class, AtlasCardinality.SINGLE); + + createIndices(mgmt, "name", String.class, false, AtlasCardinality.SINGLE); + createIndices(mgmt, WEIGHT_PROPERTY, Integer.class, false, AtlasCardinality.SINGLE); + createIndices(mgmt, "size15", String.class, false, AtlasCardinality.SINGLE); + createIndices(mgmt, "typeName", String.class, false, AtlasCardinality.SINGLE); + createIndices(mgmt, "__type", String.class, false, AtlasCardinality.SINGLE); + createIndices(mgmt, Constants.GUID_PROPERTY_KEY, String.class, true, AtlasCardinality.SINGLE); + createIndices(mgmt, Constants.TRAIT_NAMES_PROPERTY_KEY, String.class, false, AtlasCardinality.SET); + createIndices(mgmt, Constants.SUPER_TYPES_PROPERTY_KEY, String.class, false, AtlasCardinality.SET); mgmt.commit(); } + @AfterMethod + public void commitGraph() { + //force any pending actions to be committed so we can be sure they don't cause errors. + pushChangesAndFlushCache(); + getGraph().commit(); + } + @AfterClass public static void cleanUp() { Titan0Graph graph = new Titan0Graph(); @@ -112,32 +87,31 @@ public abstract class AbstractGraphDatabaseTest { } - private static void createIndices(AtlasGraphManagement management, String propertyName, Class propertyClass, - boolean isUnique, Multiplicity cardinality) { - + protected <V, E> void pushChangesAndFlushCache() { + getGraph().commit(); + } + private static void createIndices(AtlasGraphManagement management, String propertyName, Class propertyClass, + boolean isUnique, AtlasCardinality cardinality) { - if(management.containsPropertyKey(propertyName)) { + if (management.containsPropertyKey(propertyName)) { //index was already created return; } AtlasPropertyKey key = management.makePropertyKey(propertyName, propertyClass, cardinality); try { - if(propertyClass != Integer.class) { - management.addIndexKey(BACKING_INDEX_NAME, key); + if (propertyClass != Integer.class) { + management.addVertexIndexKey(BACKING_INDEX_NAME, key); } - } - catch(Throwable t) { + } catch(Throwable t) { //ok t.printStackTrace(); } try { - //if(propertyClass != Integer.class) { - management.createCompositeIndex(propertyName, key, isUnique); - //} - } - catch(Throwable t) { + management.createExactMatchIndex(propertyName, isUnique, Collections.singletonList(key)); + + } catch(Throwable t) { //ok t.printStackTrace(); } @@ -145,19 +119,14 @@ public abstract class AbstractGraphDatabaseTest { } - /** - * - */ - public AbstractGraphDatabaseTest() { - super(); - } - protected final <V,E> AtlasGraph<V, E> getGraph() { - if(graph == null) { + + protected final <V, E> AtlasGraph<V, E> getGraph() { + if (graph == null) { graph = new Titan0Graph(); } - return (AtlasGraph<V,E>)graph; + return (AtlasGraph<V, E>)graph; } protected Titan0Graph getTitan0Graph() { @@ -166,23 +135,23 @@ public abstract class AbstractGraphDatabaseTest { } - protected List<AtlasVertex> newVertices_ = new ArrayList<>(); + protected List<AtlasVertex> newVertices = new ArrayList<>(); - protected final <V, E> AtlasVertex<V, E> createVertex(AtlasGraph<V, E> graph) { - AtlasVertex<V,E> vertex = graph.addVertex(); - newVertices_.add(vertex); + protected final <V, E> AtlasVertex<V, E> createVertex(AtlasGraph<V, E> theGraph) { + AtlasVertex<V, E> vertex = theGraph.addVertex(); + newVertices.add(vertex); return vertex; } @AfterMethod public void removeVertices() { - for(AtlasVertex vertex : newVertices_) { - if(vertex.exists()) { + for(AtlasVertex vertex : newVertices) { + if (vertex.exists()) { getGraph().removeVertex(vertex); } } getGraph().commit(); - newVertices_.clear(); + newVertices.clear(); } protected void runSynchronouslyInNewThread(final Runnable r) throws Throwable { @@ -191,10 +160,32 @@ public abstract class AbstractGraphDatabaseTest { th.start(); th.join(); Throwable ex = wrapper.getExceptionThrown(); - if(ex != null) { + if (ex != null) { throw ex; } } + private static final class RunnableWrapper implements Runnable { + private final Runnable r; + private Throwable exceptionThrown = null; + + private RunnableWrapper(Runnable r) { + this.r = r; + } + + @Override + public void run() { + try { + r.run(); + } catch(Throwable e) { + exceptionThrown = e; + } + + } + + public Throwable getExceptionThrown() { + return exceptionThrown; + } + } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/GraphQueryTest.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/GraphQueryTest.java b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/GraphQueryTest.java index d02dce9..bf4519c 100644 --- a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/GraphQueryTest.java +++ b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/GraphQueryTest.java @@ -41,35 +41,35 @@ import com.google.common.collect.Collections2; /** - * Tests for Titan0GraphQuery + * Tests for Titan0GraphQuery. */ @Test public class GraphQueryTest extends AbstractGraphDatabaseTest { @Test - public <V,E> void testQueryThatCannotRunInMemory() throws AtlasException { - AtlasGraph<V,E> graph = getGraph(); - AtlasVertex<V,E> v1 = createVertex(graph); + public <V, E> void testQueryThatCannotRunInMemory() throws AtlasException { + AtlasGraph<V, E> graph = getGraph(); + AtlasVertex<V, E> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); - AtlasVertex<V,E> v2 = createVertex(graph); + AtlasVertex<V, E> v2 = createVertex(graph); v2.setProperty("name", "Fred"); - AtlasVertex<V,E> v3 = createVertex(graph); + AtlasVertex<V, E> v3 = createVertex(graph); v3.setProperty("size15", "15"); graph.commit(); - AtlasVertex<V,E> v4 = createVertex(graph); + AtlasVertex<V, E> v4 = createVertex(graph); v4.setProperty("name", "Fred"); v4.setProperty("size15", "15"); AtlasGraphQuery q = graph.query(); q.has("name", ComparisionOperator.NOT_EQUAL, "George"); - q.has("size15","15"); + q.has("size15", "15"); graph.commit(); pause(); //pause to let the index get updated @@ -81,49 +81,49 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { public void testCombinationOfAndsAndOrs() throws AtlasException { Titan0Graph graph = getTitan0Graph(); - AtlasVertex<Titan0Vertex,Titan0Edge> v1 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); v1.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v2 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v2 = createVertex(graph); v2.setProperty("name", "George"); v2.setProperty("size15", "16"); v2.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v3 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v3 = createVertex(graph); v3.setProperty("name", "Jane"); v3.setProperty("size15", "17"); v3.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v4 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v4 = createVertex(graph); v4.setProperty("name", "Bob"); v4.setProperty("size15", "18"); v4.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v5 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v5 = createVertex(graph); v5.setProperty("name", "Julia"); v5.setProperty("size15", "19"); v5.setProperty("typeName", "Manager"); AtlasGraphQuery q = getGraphQuery(); - q.has("typeName","Person"); + q.has("typeName", "Person"); //initially match AtlasGraphQuery inner1a = q.createChildQuery(); AtlasGraphQuery inner1b = q.createChildQuery(); - inner1a.has("name","Fred"); - inner1b.has("name","Jane"); + inner1a.has("name", "Fred"); + inner1b.has("name", "Jane"); q.or(toList(inner1a, inner1b)); AtlasGraphQuery inner2a = q.createChildQuery(); AtlasGraphQuery inner2b = q.createChildQuery(); AtlasGraphQuery inner2c = q.createChildQuery(); - inner2a.has("size15","18"); - inner2b.has("size15","15"); + inner2a.has("size15", "18"); + inner2b.has("size15", "15"); inner2c.has("size15", "16"); q.or(toList(inner2a, inner2b, inner2c)); @@ -137,36 +137,36 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { public void testWithinStep() throws AtlasException { Titan0Graph graph = getTitan0Graph(); - AtlasVertex<Titan0Vertex,Titan0Edge> v1 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); v1.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v2 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v2 = createVertex(graph); v2.setProperty("name", "George"); v2.setProperty("size15", "16"); v2.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v3 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v3 = createVertex(graph); v3.setProperty("name", "Jane"); v3.setProperty("size15", "17"); v3.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v4 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v4 = createVertex(graph); v4.setProperty("name", "Bob"); v4.setProperty("size15", "18"); v4.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v5 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v5 = createVertex(graph); v5.setProperty("name", "Julia"); v5.setProperty("size15", "19"); v5.setProperty("typeName", "Manager"); AtlasGraphQuery q = getGraphQuery(); - q.has("typeName","Person"); + q.has("typeName", "Person"); //initially match q.in("name", toList("Fred", "Jane")); q.in("size15", toList("18", "15", "16")); @@ -181,43 +181,44 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { public void testWithinStepWhereGraphIsStale() throws AtlasException { Titan0Graph graph = getTitan0Graph(); - AtlasVertex<Titan0Vertex,Titan0Edge> v1 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); v1.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v2 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v2 = createVertex(graph); v2.setProperty("name", "George"); v2.setProperty("size15", "16"); v2.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v3 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v3 = createVertex(graph); v3.setProperty("name", "Jane"); v3.setProperty("size15", "17"); v3.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v4 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v4 = createVertex(graph); v4.setProperty("name", "Bob"); v4.setProperty("size15", "18"); v4.setProperty("typeName", "Person"); - AtlasVertex<Titan0Vertex,Titan0Edge> v5 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v5 = createVertex(graph); v5.setProperty("name", "Julia"); v5.setProperty("size15", "19"); v5.setProperty("typeName", "Manager"); AtlasGraphQuery q = getGraphQuery(); - q.has("typeName","Person"); + q.has("typeName", "Person"); //initially match q.in("name", toList("Fred", "Jane")); graph.commit(); pause(); //let the index update assertQueryMatches(q, v1, v3); - v3.setProperty("name", "Janet"); //make v3 no longer match the query. Within step should filter out the vertex since it no longer matches. + //make v3 no longer match the query. Within step should filter out the vertex since it no longer matches. + v3.setProperty("name", "Janet"); assertQueryMatches(q, v1); } @@ -226,24 +227,24 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { Titan0Graph graph = getTitan0Graph(); - AtlasVertex<Titan0Vertex,Titan0Edge> v1 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); - AtlasVertex<Titan0Vertex,Titan0Edge> v2 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v2 = createVertex(graph); v2.setProperty("name", "Fred"); - AtlasVertex<Titan0Vertex,Titan0Edge> v3 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v3 = createVertex(graph); v3.setProperty("size15", "15"); graph.commit(); - AtlasVertex<Titan0Vertex,Titan0Edge> v4 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v4 = createVertex(graph); v4.setProperty("name", "Fred"); v4.setProperty("size15", "15"); - AtlasVertex<Titan0Vertex,Titan0Edge> v5 = createVertex(graph); + AtlasVertex<Titan0Vertex, Titan0Edge> v5 = createVertex(graph); v5.setProperty("name", "George"); v5.setProperty("size15", "16"); @@ -261,29 +262,29 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { @Test - public <V,E> void testQueryMatchesAddedVertices() throws AtlasException { - AtlasGraph<V,E> graph = getGraph(); + public <V, E> void testQueryMatchesAddedVertices() throws AtlasException { + AtlasGraph<V, E> graph = getGraph(); - AtlasVertex<V,E> v1 = createVertex(graph); + AtlasVertex<V, E> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); - AtlasVertex<V,E> v2 = createVertex(graph); + AtlasVertex<V, E> v2 = createVertex(graph); v2.setProperty("name", "Fred"); - AtlasVertex<V,E> v3 = createVertex(graph); + AtlasVertex<V, E> v3 = createVertex(graph); v3.setProperty("size15", "15"); graph.commit(); - AtlasVertex<V,E> v4 = createVertex(graph); + AtlasVertex<V, E> v4 = createVertex(graph); v4.setProperty("name", "Fred"); v4.setProperty("size15", "15"); AtlasGraphQuery q = getGraphQuery(); q.has("name", "Fred"); - q.has("size15","15"); + q.has("size15", "15"); assertQueryMatches(q, v1, v4); graph.commit(); @@ -293,21 +294,21 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { @Test - public <V,E> void testQueryDoesNotMatchRemovedVertices() throws AtlasException { - AtlasGraph<V,E> graph = getGraph(); + public <V, E> void testQueryDoesNotMatchRemovedVertices() throws AtlasException { + AtlasGraph<V, E> graph = getGraph(); - AtlasVertex<V,E> v1 = createVertex(graph); + AtlasVertex<V, E> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); - AtlasVertex<V,E> v2 = createVertex(graph); + AtlasVertex<V, E> v2 = createVertex(graph); v2.setProperty("name", "Fred"); - AtlasVertex<V,E> v3 = createVertex(graph); + AtlasVertex<V, E> v3 = createVertex(graph); v3.setProperty("size15", "15"); - AtlasVertex<V,E> v4 = createVertex(graph); + AtlasVertex<V, E> v4 = createVertex(graph); v4.setProperty("name", "Fred"); v4.setProperty("size15", "15"); @@ -317,7 +318,7 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { AtlasGraphQuery q = getGraphQuery(); q.has("name", "Fred"); - q.has("size15","15"); + q.has("size15", "15"); assertQueryMatches(q, v4); graph.commit(); @@ -326,28 +327,28 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { } @Test - public <V,E> void testQueryDoesNotMatchUncommittedAddedAndRemovedVertices() throws AtlasException { - AtlasGraph<V,E> graph = getGraph(); + public <V, E> void testQueryDoesNotMatchUncommittedAddedAndRemovedVertices() throws AtlasException { + AtlasGraph<V, E> graph = getGraph(); - AtlasVertex<V,E> v1 = createVertex(graph); + AtlasVertex<V, E> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); - AtlasVertex<V,E> v2 = createVertex(graph); + AtlasVertex<V, E> v2 = createVertex(graph); v2.setProperty("name", "Fred"); - AtlasVertex<V,E> v3 = createVertex(graph); + AtlasVertex<V, E> v3 = createVertex(graph); v3.setProperty("size15", "15"); - AtlasVertex<V,E> v4 = createVertex(graph); + AtlasVertex<V, E> v4 = createVertex(graph); v4.setProperty("name", "Fred"); v4.setProperty("size15", "15"); AtlasGraphQuery q = getGraphQuery(); q.has("name", "Fred"); - q.has("size15","15"); + q.has("size15", "15"); assertQueryMatches(q, v1, v4); @@ -362,20 +363,20 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { @Test - public <V,E> void testQueryResultsReflectPropertyAdd() throws AtlasException { - AtlasGraph<V,E> graph = getGraph(); + public <V, E> void testQueryResultsReflectPropertyAdd() throws AtlasException { + AtlasGraph<V, E> graph = getGraph(); - AtlasVertex<V,E> v1 = createVertex(graph); + AtlasVertex<V, E> v1 = createVertex(graph); v1.setProperty("name", "Fred"); v1.setProperty("size15", "15"); v1.addProperty(TRAIT_NAMES, "trait1"); v1.addProperty(TRAIT_NAMES, "trait2"); - AtlasVertex<V,E> v2 = createVertex(graph); + AtlasVertex<V, E> v2 = createVertex(graph); v2.setProperty("name", "Fred"); v2.addProperty(TRAIT_NAMES, "trait1"); - AtlasVertex<V,E> v3 = createVertex(graph); + AtlasVertex<V, E> v3 = createVertex(graph); v3.setProperty("size15", "15"); v3.addProperty(TRAIT_NAMES, "trait2"); @@ -409,22 +410,25 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { } - private <V,E >void assertQueryMatches(AtlasGraphQuery expr, AtlasVertex... expectedResults) throws AtlasException { + private <V, E> void assertQueryMatches(AtlasGraphQuery expr, AtlasVertex... expectedResults) throws AtlasException { //getGraph().commit(); Collection<AtlasVertex<Titan0Vertex, Titan0Edge>> temp = toList(expr.vertices()); //filter out vertices from previous test executions - Collection<AtlasVertex<Titan0Vertex, Titan0Edge>> result = Collections2.filter(temp, new Predicate<AtlasVertex<Titan0Vertex, Titan0Edge>>() { + Collection<AtlasVertex<Titan0Vertex, Titan0Edge>> result = + Collections2.filter(temp, new Predicate<AtlasVertex<Titan0Vertex, Titan0Edge>>() { - @Override - public boolean apply(AtlasVertex<Titan0Vertex, Titan0Edge> input) { - return newVertices_.contains(input); - } + @Override + public boolean apply(AtlasVertex<Titan0Vertex, Titan0Edge> input) { + return newVertices.contains(input); + } - }); - assertEquals("Expected/found result sizes differ. Expected: " + Arrays.asList(expectedResults).toString() +", found: " + result, expectedResults.length, result.size()); + }); + String errorMessage = "Expected/found result sizes differ. Expected: " + + Arrays.asList(expectedResults).toString() +", found: " + result; + assertEquals(errorMessage, expectedResults.length, result.size()); - for(AtlasVertex<V,E> v : expectedResults) { + for(AtlasVertex<V, E> v : expectedResults) { assertTrue(result.contains(v)); } } @@ -432,7 +436,7 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { private static List<Object> toList(Object...objects) { return Arrays.asList(objects); } - + private AtlasGraphQuery<Titan0Vertex, Titan0Edge> getGraphQuery() { return getTitan0Graph().query(); } @@ -440,8 +444,8 @@ public class GraphQueryTest extends AbstractGraphDatabaseTest { private void pause() { try { Thread.sleep(5000); + } catch(InterruptedException e) { + //ignore } - catch(InterruptedException e) - {} } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseTest.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseTest.java b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseTest.java index 6c2ea26..e82de48 100644 --- a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseTest.java +++ b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseTest.java @@ -30,10 +30,12 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import org.apache.atlas.AtlasException; import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasCardinality; import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; import org.apache.atlas.repository.graphdb.AtlasGraph; @@ -43,7 +45,6 @@ import org.apache.atlas.repository.graphdb.AtlasGraphQuery.ComparisionOperator; import org.apache.atlas.repository.graphdb.AtlasPropertyKey; import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; -import org.apache.atlas.typesystem.types.Multiplicity; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; @@ -57,16 +58,16 @@ public class Titan0DatabaseTest { private <V, E> AtlasGraph<V, E> getGraph() { if (atlasGraph == null) { - Titan0Database db = new Titan0Database(); + Titan0GraphDatabase db = new Titan0GraphDatabase(); atlasGraph = db.getGraph(); AtlasGraphManagement mgmt = atlasGraph.getManagementSystem(); // create the index (which defines these properties as being mult // many) - for (String propertyName : AtlasGraphManagement.MULTIPLICITY_MANY_PROPERTY_KEYS) { + for (String propertyName : new String[]{"__superTypeNames", "__traitNames"}) { AtlasPropertyKey propertyKey = mgmt.getPropertyKey(propertyName); if (propertyKey == null) { - propertyKey = mgmt.makePropertyKey(propertyName, String.class, Multiplicity.SET); - mgmt.createCompositeIndex(propertyName, propertyKey, false); + propertyKey = mgmt.makePropertyKey(propertyName, String.class, AtlasCardinality.SET); + mgmt.createExactMatchIndex(propertyName, false, Collections.singletonList(propertyKey)); } } mgmt.commit(); http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseValidationTest.java ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseValidationTest.java b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseValidationTest.java index 341c064..1eccc7f 100644 --- a/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseValidationTest.java +++ b/graphdb/titan0/src/test/java/org/apache/atlas/repository/graphdb/titan0/Titan0DatabaseValidationTest.java @@ -37,7 +37,7 @@ public class Titan0DatabaseValidationTest { // First get Instance graph = new Titan0Graph(); configuration = ApplicationProperties.getSubsetConfiguration(ApplicationProperties.get(), - Titan0Database.GRAPH_PREFIX); + Titan0GraphDatabase.GRAPH_PREFIX); } @AfterClass @@ -58,15 +58,15 @@ public class Titan0DatabaseValidationTest { @Test public void testValidate() throws AtlasException { try { - Titan0Database.validateIndexBackend(configuration); + Titan0GraphDatabase.validateIndexBackend(configuration); } catch (Exception e) { Assert.fail("Unexpected exception ", e); } // Change backend - configuration.setProperty(Titan0Database.INDEX_BACKEND_CONF, Titan0Database.INDEX_BACKEND_LUCENE); + configuration.setProperty(Titan0GraphDatabase.INDEX_BACKEND_CONF, Titan0GraphDatabase.INDEX_BACKEND_LUCENE); try { - Titan0Database.validateIndexBackend(configuration); + Titan0GraphDatabase.validateIndexBackend(configuration); Assert.fail("Expected exception"); } catch (Exception e) { Assert.assertEquals(e.getMessage(), http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/d2d6ff7d/graphdb/titan0/src/test/resources/atlas-application.properties ---------------------------------------------------------------------- diff --git a/graphdb/titan0/src/test/resources/atlas-application.properties b/graphdb/titan0/src/test/resources/atlas-application.properties index 1e8963e..636a9ff 100644 --- a/graphdb/titan0/src/test/resources/atlas-application.properties +++ b/graphdb/titan0/src/test/resources/atlas-application.properties @@ -17,7 +17,7 @@ # ######### Graph Database to Use ######### -atlas.graphdb.backend=org.apache.atlas.repository.graphdb.titan0.Titan0Database +atlas.graphdb.backend=org.apache.atlas.repository.graphdb.titan0.Titan0GraphDatabase ######### Atlas Server Configs ######### atlas.rest.address=http://localhost:31000
