Search in sources :

Example 1 with JanusGraphRelation

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

the class RelationIdentifier method findRelation.

JanusGraphRelation findRelation(JanusGraphTransaction tx) {
    JanusGraphVertex v = ((StandardJanusGraphTx) tx).getInternalVertex(outVertexId);
    if (v == null || v.isRemoved())
        return null;
    JanusGraphVertex typeVertex = tx.getVertex(typeId);
    if (typeVertex == null)
        return null;
    if (!(typeVertex instanceof RelationType))
        throw new IllegalArgumentException("Invalid RelationIdentifier: typeID does not reference a type");
    RelationType type = (RelationType) typeVertex;
    Iterable<? extends JanusGraphRelation> relations;
    if (((RelationType) typeVertex).isEdgeLabel()) {
        Direction dir = Direction.OUT;
        JanusGraphVertex other = ((StandardJanusGraphTx) tx).getInternalVertex(inVertexId);
        if (other == null || other.isRemoved())
            return null;
        if (((StandardJanusGraphTx) tx).isPartitionedVertex(v) && !((StandardJanusGraphTx) tx).isPartitionedVertex(other)) {
            // Swap for likely better performance
            JanusGraphVertex tmp = other;
            other = v;
            v = tmp;
            dir = Direction.IN;
        }
        relations = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((EdgeLabel) type).direction(dir).adjacent(other).edges();
    } else {
        relations = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((PropertyKey) type).properties();
    }
    for (JanusGraphRelation r : relations) {
        // Find current or previous relation
        if (r.longId() == relationId || ((r instanceof StandardRelation) && ((StandardRelation) r).getPreviousID() == relationId))
            return r;
    }
    return null;
}
Also used : JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) RelationType(org.janusgraph.core.RelationType) Direction(org.apache.tinkerpop.gremlin.structure.Direction)

Example 2 with JanusGraphRelation

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

the class GhostVertexRemover method process.

@Override
public void process(StaticBuffer key, Map<SliceQuery, EntryList> entries, ScanMetrics metrics) {
    long vertexId = getVertexId(key);
    assert entries.size() == 1;
    assert entries.get(everythingQueryLimit) != null;
    final EntryList everything = entries.get(everythingQueryLimit);
    if (!isGhostVertex(vertexId, everything)) {
        return;
    }
    if (everything.size() >= RELATION_COUNT_LIMIT) {
        metrics.incrementCustom(SKIPPED_GHOST_LIMIT_COUNT);
        return;
    }
    JanusGraphVertex vertex = tx.getInternalVertex(vertexId);
    Preconditions.checkArgument(vertex instanceof CacheVertex, "The bounding transaction is not configured correctly");
    CacheVertex v = (CacheVertex) vertex;
    v.loadRelations(EVERYTHING_QUERY, input -> everything);
    int removedRelations = 0;
    Iterator<JanusGraphRelation> iterator = v.query().noPartitionRestriction().relations().iterator();
    while (iterator.hasNext()) {
        iterator.next();
        iterator.remove();
        removedRelations++;
    }
    v.remove();
    // There should be no more system relations to remove
    metrics.incrementCustom(REMOVED_VERTEX_COUNT);
    metrics.incrementCustom(REMOVED_RELATION_COUNT, removedRelations);
}
Also used : JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EntryList(org.janusgraph.diskstorage.EntryList) CacheVertex(org.janusgraph.graphdb.vertices.CacheVertex)

Example 3 with JanusGraphRelation

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

the class QueryUtil method extractOrCondition.

public static Map.Entry<RelationType, Collection> extractOrCondition(Or<JanusGraphRelation> condition) {
    RelationType masterType = null;
    final List<Object> values = new ArrayList<>();
    for (final Condition c : condition.getChildren()) {
        if (!(c instanceof PredicateCondition))
            return null;
        final PredicateCondition<RelationType, JanusGraphRelation> atom = (PredicateCondition) c;
        if (atom.getPredicate() != Cmp.EQUAL)
            return null;
        final Object value = atom.getValue();
        if (value == null)
            return null;
        final RelationType type = atom.getKey();
        if (masterType == null)
            masterType = type;
        else if (!masterType.equals(type))
            return null;
        values.add(value);
    }
    if (masterType == null)
        return null;
    assert !values.isEmpty();
    return new AbstractMap.SimpleImmutableEntry(masterType, values);
}
Also used : PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) MultiCondition(org.janusgraph.graphdb.query.condition.MultiCondition) Condition(org.janusgraph.graphdb.query.condition.Condition) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) ArrayList(java.util.ArrayList)

Example 4 with JanusGraphRelation

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

the class IndexSerializer method getIndexEntry.

private Entry getIndexEntry(CompositeIndexType index, RecordEntry[] record, JanusGraphElement element) {
    final DataOutput out = serializer.getDataOutput(1 + 8 + 8 * record.length + 4 * 8);
    out.putByte(FIRST_INDEX_COLUMN_BYTE);
    if (index.getCardinality() != Cardinality.SINGLE) {
        VariableLong.writePositive(out, element.longId());
        if (index.getCardinality() != Cardinality.SET) {
            for (final RecordEntry re : record) {
                VariableLong.writePositive(out, re.relationId);
            }
        }
    }
    final int valuePosition = out.getPosition();
    if (element instanceof JanusGraphVertex) {
        VariableLong.writePositive(out, element.longId());
    } else {
        assert element instanceof JanusGraphRelation;
        final RelationIdentifier rid = (RelationIdentifier) element.id();
        final long[] longs = rid.getLongRepresentation();
        Preconditions.checkArgument(longs.length == 3 || longs.length == 4);
        for (final long aLong : longs) VariableLong.writePositive(out, aLong);
    }
    return new StaticArrayEntry(out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry)

Example 5 with JanusGraphRelation

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

the class JanusGraphEventualGraphTest method testConsistencyModifier.

/**
 * Tests that consistency modes are correctly interpreted in the absence of locks (or tx isolation)
 */
@Test
public void testConsistencyModifier() throws InterruptedException {
    makeKey("sig", Integer.class);
    makeKey("weight", Double.class);
    mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make();
    mgmt.makePropertyKey("value").dataType(Integer.class).cardinality(Cardinality.LIST).make();
    PropertyKey valuef = mgmt.makePropertyKey("valuef").dataType(Integer.class).cardinality(Cardinality.LIST).make();
    mgmt.setConsistency(valuef, ConsistencyModifier.FORK);
    mgmt.makeEdgeLabel("em").multiplicity(Multiplicity.MULTI).make();
    EdgeLabel emf = mgmt.makeEdgeLabel("emf").multiplicity(Multiplicity.MULTI).make();
    mgmt.setConsistency(emf, ConsistencyModifier.FORK);
    mgmt.makeEdgeLabel("es").multiplicity(Multiplicity.SIMPLE).make();
    mgmt.makeEdgeLabel("o2o").multiplicity(Multiplicity.ONE2ONE).make();
    mgmt.makeEdgeLabel("o2m").multiplicity(Multiplicity.ONE2MANY).make();
    finishSchema();
    JanusGraphVertex u = tx.addVertex(), v = tx.addVertex();
    JanusGraphRelation[] rs = new JanusGraphRelation[9];
    final int transactionId = 1;
    rs[0] = sign(v.property("weight", 5.0), transactionId);
    rs[1] = sign(v.property("name", "John"), transactionId);
    rs[2] = sign(v.property("value", 2), transactionId);
    rs[3] = sign(v.property("valuef", 2), transactionId);
    rs[6] = sign(v.addEdge("es", u), transactionId);
    rs[7] = sign(v.addEdge("o2o", u), transactionId);
    rs[8] = sign(v.addEdge("o2m", u), transactionId);
    rs[4] = sign(v.addEdge("em", u), transactionId);
    rs[5] = sign(v.addEdge("emf", u), transactionId);
    newTx();
    long vid = getId(v), uid = getId(u);
    JanusGraphTransaction tx1 = graph.newTransaction();
    JanusGraphTransaction tx2 = graph.newTransaction();
    final int wintx = 20;
    processTx(tx1, wintx - 10, vid, uid);
    processTx(tx2, wintx, vid, uid);
    tx1.commit();
    Thread.sleep(5);
    // tx2 should win using time-based eventual consistency
    tx2.commit();
    newTx();
    v = getV(tx, vid);
    assertEquals(6.0, v.<Double>value("weight"), 0.00001);
    VertexProperty p = getOnlyElement(v.properties("weight"));
    assertEquals(wintx, p.<Integer>value("sig").intValue());
    p = getOnlyElement(v.properties("name"));
    assertEquals("Bob", p.value());
    assertEquals(wintx, p.<Integer>value("sig").intValue());
    p = getOnlyElement(v.properties("value"));
    assertEquals(rs[2].longId(), getId(p));
    assertEquals(wintx, p.<Integer>value("sig").intValue());
    assertCount(2, v.properties("valuef"));
    for (Iterator<VertexProperty<Object>> ppiter = v.properties("valuef"); ppiter.hasNext(); ) {
        VertexProperty pp = ppiter.next();
        assertNotEquals(rs[3].longId(), getId(pp));
        assertEquals(2, pp.value());
    }
    Edge e = Iterables.getOnlyElement(v.query().direction(OUT).labels("es").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertNotEquals(rs[6].longId(), getId(e));
    e = Iterables.getOnlyElement(v.query().direction(OUT).labels("o2o").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertEquals(rs[7].longId(), getId(e));
    e = Iterables.getOnlyElement(v.query().direction(OUT).labels("o2m").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertNotEquals(rs[8].longId(), getId(e));
    e = Iterables.getOnlyElement(v.query().direction(OUT).labels("em").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertEquals(rs[4].longId(), getId(e));
    for (Edge o : v.query().direction(OUT).labels("emf").edges()) {
        assertNotEquals(rs[5].longId(), getId(o));
        assertEquals(uid, o.inVertex().id());
    }
}
Also used : JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.jupiter.api.Test)

Aggregations

JanusGraphRelation (org.janusgraph.core.JanusGraphRelation)11 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)6 RelationType (org.janusgraph.core.RelationType)5 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)4 HashMap (java.util.HashMap)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)2 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)2 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)2 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)2 PropertyKey (org.janusgraph.core.PropertyKey)2 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)2 StandardJanusGraphTx (org.janusgraph.graphdb.transaction.StandardJanusGraphTx)2 SystemRelationType (org.janusgraph.graphdb.types.system.SystemRelationType)2 Interval (org.janusgraph.util.datastructures.Interval)2 PointInterval (org.janusgraph.util.datastructures.PointInterval)2 RangeInterval (org.janusgraph.util.datastructures.RangeInterval)2 Test (org.junit.jupiter.api.Test)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1