Search in sources :

Example 1 with RelationType

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

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

the class StandardTransactionLogProcessor method restoreExternalIndexes.

private void restoreExternalIndexes(Predicate<String> isFailedIndex, TransactionLogHeader.Entry entry) {
    // 1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
    SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
    BackendOperation.execute(() -> {
        final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
        try {
            entry.getContentAsModifications(serializer).stream().map(m -> ModificationDeserializer.parseRelation(m, tx)).forEach(rel -> {
                // Collect affected vertex indexes
                for (final MixedIndexType index : getMixedIndexes(rel.getType())) {
                    if (index.getElement() == ElementCategory.VERTEX && isFailedIndex.apply(index.getBackingIndexName())) {
                        assert rel.isProperty();
                        indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
                    }
                }
                // See if relation itself is affected
                for (final RelationType relType : rel.getPropertyKeysDirect()) {
                    for (final MixedIndexType index : getMixedIndexes(relType)) {
                        if (index.getElement().isInstance(rel) && isFailedIndex.apply(index.getBackingIndexName())) {
                            assert rel.id() instanceof RelationIdentifier;
                            indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(), ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
                        }
                    }
                }
            });
        } finally {
            if (tx.isOpen())
                tx.rollback();
        }
        return true;
    }, readTime);
    // 2) Restore elements per backing index
    for (final String indexName : indexRestores.keySet()) {
        final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
        try {
            BackendTransaction btx = tx.getTxHandle();
            final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
            BackendOperation.execute(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
                    indexRestores.get(indexName).forEach(restore -> {
                        JanusGraphSchemaVertex indexV = (JanusGraphSchemaVertex) tx.getVertex(restore.indexId);
                        MixedIndexType index = (MixedIndexType) indexV.asIndexType();
                        JanusGraphElement element = restore.retrieve(tx);
                        if (element != null) {
                            graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
                        } else {
                            // Element is deleted
                            graph.getIndexSerializer().removeElement(restore.elementId, index, restoredDocs);
                        }
                    });
                    indexTx.restore(restoredDocs);
                    indexTx.commit();
                    return true;
                }

                @Override
                public String toString() {
                    return "IndexMutation";
                }
            }, persistenceTime);
        } finally {
            if (tx.isOpen())
                tx.rollback();
        }
    }
}
Also used : ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Log(org.janusgraph.diskstorage.log.Log) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) MessageReader(org.janusgraph.diskstorage.log.MessageReader) BackendOperation(org.janusgraph.diskstorage.util.BackendOperation) LoggerFactory(org.slf4j.LoggerFactory) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Future(java.util.concurrent.Future) HashMultimap(com.google.common.collect.HashMultimap) Serializer(org.janusgraph.graphdb.database.serialize.Serializer) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Duration(java.time.Duration) Map(java.util.Map) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexType(org.janusgraph.graphdb.types.IndexType) LogTxStatus(org.janusgraph.graphdb.database.log.LogTxStatus) TransactionRecovery(org.janusgraph.core.log.TransactionRecovery) RelationType(org.janusgraph.core.RelationType) TimestampProvider(org.janusgraph.diskstorage.util.time.TimestampProvider) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) LogTxMeta(org.janusgraph.graphdb.database.log.LogTxMeta) Instant(java.time.Instant) Objects(java.util.Objects) List(java.util.List) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) Predicate(com.google.common.base.Predicate) CacheBuilder(com.google.common.cache.CacheBuilder) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) Iterables(com.google.common.collect.Iterables) SchemaSource(org.janusgraph.graphdb.types.SchemaSource) BackgroundThread(org.janusgraph.util.system.BackgroundThread) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction) Callable(java.util.concurrent.Callable) IndexTypeWrapper(org.janusgraph.graphdb.types.indextype.IndexTypeWrapper) Predicates(com.google.common.base.Predicates) JanusGraphException(org.janusgraph.core.JanusGraphException) Message(org.janusgraph.diskstorage.log.Message) JanusGraphElement(org.janusgraph.core.JanusGraphElement) TransactionLogHeader(org.janusgraph.graphdb.database.log.TransactionLogHeader) Logger(org.slf4j.Logger) Maps(com.google.common.collect.Maps) SetMultimap(com.google.common.collect.SetMultimap) ElementCategory(org.janusgraph.graphdb.internal.ElementCategory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RemovalCause(com.google.common.cache.RemovalCause) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) Preconditions(com.google.common.base.Preconditions) RemovalListener(com.google.common.cache.RemovalListener) Cache(com.google.common.cache.Cache) ReadMarker(org.janusgraph.diskstorage.log.ReadMarker) Collections(java.util.Collections) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) JanusGraphException(org.janusgraph.core.JanusGraphException) ExecutionException(java.util.concurrent.ExecutionException) JanusGraphElement(org.janusgraph.core.JanusGraphElement) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) List(java.util.List) Map(java.util.Map) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction)

