Search in sources :

Example 1 with StandardEdgeLabelMaker

use of org.janusgraph.graphdb.types.StandardEdgeLabelMaker in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method testIndexing.

@Test
public void testIndexing() throws InterruptedException {
    final PropertyKey text = makeKey("text", String.class);
    createExternalVertexIndex(text, INDEX);
    createExternalEdgeIndex(text, INDEX);
    PropertyKey name = makeKey("name", String.class);
    mgmt.addIndexKey(getExternalIndex(Vertex.class, INDEX), name, Parameter.of("mapping", Mapping.TEXT));
    mgmt.addIndexKey(getExternalIndex(Edge.class, INDEX), name, Parameter.of("mapping", Mapping.TEXT));
    final PropertyKey location = makeKey("location", Geoshape.class);
    createExternalVertexIndex(location, INDEX);
    createExternalEdgeIndex(location, INDEX);
    final PropertyKey boundary = makeKey("boundary", Geoshape.class);
    mgmt.addIndexKey(getExternalIndex(Vertex.class, INDEX), boundary, Parameter.of("mapping", Mapping.PREFIX_TREE), Parameter.of("index-geo-dist-error-pct", 0.0025));
    mgmt.addIndexKey(getExternalIndex(Edge.class, INDEX), boundary, Parameter.of("mapping", Mapping.PREFIX_TREE), Parameter.of("index-geo-dist-error-pct", 0.0025));
    final PropertyKey time = makeKey("time", Long.class);
    createExternalVertexIndex(time, INDEX);
    createExternalEdgeIndex(time, INDEX);
    final PropertyKey category = makeKey("category", Integer.class);
    mgmt.buildIndex("vcategory", Vertex.class).addKey(category).buildCompositeIndex();
    mgmt.buildIndex("ecategory", Edge.class).addKey(category).buildCompositeIndex();
    final PropertyKey group = makeKey("group", Byte.class);
    createExternalVertexIndex(group, INDEX);
    createExternalEdgeIndex(group, INDEX);
    makeVertexIndexedKey("uid", Integer.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(time).signature(location, boundary).make();
    finishSchema();
    clopen();
    final String[] words = { "world", "aurelius", "janusgraph", "graph" };
    final int numCategories = 5;
    final int numGroups = 10;
    double offset;
    int numV = 100;
    final int originalNumV = numV;
    for (int i = 0; i < numV; i++) {
        final JanusGraphVertex v = tx.addVertex();
        v.property(VertexProperty.Cardinality.single, "uid", i);
        v.property(VertexProperty.Cardinality.single, "category", i % numCategories);
        v.property(VertexProperty.Cardinality.single, "group", i % numGroups);
        v.property(VertexProperty.Cardinality.single, "text", "Vertex " + words[i % words.length]);
        v.property(VertexProperty.Cardinality.single, "name", words[i % words.length]);
        v.property(VertexProperty.Cardinality.single, "time", i);
        offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV);
        v.property(VertexProperty.Cardinality.single, "location", Geoshape.point(0.0 + offset, 0.0 + offset));
        if (i % 2 == 0) {
            v.property(VertexProperty.Cardinality.single, "boundary", Geoshape.line(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 } })));
        } else {
            v.property(VertexProperty.Cardinality.single, "boundary", Geoshape.polygon(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 }, { offset - 0.1, offset - 0.1 } })));
        }
        final Edge e = v.addEdge("knows", getVertex("uid", Math.max(0, i - 1)));
        e.property("text", "Vertex " + words[i % words.length]);
        e.property("name", words[i % words.length]);
        e.property("time", i);
        e.property("category", i % numCategories);
        e.property("group", i % numGroups);
        e.property("location", Geoshape.point(0.0 + offset, 0.0 + offset));
        if (i % 2 == 0) {
            e.property("boundary", Geoshape.line(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 } })));
        } else {
            e.property("boundary", Geoshape.polygon(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 }, { offset - 0.1, offset - 0.1 } })));
        }
    }
    checkIndexingCounts(words, numV, originalNumV, true);
    // some indexing backends may guarantee only eventual consistency
    for (int retry = 0, status = 1; retry < RETRY_COUNT && status > 0; retry++) {
        clopen();
        try {
            checkIndexingCounts(words, numV, originalNumV, true);
            status = 0;
        } catch (final AssertionError e) {
            if (retry >= RETRY_COUNT - 1)
                throw e;
            Thread.sleep(RETRY_INTERVAL);
        }
    }
    newTx();
    final int numDelete = 12;
    for (int i = numV - numDelete; i < numV; i++) {
        getVertex("uid", i).remove();
    }
    numV -= numDelete;
    checkIndexingCounts(words, numV, originalNumV, false);
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) RepeatedIfExceptionsTest(io.github.artsok.RepeatedIfExceptionsTest) Test(org.junit.jupiter.api.Test)

