Search in sources :

Example 71 with PropertyKey

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

the class JanusGraphPerformanceMemoryTest method testTransactionalMemory.

@Test
public void testTransactionalMemory() throws Exception {
    makeVertexIndexedUniqueKey("uid", Long.class);
    makeKey("name", String.class);
    PropertyKey time = makeKey("time", Integer.class);
    mgmt.makeEdgeLabel("friend").signature(time).directed().make();
    finishSchema();
    final Random random = new Random();
    final int rounds = 100;
    final int commitSize = 1500;
    final AtomicInteger uidCounter = new AtomicInteger(0);
    Thread[] writeThreads = new Thread[4];
    long start = System.currentTimeMillis();
    for (int t = 0; t < writeThreads.length; t++) {
        writeThreads[t] = new Thread(() -> {
            for (int r = 0; r < rounds; r++) {
                JanusGraphTransaction tx = graph.newTransaction();
                JanusGraphVertex previous = null;
                for (int c = 0; c < commitSize; c++) {
                    JanusGraphVertex v = tx.addVertex();
                    long uid = uidCounter.incrementAndGet();
                    v.property(VertexProperty.Cardinality.single, "uid", uid);
                    v.property(VertexProperty.Cardinality.single, "name", "user" + uid);
                    if (previous != null) {
                        v.addEdge("friend", previous, "time", Math.abs(random.nextInt()));
                    }
                    previous = v;
                }
                tx.commit();
            }
        });
        writeThreads[t].start();
    }
    for (final Thread writeThread : writeThreads) {
        writeThread.join();
    }
    System.out.println("Write time for " + (rounds * commitSize * writeThreads.length) + " vertices & edges: " + (System.currentTimeMillis() - start));
    final int maxUID = uidCounter.get();
    final int trials = 1000;
    final String fixedName = "john";
    Thread[] readThreads = new Thread[Runtime.getRuntime().availableProcessors() * 2];
    start = System.currentTimeMillis();
    for (int t = 0; t < readThreads.length; t++) {
        readThreads[t] = new Thread(() -> {
            JanusGraphTransaction tx = graph.newTransaction();
            long randomUniqueId = random.nextInt(maxUID) + 1;
            getVertex(tx, "uid", randomUniqueId).property(VertexProperty.Cardinality.single, "name", fixedName);
            for (int t1 = 1; t1 <= trials; t1++) {
                JanusGraphVertex v = getVertex(tx, "uid", random.nextInt(maxUID) + 1);
                assertCount(2, v.properties());
                int count = 0;
                for (Object e : v.query().direction(Direction.BOTH).edges()) {
                    count++;
                    assertTrue(((JanusGraphEdge) e).<Integer>value("time") >= 0);
                }
                assertTrue(count <= 2);
            // if (t%(trials/10)==0) System.out.println(t);
            }
            assertEquals(fixedName, getVertex(tx, "uid", randomUniqueId).value("name"));
            tx.commit();
        });
        readThreads[t].start();
    }
    for (final Thread readThread : readThreads) {
        readThread.join();
    }
    System.out.println("Read time for " + (trials * readThreads.length) + " vertex lookups: " + (System.currentTimeMillis() - start));
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 72 with PropertyKey

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

the class JanusGraphTest method testVertexTTLWithCompositeIndex.

@Test
public void testVertexTTLWithCompositeIndex() throws Exception {
    if (!features.hasCellTTL()) {
        return;
    }
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Long.class).make();
    final JanusGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
    final JanusGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).addKey(name).addKey(time).buildCompositeIndex();
    VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make();
    mgmt.setTTL(label1, Duration.ofSeconds(1));
    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ZERO, mgmt.getTTL(time));
    assertEquals(Duration.ofSeconds(1), mgmt.getTTL(label1));
    mgmt.commit();
    JanusGraphVertex v1 = tx.addVertex(T.label, "event", "name", "some event", "time", System.currentTimeMillis());
    tx.commit();
    Object id = v1.id();
    v1 = getV(graph, id);
    assertNotNull(v1);
    assertNotEmpty(graph.query().has("name", "some event").vertices());
    Thread.sleep(1001);
    graph.tx().rollback();
    v1 = getV(graph, id);
    assertNull(v1);
    assertEmpty(graph.query().has("name", "some event").vertices());
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) 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 73 with PropertyKey

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

the class JanusGraphTest method testBasic.

/**
 * Very simple graph operation to ensure minimal functionality and cleanup
 */
@Test
public void testBasic() throws BackendException {
    PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class);
    finishSchema();
    JanusGraphVertex n1 = tx.addVertex();
    uid = tx.getPropertyKey("name");
    n1.property(uid.name(), "abcd");
    clopen();
    long nid = n1.longId();
    uid = tx.getPropertyKey("name");
    assertTrue(getV(tx, nid) != null);
    assertTrue(getV(tx, uid.longId()) != null);
    assertMissing(tx, nid + 64);
    uid = tx.getPropertyKey(uid.name());
    n1 = getV(tx, nid);
    assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd")));
    // TODO: how to expose relations?
    assertEquals(1, Iterables.size(n1.query().relations()));
    assertEquals("abcd", n1.value(uid.name()));
    assertCount(1, tx.query().vertices());
    close();
    JanusGraphFactory.drop(graph);
    open(config);
    assertEmpty(tx.query().vertices());
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 74 with PropertyKey

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

the class JanusGraphTest method testWithoutIndex.

@Test
public void testWithoutIndex() {
    PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makeEdgeLabel("knows").signature(kid).make();
    finishSchema();
    Random random = new Random();
    int numV = 1000;
    JanusGraphVertex previous = null;
    for (int i = 0; i < numV; i++) {
        JanusGraphVertex v = graph.addVertex("kid", random.nextInt(numV), "name", "v" + i);
        if (previous != null) {
            Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2));
        }
        previous = v;
    }
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
    clopen();
    // Copied from above
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
}
Also used : Random(java.util.Random) 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 75 with PropertyKey

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

the class JanusGraphTest method testDataTypes.

/**
 * Test the different data types that JanusGraph supports natively and ensure that invalid data types aren't allowed
 */
