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();
}
Aggregations