Search in sources :

Example 16 with Direction

use of org.apache.tinkerpop.gremlin.structure.Direction in project titan by thinkaurelius.

the class AdjacentVertexFilterOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
        Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
        List<Step> steps = filterTraversal.getSteps();
        if (steps.size() == 2 && (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) && (steps.get(1) instanceof IsStep)) {
            Direction direction = null;
            if (steps.get(0) instanceof EdgeVertexStep) {
                EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
                if (evs.getDirection() != Direction.BOTH)
                    direction = evs.getDirection();
            } else {
                assert steps.get(0) instanceof EdgeOtherVertexStep;
                direction = Direction.BOTH;
            }
            P predicate = ((IsStep) steps.get(1)).getPredicate();
            if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
                TitanVertex vertex = TitanTraversalUtil.getTitanVertex((Vertex) predicate.getValue());
                Step<?, ?> currentStep = originalStep.getPreviousStep();
                while (true) {
                    if (currentStep instanceof HasStep || currentStep instanceof IdentityStep) {
                    } else
                        break;
                }
                if (currentStep instanceof VertexStep) {
                    VertexStep vstep = (VertexStep) currentStep;
                    if (vstep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vstep.getDirection().opposite()))) {
                        TraversalHelper.replaceStep(originalStep, new HasStep(traversal, HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal);
                    }
                }
            }
        }
    });
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.GraphStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) Direction(org.apache.tinkerpop.gremlin.structure.Direction) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep)

Example 17 with Direction

use of org.apache.tinkerpop.gremlin.structure.Direction in project titan by thinkaurelius.

the class EdgeSerializer method parseRelation.

@Override
public RelationCache parseRelation(Entry data, boolean excludeProperties, TypeInspector tx) {
    ReadBuffer in = data.asReadBuffer();
    LongObjectHashMap properties = excludeProperties ? null : new LongObjectHashMap(4);
    RelationTypeParse typeAndDir = IDHandler.readRelationType(in);
    long typeId = typeAndDir.typeId;
    Direction dir = typeAndDir.dirID.getDirection();
    RelationCategory rtype = typeAndDir.dirID.getRelationCategory();
    RelationType relationType = tx.getExistingRelationType(typeId);
    InternalRelationType def = (InternalRelationType) relationType;
    Multiplicity multiplicity = def.multiplicity();
    long[] keysig = def.getSortKey();
    long relationId;
    Object other;
    int startKeyPos = in.getPosition();
    int endKeyPos = 0;
    if (relationType.isEdgeLabel()) {
        long otherVertexId;
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                otherVertexId = VariableLong.readPositive(in);
            } else {
                in.movePositionTo(data.getValuePosition());
                otherVertexId = VariableLong.readPositiveBackward(in);
                in.movePositionTo(data.getValuePosition());
            }
            relationId = VariableLong.readPositive(in);
        } else {
            in.movePositionTo(data.getValuePosition());
            relationId = VariableLong.readPositiveBackward(in);
            otherVertexId = VariableLong.readPositiveBackward(in);
            endKeyPos = in.getPosition();
            in.movePositionTo(data.getValuePosition());
        }
        other = otherVertexId;
    } else {
        assert relationType.isPropertyKey();
        PropertyKey key = (PropertyKey) relationType;
        if (multiplicity.isConstrained()) {
            other = readPropertyValue(in, key);
            relationId = VariableLong.readPositive(in);
        } else {
            in.movePositionTo(data.getValuePosition());
            relationId = VariableLong.readPositiveBackward(in);
            endKeyPos = in.getPosition();
            in.movePositionTo(data.getValuePosition());
            other = readPropertyValue(in, key);
        }
        Preconditions.checkState(other != null, "Encountered error in deserializer [null value returned]. Check serializer compatibility.");
    }
    assert other != null;
    if (!excludeProperties && !multiplicity.isConstrained() && keysig.length > 0) {
        int currentPos = in.getPosition();
        //Read sort key which only exists if type is not unique in this direction
        assert endKeyPos > startKeyPos;
        //after reading the ids, we are on the last byte of the key
        int keyLength = endKeyPos - startKeyPos;
        in.movePositionTo(startKeyPos);
        ReadBuffer inkey = in;
        if (def.getSortOrder() == Order.DESC)
            inkey = in.subrange(keyLength, true);
        readInlineTypes(keysig, properties, inkey, tx, InlineType.KEY);
        in.movePositionTo(currentPos);
    }
    if (!excludeProperties) {
        //read value signature
        readInlineTypes(def.getSignature(), properties, in, tx, InlineType.SIGNATURE);
        //Third: read rest
        while (in.hasRemaining()) {
            PropertyKey type = tx.getExistingPropertyKey(IDHandler.readInlineRelationType(in));
            Object pvalue = readInline(in, type, InlineType.NORMAL);
            assert pvalue != null;
            properties.put(type.longId(), pvalue);
        }
        if (data.hasMetaData()) {
            for (Map.Entry<EntryMetaData, Object> metas : data.getMetaData().entrySet()) {
                ImplicitKey key = ImplicitKey.MetaData2ImplicitKey.get(metas.getKey());
                if (key != null) {
                    assert metas.getValue() != null;
                    properties.put(key.longId(), metas.getValue());
                }
            }
        }
    }
    return new RelationCache(dir, typeId, relationId, other, properties);
}
Also used : LongObjectHashMap(com.carrotsearch.hppc.LongObjectHashMap) RelationCache(com.thinkaurelius.titan.graphdb.relations.RelationCache) EdgeDirection(com.thinkaurelius.titan.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) EntryMetaData(com.thinkaurelius.titan.diskstorage.EntryMetaData) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) RelationTypeParse(com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.RelationTypeParse) Map(java.util.Map) LongObjectHashMap(com.carrotsearch.hppc.LongObjectHashMap) ImplicitKey(com.thinkaurelius.titan.graphdb.types.system.ImplicitKey)

Example 18 with Direction

use of org.apache.tinkerpop.gremlin.structure.Direction in project janusgraph by JanusGraph.

the class AdjacentVertexFilterOptimizerStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
        // Check if this filter traversal matches the pattern: _.inV/outV/otherV.is(x)
        Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
        List<Step> steps = filterTraversal.getSteps();
        if (steps.size() == 2 && (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) && (steps.get(1) instanceof IsStep)) {
            // Get the direction in which we filter on the adjacent vertex (or null if not a valid adjacency filter)
            Direction direction = null;
            if (steps.get(0) instanceof EdgeVertexStep) {
                EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
                if (evs.getDirection() != Direction.BOTH)
                    direction = evs.getDirection();
            } else {
                assert steps.get(0) instanceof EdgeOtherVertexStep;
                direction = Direction.BOTH;
            }
            P predicate = ((IsStep) steps.get(1)).getPredicate();
            // Check that we have a valid direction and a valid vertex filter predicate
            if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
                JanusGraphVertex vertex = JanusGraphTraversalUtil.getJanusGraphVertex((Vertex) predicate.getValue());
                // Now, check that this step is preceded by VertexStep that returns edges
                Step<?, ?> currentStep = originalStep.getPreviousStep();
                while (true) {
                    if (!(currentStep instanceof HasStep) && !(currentStep instanceof IdentityStep)) {
                        break;
                    }
                // We can jump over other steps as we move backward
                }
                if (currentStep instanceof VertexStep) {
                    VertexStep vertexStep = (VertexStep) currentStep;
                    if (vertexStep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vertexStep.getDirection().opposite()))) {
                        // Now replace the step with a has condition
                        TraversalHelper.replaceStep(originalStep, new HasStep(traversal, HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal);
                    }
                }
            }
        }
    });
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) Direction(org.apache.tinkerpop.gremlin.structure.Direction) P(org.apache.tinkerpop.gremlin.process.traversal.P) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep)

Example 19 with Direction

use of org.apache.tinkerpop.gremlin.structure.Direction in project atlas by apache.

the class AtlasJanusGraphManagement method createEdgeIndex.

