Search in sources :

Example 1 with JanusGraph

use of org.janusgraph.core.JanusGraph in project grakn by graknlabs.

the class TxFactoryJanusTest method testSingleton.

@Test
public void testSingleton() {
    when(session.keyspace()).thenReturn(Keyspace.of("anothertest"));
    TxFactoryJanus factory = new TxFactoryJanus(session);
    GraknTxJanus mg1 = factory.open(GraknTxType.BATCH);
    JanusGraph tinkerGraphMg1 = mg1.getTinkerPopGraph();
    mg1.close();
    GraknTxJanus mg2 = factory.open(GraknTxType.WRITE);
    JanusGraph tinkerGraphMg2 = mg2.getTinkerPopGraph();
    mg2.close();
    GraknTxJanus mg3 = factory.open(GraknTxType.BATCH);
    assertEquals(mg1, mg3);
    assertEquals(tinkerGraphMg1, mg3.getTinkerPopGraph());
    assertTrue(mg1.isBatchTx());
    assertFalse(mg2.isBatchTx());
    assertNotEquals(mg1, mg2);
    assertNotEquals(tinkerGraphMg1, tinkerGraphMg2);
    StandardJanusGraph standardJanusGraph1 = (StandardJanusGraph) tinkerGraphMg1;
    StandardJanusGraph standardJanusGraph2 = (StandardJanusGraph) tinkerGraphMg2;
    assertTrue(standardJanusGraph1.getConfiguration().isBatchLoading());
    assertFalse(standardJanusGraph2.getConfiguration().isBatchLoading());
}
Also used : GraknTxJanus(ai.grakn.kb.internal.GraknTxJanus) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraph(org.janusgraph.core.JanusGraph) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Test(org.junit.Test)

Example 2 with JanusGraph

use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.

the class JanusGraphTest method setAndCheckGraphOption.

private <A> void setAndCheckGraphOption(ConfigOption<A> opt, ConfigOption.Type requiredType, A firstValue, A secondValue) {
    // Sanity check: make sure the Type of the configuration option is what we expect
    Preconditions.checkState(opt.getType().equals(requiredType));
    final EnumSet<ConfigOption.Type> allowedTypes = EnumSet.of(ConfigOption.Type.GLOBAL, ConfigOption.Type.GLOBAL_OFFLINE, ConfigOption.Type.MASKABLE);
    Preconditions.checkState(allowedTypes.contains(opt.getType()));
    // Sanity check: it's kind of pointless for the first and second values to be identical
    Preconditions.checkArgument(!firstValue.equals(secondValue));
    // Get full string path of config option
    final String path = ConfigElement.getPath(opt);
    // Set and check initial value before and after database restart
    mgmt.set(path, firstValue);
    assertEquals(firstValue.toString(), mgmt.get(path));
    // Close open tx first.  This is specific to BDB + GLOBAL_OFFLINE.
    // Basically: the BDB store manager throws a fit if shutdown is called
    // with one or more transactions still open, and GLOBAL_OFFLINE calls
    // shutdown on our behalf when we commit this change.
    tx.rollback();
    mgmt.commit();
    clopen();
    // Close tx again following clopen
    tx.rollback();
    assertEquals(firstValue.toString(), mgmt.get(path));
    // Set and check updated value before and after database restart
    mgmt.set(path, secondValue);
    assertEquals(secondValue.toString(), mgmt.get(path));
    mgmt.commit();
    clopen();
    tx.rollback();
    assertEquals(secondValue.toString(), mgmt.get(path));
    // Open a separate graph "g2"
    JanusGraph g2 = JanusGraphFactory.open(config);
    JanusGraphManagement m2 = g2.openManagement();
    assertEquals(secondValue.toString(), m2.get(path));
    // GLOBAL_OFFLINE options should be unmodifiable with g2 open
    if (opt.getType().equals(ConfigOption.Type.GLOBAL_OFFLINE)) {
        try {
            mgmt.set(path, firstValue);
            mgmt.commit();
            fail("Option " + path + " with type " + ConfigOption.Type.GLOBAL_OFFLINE + " should not be modifiable with concurrent instances");
        } catch (RuntimeException e) {
            log.debug("Caught expected exception", e);
        }
        assertEquals(secondValue.toString(), mgmt.get(path));
    // GLOBAL and MASKABLE should be modifiable even with g2 open
    } else {
        mgmt.set(path, firstValue);
        assertEquals(firstValue.toString(), mgmt.get(path));
        mgmt.commit();
        clopen();
        assertEquals(firstValue.toString(), mgmt.get(path));
    }
    m2.rollback();
    g2.close();
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) JanusGraphSchemaType(org.janusgraph.core.schema.JanusGraphSchemaType) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraph(org.janusgraph.core.JanusGraph)