Example 2 with StandardEdgeLabelMaker

use of org.janusgraph.graphdb.types.StandardEdgeLabelMaker in project janusgraph by JanusGraph.

the class ManagementSystem method buildRelationTypeIndex.

private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT, "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isUnique(direction), "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);
    String composedName = composeRelationTypeIndexName(type, name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    boolean canIndexBeEnabled = type.isNew() || Arrays.stream(sortKeys).anyMatch(JanusGraphElement::isNew);
    maker.status(canIndexBeEnabled ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);
    // Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = new HashSet<>(typeSig.length);
    for (long typeId : typeSig) {
        signature.add(transaction.getExistingPropertyKey(typeId));
    }
    for (RelationType sortType : sortKeys) {
        signature.remove(sortType);
    }
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!canIndexBeEnabled) {
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    }
    return index;
}
Also used : StandardRelationTypeMaker(org.janusgraph.graphdb.types.StandardRelationTypeMaker) EdgeLabel(org.janusgraph.core.EdgeLabel) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) StandardPropertyKeyMaker(org.janusgraph.graphdb.types.StandardPropertyKeyMaker) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) PropertyKey(org.janusgraph.core.PropertyKey) HashSet(java.util.HashSet)

Example 3 with StandardEdgeLabelMaker

use of org.janusgraph.graphdb.types.StandardEdgeLabelMaker in project janusgraph by JanusGraph.

the class JanusGraphTest method testVertexCentricQuery.

public void testVertexCentricQuery(int noVertices) {
    makeVertexIndexedUniqueKey("name", String.class);
    PropertyKey time = makeKey("time", Integer.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey number = makeKey("number", Long.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("connect")).sortKey(time).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("connectDesc")).sortKey(time).sortOrder(Order.DESC).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friend")).sortKey(weight, time).sortOrder(Order.ASC).signature(number).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friendDesc")).sortKey(weight, time).sortOrder(Order.DESC).signature(number).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(number, weight).make();
    mgmt.makeEdgeLabel("follows").make();
    finishSchema();
    JanusGraphVertex v = tx.addVertex("name", "v");
    JanusGraphVertex u = tx.addVertex("name", "u");
    assertEquals(0, (noVertices - 1) % 3);
    JanusGraphVertex[] vs = new JanusGraphVertex[noVertices];
    for (int i = 0; i < noVertices; i++) {
        vs[i] = tx.addVertex("name", "v" + i);
    }
    EdgeLabel[] labelsV = { tx.getEdgeLabel("connect"), tx.getEdgeLabel("friend"), tx.getEdgeLabel("knows") };
    EdgeLabel[] labelsU = { tx.getEdgeLabel("connectDesc"), tx.getEdgeLabel("friendDesc"), tx.getEdgeLabel("knows") };
    for (int i = 1; i < noVertices; i++) {
        for (final JanusGraphVertex vertex : new JanusGraphVertex[] { v, u }) {
            for (final Direction d : new Direction[] { OUT, IN }) {
                EdgeLabel label = vertex == v ? labelsV[i % 3] : labelsU[i % 3];
                JanusGraphEdge e = d == OUT ? vertex.addEdge(n(label), vs[i]) : vs[i].addEdge(n(label), vertex);
                e.property("time", i);
                e.property("weight", i % 4 + 0.5);
                e.property("name", "e" + i);
                e.property("number", i % 5);
            }
        }
    }
    int edgesPerLabel = noVertices / 3;
    VertexList vl;
    Map<JanusGraphVertex, Iterable<JanusGraphEdge>> results;
    Map<JanusGraphVertex, Iterable<JanusGraphVertexProperty>> results2;
    JanusGraphVertex[] qvs;
    int lastTime;
    Iterator<? extends Edge> outer;
    clopen();
    long[] vertexIdSubset = new long[31 - 3];
    for (int i = 0; i < vertexIdSubset.length; i++) vertexIdSubset[i] = vs[i + 3].longId();
    Arrays.sort(vertexIdSubset);
    // ##################################################
    // Queries from Cache
    // ##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = getV(tx, vs[i].longId());
    v = getV(tx, v.longId());
    u = getV(tx, u.longId());
    qvs = new JanusGraphVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    // To trigger queries from cache (don't copy!!!)
    assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
    assertEquals(1, v.query().propertyCount());
    assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (final JanusGraphEdge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime <= nowTime, lastTime + " vs. " + nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (final Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime >= nowTime, lastTime + " vs. " + nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (final Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", 15).has("weight", 3.5), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).edgeCount());
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
    assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 31).count());
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).limit(3).count());
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", 0.5).limit(3), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", Contain.IN, ImmutableList.of(0.5)), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5, 2.5)).interval("time", 3, 33), EDGE, 7, 3, new boolean[] { true, true });
    int friendsWhoHaveOutEdgesWithWeightBetweenPointFiveAndOnePointFive = (int) Math.round(Math.ceil(1667 * noVertices / 10000.0));
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5)), EDGE, friendsWhoHaveOutEdgesWithWeightBetweenPointFiveAndOnePointFive, 2, new boolean[] { true, true });
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).edgeCount());
    assertEquals(2, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN_EQUAL, 5).edgeCount());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Cmp.GREATER_THAN, 10).edgeCount());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 4.0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 2.0).edgeCount());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 2.1, 4.0).edgeCount());
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
    assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).edgeCount());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).edgeCount());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(IN).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().labels("knows").direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).has("weight", 3.5).edgeCount());
    assertEquals(2, v.query().labels("connect").adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(0, v.query().labels("connect").adjacent(vs[8]).has("time", 8).edgeCount());
    assertEquals(2, v.query().labels().direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().adjacent(vs[11]).edgeCount());
    // v and vs[0] are not adjacent
    assertEquals(0, v.query().adjacent(vs[0]).edgeCount());
    assertEquals(0, v.query().labels().adjacent(vs[0]).edgeCount());
    assertEquals(0, v.query().direction(BOTH).adjacent(vs[0]).edgeCount());
    assertEquals(0, v.query().labels().direction(BOTH).adjacent(vs[0]).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).edgeCount());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).edgeCount());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).edgeCount());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).edgeCount());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
    for (final Direction dir : new Direction[] { IN, OUT }) {
        vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
        vl.sort();
        for (int i = 0; i < vl.size(); i++) assertEquals(vertexIdSubset[i], vl.getID(i));
    }
    assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
    // Property queries
    assertEquals(1, Iterables.size(v.query().properties()));
    assertEquals(1, Iterables.size(v.query().keys("name").properties()));
    // MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").edges();
    for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(1, Iterables.size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").edges();
    for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(2, Iterables.size(result));
    results = tx.multiQuery(qvs).labels("knows").edges();
    for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(0, Iterables.size(result));
    results = tx.multiQuery(qvs).edges();
    for (Iterable<JanusGraphEdge> result : results.values()) assertEquals(4, Iterables.size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    // ##################################################
    // Same queries as above but without memory loading (i.e. omitting the first query)
    // ##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = getV(tx, vs[i].longId());
    v = getV(tx, v.longId());
    u = getV(tx, u.longId());
    qvs = new JanusGraphVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (final Edge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime <= nowTime, lastTime + " vs. " + nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (final Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime >= nowTime, lastTime + " vs. " + nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (final Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", 15).has("weight", 3.5), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).edgeCount());
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
    assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 31).count());
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).limit(3).count());
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", 0.5).limit(3), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", Contain.IN, ImmutableList.of(0.5)), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5, 2.5)).interval("time", 3, 33), EDGE, 7, 3, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5)), EDGE, friendsWhoHaveOutEdgesWithWeightBetweenPointFiveAndOnePointFive, 2, new boolean[] { true, true });
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).edgeCount());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Cmp.GREATER_THAN, 10).edgeCount());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 4.0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 2.0).edgeCount());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 2.1, 4.0).edgeCount());
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
    assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).edgeCount());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).edgeCount());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(IN).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().labels("knows").direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).has("weight", 3.5).edgeCount());
    assertEquals(2, v.query().labels("connect").adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(0, v.query().labels("connect").adjacent(vs[8]).has("time", 8).edgeCount());
    assertEquals(2, v.query().labels().direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().adjacent(vs[11]).edgeCount());
    // v and vs[0] are not adjacent
    assertEquals(0, v.query().adjacent(vs[0]).edgeCount());
    assertEquals(0, v.query().labels().adjacent(vs[0]).edgeCount());
    assertEquals(0, v.query().direction(BOTH).adjacent(vs[0]).edgeCount());
    assertEquals(0, v.query().labels().direction(BOTH).adjacent(vs[0]).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).edgeCount());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).edgeCount());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).edgeCount());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).edgeCount());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
    for (final Direction dir : new Direction[] { IN, OUT }) {
        vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
        vl.sort();
        for (int i = 0; i < vl.size(); i++) assertEquals(vertexIdSubset[i], vl.getID(i));
    }
    assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
    // Property queries
    assertEquals(1, Iterables.size(v.query().properties()));
    assertEquals(1, Iterables.size(v.query().keys("name").properties()));
    // MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").edges();
    for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(1, Iterables.size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").edges();
    for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(2, Iterables.size(result));
    results = tx.multiQuery(qvs).labels("knows").edges();
    for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(0, Iterables.size(result));
    results = tx.multiQuery(qvs).edges();
    for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(4, Iterables.size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (final Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (final Iterable<JanusGraphVertexProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    // ##################################################
    // End copied queries
    // ##################################################
    newTx();
    v = Iterables.getOnlyElement(tx.query().has("name", "v").vertices());
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).vertexIds().size());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).vertexIds().size());
    newTx();
    v = Iterables.getOnlyElement(tx.query().has("name", "v").vertices());
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).edgeCount());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).edgeCount());
    newTx();
    // Test partially new vertex queries
    final JanusGraphVertex[] qvs2 = new JanusGraphVertex[qvs.length + 2];
    qvs2[0] = tx.addVertex();
    for (int i = 0; i < qvs.length; i++) qvs2[i + 1] = getV(tx, qvs[i].longId());
    qvs2[qvs2.length - 1] = tx.addVertex();
    qvs2[0].addEdge("connect", qvs2[qvs2.length - 1]);
    qvs2[qvs2.length - 1].addEdge("connect", qvs2[0]);
    results = tx.multiQuery(qvs2).direction(IN).labels("connect").edges();
    for (final Iterable<JanusGraphEdge> result : results.values()) assertEquals(1, Iterables.size(result));
}
Also used : JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) EdgeLabel(org.janusgraph.core.EdgeLabel) Direction(org.apache.tinkerpop.gremlin.structure.Direction) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) AbstractEdge(org.janusgraph.graphdb.relations.AbstractEdge) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) VertexList(org.janusgraph.core.VertexList)