@Test
public void testDataTypes() {
    clopen(option(CUSTOM_ATTRIBUTE_CLASS, "attribute10"), SpecialInt.class.getCanonicalName(), option(CUSTOM_SERIALIZER_CLASS, "attribute10"), SpecialIntSerializer.class.getCanonicalName());
    PropertyKey num = makeKey("num", SpecialInt.class);
    PropertyKey barr = makeKey("barr", byte[].class);
    PropertyKey booleanValue = makeKey("boolval", Boolean.class);
    PropertyKey birthday = makeKey("birthday", Instant.class);
    PropertyKey location = makeKey("location", Geoshape.class);
    PropertyKey boundary = makeKey("boundary", Geoshape.class);
    PropertyKey precise = makeKey("precise", Double.class);
    PropertyKey any = mgmt.makePropertyKey("any").cardinality(Cardinality.LIST).dataType(Object.class).make();
    try {
        // Not a valid data type - primitive
        makeKey("pint", int.class);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        // Not a valid data type - interface
        makeKey("number", Number.class);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    finishSchema();
    clopen();
    booleanValue = tx.getPropertyKey("boolval");
    num = tx.getPropertyKey("num");
    barr = tx.getPropertyKey("barr");
    birthday = tx.getPropertyKey("birthday");
    location = tx.getPropertyKey("location");
    boundary = tx.getPropertyKey("boundary");
    precise = tx.getPropertyKey("precise");
    any = tx.getPropertyKey("any");
    assertEquals(Boolean.class, booleanValue.dataType());
    assertEquals(byte[].class, barr.dataType());
    assertEquals(Object.class, any.dataType());
    final Instant c = Instant.ofEpochSecond(1429225756);
    final Geoshape point = Geoshape.point(10.0, 10.0);
    final Geoshape shape = Geoshape.box(10.0, 10.0, 20.0, 20.0);
    JanusGraphVertex v = tx.addVertex();
    v.property(n(booleanValue), true);
    v.property(VertexProperty.Cardinality.single, n(birthday), c);
    v.property(VertexProperty.Cardinality.single, n(num), new SpecialInt(10));
    v.property(VertexProperty.Cardinality.single, n(barr), new byte[] { 1, 2, 3, 4 });
    v.property(VertexProperty.Cardinality.single, n(location), point);
    v.property(VertexProperty.Cardinality.single, n(boundary), shape);
    v.property(VertexProperty.Cardinality.single, n(precise), 10.12345);
    v.property(n(any), "Hello");
    v.property(n(any), 10L);
    int[] testArray = { 5, 6, 7 };
    v.property(n(any), testArray);
    // ######## VERIFICATION ##########
    assertTrue(v.<Boolean>value("boolval"));
    assertEquals(10, v.<SpecialInt>value("num").getValue());
    assertEquals(c, v.value("birthday"));
    assertEquals(4, v.<byte[]>value("barr").length);
    assertEquals(point, v.<Geoshape>value("location"));
    assertEquals(shape, v.<Geoshape>value("boundary"));
    assertEquals(10.12345, v.<Double>value("precise"), 0.000001);
    assertCount(3, v.properties("any"));
    for (Object prop : v.query().labels("any").properties()) {
        Object value = ((JanusGraphVertexProperty<?>) prop).value();
        if (value instanceof String)
            assertEquals("Hello", value);
        else if (value instanceof Long)
            assertEquals(10L, value);
        else if (value.getClass().isArray()) {
            assertTrue(Arrays.equals(testArray, (int[]) value));
        } else
            fail();
    }
    clopen();
    v = getV(tx, v);
    // ######## VERIFICATION (copied from above) ##########
    assertTrue(v.<Boolean>value("boolval"));
    assertEquals(10, v.<SpecialInt>value("num").getValue());
    assertEquals(c, v.value("birthday"));
    assertEquals(4, v.<byte[]>value("barr").length);
    assertEquals(point, v.<Geoshape>value("location"));
    assertEquals(shape, v.<Geoshape>value("boundary"));
    assertEquals(10.12345, v.<Double>value("precise"), 0.000001);
    assertCount(3, v.properties("any"));
    for (Object prop : v.query().labels("any").properties()) {
        Object value = ((JanusGraphVertexProperty<?>) prop).value();
        if (value instanceof String)
            assertEquals("Hello", value);
        else if (value instanceof Long)
            assertEquals(10L, value);
        else if (value.getClass().isArray()) {
            assertTrue(Arrays.equals(testArray, (int[]) value));
        } else
            fail();
    }
}
Also used : SpecialIntSerializer(org.janusgraph.graphdb.serializer.SpecialIntSerializer) SpecialInt(org.janusgraph.graphdb.serializer.SpecialInt) Instant(java.time.Instant) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Geoshape(org.janusgraph.core.attribute.Geoshape) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Aggregations

PropertyKey (org.janusgraph.core.PropertyKey)84 Test (org.junit.Test)59 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)57 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)28 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)28 EdgeLabel (org.janusgraph.core.EdgeLabel)21 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Edge (org.apache.tinkerpop.gremlin.structure.Edge)14 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)13 VertexLabel (org.janusgraph.core.VertexLabel)13 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)11 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)9 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)9 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)8 JanusGraph (org.janusgraph.core.JanusGraph)8 JanusGraphException (org.janusgraph.core.JanusGraphException)8 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)8 Instant (java.time.Instant)6 ExecutionException (java.util.concurrent.ExecutionException)6