use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class InMemoryConfigurationTest method testReadOnly.
@Test
public void testReadOnly() {
initialize(GraphDatabaseConfiguration.STORAGE_READONLY, true);
JanusGraphTransaction tx = graph.newTransaction();
try {
tx.addVertex();
fail();
} catch (Exception ignored) {
} finally {
tx.rollback();
}
try {
graph.addVertex();
fail();
} catch (Exception ignored) {
} finally {
graph.tx().rollback();
}
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class GraphOfTheGodsFactory method load.
public static void load(final JanusGraph graph, String mixedIndexName, boolean uniqueNameCompositeIndex) {
if (graph instanceof StandardJanusGraph) {
Preconditions.checkState(mixedIndexNullOrExists((StandardJanusGraph) graph, mixedIndexName), ERR_NO_INDEXING_BACKEND, mixedIndexName);
}
// Create Schema
JanusGraphManagement management = graph.openManagement();
final PropertyKey name = management.makePropertyKey("name").dataType(String.class).make();
JanusGraphManagement.IndexBuilder nameIndexBuilder = management.buildIndex("name", Vertex.class).addKey(name);
if (uniqueNameCompositeIndex)
nameIndexBuilder.unique();
JanusGraphIndex nameIndex = nameIndexBuilder.buildCompositeIndex();
management.setConsistency(nameIndex, ConsistencyModifier.LOCK);
final PropertyKey age = management.makePropertyKey("age").dataType(Integer.class).make();
if (null != mixedIndexName)
management.buildIndex("vertices", Vertex.class).addKey(age).buildMixedIndex(mixedIndexName);
final PropertyKey time = management.makePropertyKey("time").dataType(Integer.class).make();
final PropertyKey reason = management.makePropertyKey("reason").dataType(String.class).make();
final PropertyKey place = management.makePropertyKey("place").dataType(Geoshape.class).make();
if (null != mixedIndexName)
management.buildIndex("edges", Edge.class).addKey(reason).addKey(place).buildMixedIndex(mixedIndexName);
management.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
management.makeEdgeLabel("mother").multiplicity(Multiplicity.MANY2ONE).make();
EdgeLabel battled = management.makeEdgeLabel("battled").signature(time).make();
management.buildEdgeIndex(battled, "battlesByTime", Direction.BOTH, Order.decr, time);
management.makeEdgeLabel("lives").signature(reason).make();
management.makeEdgeLabel("pet").make();
management.makeEdgeLabel("brother").make();
management.makeVertexLabel("titan").make();
management.makeVertexLabel("location").make();
management.makeVertexLabel("god").make();
management.makeVertexLabel("demigod").make();
management.makeVertexLabel("human").make();
management.makeVertexLabel("monster").make();
management.commit();
JanusGraphTransaction tx = graph.newTransaction();
// vertices
Vertex saturn = tx.addVertex(T.label, "titan", "name", "saturn", "age", 10000);
Vertex sky = tx.addVertex(T.label, "location", "name", "sky");
Vertex sea = tx.addVertex(T.label, "location", "name", "sea");
Vertex jupiter = tx.addVertex(T.label, "god", "name", "jupiter", "age", 5000);
Vertex neptune = tx.addVertex(T.label, "god", "name", "neptune", "age", 4500);
Vertex hercules = tx.addVertex(T.label, "demigod", "name", "hercules", "age", 30);
Vertex alcmene = tx.addVertex(T.label, "human", "name", "alcmene", "age", 45);
Vertex pluto = tx.addVertex(T.label, "god", "name", "pluto", "age", 4000);
Vertex nemean = tx.addVertex(T.label, "monster", "name", "nemean");
Vertex hydra = tx.addVertex(T.label, "monster", "name", "hydra");
Vertex cerberus = tx.addVertex(T.label, "monster", "name", "cerberus");
Vertex tartarus = tx.addVertex(T.label, "location", "name", "tartarus");
// edges
jupiter.addEdge("father", saturn);
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
jupiter.addEdge("brother", neptune);
jupiter.addEdge("brother", pluto);
neptune.addEdge("lives", sea).property("reason", "loves waves");
neptune.addEdge("brother", jupiter);
neptune.addEdge("brother", pluto);
hercules.addEdge("father", jupiter);
hercules.addEdge("mother", alcmene);
hercules.addEdge("battled", nemean, "time", 1, "place", Geoshape.point(38.1f, 23.7f));
hercules.addEdge("battled", hydra, "time", 2, "place", Geoshape.point(37.7f, 23.9f));
hercules.addEdge("battled", cerberus, "time", 12, "place", Geoshape.point(39f, 22f));
pluto.addEdge("brother", jupiter);
pluto.addEdge("brother", neptune);
pluto.addEdge("lives", tartarus, "reason", "no fear of death");
pluto.addEdge("pet", cerberus);
cerberus.addEdge("lives", tartarus);
// commit the transaction to disk
tx.commit();
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class HasStepFolder method validJanusGraphOrder.
static boolean validJanusGraphOrder(OrderGlobalStep orderGlobalStep, Traversal rootTraversal, boolean isVertexOrder) {
final List<Pair<Traversal.Admin, Object>> comparators = orderGlobalStep.getComparators();
for (Pair<Traversal.Admin, Object> comp : comparators) {
if (comp.getValue0() instanceof ElementValueTraversal && comp.getValue1() instanceof Order) {
final String key = ((ElementValueTraversal) comp.getValue0()).getPropertyKey();
final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin());
final PropertyKey pKey = tx.getPropertyKey(key);
if (pKey == null || !(Comparable.class.isAssignableFrom(pKey.dataType())) || (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) {
return false;
}
} else {
// do not fold comparators that include nested traversals that are not simple ElementValues
return false;
}
}
return true;
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphTraversalUtil method getTx.
public static JanusGraphTransaction getTx(Traversal.Admin<?, ?> traversal) {
final JanusGraphTransaction tx;
Optional<Graph> optGraph = TraversalHelper.getRootTraversal(traversal.asAdmin()).getGraph();
if (traversal instanceof FulgoraElementTraversal) {
tx = (JanusGraphTransaction) optGraph.get();
} else {
if (!optGraph.isPresent())
throw new IllegalArgumentException("Traversal is not bound to a graph: " + traversal);
Graph graph = optGraph.get();
if (graph instanceof JanusGraphTransaction)
tx = (JanusGraphTransaction) graph;
else if (graph instanceof JanusGraphBlueprintsGraph)
tx = ((JanusGraphBlueprintsGraph) graph).getCurrentThreadTx();
else
throw new IllegalArgumentException("Traversal is not bound to a JanusGraph Graph, but: " + graph);
}
if (tx == null)
throw new IllegalArgumentException("Not a valid start step for a JanusGraph traversal: " + traversal);
if (tx.isOpen())
return tx;
else
return ((StandardJanusGraphTx) tx).getNextTx();
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphTest method failTransactionOnCommit.
private void failTransactionOnCommit(final TransactionJob job) {
final JanusGraphTransaction tx = graph.newTransaction();
try {
job.run(tx);
tx.commit();
fail();
} catch (Exception e) {
// e.printStackTrace();
} finally {
if (tx.isOpen())
tx.rollback();
}
}
Aggregations