Search in sources :

Example 66 with PropertyKey

use of org.janusgraph.core.PropertyKey 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 67 with PropertyKey

use of org.janusgraph.core.PropertyKey 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 68 with PropertyKey

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

the class JanusGraphIndexTest method testIndexing.

private void testIndexing(Cardinality cardinality) {
    if (supportsCollections()) {
        PropertyKey stringProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(cardinality).make();
        PropertyKey intProperty = mgmt.makePropertyKey("age").dataType(Integer.class).cardinality(cardinality).make();
        PropertyKey longProperty = mgmt.makePropertyKey("long").dataType(Long.class).cardinality(cardinality).make();
        PropertyKey uuidProperty = mgmt.makePropertyKey("uuid").dataType(UUID.class).cardinality(cardinality).make();
        PropertyKey geopointProperty = mgmt.makePropertyKey("geopoint").dataType(Geoshape.class).cardinality(cardinality).make();
        mgmt.buildIndex("collectionIndex", Vertex.class).addKey(stringProperty, getStringMapping()).addKey(intProperty).addKey(longProperty).addKey(uuidProperty).addKey(geopointProperty).buildMixedIndex(INDEX);
        finishSchema();
        testCollection(cardinality, "name", "Totoro", "Hiro");
        testCollection(cardinality, "age", 1, 2);
        testCollection(cardinality, "long", 1L, 2L);
        testCollection(cardinality, "geopoint", Geoshape.point(1.0, 1.0), Geoshape.point(2.0, 2.0));
        String backend = readConfig.get(INDEX_BACKEND, INDEX);
        // https://issues.apache.org/jira/browse/SOLR-11264
        if (!"solr".equals(backend)) {
            testCollection(cardinality, "uuid", UUID.randomUUID(), UUID.randomUUID());
        }
    } else {
        try {
            PropertyKey stringProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(cardinality).make();
            // This should throw an exception
            mgmt.buildIndex("collectionIndex", Vertex.class).addKey(stringProperty, getStringMapping()).buildMixedIndex(INDEX);
            Assert.fail("Should have thrown an exception");
        } catch (JanusGraphException ignored) {
        }
    }
}
Also used : JanusGraphException(org.janusgraph.core.JanusGraphException) PropertyKey(org.janusgraph.core.PropertyKey)

Example 69 with PropertyKey

use of org.janusgraph.core.PropertyKey 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 70 with PropertyKey

use of org.janusgraph.core.PropertyKey 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

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