Example 3 with RelationType

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

the class QueryUtil method constraints2QNF.

/**
 * Prepares the constraints from the query builder into a QNF compliant condition.
 * If the condition is invalid or trivially false, it returns null.
 *
 * @param tx
 * @param constraints
 * @param <E>
 * @return
 * @see #isQueryNormalForm(org.janusgraph.graphdb.query.condition.Condition)
 */
public static <E extends JanusGraphElement> And<E> constraints2QNF(StandardJanusGraphTx tx, List<PredicateCondition<String, E>> constraints) {
    final And<E> conditions = new And<>(constraints.size() + 4);
    for (final PredicateCondition<String, E> atom : constraints) {
        final RelationType type = getType(tx, atom.getKey());
        if (type == null) {
            if (atom.getPredicate() == Cmp.EQUAL && atom.getValue() == null)
                // Ignore condition, its trivially satisfied
                continue;
            return null;
        }
        final Object value = atom.getValue();
        final JanusGraphPredicate predicate = atom.getPredicate();
        if (type.isPropertyKey()) {
            final PropertyKey key = (PropertyKey) type;
            assert predicate.isValidCondition(value);
            Preconditions.checkArgument(key.dataType() == Object.class || predicate.isValidValueType(key.dataType()), "Data type of key is not compatible with condition");
        } else {
            // its a label
            Preconditions.checkArgument(((EdgeLabel) type).isUnidirected());
            Preconditions.checkArgument(predicate.isValidValueType(JanusGraphVertex.class), "Data type of key is not compatible with condition");
        }
        if (predicate instanceof Contain) {
            // Rewrite contains conditions
            final Collection values = (Collection) value;
            if (predicate == Contain.NOT_IN) {
                // Simply ignore since trivially satisfied
                if (values.isEmpty())
                    continue;
                for (final Object inValue : values) addConstraint(type, Cmp.NOT_EQUAL, inValue, conditions, tx);
            } else {
                Preconditions.checkArgument(predicate == Contain.IN);
                if (values.isEmpty()) {
                    // Cannot be satisfied
                    return null;
                }
                if (values.size() == 1) {
                    addConstraint(type, Cmp.EQUAL, values.iterator().next(), conditions, tx);
                } else {
                    final Or<E> nested = new Or<>(values.size());
                    for (final Object invalue : values) addConstraint(type, Cmp.EQUAL, invalue, nested, tx);
                    conditions.add(nested);
                }
            }
        } else if (predicate instanceof AndJanusPredicate) {
            if (addConstraint(type, (AndJanusPredicate) (predicate), (List<Object>) (value), conditions, tx) == null) {
                return null;
            }
        } else if (predicate instanceof OrJanusPredicate) {
            final List<Object> values = (List<Object>) (value);
            // FIXME: this might generate a non QNF-compliant form, e.g. nested = PredicateCondition OR (PredicateCondition AND PredicateCondition)
            final Or<E> nested = addConstraint(type, (OrJanusPredicate) predicate, values, new Or<>(values.size()), tx);
            if (nested == null) {
                return null;
            }
            conditions.add(nested);
        } else {
            addConstraint(type, predicate, value, conditions, tx);
        }
    }
    return conditions;
}
Also used : Or(org.janusgraph.graphdb.query.condition.Or) AndJanusPredicate(org.janusgraph.graphdb.predicate.AndJanusPredicate) OrJanusPredicate(org.janusgraph.graphdb.predicate.OrJanusPredicate) And(org.janusgraph.graphdb.query.condition.And) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Contain(org.janusgraph.core.attribute.Contain) PropertyKey(org.janusgraph.core.PropertyKey)

