Commit 25ff452b by Venkatesh Seetharam

Add type specific indexes. Contributed by Venkatesh Seetharam

parent 51d00a85
...@@ -24,10 +24,27 @@ final class Constants { ...@@ -24,10 +24,27 @@ final class Constants {
} }
static final String GUID_PROPERTY_KEY = "guid"; static final String GUID_PROPERTY_KEY = "guid";
static final String GUID_INDEX = "guid_index";
static final String ENTITY_TYPE_PROPERTY_KEY = "type"; static final String ENTITY_TYPE_PROPERTY_KEY = "type";
static final String ENTITY_TYPE_INDEX = "type_index";
static final String VERSION_PROPERTY_KEY = "version"; static final String VERSION_PROPERTY_KEY = "version";
static final String TIMESTAMP_PROPERTY_KEY = "timestamp"; static final String TIMESTAMP_PROPERTY_KEY = "timestamp";
/**
* search backing index name.
*/
static final String BACKING_INDEX = "search"; static final String BACKING_INDEX = "search";
static final String INDEX_NAME = "metadata"; static final String INDEX_NAME = "metadata";
/**
* search backing index name for vertex keys.
*/
static final String VERTEX_INDEX = "vertex_index";
/**
* search backing index name for edge labels.
*/
static final String EDGE_INDEX = "edge_index";
} }
...@@ -18,12 +18,14 @@ ...@@ -18,12 +18,14 @@
package org.apache.hadoop.metadata.repository.graph; package org.apache.hadoop.metadata.repository.graph;
import com.thinkaurelius.titan.core.Cardinality; import com.thinkaurelius.titan.core.EdgeLabel;
import com.thinkaurelius.titan.core.Order;
import com.thinkaurelius.titan.core.PropertyKey; import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
import com.thinkaurelius.titan.core.schema.TitanManagement; import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import org.apache.hadoop.metadata.MetadataException; import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.repository.SearchIndexer; import org.apache.hadoop.metadata.repository.SearchIndexer;
...@@ -69,39 +71,26 @@ public class GraphBackedSearchIndexer implements SearchIndexer { ...@@ -69,39 +71,26 @@ public class GraphBackedSearchIndexer implements SearchIndexer {
LOG.info("Indexes do not exist, Creating indexes for titanGraph."); LOG.info("Indexes do not exist, Creating indexes for titanGraph.");
try { try {
createIndex(management, Constants.GUID_PROPERTY_KEY, String.class, true); management.buildIndex(Constants.VERTEX_INDEX, Vertex.class)
createIndex(management, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class); .buildMixedIndex(Constants.BACKING_INDEX);
createIndex(management, Constants.TIMESTAMP_PROPERTY_KEY, Long.class); management.buildIndex(Constants.EDGE_INDEX, Edge.class)
.buildMixedIndex(Constants.BACKING_INDEX);
// create a composite index for guid as its unique
createCompositeIndex(management, Constants.GUID_INDEX,
Constants.GUID_PROPERTY_KEY, String.class, true);
// create a composite and mixed index for type since it can be combined with other keys
createCompositeIndex(management, Constants.ENTITY_TYPE_INDEX,
Constants.ENTITY_TYPE_PROPERTY_KEY, String.class, false);
createVertexMixedIndex(management, Constants.ENTITY_TYPE_PROPERTY_KEY, String.class);
} finally { } finally {
management.commit(); management.commit();
} }
LOG.info("Index creation for global keys complete."); LOG.info("Index creation for global keys complete.");
// dumpIndexKeys();
} }
/*
private void dumpIndexKeys() {
for (TitanGraphIndex index : management.getGraphIndexes(Vertex.class)) {
System.out.println("index.getName() = " + index.getName());
for (PropertyKey key : index.getFieldKeys()) {
System.out.println("key.getName() = " + key.getName());
System.out.println("key = " + key);
}
}
for (TitanGraphIndex index : management.getGraphIndexes(Edge.class)) {
System.out.println("index.getName() = " + index.getName());
for (PropertyKey key : index.getFieldKeys()) {
System.out.println("key.getName() = " + key.getName());
System.out.println("key = " + key);
}
}
}
*/
/** /**
* This is upon adding a new type to Store. * This is upon adding a new type to Store.
* *
...@@ -113,18 +102,16 @@ public class GraphBackedSearchIndexer implements SearchIndexer { ...@@ -113,18 +102,16 @@ public class GraphBackedSearchIndexer implements SearchIndexer {
public void onAdd(String typeName, IDataType dataType) throws MetadataException { public void onAdd(String typeName, IDataType dataType) throws MetadataException {
LOG.info("Creating indexes for type name={}, definition={}", typeName, dataType); LOG.info("Creating indexes for type name={}, definition={}", typeName, dataType);
try {
TitanManagement management = titanGraph.getManagementSystem(); TitanManagement management = titanGraph.getManagementSystem();
try {
addIndexForType(management, dataType); addIndexForType(management, dataType);
management.commit(); management.commit();
LOG.info("Index creation for type {} complete", typeName); LOG.info("Index creation for type {} complete", typeName);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error creating index for type {}", dataType, e); LOG.error("Error creating index for type {}", dataType, e);
// management.rollback(); management.rollback();
} }
// dumpIndexKeys();
} }
private void addIndexForType(TitanManagement management, IDataType dataType) { private void addIndexForType(TitanManagement management, IDataType dataType) {
...@@ -170,18 +157,18 @@ public class GraphBackedSearchIndexer implements SearchIndexer { ...@@ -170,18 +157,18 @@ public class GraphBackedSearchIndexer implements SearchIndexer {
final String propertyName = typeName + "." + field.name; final String propertyName = typeName + "." + field.name;
switch (field.dataType().getTypeCategory()) { switch (field.dataType().getTypeCategory()) {
case PRIMITIVE: case PRIMITIVE:
createIndex(management, propertyName, createVertexMixedIndex(
getPrimitiveClass(field.dataType()), field.isUnique); management, propertyName, getPrimitiveClass(field.dataType()));
break; break;
case ENUM: case ENUM:
createIndex(management, propertyName, Integer.class, field.isUnique); createVertexMixedIndex(management, propertyName, Integer.class);
break; break;
case ARRAY: case ARRAY:
case MAP: case MAP:
// index the property holder for element names // index the property holder for element names
createIndex(management, propertyName, String.class, field.isUnique); createVertexMixedIndex(management, propertyName, String.class);
break; break;
case STRUCT: case STRUCT:
...@@ -195,7 +182,7 @@ public class GraphBackedSearchIndexer implements SearchIndexer { ...@@ -195,7 +182,7 @@ public class GraphBackedSearchIndexer implements SearchIndexer {
case CLASS: case CLASS:
// this is only A reference, index the attribute for edge // this is only A reference, index the attribute for edge
createEdgeIndex(management, propertyName, String.class); createEdgeMixedIndex(management, propertyName);
break; break;
default: default:
...@@ -229,39 +216,18 @@ public class GraphBackedSearchIndexer implements SearchIndexer { ...@@ -229,39 +216,18 @@ public class GraphBackedSearchIndexer implements SearchIndexer {
throw new IllegalArgumentException("unknown data type " + dataType); throw new IllegalArgumentException("unknown data type " + dataType);
} }
private static PropertyKey createIndex(TitanManagement management, private static PropertyKey createCompositeIndex(TitanManagement management, String indexName,
String propertyName, Class propertyClass) {
return createIndex(management,
propertyName, propertyClass, Cardinality.SINGLE, false, Vertex.class);
}
private static PropertyKey createIndex(TitanManagement management,
String propertyName,
Class propertyClass, boolean isUnique) {
return createIndex(management,
propertyName, propertyClass, Cardinality.SINGLE, isUnique, Vertex.class);
}
private static PropertyKey createEdgeIndex(TitanManagement management,
String propertyName, Class propertyClass) {
return createIndex(management,
propertyName, propertyClass, Cardinality.SINGLE, false, Edge.class);
}
private static PropertyKey createIndex(TitanManagement management,
String propertyName, Class propertyClass, String propertyName, Class propertyClass,
Cardinality cardinality, boolean isUnique, boolean isUnique) {
Class<? extends Element> elementClass) {
PropertyKey propertyKey = management.getPropertyKey(propertyName); PropertyKey propertyKey = management.getPropertyKey(propertyName);
if (propertyKey == null) { if (propertyKey == null) {
propertyKey = management propertyKey = management
.makePropertyKey(propertyName) .makePropertyKey(propertyName)
.dataType(propertyClass) .dataType(propertyClass)
.cardinality(cardinality)
.make(); .make();
TitanManagement.IndexBuilder indexBuilder = management TitanManagement.IndexBuilder indexBuilder = management
.buildIndex("index_" + propertyName, elementClass) .buildIndex(indexName, Vertex.class)
.addKey(propertyKey); .addKey(propertyKey);
if (isUnique) { if (isUnique) {
...@@ -273,4 +239,26 @@ public class GraphBackedSearchIndexer implements SearchIndexer { ...@@ -273,4 +239,26 @@ public class GraphBackedSearchIndexer implements SearchIndexer {
return propertyKey; return propertyKey;
} }
private static PropertyKey createVertexMixedIndex(TitanManagement management,
String propertyName, Class propertyClass) {
PropertyKey propertyKey = management.getPropertyKey(propertyName);
if (propertyKey == null) {
propertyKey = management
.makePropertyKey(propertyName)
.dataType(propertyClass)
.make();
}
TitanGraphIndex vertexIndex = management.getGraphIndex(Constants.VERTEX_INDEX);
management.addIndexKey(vertexIndex, propertyKey);
return propertyKey;
}
private static void createEdgeMixedIndex(TitanManagement management,
String propertyName) {
EdgeLabel edgeLabel = management.makeEdgeLabel(propertyName).make();
management.buildEdgeIndex(edgeLabel, propertyName, Direction.BOTH, Order.DEFAULT);
}
} }
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
package org.apache.hadoop.metadata.repository.graph; package org.apache.hadoop.metadata.repository.graph;
import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.TitanIndexQuery;
import com.tinkerpop.blueprints.Compare;
import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Graph;
...@@ -73,34 +71,25 @@ public final class GraphHelper { ...@@ -73,34 +71,25 @@ public final class GraphHelper {
return instanceVertex; return instanceVertex;
} }
public static Edge addEdge(Vertex fromVertex, Vertex toVertex, String edgeLabel) { public static Edge addEdge(TitanGraph titanGraph, Vertex fromVertex, Vertex toVertex,
String edgeLabel) {
LOG.debug("Adding edge for {} -> struct label {} -> v{}", LOG.debug("Adding edge for {} -> struct label {} -> v{}",
fromVertex, edgeLabel, toVertex); fromVertex, edgeLabel, toVertex);
return fromVertex.addEdge(edgeLabel, toVertex);
return titanGraph.addEdge(null, fromVertex, toVertex, edgeLabel);
} }
public static Vertex findVertexByGUID(Graph blueprintsGraph, public static Vertex findVertexByGUID(TitanGraph titanGraph,
String value) { String value) {
LOG.debug("Finding vertex for key={}, value={}", Constants.GUID_PROPERTY_KEY, value); LOG.debug("Finding vertex for key={}, value={}", Constants.GUID_PROPERTY_KEY, value);
GraphQuery query = blueprintsGraph.query() GraphQuery query = titanGraph.query()
.has(Constants.GUID_PROPERTY_KEY, Compare.EQUAL, value); .has(Constants.GUID_PROPERTY_KEY, value);
Iterator<Vertex> results = query.vertices().iterator(); Iterator<Vertex> results = query.vertices().iterator();
// returning one since guid should be unique // returning one since guid should be unique
return results.hasNext() ? results.next() : null; return results.hasNext() ? results.next() : null;
} }
public static Vertex findVertexByGUIDUsingIndex(TitanGraph titanGraph,
String value) {
LOG.debug("Finding vertex for key={}, value={}", Constants.GUID_PROPERTY_KEY, value);
TitanIndexQuery query = titanGraph.indexQuery("index_" + Constants.GUID_PROPERTY_KEY,
Constants.GUID_PROPERTY_KEY + " = " + value);
Iterator<TitanIndexQuery.Result<Vertex>> results = query.vertices().iterator();
// returning one since guid should be unique
return results.hasNext() ? results.next().getElement() : null;
}
public static String vertexString(final Vertex vertex) { public static String vertexString(final Vertex vertex) {
StringBuilder properties = new StringBuilder(); StringBuilder properties = new StringBuilder();
for (String propertyKey : vertex.getPropertyKeys()) { for (String propertyKey : vertex.getPropertyKeys()) {
...@@ -120,6 +109,7 @@ public final class GraphHelper { ...@@ -120,6 +109,7 @@ public final class GraphHelper {
+ "]"; + "]";
} }
/*
public static void dumpToLog(final Graph graph) { public static void dumpToLog(final Graph graph) {
LOG.debug("*******************Graph Dump****************************"); LOG.debug("*******************Graph Dump****************************");
LOG.debug("Vertices of {}", graph); LOG.debug("Vertices of {}", graph);
...@@ -133,4 +123,5 @@ public final class GraphHelper { ...@@ -133,4 +123,5 @@ public final class GraphHelper {
} }
LOG.debug("*******************Graph Dump****************************"); LOG.debug("*******************Graph Dump****************************");
} }
*/
} }
\ No newline at end of file
...@@ -70,12 +70,14 @@ public class GraphBackedMetadataRepositoryTest { ...@@ -70,12 +70,14 @@ public class GraphBackedMetadataRepositoryTest {
// start the injected repository service // start the injected repository service
repositoryService.start(); repositoryService.start();
new GraphBackedSearchIndexer(titanGraphService);
ts = TypeSystem.getInstance(); ts = TypeSystem.getInstance();
defineDeptEmployeeTypes(ts); defineDeptEmployeeTypes(ts);
} }
@Test(enabled = false) @Test
public void testSubmitEntity() throws Exception { public void testSubmitEntity() throws Exception {
Referenceable hrDept = createDeptEg1(ts); Referenceable hrDept = createDeptEg1(ts);
ClassType deptType = ts.getDataType(ClassType.class, "Department"); ClassType deptType = ts.getDataType(ClassType.class, "Department");
...@@ -97,7 +99,7 @@ public class GraphBackedMetadataRepositoryTest { ...@@ -97,7 +99,7 @@ public class GraphBackedMetadataRepositoryTest {
} }
} }
@Test(dependsOnMethods = "testSubmitEntity", enabled = false) @Test(dependsOnMethods = "testSubmitEntity")
public void testGetEntityDefinition() throws Exception { public void testGetEntityDefinition() throws Exception {
ITypedReferenceableInstance entity = repositoryService.getEntityDefinition(guid); ITypedReferenceableInstance entity = repositoryService.getEntityDefinition(guid);
Assert.assertNotNull(entity); Assert.assertNotNull(entity);
...@@ -112,11 +114,12 @@ public class GraphBackedMetadataRepositoryTest { ...@@ -112,11 +114,12 @@ public class GraphBackedMetadataRepositoryTest {
@Test(enabled = false) @Test(enabled = false)
public void testGetEntityList() throws Exception { public void testGetEntityList() throws Exception {
List<String> entityList = repositoryService.getEntityList(ENTITY_TYPE); List<String> entityList = repositoryService.getEntityList(ENTITY_TYPE);
System.out.println("entityList = " + entityList);
Assert.assertNotNull(entityList); Assert.assertNotNull(entityList);
Assert.assertEquals(entityList.size(), 1); // one department Assert.assertEquals(entityList.size(), 1); // one department
} }
@Test(enabled = false) @Test
public void testRawSearch1() throws Exception { public void testRawSearch1() throws Exception {
Referenceable hrDept = createDeptEg1(ts); Referenceable hrDept = createDeptEg1(ts);
ClassType deptType = ts.getDataType(ClassType.class, "Department"); ClassType deptType = ts.getDataType(ClassType.class, "Department");
......
...@@ -73,6 +73,8 @@ public class GraphRepoMapperTest { ...@@ -73,6 +73,8 @@ public class GraphRepoMapperTest {
// start the injected repository service // start the injected repository service
repositoryService.start(); repositoryService.start();
new GraphBackedSearchIndexer(titanGraphService);
typeSystem = TypeSystem.getInstance(); typeSystem = TypeSystem.getInstance();
createHiveTypes(); createHiveTypes();
...@@ -109,7 +111,6 @@ public class GraphRepoMapperTest { ...@@ -109,7 +111,6 @@ public class GraphRepoMapperTest {
public void testGetEntityDefinition() throws Exception { public void testGetEntityDefinition() throws Exception {
TitanGraph graph = titanGraphService.getTitanGraph(); TitanGraph graph = titanGraphService.getTitanGraph();
GraphQuery query = graph.query() GraphQuery query = graph.query()
.has("type", Compare.EQUAL, TABLE_TYPE)
.has("type", Compare.EQUAL, TABLE_TYPE); .has("type", Compare.EQUAL, TABLE_TYPE);
Iterator<Vertex> results = query.vertices().iterator(); Iterator<Vertex> results = query.vertices().iterator();
// returning one since guid should be unique // returning one since guid should be unique
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment