Search in sources :

Example 11 with JanusGraphVertexProperty

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

the class RelationComparator method compare.

@Override
public int compare(final InternalRelation r1, final InternalRelation r2) {
    if (r1.equals(r2))
        return 0;
    // 1) Based on orders (if any)
    if (!orders.isEmpty()) {
        for (OrderList.OrderEntry order : orders) {
            int orderCompare = compareOnKey(r1, r2, order.getKey(), order.getOrder());
            if (orderCompare != 0)
                return orderCompare;
        }
    }
    // 2) RelationType (determine if property or edge - properties come first)
    int relationTypeCompare = (r1.isProperty() ? 1 : 2) - (r2.isProperty() ? 1 : 2);
    if (relationTypeCompare != 0)
        return relationTypeCompare;
    // 3) JanusGraphType
    InternalRelationType t1 = (InternalRelationType) r1.getType(), t2 = (InternalRelationType) r2.getType();
    int typeCompare = AbstractElement.compare(t1, t2);
    if (typeCompare != 0)
        return typeCompare;
    assert t1.equals(t2);
    // 4) Direction
    Direction dir1 = null, dir2 = null;
    for (int i = 0; i < r1.getLen(); i++) if (r1.getVertex(i).equals(vertex)) {
        dir1 = EdgeDirection.fromPosition(i);
        break;
    }
    for (int i = 0; i < r2.getLen(); i++) if (r2.getVertex(i).equals(vertex)) {
        dir2 = EdgeDirection.fromPosition(i);
        break;
    }
    // ("Either relation is not incident on vertex [%s]", vertex);
    assert dir1 != null && dir2 != null;
    int dirCompare = EdgeDirection.position(dir1) - EdgeDirection.position(dir2);
    if (dirCompare != 0)
        return dirCompare;
    // Breakout: If type&direction are the same and the type is unique in the direction it follows that the relations are the same
    if (t1.multiplicity().isUnique(dir1))
        return 0;
    // 5) Compare sort key values (this is empty and hence skipped if the type multiplicity is constrained)
    for (long typeId : t1.getSortKey()) {
        int keyCompare = compareOnKey(r1, r2, typeId, t1.getSortOrder());
        if (keyCompare != 0)
            return keyCompare;
    }
    // 6) Compare property objects or other vertices
    if (r1.isProperty()) {
        Object o1 = ((JanusGraphVertexProperty) r1).value();
        Object o2 = ((JanusGraphVertexProperty) r2).value();
        Preconditions.checkArgument(o1 != null && o2 != null);
        if (!o1.equals(o2)) {
            final int objectCompare;
            if (Comparable.class.isAssignableFrom(((PropertyKey) t1).dataType())) {
                objectCompare = ((Comparable) o1).compareTo(o2);
            } else {
                objectCompare = System.identityHashCode(o1) - System.identityHashCode(o2);
            }
            if (objectCompare != 0)
                return objectCompare;
        }
    } else {
        Preconditions.checkArgument(r1.isEdge() && r2.isEdge());
        int vertexCompare = AbstractElement.compare(r1.getVertex(EdgeDirection.position(dir1.opposite())), r2.getVertex(EdgeDirection.position(dir1.opposite())));
        if (vertexCompare != 0)
            return vertexCompare;
    }
    // Breakout: if type&direction are the same, and the end points of the relation are the same and the type is constrained, the relations must be the same
    if (t1.multiplicity().isConstrained())
        return 0;
    // 7)compare relation ids
    return AbstractElement.compare(r1, r2);
}
Also used : InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) OrderList(org.janusgraph.graphdb.internal.OrderList) Direction(org.apache.tinkerpop.gremlin.structure.Direction) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty)

Example 12 with JanusGraphVertexProperty

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

the class IndexSerializer method indexMatches.

private static void indexMatches(JanusGraphVertex vertex, RecordEntry[] current, IndexRecords matches, IndexField[] fields, int pos, boolean onlyLoaded, PropertyKey replaceKey, RecordEntry replaceValue) {
    if (pos >= fields.length) {
        matches.add(current);
        return;
    }
    final PropertyKey key = fields[pos].getFieldKey();
    List<RecordEntry> values;
    if (key.equals(replaceKey)) {
        values = ImmutableList.of(replaceValue);
    } else {
        values = new ArrayList<>();
        Iterable<JanusGraphVertexProperty> props;
        if (onlyLoaded || (!vertex.isNew() && IDManager.VertexIDType.PartitionedVertex.is(vertex.longId()))) {
            // going through transaction so we can query deleted vertices
            final VertexCentricQueryBuilder qb = ((InternalVertex) vertex).tx().query(vertex);
            qb.noPartitionRestriction().type(key);
            if (onlyLoaded)
                qb.queryOnlyLoaded();
            props = qb.properties();
        } else {
            props = vertex.query().keys(key.name()).properties();
        }
        for (final JanusGraphVertexProperty p : props) {
            assert !onlyLoaded || p.isLoaded() || p.isRemoved();
            assert key.dataType().equals(p.value().getClass()) : key + " -> " + p;
            values.add(new RecordEntry(p));
        }
    }
    for (final RecordEntry value : values) {
        current[pos] = value;
        indexMatches(vertex, current, matches, fields, pos + 1, onlyLoaded, replaceKey, replaceValue);
    }
}
Also used : VertexCentricQueryBuilder(org.janusgraph.graphdb.query.vertex.VertexCentricQueryBuilder) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) PropertyKey(org.janusgraph.core.PropertyKey)

Example 13 with JanusGraphVertexProperty

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

the class ManagementSystem method setStatusVertex.

private void setStatusVertex(JanusGraphSchemaVertex vertex, SchemaStatus status) {
    Preconditions.checkArgument(vertex instanceof RelationTypeVertex || vertex.asIndexType().isCompositeIndex());
    // Delete current status
    for (JanusGraphVertexProperty p : vertex.query().types(BaseKey.SchemaDefinitionProperty).properties()) {
        if (p.<TypeDefinitionDescription>valueOrNull(BaseKey.SchemaDefinitionDesc).getCategory() == TypeDefinitionCategory.STATUS) {
            if (p.value().equals(status))
                return;
            else
                p.remove();
        }
    }
    // Add new status
    JanusGraphVertexProperty p = transaction.addProperty(vertex, BaseKey.SchemaDefinitionProperty, status);
    p.property(BaseKey.SchemaDefinitionDesc.name(), TypeDefinitionDescription.of(TypeDefinitionCategory.STATUS));
}
Also used : TypeDefinitionDescription(org.janusgraph.graphdb.types.TypeDefinitionDescription) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) RelationTypeVertex(org.janusgraph.graphdb.types.vertices.RelationTypeVertex)

Example 14 with JanusGraphVertexProperty

use of org.janusgraph.core.JanusGraphVertexProperty 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 15 with JanusGraphVertexProperty

use of org.janusgraph.core.JanusGraphVertexProperty 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 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();
    any = tx.getPropertyKey("any");
    assertEquals(Object.class, any.dataType());
    JanusGraphVertex v = tx.addVertex();
    v.property(n(any), "Hello");
    v.property(n(any), 10L);
    int[] testArray = { 5, 6, 7 };
    v.property(n(any), testArray);
    // ######## VERIFICATION ##########
    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()) {
            assertArrayEquals(testArray, (int[]) value);
        } else
            fail();
    }
    clopen();
    v = getV(tx, v);
    // ######## VERIFICATION (copied from above) ##########
    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()) {
            assertArrayEquals(testArray, (int[]) value);
        } else
            fail();
    }
}
Also used : SpecialIntSerializer(org.janusgraph.graphdb.serializer.SpecialIntSerializer) SpecialInt(org.janusgraph.graphdb.serializer.SpecialInt) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) PropertyKey(org.janusgraph.core.PropertyKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)19 PropertyKey (org.janusgraph.core.PropertyKey)10 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)9 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)7 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)6 Test (org.junit.jupiter.api.Test)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)4 EdgeLabel (org.janusgraph.core.EdgeLabel)4 SchemaViolationException (org.janusgraph.core.SchemaViolationException)4 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)4 LongArrayList (com.carrotsearch.hppc.LongArrayList)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Property (org.apache.tinkerpop.gremlin.structure.Property)3 InternalRelation (org.janusgraph.graphdb.internal.InternalRelation)3 TypeDefinitionDescription (org.janusgraph.graphdb.types.TypeDefinitionDescription)3 TypeDefinitionMap (org.janusgraph.graphdb.types.TypeDefinitionMap)3 NonBlockingHashMap (org.jctools.maps.NonBlockingHashMap)3 ImmutableMap (com.google.common.collect.ImmutableMap)2