@Override
public void createEdgeIndex(String label, String indexName, AtlasEdgeDirection edgeDirection, List<AtlasPropertyKey> propertyKeys) {
    EdgeLabel edgeLabel = management.getEdgeLabel(label);
    if (edgeLabel == null) {
        edgeLabel = management.makeEdgeLabel(label).make();
    }
    Direction direction = AtlasJanusObjectFactory.createDirection(edgeDirection);
    PropertyKey[] keys = AtlasJanusObjectFactory.createPropertyKeys(propertyKeys);
    if (management.getRelationIndex(edgeLabel, indexName) == null) {
        management.buildEdgeIndex(edgeLabel, indexName, direction, keys);
    }
}
Also used : EdgeLabel(org.janusgraph.core.EdgeLabel) AtlasEdgeLabel(org.apache.atlas.repository.graphdb.AtlasEdgeLabel) AtlasEdgeDirection(org.apache.atlas.repository.graphdb.AtlasEdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) AtlasPropertyKey(org.apache.atlas.repository.graphdb.AtlasPropertyKey) PropertyKey(org.janusgraph.core.PropertyKey)

Example 20 with Direction

use of org.apache.tinkerpop.gremlin.structure.Direction in project unipop by unipop-graph.

the class UniGraphEdgeVertexStep method process.

@Override
protected Iterator<Traverser.Admin<Vertex>> process(List<Traverser.Admin<Edge>> traversers) {
    List<Traverser.Admin<Vertex>> vertices = new ArrayList<>();
    traversers.forEach(travrser -> {
        travrser.get().vertices(direction).forEachRemaining(vertex -> vertices.add(travrser.split(vertex, this)));
    });
    if (propertyKeys == null || propertyKeys.size() > 1) {
        List<DeferredVertex> v = vertices.stream().map(Attachable::get).filter(vertex -> vertex instanceof DeferredVertex).map(vertex -> ((DeferredVertex) vertex)).filter(DeferredVertex::isDeferred).collect(Collectors.toList());
        if (v.size() > 0) {
            DeferredVertexQuery query = new DeferredVertexQuery(v, propertyKeys, orders, this.stepDescriptor, traversal);
            deferredVertexControllers.forEach(deferredVertexController -> deferredVertexController.fetchProperties(query));
        }
    }
    return vertices.iterator();
}
Also used : DeferredVertex(org.unipop.schema.reference.DeferredVertex) java.util(java.util) Attachable(org.apache.tinkerpop.gremlin.structure.util.Attachable) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Collectors(java.util.stream.Collectors) Pair(org.javatuples.Pair) StepDescriptor(org.unipop.query.StepDescriptor) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Orderable(org.unipop.process.order.Orderable) Profiling(org.apache.tinkerpop.gremlin.process.traversal.step.Profiling) TraverserRequirement(org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement) MutableMetrics(org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics) ControllerManager(org.unipop.query.controller.ControllerManager) UniGraph(org.unipop.structure.UniGraph) StringFactory(org.apache.tinkerpop.gremlin.structure.util.StringFactory) DeferredVertexQuery(org.unipop.query.search.DeferredVertexQuery) UniPredicatesStep(org.unipop.process.UniPredicatesStep) Edge(org.apache.tinkerpop.gremlin.structure.Edge) DeferredVertex(org.unipop.schema.reference.DeferredVertex) DeferredVertexQuery(org.unipop.query.search.DeferredVertexQuery) Attachable(org.apache.tinkerpop.gremlin.structure.util.Attachable)

Aggregations

Direction (org.apache.tinkerpop.gremlin.structure.Direction)30 Edge (org.apache.tinkerpop.gremlin.structure.Edge)10 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)8 PropertyKey (org.janusgraph.core.PropertyKey)8 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)7 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)7 RelationType (org.janusgraph.core.RelationType)6 AtlasEdgeDirection (org.apache.atlas.repository.graphdb.AtlasEdgeDirection)5 Map (java.util.Map)4 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)4 StandardJanusGraphTx (org.janusgraph.graphdb.transaction.StandardJanusGraphTx)4 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)3 ImplicitKey (com.thinkaurelius.titan.graphdb.types.system.ImplicitKey)3 HashMap (java.util.HashMap)3 P (org.apache.tinkerpop.gremlin.process.traversal.P)3 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)3 EdgeOtherVertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep)3 EdgeVertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep)3