Last active
August 29, 2015 14:04
-
-
Save ubit-ee/8520304273cd2024af29 to your computer and use it in GitHub Desktop.
Titan Graph DB test producing multiple vertices with name key.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dbexample; | |
import com.thinkaurelius.titan.core.Cardinality; | |
import com.thinkaurelius.titan.core.PropertyKey; | |
import com.thinkaurelius.titan.core.TitanFactory; | |
import com.thinkaurelius.titan.core.TitanGraph; | |
import com.thinkaurelius.titan.core.schema.ConsistencyModifier; | |
import com.thinkaurelius.titan.core.schema.TitanGraphIndex; | |
import com.thinkaurelius.titan.core.schema.TitanManagement; | |
import com.tinkerpop.blueprints.Vertex; | |
import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter; | |
import org.apache.commons.configuration.PropertiesConfiguration; | |
import org.apache.commons.io.FileUtils; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.io.File; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.concurrent.CountDownLatch; | |
import static com.google.common.collect.Lists.newArrayList; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.Matchers.hasSize; | |
public class TitanUniqueKeyTest { | |
public static final String GRAPH_XML_DUMP = "/tmp/uniqueKeyGraphTest.xml"; | |
static String TITAN_STORAGE_DIRECTORY = "/tmp/titanDbTest"; | |
static String KEY_NAME = "MY_KEY_NAME"; | |
String KEY_VALUE_A = "KEY_VALUE_A"; | |
String KEY_VALUE_B = "KEY_VALUE_B"; | |
static String LABEL_NAME = "MY_LABEL_NAME"; | |
TitanGraph graph; | |
@Before | |
public void setUp() { | |
clearAndInitializeDatabase(); | |
} | |
@After | |
public void dumpAndShutdownDatabase() throws Exception { | |
GraphMLWriter.outputGraph(graph, GRAPH_XML_DUMP); | |
graph.shutdown(); | |
} | |
@Test | |
public void demonstrateKeyCollisions() { | |
int threadCount = 3; | |
CountDownLatch startLatch = new CountDownLatch(threadCount); | |
CountDownLatch finishLatch = new CountDownLatch(threadCount); | |
for (int i = 0; i < threadCount; i++) { | |
spawnCreationThread(startLatch, finishLatch); | |
} | |
try { | |
finishLatch.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
Iterator<Vertex> iterator = graph.query().has(KEY_NAME, KEY_VALUE_A).vertices().iterator(); | |
List<Vertex> vertices = newArrayList(iterator); | |
assertThat(vertices, hasSize(1)); | |
iterator = graph.query().has(KEY_NAME, KEY_VALUE_B).vertices().iterator(); | |
vertices = newArrayList(iterator); | |
assertThat(vertices, hasSize(1)); | |
} | |
private void spawnCreationThread(final CountDownLatch startLatch, | |
final CountDownLatch finishLatch) { | |
new Thread() { | |
@Override | |
public void run() { | |
awaitAllThreadsReady(); | |
Vertex vA = graph.addVertex(null); | |
vA.setProperty(KEY_NAME, KEY_VALUE_A); | |
Vertex vB = graph.addVertex(null); | |
vB.setProperty(KEY_NAME, KEY_VALUE_B); | |
vB.addEdge(LABEL_NAME, vA); | |
graph.commit(); | |
System.out.println("New ID: " + vA.getId()); | |
System.out.println("New ID: " + vB.getId()); | |
finishLatch.countDown(); | |
} | |
private void awaitAllThreadsReady() { | |
startLatch.countDown(); | |
try { | |
startLatch.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}.start(); | |
} | |
private void clearAndInitializeDatabase() { | |
clearDatabase(); | |
graph = TitanFactory.open(localStorageConfigurationProperties()); | |
initializeSchema(graph); | |
} | |
private void clearDatabase() { | |
FileUtils.deleteQuietly(new File(TITAN_STORAGE_DIRECTORY)); | |
} | |
private static PropertiesConfiguration localStorageConfigurationProperties() { | |
PropertiesConfiguration conf = new PropertiesConfiguration(); | |
conf.addProperty("storage.directory", TITAN_STORAGE_DIRECTORY); | |
conf.addProperty("storage.backend", "berkeleyje"); | |
return conf; | |
} | |
private static void initializeSchema(TitanGraph graph) { | |
createKeys(graph); | |
createLabels(graph); | |
} | |
private static void createLabels(TitanGraph graph) { | |
createLabel(graph, LABEL_NAME); | |
} | |
private static void createLabel(TitanGraph graph, String label) { | |
graph.makeEdgeLabel(label).make(); | |
graph.commit(); | |
} | |
private static void createKeys(TitanGraph graph) { | |
createKey(graph, KEY_NAME); | |
} | |
private static void createKey(TitanGraph graph, String keyName) { | |
TitanManagement management = graph.getManagementSystem(); | |
PropertyKey name = management.makePropertyKey(keyName) | |
.dataType(String.class) | |
.cardinality(Cardinality.SINGLE) | |
.make(); | |
TitanGraphIndex nameIndex = management.buildIndex(keyName, Vertex.class) | |
.indexKey(name) | |
.unique() | |
.buildCompositeIndex(); | |
management.setConsistency(nameIndex, ConsistencyModifier.LOCK); | |
management.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment