Search in sources :

Example 26 with JanusGraphVertex

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

the class JanusGraphTest method testIndexQueryWithLabelsAndContainsIN.

@Test
public void testIndexQueryWithLabelsAndContainsIN() {
    // This test is based on the steps to reproduce #882
    String labelName = "labelName";
    VertexLabel label = mgmt.makeVertexLabel(labelName).make();
    PropertyKey uid = mgmt.makePropertyKey("uid").dataType(String.class).make();
    JanusGraphIndex uidCompositeIndex = mgmt.buildIndex("uidIndex", Vertex.class).indexOnly(label).addKey(uid).unique().buildCompositeIndex();
    mgmt.setConsistency(uidCompositeIndex, ConsistencyModifier.LOCK);
    finishSchema();
    JanusGraphVertex foo = graph.addVertex(labelName);
    JanusGraphVertex bar = graph.addVertex(labelName);
    foo.property("uid", "foo");
    bar.property("uid", "bar");
    graph.tx().commit();
    Iterable<JanusGraphVertex> vertexes = graph.query().has("uid", Contain.IN, ImmutableList.of("foo", "bar")).has(LABEL_NAME, labelName).vertices();
    assertEquals(2, Iterables.size(vertexes));
    for (JanusGraphVertex v : vertexes) {
        assertEquals(labelName, v.vertexLabel().name());
    }
}
Also used : VertexLabel(org.janusgraph.core.VertexLabel) BaseVertexLabel(org.janusgraph.graphdb.types.system.BaseVertexLabel) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 27 with JanusGraphVertex

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

the class JanusGraphTest method testEdgeTTLWithIndex.

@Category({ BrittleTests.class })
@Test
public void testEdgeTTLWithIndex() throws Exception {
    if (!features.hasCellTTL()) {
        return;
    }
    // artificially low TTL for test
    int ttl = 1;
    final PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).make();
    EdgeLabel wavedAt = mgmt.makeEdgeLabel("wavedAt").signature(time).make();
    mgmt.buildEdgeIndex(wavedAt, "timeindex", Direction.BOTH, decr, time);
    mgmt.buildIndex("edge-time", Edge.class).addKey(time).buildCompositeIndex();
    mgmt.setTTL(wavedAt, Duration.ofSeconds(ttl));
    assertEquals(Duration.ZERO, mgmt.getTTL(time));
    assertEquals(Duration.ofSeconds(ttl), mgmt.getTTL(wavedAt));
    mgmt.commit();
    JanusGraphVertex v1 = graph.addVertex(), v2 = graph.addVertex();
    v1.addEdge("wavedAt", v2, "time", 42);
    assertTrue(v1.query().direction(Direction.OUT).interval("time", 0, 100).edges().iterator().hasNext());
    assertNotEmpty(v1.query().direction(Direction.OUT).edges());
    assertNotEmpty(graph.query().has("time", 42).edges());
    graph.tx().commit();
    long commitTime = System.currentTimeMillis();
    assertTrue(v1.query().direction(Direction.OUT).interval("time", 0, 100).edges().iterator().hasNext());
    assertNotEmpty(v1.query().direction(Direction.OUT).edges());
    assertNotEmpty(graph.query().has("time", 42).edges());
    Thread.sleep(commitTime + (ttl * 1000L + 100) - System.currentTimeMillis());
    graph.tx().rollback();
    assertFalse(v1.query().direction(Direction.OUT).interval("time", 0, 100).edges().iterator().hasNext());
    assertEmpty(v1.query().direction(Direction.OUT).edges());
    assertEmpty(graph.query().has("time", 42).edges());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) PropertyKey(org.janusgraph.core.PropertyKey) Category(org.junit.experimental.categories.Category) RelationCategory(org.janusgraph.graphdb.internal.RelationCategory) ElementCategory(org.janusgraph.graphdb.internal.ElementCategory) Test(org.junit.Test)

Example 28 with JanusGraphVertex

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

the class JanusGraphTest method testAutomaticTypeCreation.

/**
 * Tests the automatic creation of types
 */
@Test
public void testAutomaticTypeCreation() {
    assertFalse(tx.containsVertexLabel("person"));
    assertFalse(tx.containsVertexLabel("person"));
    assertFalse(tx.containsRelationType("value"));
    assertNull(tx.getPropertyKey("value"));
    PropertyKey value = tx.getOrCreatePropertyKey("value");
    assertNotNull(value);
    assertTrue(tx.containsRelationType("value"));
    JanusGraphVertex v = tx.addVertex("person");
    assertTrue(tx.containsVertexLabel("person"));
    assertEquals("person", v.label());
    assertFalse(tx.containsRelationType("knows"));
    Edge e = v.addEdge("knows", v);
    assertTrue(tx.containsRelationType("knows"));
    assertNotNull(tx.getEdgeLabel(e.label()));
    clopen(option(AUTO_TYPE), "none");
    assertTrue(tx.containsRelationType("value"));
    assertTrue(tx.containsVertexLabel("person"));
    assertTrue(tx.containsRelationType("knows"));
    v = getV(tx, v);
    // Cannot create new labels
    try {
        tx.addVertex("org");
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        v.property("bla", 5);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        v.addEdge("blub", v);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 29 with JanusGraphVertex

use of org.janusgraph.core.JanusGraphVertex 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 30 with JanusGraphVertex

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

the class JanusGraphHadoopSetupImpl method getTypeInspector.

@Override
public TypeInspector getTypeInspector() {
    // Pre-load schema
    for (JanusGraphSchemaCategory sc : JanusGraphSchemaCategory.values()) {
        for (JanusGraphVertex k : QueryUtil.getVertices(tx, BaseKey.SchemaCategory, sc)) {
            assert k instanceof JanusGraphSchemaVertex;
            JanusGraphSchemaVertex s = (JanusGraphSchemaVertex) k;
            if (sc.hasName()) {
                String name = s.name();
                Preconditions.checkNotNull(name);
            }
            TypeDefinitionMap dm = s.getDefinition();
            Preconditions.checkNotNull(dm);
            s.getRelated(TypeDefinitionCategory.TYPE_MODIFIER, Direction.OUT);
            s.getRelated(TypeDefinitionCategory.TYPE_MODIFIER, Direction.IN);
        }
    }
    return tx;
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) JanusGraphSchemaCategory(org.janusgraph.graphdb.internal.JanusGraphSchemaCategory) TypeDefinitionMap(org.janusgraph.graphdb.types.TypeDefinitionMap)

Aggregations

JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)87 Test (org.junit.Test)72 PropertyKey (org.janusgraph.core.PropertyKey)54 EdgeLabel (org.janusgraph.core.EdgeLabel)20 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)18 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)18 Edge (org.apache.tinkerpop.gremlin.structure.Edge)17 VertexLabel (org.janusgraph.core.VertexLabel)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)13 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)12 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)11 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)8 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)8 Instant (java.time.Instant)5 ArrayList (java.util.ArrayList)5 Random (java.util.Random)5 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)5 JanusGraph (org.janusgraph.core.JanusGraph)5