Search in sources :

Example 61 with JanusGraphVertex

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

the class JanusGraphIndexTest method testSetUpdate.

@Test
public void testSetUpdate() {
    if (!indexFeatures.supportsCardinality(Cardinality.SET)) {
        return;
    }
    PropertyKey name = makeKey("name", String.class);
    PropertyKey alias = mgmt.makePropertyKey("alias").dataType(String.class).cardinality(Cardinality.SET).make();
    mgmt.buildIndex("namev", Vertex.class).addKey(name).addKey(alias, indexFeatures.supportsStringMapping(Mapping.TEXTSTRING) ? Mapping.TEXTSTRING.asParameter() : Mapping.DEFAULT.asParameter()).buildMixedIndex(INDEX);
    finishSchema();
    JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez");
    assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices());
    clopen();
    assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices());
    v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko"));
    v.property(VertexProperty.Cardinality.set, "alias", "Marko");
    assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices());
    clopen();
    assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices());
    v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko"));
    v.property(VertexProperty.Cardinality.set, "alias", "mRodriguez");
    assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices());
    clopen();
    assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices());
    assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices());
    if (indexFeatures.supportsStringMapping(Mapping.TEXTSTRING)) {
        assertCount(1, tx.query().has("alias", Cmp.EQUAL, "Marko").vertices());
        assertCount(1, tx.query().has("alias", Cmp.EQUAL, "mRodriguez").vertices());
    }
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 62 with JanusGraphVertex

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

the class JanusGraphIndexTest method testRawQueriesWithParameters.

/**
 * Tests query parameters with raw indexQuery
 */
@Test
public void testRawQueriesWithParameters() {
    if (!supportsLuceneStyleQueries())
        return;
    Parameter asc_sort_p = null;
    Parameter desc_sort_p = null;
    // ElasticSearch and Solr have different formats for sort parameters
    String backend = readConfig.get(INDEX_BACKEND, INDEX);
    switch(backend) {
        case "elasticsearch":
            final Map<String, String> sortAsc = new HashMap<>();
            sortAsc.put("_score", "asc");
            asc_sort_p = new Parameter("sort", Collections.singletonList(sortAsc));
            final Map<String, String> sortDesc = new HashMap<>();
            sortDesc.put("_score", "desc");
            desc_sort_p = new Parameter("sort", Collections.singletonList(sortDesc));
            break;
        case "solr":
            asc_sort_p = new Parameter("sort", new String[] { "score asc" });
            desc_sort_p = new Parameter("sort", new String[] { "score desc" });
            break;
        case "lucene":
            // Ignore for lucene
            return;
        default:
            Assert.fail("Unknown index backend:" + backend);
            break;
    }
    final PropertyKey field1Key = mgmt.makePropertyKey("field1").dataType(String.class).make();
    mgmt.buildIndex("store1", Vertex.class).addKey(field1Key).buildMixedIndex(INDEX);
    mgmt.commit();
    JanusGraphVertex v1 = tx.addVertex();
    JanusGraphVertex v2 = tx.addVertex();
    JanusGraphVertex v3 = tx.addVertex();
    v1.property("field1", "Hello Hello Hello Hello Hello Hello Hello Hello world");
    v2.property("field1", "Hello blue and yellow meet green");
    v3.property("field1", "Hello Hello world world");
    tx.commit();
    final List<JanusGraphVertex> vertices = graph.indexQuery("store1", "v.field1:(Hello)").addParameter(asc_sort_p).vertexStream().map(JanusGraphIndexQuery.Result::getElement).collect(Collectors.toList());
    assertNotEmpty(vertices);
    final AtomicInteger idx = new AtomicInteger(vertices.size() - 1);
    // Verify this query returns the items in reverse order.
    graph.indexQuery("store1", "v.field1:(Hello)").addParameter(desc_sort_p).vertexStream().map(JanusGraphIndexQuery.Result::getElement).forEachOrdered(e -> assertEquals(vertices.get(idx.getAndDecrement()), e));
}
Also used : HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphIndexQuery(org.janusgraph.core.JanusGraphIndexQuery) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Parameter(org.janusgraph.core.schema.Parameter) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 63 with JanusGraphVertex

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