Example 3 with JanusGraph

use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.

the class JanusGraphStepStrategyTest method generateTestParameters.

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> generateTestParameters() {
    final StandardJanusGraph graph = (StandardJanusGraph) StorageSetup.getInMemoryGraph();
    final GraphTraversalSource g = graph.traversal();
    // create a basic schema so that order pushdown can be tested as this optimization requires a JanusGraph
    // transaction registered against a non-EmptyGraph
    final JanusGraphManagement mgmt = graph.openManagement();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makePropertyKey("lang").dataType(String.class).make();
    mgmt.commit();
    return Arrays.asList(new Object[][] { { g.V().out(), g_V().out(), Collections.emptyList() }, { g.V().has("name", "marko").out(), g_V("name", eq("marko")).out(), Collections.emptyList() }, { g.V().has("name", "marko").has("age", gt(31).and(lt(10))).out(), g_V("name", eq("marko"), "age", gt(31), "age", lt(10)).out(), Collections.emptyList() }, { g.V().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko")).as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), Collections.emptyList() }, { g.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("a"), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup(), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup().as("a"), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().as("a").has("name", "marko").as("b").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("b", "a"), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob")).or(has("age"), has("age", gt(32))).dedup().as("a"), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance()) }, { g.V().has("name", "marko").or(not(has("age")), has("age", gt(32))).has("name", "bob").has("lang", "java"), g_V("name", eq("marko"), "name", eq("bob"), "lang", eq("java")).or(not(filter(properties("age"))), has("age", gt(32))), TraversalStrategies.GlobalCache.getStrategies(JanusGraph.class).toList() }, { g.V().has("name", eq("marko").and(eq("bob").and(eq("stephen")))).out("knows"), g_V("name", eq("marko"), "name", eq("bob"), "name", eq("stephen")).out("knows"), Collections.emptyList() }, { g.V().hasId(1), g_V(T.id, 1), Collections.emptyList() }, { g.V().hasId(within(1, 2)), g_V(T.id, 1, T.id, 2), Collections.emptyList() }, { g.V().hasId(1).has("name", "marko"), g_V(T.id, 1, "name", eq("marko")), Collections.emptyList() }, { g.V().hasId(1).hasLabel("Person"), g_V(T.id, 1, "~label", eq("Person")), Collections.emptyList() }, { g.V().hasLabel("Person").has("lang", "java").order().by("name"), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.incr)), Collections.emptyList() }, // same as above, different order
    { g.V().hasLabel("Person").has("lang", "java").order().by("name", Order.decr), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.decr)), Collections.emptyList() }, // if multiple order steps are specified in a row, only the last will be folded in because it overrides previous ordering
    { g.V().hasLabel("Person").has("lang", "java").order().by("lang", Order.incr).order().by("name", Order.decr), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.decr)), Collections.emptyList() }, // do not folder in orders that include a nested traversal
    { g.V().hasLabel("Person").order().by(values("age")), g_V("~label", eq("Person")).order().by(values("age")), Collections.emptyList() }, // age property is not registered in the schema so the order should not be folded in
    { g.V().hasLabel("Person").has("lang", "java").order().by("age"), g_V("~label", eq("Person"), "lang", eq("java")).order().by("age"), Collections.emptyList() }, // into a single within(ids) lookup
    { g.V().hasId(1).hasId(2), g_V(T.id, 1).hasId(2), Collections.emptyList() } });
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraph(org.janusgraph.core.JanusGraph) HasStepFolder(org.janusgraph.graphdb.tinkerpop.optimize.HasStepFolder) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 4 with JanusGraph

use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.

the class VertexIDAssignerTest method testCustomIdAssignment.

