Search in sources :

Example 1 with GraphType

use of com.alibaba.graphscope.gaia.store.GraphType in project GraphScope by alibaba.

the class SchemaIdMakerStrategy method apply.

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    try {
        // label string -> label id
        List<Step> steps = traversal.getSteps();
        for (int i = 0; i < steps.size(); ++i) {
            Step step = steps.get(i);
            if (step instanceof HasContainerHolder) {
                List<HasContainer> containers = ((HasContainerHolder) step).getHasContainers();
                for (HasContainer container : containers) {
                    if (container.getKey().equals(T.label.getAccessor())) {
                        P predicate = container.getPredicate();
                        if (predicate.getValue() instanceof List && ((List) predicate.getValue()).get(0) instanceof String) {
                            List<String> values = (List<String>) predicate.getValue();
                            predicate.setValue(values.stream().map(k -> {
                                if (StringUtils.isNumeric(k)) {
                                    return k;
                                } else {
                                    long labelId = graphStore.getLabelId(k);
                                    return String.valueOf(labelId);
                                }
                            }).collect(Collectors.toList()));
                        } else if (predicate.getValue() instanceof String) {
                            String value = (String) predicate.getValue();
                            if (StringUtils.isNumeric(value)) {
                                predicate.setValue(value);
                            } else {
                                long labelId = graphStore.getLabelId(value);
                                predicate.setValue(String.valueOf(labelId));
                            }
                        } else {
                            throw new UnsupportedOperationException("hasLabel value type not support " + predicate.getValue().getClass());
                        }
                    }
                }
            } else if (step instanceof VertexStep) {
                String[] edgeLabels = ((VertexStep) step).getEdgeLabels();
                for (int j = 0; j < edgeLabels.length; ++j) {
                    if (StringUtils.isNumeric(edgeLabels[j])) {
                    // do nothing
                    } else {
                        long labelId = graphStore.getLabelId(edgeLabels[j]);
                        edgeLabels[j] = String.valueOf(labelId);
                    }
                }
            }
        }
        GraphType graphType = config.getGraphType();
        // property string -> property id
        if (graphType == GraphType.MAXGRAPH) {
            for (int i = 0; i < steps.size(); ++i) {
                Step step = steps.get(i);
                if (step instanceof HasContainerHolder) {
                    List<HasContainer> containers = ((HasContainerHolder) step).getHasContainers();
                    for (HasContainer container : containers) {
                        container.setKey(PlanUtils.convertToPropertyId(graphStore, container.getKey()));
                    }
                } else if (step instanceof PropertiesStep || step instanceof PropertyMapStep) {
                    String[] oldKeys;
                    if (step instanceof PropertiesStep) {
                        oldKeys = ((PropertiesStep) step).getPropertyKeys();
                    } else {
                        oldKeys = ((PropertyMapStep) step).getPropertyKeys();
                    }
                    String[] newKeys = Arrays.stream(oldKeys).map(k -> PlanUtils.convertToPropertyId(graphStore, k)).toArray(String[]::new);
                    FieldUtils.writeField(step, "propertyKeys", newKeys, true);
                } else if (step instanceof ByModulating) {
                    TraversalParent byParent = (TraversalParent) step;
                    for (Traversal.Admin k : byParent.getLocalChildren()) {
                        if (k instanceof ElementValueTraversal) {
                            ElementValueTraversal value = (ElementValueTraversal) k;
                            String propertyId = PlanUtils.convertToPropertyId(graphStore, value.getPropertyKey());
                            FieldUtils.writeField(value, "propertyKey", propertyId, true);
                        }
                    }
                }
            }
        }
    } catch (SchemaNotFoundException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ByModulating(org.apache.tinkerpop.gremlin.process.traversal.step.ByModulating) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) PropertyMapStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) PropertyMapStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException) P(org.apache.tinkerpop.gremlin.process.traversal.P) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent) GraphType(com.alibaba.graphscope.gaia.store.GraphType) HasContainerHolder(org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder) List(java.util.List) SchemaNotFoundException(com.alibaba.graphscope.gaia.store.SchemaNotFoundException)

Aggregations

GraphType (com.alibaba.graphscope.gaia.store.GraphType)1 SchemaNotFoundException (com.alibaba.graphscope.gaia.store.SchemaNotFoundException)1 List (java.util.List)1 P (org.apache.tinkerpop.gremlin.process.traversal.P)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 ElementValueTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal)1 ByModulating (org.apache.tinkerpop.gremlin.process.traversal.step.ByModulating)1 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)1 TraversalParent (org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent)1 PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)1 PropertyMapStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertyMapStep)1 VertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep)1 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)1