Example 4 with StandardEdgeLabelMaker

use of org.janusgraph.graphdb.types.StandardEdgeLabelMaker in project janusgraph by JanusGraph.

the class JanusGraphTest method testMediumCreateRetrieve.

@Test
public void testMediumCreateRetrieve() {
    // Create schema
    makeLabel("connect");
    makeVertexIndexedUniqueKey("name", String.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey id = makeVertexIndexedUniqueKey("uid", Integer.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(id).signature(weight).make();
    finishSchema();
    // Create Nodes
    int noVertices = 500;
    String[] names = new String[noVertices];
    int[] ids = new int[noVertices];
    JanusGraphVertex[] nodes = new JanusGraphVertex[noVertices];
    long[] nodeIds = new long[noVertices];
    List<Edge>[] nodeEdges = new List[noVertices];
    for (int i = 0; i < noVertices; i++) {
        names[i] = "vertex" + i;
        ids[i] = i;
        nodes[i] = tx.addVertex("name", names[i], "uid", ids[i]);
        if ((i + 1) % 100 == 0)
            log.debug("Added 100 nodes");
    }
    log.debug("Nodes created");
    int[] connectOff = { -100, -34, -4, 10, 20 };
    int[] knowsOff = { -400, -18, 8, 232, 334 };
    for (int i = 0; i < noVertices; i++) {
        JanusGraphVertex n = nodes[i];
        nodeEdges[i] = new ArrayList<>(10);
        for (int c : connectOff) {
            Edge r = n.addEdge("connect", nodes[wrapAround(i + c, noVertices)]);
            nodeEdges[i].add(r);
        }
        for (int k : knowsOff) {
            JanusGraphVertex n2 = nodes[wrapAround(i + k, noVertices)];
            Edge r = n.addEdge("knows", n2, "uid", ((Number) n.value("uid")).intValue() + ((Number) n2.value("uid")).intValue(), "weight", k * 1.5, "name", i + "-" + k);
            nodeEdges[i].add(r);
        }
        if (i % 100 == 99)
            log.debug(".");
    }
    tx.commit();
    tx = null;
    Set[] nodeEdgeIds = new Set[noVertices];
    for (int i = 0; i < noVertices; i++) {
        nodeIds[i] = (Long) nodes[i].id();
        nodeEdgeIds[i] = new HashSet(10);
        for (Object r : nodeEdges[i]) {
            nodeEdgeIds[i].add(((JanusGraphEdge) r).longId());
        }
    }
    clopen();
    nodes = new JanusGraphVertex[noVertices];
    for (int i = 0; i < noVertices; i++) {
        JanusGraphVertex n = getVertex("uid", ids[i]);
        assertEquals(n, getVertex("name", names[i]));
        assertEquals(names[i], n.value("name"));
        nodes[i] = n;
        assertEquals(nodeIds[i], n.longId());
    }
    for (int i = 0; i < noVertices; i++) {
        JanusGraphVertex n = nodes[i];
        assertCount(connectOff.length + knowsOff.length, n.query().direction(Direction.OUT).edges());
        assertCount(connectOff.length, n.query().direction(Direction.OUT).labels("connect").edges());
        assertCount(connectOff.length * 2, n.query().direction(Direction.BOTH).labels("connect").edges());
        assertCount(knowsOff.length * 2, n.query().direction(Direction.BOTH).labels("knows").edges());
        assertCount(connectOff.length + knowsOff.length, n.query().direction(Direction.OUT).edges());
        assertCount(2, n.properties());
        for (JanusGraphEdge o : n.query().direction(Direction.OUT).labels("knows").edges()) {
            JanusGraphEdge r = o;
            JanusGraphVertex n2 = r.vertex(Direction.IN);
            int idSum = ((Number) n.value("uid")).intValue() + ((Number) n2.value("uid")).intValue();
            assertEquals(idSum, ((Number) r.value("uid")).intValue());
            double k = ((Number) r.value("weight")).doubleValue() / 1.5;
            int ki = (int) k;
            assertEquals(i + "-" + ki, r.value("name"));
        }
        Set edgeIds = new HashSet(10);
        for (JanusGraphEdge r : n.query().direction(Direction.OUT).edges()) {
            edgeIds.add(r.longId());
        }
        assertEquals(edgeIds, nodeEdgeIds[i], edgeIds + " vs " + nodeEdgeIds[i]);
    }
    newTx();
    // Bulk vertex retrieval
    long[] vertexIdsOne = new long[noVertices / 10];
    System.arraycopy(nodeIds, 0, vertexIdsOne, 0, vertexIdsOne.length);
    // All non-cached
    verifyVerticesRetrieval(vertexIdsOne, Lists.newArrayList(tx.getVertices(vertexIdsOne)));
    // All cached
    verifyVerticesRetrieval(vertexIdsOne, Lists.newArrayList(tx.getVertices(vertexIdsOne)));
    long[] vertexIdsTwo = new long[noVertices / 10 * 2];
    System.arraycopy(nodeIds, 0, vertexIdsTwo, 0, vertexIdsTwo.length);
    // Partially cached
    verifyVerticesRetrieval(vertexIdsTwo, Lists.newArrayList(tx.getVertices(vertexIdsTwo)));
}
Also used : EnumSet(java.util.EnumSet) HashSet(java.util.HashSet) Set(java.util.Set) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ImmutableList(com.google.common.collect.ImmutableList) VertexList(org.janusgraph.core.VertexList) ArrayList(java.util.ArrayList) OrderList(org.janusgraph.graphdb.internal.OrderList) List(java.util.List) AbstractEdge(org.janusgraph.graphdb.relations.AbstractEdge) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyKey (org.janusgraph.core.PropertyKey)4 StandardEdgeLabelMaker (org.janusgraph.graphdb.types.StandardEdgeLabelMaker)4 Edge (org.apache.tinkerpop.gremlin.structure.Edge)3 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)3 HashSet (java.util.HashSet)2 EdgeLabel (org.janusgraph.core.EdgeLabel)2 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)2 VertexList (org.janusgraph.core.VertexList)2 AbstractEdge (org.janusgraph.graphdb.relations.AbstractEdge)2 Test (org.junit.jupiter.api.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 RepeatedIfExceptionsTest (io.github.artsok.RepeatedIfExceptionsTest)1 ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Set (java.util.Set)1 Direction (org.apache.tinkerpop.gremlin.structure.Direction)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)1 RelationType (org.janusgraph.core.RelationType)1