package com.example.orientdb.index;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public class EdgeIndexTxProblem {
private static final String CUSTOM_VERTEX = "CustomVertex";
private static final String CUSTOM_EDGE = "CustomEdge";
private static final String IN_INDEX_NAME = CUSTOM_EDGE + ".in";
private static final String OUT_INDEX_NAME = CUSTOM_EDGE + ".out";
private static final String COMPOSITE_INDEX_NAME = CUSTOM_EDGE + ".compositeInOut";
public static void main(String[] args) {
OrientGraphFactory graphFactory = null;
// Use NoTx for schema config to avoid warning messages
OrientGraphNoTx graphNoTx = null;
OrientGraph graph = null;
try {
graphFactory = new OrientGraphFactory("remote:localhost/edgeTxProblem", "user", "pass");
graphNoTx = graphFactory.getNoTx();
OrientVertexType vertexType = null;
if( ! graphNoTx.getRawGraph().existsCluster(CUSTOM_VERTEX)) {
vertexType = graphNoTx.createVertexType(CUSTOM_VERTEX);
}
else {
vertexType = graphNoTx.getVertexType(CUSTOM_VERTEX);
}
if( ! graphNoTx.getRawGraph().existsCluster(CUSTOM_EDGE)) {
OrientEdgeType edgeType = graphNoTx.createEdgeType(CUSTOM_EDGE);
edgeType.createProperty("out", OType.LINK, vertexType);
edgeType.createProperty("in", OType.LINK, vertexType);
edgeType.createIndex(IN_INDEX_NAME, OClass.INDEX_TYPE.UNIQUE, "in");
edgeType.createIndex(OUT_INDEX_NAME, OClass.INDEX_TYPE.UNIQUE, "out");
edgeType.createIndex(COMPOSITE_INDEX_NAME, OClass.INDEX_TYPE.UNIQUE, "out", "in");
}
graphNoTx.shutdown();
graph = graphFactory.getTx();
Vertex inVert = null;
for(int i = 0; i < 5; ++i) {
Vertex currentVert = graph.addVertex("class:" + CUSTOM_VERTEX);
if(inVert != null) {
graph.addEdge("class:" + CUSTOM_EDGE, currentVert, inVert, CUSTOM_EDGE);
}
inVert = currentVert;
}
graph.commit();
Iterable<Vertex> verts = graph.getVertices();
StringBuilder vertIds = new StringBuilder();
for(Vertex vert : verts) {
vertIds.append(vert.getId().toString()).append(" ");
}
System.out.println("Vertices: " + vertIds);
System.out.println();
printIndexData(graph, IN_INDEX_NAME);
printIndexData(graph, OUT_INDEX_NAME);
printIndexData(graph, COMPOSITE_INDEX_NAME);
}
finally {
if(graphNoTx != null) {
try {
graphNoTx.shutdown();
}
catch(Throwable t) {
System.err.println("Error shutting down graphNoTx:" + t);
}
}
if(graph != null) {
try {
graph.shutdown();
}
catch(Throwable t) {
System.err.println("Error shutting down graph:" + t);
}
}
if(graphFactory != null) {
graphFactory.close();
}
}
}
private static void printIndexData(OrientGraph graph, String indexName) {
System.out.println("Index data: " + indexName);
Iterable<ODocument> indexDataDocs = graph.getRawGraph().query(
new OSQLSynchQuery<ODocument>("select from index:" + indexName));
for(ODocument indexDataDoc : indexDataDocs) {
System.out.println("key: " + indexDataDoc.field("key") + ", rid: " + indexDataDoc.field("rid"));
}
System.out.println("");
}
}