the class JanusGraphIndexTest method testInstantIndexing.

/**
 * Tests indexing instants
 */
@Test
public void testInstantIndexing() {
    PropertyKey name = makeKey("instant", Instant.class);
    mgmt.buildIndex("instantIndex", Vertex.class).addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    clopen();
    Instant firstTimestamp = Instant.ofEpochMilli(1);
    Instant secondTimestamp = Instant.ofEpochMilli(2000);
    JanusGraphVertex v1 = graph.addVertex();
    v1.property("instant", firstTimestamp);
    JanusGraphVertex v2 = graph.addVertex();
    v2.property("instant", secondTimestamp);
    testInstant(firstTimestamp, secondTimestamp, v1, v2);
    firstTimestamp = Instant.ofEpochSecond(0, 1);
    v1 = (JanusGraphVertex) graph.vertices(v1.id()).next();
    v1.property("instant", firstTimestamp);
    if (indexFeatures.supportsNanoseconds()) {
        testInstant(firstTimestamp, secondTimestamp, v1, v2);
    } else {
        // Flush the index
        clopen();
        try {
            assertEquals(v1, getOnlyVertex(graph.query().has("instant", Cmp.EQUAL, firstTimestamp)));
            Assert.fail("Should have failed to update the index");
        } catch (Exception ignored) {
        }
    }
}
Also used : Instant(java.time.Instant) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) JanusGraphException(org.janusgraph.core.JanusGraphException) BackendException(org.janusgraph.diskstorage.BackendException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 64 with JanusGraphVertex

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

the class JanusGraphIndexTest method testSimpleUpdate.

@Test
public void testSimpleUpdate() {
    PropertyKey name = makeKey("name", String.class);
    makeLabel("knows");
    mgmt.buildIndex("namev", Vertex.class).addKey(name).buildMixedIndex(INDEX);
    mgmt.buildIndex("namee", Edge.class).addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez");
    Edge e = v.addEdge("knows", v, "name", "Hulu Bubab");
    assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices());
    assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges());
    for (Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name"));
    clopen();
    assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices());
    assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges());
    for (Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name"));
    v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko"));
    v.property(VertexProperty.Cardinality.single, "name", "Marko");
    e = getOnlyEdge(v.query().direction(Direction.OUT));
    e.property("name", "Tubu Rubu");
    assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices());
    assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges());
    assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges());
    for (Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name"));
    clopen();
    assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices());
    assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges());
    assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges());
    for (Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name"));
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 65 with JanusGraphVertex

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

the class JanusGraphIterativeBenchmark method loadData.

public void loadData(final int numVertices, final int numThreads) throws Exception {
    makeKey("w", Integer.class);
    PropertyKey time = makeKey("t", Long.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("l")).sortKey(time).make();
    finishSchema();
    final int maxQueue = 1000;
    final int verticesPerTask = 1000;
    final int maxWeight = 10;
    final int maxTime = 10000;
    final BlockingQueue<Runnable> tasks = new ArrayBlockingQueue<>(maxQueue);
    ExecutorService exe = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numVertices / verticesPerTask; i++) {
        while (tasks.size() >= maxQueue) Thread.sleep(maxQueue);
        assert tasks.size() < maxQueue;
        exe.submit(() -> {
            final JanusGraphTransaction tx = graph.newTransaction();
            final JanusGraphVertex[] vs = new JanusGraphVertex[verticesPerTask];
            for (int j = 0; j < verticesPerTask; j++) {
                vs[j] = tx.addVertex();
                vs[j].property(VertexProperty.Cardinality.single, "w", random.nextInt(maxWeight));
            }
            for (int j = 0; j < verticesPerTask * 10; j++) {
                final JanusGraphEdge e = vs[random.nextInt(verticesPerTask)].addEdge("l", vs[random.nextInt(verticesPerTask)]);
                e.property("t", random.nextInt(maxTime));
            }
            System.out.print(".");
            tx.commit();
        });
    }
    exe.shutdown();
    exe.awaitTermination(numVertices / 1000, TimeUnit.SECONDS);
    if (!exe.isTerminated())
        System.err.println("Could not load data in time");
    System.out.println("Loaded " + numVertices + "vertices");
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey)

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