Example 4 with RelationType

use of org.janusgraph.core.RelationType 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 5 with RelationType

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

the class JanusGraphTest method performReindexAndVerifyEdgeCount.

public void performReindexAndVerifyEdgeCount(String indexName, String edgeLabel, String propKey, Vertex[] vertices, int[] propValues, Direction[] dirs, int[] resultsBeforeReindex) throws InterruptedException, ExecutionException {
    assert propValues.length == dirs.length;
    assert propValues.length == resultsBeforeReindex.length;
    RelationType t = mgmt.getRelationType(edgeLabel);
    RelationTypeIndex relationIndex = mgmt.getRelationIndex(t, indexName);
    assertEquals(SchemaStatus.ENABLED, relationIndex.getIndexStatus());
    GraphTraversalSource g = graph.traversal();
    // asserting before reindex
    for (int i = 0; i < propValues.length; i++) {
        final int expectedCount = resultsBeforeReindex[i];
        final Vertex v = vertices[i];
        long count = 0;
        if (OUT.equals(dirs[i])) {
            count = g.V(v).outE(edgeLabel).has(propKey, propValues[i]).count().next();
        } else if (IN.equals(dirs[i])) {
            count = g.V(v).inE(edgeLabel).has(propKey, propValues[i]).count().next();
        } else {
            count = g.V(v).bothE(edgeLabel).has(propKey, propValues[i]).count().next();
        }
        assertEquals(expectedCount, count, String.format("v = %s, index = %s, direction = %s, prop value = %d", g.V(v).properties("vtName").next().value(), indexName, dirs[i], propValues[i]));
    }
    // Reindexing
    mgmt.updateIndex(relationIndex, SchemaAction.REINDEX, 1).get();
    finishSchema();
    relationIndex = mgmt.getRelationIndex(t, indexName);
    assertEquals(SchemaStatus.ENABLED, relationIndex.getIndexStatus());
    final int[] expectedResultsAfterReindex = new int[] { 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1 };
    for (int i = 0; i < propValues.length; i++) {
        final int expectedCount = expectedResultsAfterReindex[i];
        final Vertex v = vertices[i];
        long count = 0;
        if (OUT.equals(dirs[i])) {
            count = g.V(v).outE(edgeLabel).has(propKey, propValues[i]).count().next();
        } else if (IN.equals(dirs[i])) {
            count = g.V(v).inE(edgeLabel).has(propKey, propValues[i]).count().next();
        } else {
            count = g.V(v).bothE(edgeLabel).has(propKey, propValues[i]).count().next();
        }
        assertEquals(expectedCount, count, String.format("v = %s, index = %s, direction = %s, prop value = %d", g.V(v).properties("vtName").next().value(), indexName, dirs[i], propValues[i]));
    }
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) CacheVertex(org.janusgraph.graphdb.vertices.CacheVertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex)

Aggregations

RelationType (org.janusgraph.core.RelationType)30 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)22 PropertyKey (org.janusgraph.core.PropertyKey)10 JanusGraphRelation (org.janusgraph.core.JanusGraphRelation)6 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)6 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)6 StandardJanusGraphTx (org.janusgraph.graphdb.transaction.StandardJanusGraphTx)6 Map (java.util.Map)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)5 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)4 SystemRelationType (org.janusgraph.graphdb.types.system.SystemRelationType)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 EdgeLabel (org.janusgraph.core.EdgeLabel)3 VertexLabel (org.janusgraph.core.VertexLabel)3 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)3 ImplicitKey (org.janusgraph.graphdb.types.system.ImplicitKey)3 Preconditions (com.google.common.base.Preconditions)2 Collection (java.util.Collection)2 List (java.util.List)2