Search in sources :

Example 6 with JanusGraphTransaction

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();
    }
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Test(org.junit.Test)

Example 7 with JanusGraphTransaction

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();
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Geoshape(org.janusgraph.core.attribute.Geoshape) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 8 with JanusGraphTransaction

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;
}
Also used : Order(org.apache.tinkerpop.gremlin.process.traversal.Order) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) PropertyKey(org.janusgraph.core.PropertyKey) Pair(org.javatuples.Pair)

Example 9 with JanusGraphTransaction

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();
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Graph(org.apache.tinkerpop.gremlin.structure.Graph) JanusGraphBlueprintsGraph(org.janusgraph.graphdb.tinkerpop.JanusGraphBlueprintsGraph) FulgoraElementTraversal(org.janusgraph.graphdb.olap.computer.FulgoraElementTraversal) JanusGraphBlueprintsGraph(org.janusgraph.graphdb.tinkerpop.JanusGraphBlueprintsGraph)

Example 10 with JanusGraphTransaction

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();
    }
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphException(org.janusgraph.core.JanusGraphException) ExecutionException(java.util.concurrent.ExecutionException) JanusGraphConfigurationException(org.janusgraph.core.JanusGraphConfigurationException) SchemaViolationException(org.janusgraph.core.SchemaViolationException) BackendException(org.janusgraph.diskstorage.BackendException)

Aggregations

JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)20 Test (org.junit.Test)10 PropertyKey (org.janusgraph.core.PropertyKey)8 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)7 EdgeLabel (org.janusgraph.core.EdgeLabel)4 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)4 JanusGraphException (org.janusgraph.core.JanusGraphException)4 SchemaViolationException (org.janusgraph.core.SchemaViolationException)4 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)4 Random (java.util.Random)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 JanusGraphConfigurationException (org.janusgraph.core.JanusGraphConfigurationException)3 Geoshape (org.janusgraph.core.attribute.Geoshape)3 BackendException (org.janusgraph.diskstorage.BackendException)3 ElementCategory (org.janusgraph.graphdb.internal.ElementCategory)3 RelationCategory (org.janusgraph.graphdb.internal.RelationCategory)3 Category (org.junit.experimental.categories.Category)3 Instant (java.time.Instant)2 CountDownLatch (java.util.concurrent.CountDownLatch)2