private void testCustomIdAssignment(CustomIdStrategy idStrategy) {
    LongSet vertexIds = new LongHashSet();
    final long maxCount = idAssigner.getIDManager().getVertexCountBound();
    long count = 1;
    for (int trial = 0; trial < 10; trial++) {
        final JanusGraph graph = getInMemoryGraph(true, true);
        int numVertices = 1000;
        final List<JanusGraphVertex> vertices = new ArrayList<>(numVertices);
        try {
            for (int i = 0; i < numVertices; i++, count++) {
                final long userVertexId;
                switch(idStrategy) {
                    case LOW:
                        userVertexId = count;
                        break;
                    case HIGH:
                        userVertexId = maxCount - count;
                        break;
                    default:
                        throw new RuntimeException("Unsupported custom id strategy: " + idStrategy);
                }
                final long id = idAssigner.getIDManager().toVertexId(userVertexId);
                JanusGraphVertex next = graph.addVertex(T.id, id, "user_id", userVertexId);
                vertices.add(next);
            }
            // Verify that ids are set, unique and consistent with user id basis
            for (JanusGraphVertex v : vertices) {
                assertTrue(v.hasId());
                long id = v.longId();
                assertTrue(id > 0 && id < Long.MAX_VALUE);
                assertTrue(vertexIds.add(id));
                assertEquals((long) v.value("user_id"), idAssigner.getIDManager().fromVertexId(id));
            }
        } finally {
            graph.tx().rollback();
            graph.close();
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ArrayList(java.util.ArrayList)

Example 5 with JanusGraph

use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.

the class JanusGraphAppTest method createSchema.

@Test
public void createSchema() throws ConfigurationException {
    final JanusGraphApp app = new JanusGraphApp(CONF_FILE);
    final GraphTraversalSource g = app.openGraph();
    app.createSchema();
    final JanusGraph janusGraph = (JanusGraph) g.getGraph();
    final JanusGraphManagement management = janusGraph.openManagement();
    final List<String> vertexLabels = StreamSupport.stream(management.getVertexLabels().spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
    final List<String> expectedVertexLabels = Stream.of("titan", "location", "god", "demigod", "human", "monster").collect(Collectors.toList());
    assertTrue(vertexLabels.containsAll(expectedVertexLabels));
    final List<String> edgeLabels = StreamSupport.stream(management.getRelationTypes(EdgeLabel.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
    final List<String> expectedEdgeLabels = Stream.of("father", "mother", "brother", "pet", "lives", "battled").collect(Collectors.toList());
    assertTrue(edgeLabels.containsAll(expectedEdgeLabels));
    final EdgeLabel father = management.getEdgeLabel("father");
    assertTrue(father.isDirected());
    assertFalse(father.isUnidirected());
    assertEquals(Multiplicity.MANY2ONE, father.multiplicity());
    final List<String> propertyKeys = StreamSupport.stream(management.getRelationTypes(PropertyKey.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
    final List<String> expectedPropertyKeys = Stream.of("name", "age", "time", "place", "reason").collect(Collectors.toList());
    assertTrue(propertyKeys.containsAll(expectedPropertyKeys));
    final PropertyKey place = management.getPropertyKey("place");
    assertEquals(Cardinality.SINGLE, place.cardinality());
    assertEquals(Geoshape.class, place.dataType());
    final JanusGraphIndex nameIndex = management.getGraphIndex("nameIndex");
    assertTrue(nameIndex.isCompositeIndex());
    assertTrue(nameIndex.getIndexedElement().equals(JanusGraphVertex.class));
    final PropertyKey[] nameIndexKeys = nameIndex.getFieldKeys();
    assertEquals(1, nameIndexKeys.length);
    assertEquals("name", nameIndexKeys[0].name());
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Aggregations

JanusGraph (org.janusgraph.core.JanusGraph)38 Test (org.junit.Test)23 HashMap (java.util.HashMap)20 CredentialGraph (org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph)20 ManagementSystem (org.janusgraph.graphdb.database.management.ManagementSystem)15 Transaction (org.apache.tinkerpop.gremlin.structure.Transaction)14 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)12 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)9 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)7 SimpleAuthenticator (org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator)5 PropertyKey (org.janusgraph.core.PropertyKey)5 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)5 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)4 PropertyKeyMaker (org.janusgraph.core.schema.PropertyKeyMaker)3 LongHashSet (com.carrotsearch.hppc.LongHashSet)2 LongSet (com.carrotsearch.hppc.LongSet)2 ArrayList (java.util.ArrayList)2 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 AuthenticationException (org.apache.tinkerpop.gremlin.server.auth.AuthenticationException)2 Graph (org.apache.tinkerpop.gremlin.structure.Graph)2