Search in sources :

Example 1 with PreBySubTraversal

use of com.alibaba.graphscope.gaia.plan.strategy.PreBySubTraversal in project GraphScope by alibaba.

the class TagKeyExtractor method modulateBy.

default Gremlin.ByKey modulateBy(Traversal.Admin value) {
    Gremlin.ByKey.Builder builder = Gremlin.ByKey.newBuilder();
    if (value == null || value instanceof IdentityTraversal) {
        return Gremlin.ByKey.newBuilder().build();
    } else if (value instanceof ElementValueTraversal) {
        String propertyKey = ((ElementValueTraversal) value).getPropertyKey();
        if (StringUtils.isNumeric(propertyKey)) {
            builder.setKey(Common.Key.newBuilder().setNameId(Integer.valueOf(propertyKey)));
        } else {
            builder.setKey(Common.Key.newBuilder().setName(propertyKey));
        }
    } else if (value instanceof TokenTraversal) {
        // resultKeys.add(((TokenTraversal) value).getToken().getAccessor());
        T token = ((TokenTraversal) value).getToken();
        if (token == T.label) {
            builder.setKey(Common.Key.newBuilder().setLabel(Common.LabelKey.newBuilder()));
        } else if (token == T.id) {
            builder.setKey(Common.Key.newBuilder().setId(Common.IdKey.newBuilder()));
        } else {
            throw new UnsupportedOperationException("cannot support other T type " + token.name());
        }
    } else if (value instanceof ColumnTraversal) {
        Column column = ((ColumnTraversal) value).getColumn();
        if (column == Column.keys) {
            builder.setMapKeys(Gremlin.MapKey.newBuilder());
        } else {
            builder.setMapValues(Gremlin.MapValue.newBuilder());
        }
    } else if (value != null && value.getSteps().size() == 1 && value.getStartStep() instanceof PropertyMapStep) {
        PropertyMapStep propertyMapStep = (PropertyMapStep) value.getStartStep();
        String[] propertyKeys = propertyMapStep.getPropertyKeys();
        boolean needAllProps = (propertyKeys == null || propertyKeys.length == 0) ? true : false;
        builder.setPropKeys(PlanUtils.convertFrom(new ToFetchProperties(needAllProps, Arrays.asList(propertyKeys))));
    } else if (value != null && value.getSteps().size() == 1 && value.getStartStep() instanceof PropertiesStep) {
        PropertiesStep propertiesStep = (PropertiesStep) value.getStartStep();
        // todo: if value() -> fetch first from all properties (support by runtime)
        if (propertiesStep.getPropertyKeys() != null && propertiesStep.getPropertyKeys().length > 0) {
            String propertyKey = (propertiesStep.getPropertyKeys())[0];
            if (StringUtils.isNumeric(propertyKey)) {
                builder.setKey(Common.Key.newBuilder().setNameId(Integer.valueOf(propertyKey)));
            } else {
                builder.setKey(Common.Key.newBuilder().setName(propertyKey));
            }
        }
    } else if (value != null && value instanceof PreBySubTraversal) {
        builder.setComputed(Gremlin.SubValue.newBuilder());
    } else if (value.getSteps().size() == 1 && value.getStartStep() instanceof TraversalMapStep) {
        TraversalMapStep startStep = (TraversalMapStep) value.getStartStep();
        Traversal.Admin mapTraversal = (Traversal.Admin) startStep.getLocalChildren().get(0);
        if (!(mapTraversal instanceof ColumnTraversal)) {
            throw new UnsupportedOperationException();
        } else {
            return modulateBy(mapTraversal);
        }
    } else if (value.getSteps().size() == 2 && (value.getStartStep() instanceof TraversalMapStep && value.getEndStep() instanceof PropertiesStep)) {
        TraversalMapStep startStep = (TraversalMapStep) value.getStartStep();
        Traversal.Admin mapTraversal = (Traversal.Admin) startStep.getLocalChildren().get(0);
        if (!(mapTraversal instanceof ColumnTraversal)) {
            throw new UnsupportedOperationException();
        } else {
            Gremlin.ByKey selectMap = modulateBy(mapTraversal);
            PropertiesStep valuePropertiesStep = (PropertiesStep) value.getEndStep();
            Traversal.Admin tmp = new DefaultTraversal();
            tmp.addStep(new PropertiesStep(tmp, valuePropertiesStep.getReturnType(), valuePropertiesStep.getPropertyKeys()));
            // select(keys).values("xxx")
            Gremlin.ByKey selectMapWithValue = modulateBy(tmp);
            if (selectMap.getItemCase() == Gremlin.ByKey.ItemCase.MAP_KEYS) {
                return Gremlin.ByKey.newBuilder().setMapKeys(Gremlin.MapKey.newBuilder().setKey(selectMapWithValue.getKey())).build();
            } else {
                return Gremlin.ByKey.newBuilder().setMapValues(Gremlin.MapValue.newBuilder().setKey(selectMapWithValue.getKey())).build();
            }
        }
    } else {
        throw new UnsupportedOperationException("cannot support other value traversal " + value);
    }
    return builder.build();
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) DefaultTraversal(org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal) PropertyMapStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep) ToFetchProperties(com.alibaba.graphscope.gaia.plan.strategy.global.property.cache.ToFetchProperties) IdentityTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) DefaultTraversal(org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal) ColumnTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ColumnTraversal) PreBySubTraversal(com.alibaba.graphscope.gaia.plan.strategy.PreBySubTraversal) TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) PreBySubTraversal(com.alibaba.graphscope.gaia.plan.strategy.PreBySubTraversal) IdentityTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal) TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) T(org.apache.tinkerpop.gremlin.structure.T) Column(org.apache.tinkerpop.gremlin.structure.Column) TraversalMapStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.TraversalMapStep) Gremlin(com.alibaba.graphscope.common.proto.Gremlin) ColumnTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ColumnTraversal)

Aggregations

Gremlin (com.alibaba.graphscope.common.proto.Gremlin)1 PreBySubTraversal (com.alibaba.graphscope.gaia.plan.strategy.PreBySubTraversal)1 ToFetchProperties (com.alibaba.graphscope.gaia.plan.strategy.global.property.cache.ToFetchProperties)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 ColumnTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ColumnTraversal)1 ElementValueTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal)1 IdentityTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal)1 TokenTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal)1 PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)1 PropertyMapStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep)1 TraversalMapStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.TraversalMapStep)1 DefaultTraversal (org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal)1 Column (org.apache.tinkerpop.gremlin.structure.Column)1 T (org.apache.tinkerpop.gremlin.structure.T)1