Skip to content

Instantly share code, notes, and snippets.

@yhemanth
Created May 3, 2016 13:21
Show Gist options
  • Save yhemanth/f6ecd71e3da20d1cd3a394ff275d3070 to your computer and use it in GitHub Desktop.
Save yhemanth/f6ecd71e3da20d1cd3a394ff275d3070 to your computer and use it in GitHub Desktop.
Test case that can be embedded in Apache Atlas to demonstrate Titan index query inconsistency
package org.apache.atlas.service;
import com.thinkaurelius.titan.core.Cardinality;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.tinkerpop.blueprints.Vertex;
import org.apache.atlas.AtlasException;
import org.apache.atlas.repository.graph.TitanGraphProvider;
import org.testng.annotations.Test;
import java.util.Iterator;
public class TitanIndexTest {
public static final String PROP_NAME = "prop.Name";
public static final String PROPVALUE = "propvalue";
public static final String STATE = "__state";
public static final String ACTIVE = "active";
public static final String DELETED = "deleted";
@Test
public void testIndex() throws AtlasException {
TitanGraph graphInstance = TitanGraphProvider.getGraphInstance();
TitanManagement managementSystem = graphInstance.getManagementSystem();
PropertyKey stateKey = managementSystem.makePropertyKey(STATE).dataType(String.class).cardinality(Cardinality.SINGLE).make();
PropertyKey propertyKey = managementSystem.makePropertyKey(PROP_NAME).dataType(String.class).cardinality(Cardinality.SINGLE).make();
// managementSystem.buildIndex(PROP_NAME, Vertex.class).addKey(propertyKey).addKey(stateKey).buildCompositeIndex();
managementSystem.buildIndex(PROP_NAME, Vertex.class).addKey(propertyKey).buildCompositeIndex();
managementSystem.buildIndex(STATE, Vertex.class).addKey(stateKey).buildCompositeIndex();
managementSystem.commit();
Vertex vertex = graphInstance.addVertex(null);
vertex.setProperty(PROP_NAME, PROPVALUE);
vertex.setProperty(STATE, ACTIVE);
graphInstance.commit();
Iterator<Vertex> results = graphInstance.query().has(PROP_NAME, PROPVALUE).has(STATE, ACTIVE).vertices().iterator();
if (results.hasNext()) {
Vertex result = results.next();
System.out.println("Got result querying with active state: " + result + ", actual state: " + result.getProperty(STATE));
result.setProperty(STATE, DELETED);
}
graphInstance.commit();
results = graphInstance.query().has(PROP_NAME, PROPVALUE).has(STATE, ACTIVE).vertices().iterator();
if (results.hasNext()) {
Vertex result = results.next();
System.out.println("Got result querying with active state after change: " + result + ", actual state: " + result.getProperty(STATE));
} else {
System.out.println("Did not find active instance.");
}
results = graphInstance.query().has(PROP_NAME, PROPVALUE).has(STATE, DELETED).vertices().iterator();
if (results.hasNext()) {
Vertex result = results.next();
System.out.println("Got result querying with deleted state after change: " + result + ", actual state: " + result.getProperty(STATE));
} else {
System.out.println("Did not find deleted instance.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment