imbajin commented on code in PR #2864: URL: https://github.com/apache/incubator-hugegraph/pull/2864#discussion_r2313296382
########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/AdjacentEdgesQuery.java: ########## @@ -0,0 +1,396 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.backend.query; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.query.Condition.Relation; +import org.apache.hugegraph.backend.query.Condition.RelationType; +import org.apache.hugegraph.structure.HugeEdge; +import org.apache.hugegraph.structure.HugeElement; +import org.apache.hugegraph.type.HugeType; +import org.apache.hugegraph.type.define.Directions; +import org.apache.hugegraph.type.define.HugeKeys; +import org.apache.hugegraph.util.CollectionUtil; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.InsertionOrderUtil; + +import com.google.common.collect.ImmutableList; + +public class AdjacentEdgesQuery extends ConditionQuery { + + private Id ownerVertex; + private Directions direction; + private Id[] edgeLabels; + + private List<Condition> selfConditions; + + private static final Id[] EMPTY = new Id[0]; + + public AdjacentEdgesQuery(Id ownerVertex, Directions direction) { + this(ownerVertex, direction, EMPTY); + } + + public AdjacentEdgesQuery(Id ownerVertex, Directions direction, + Id[] edgeLabels) { + super(HugeType.EDGE); + E.checkArgument(ownerVertex != null, + "The edge query must contain source vertex"); + E.checkArgument(direction != null, + "The edge query must contain direction"); + this.ownerVertex = ownerVertex; + this.direction = direction; + this.edgeLabels = edgeLabels; + this.selfConditions = null; + } + + @Override + public int conditionsSize() { + int size = super.conditionsSize() + 2; + if (this.edgeLabels.length > 0) { + size += 1; + } + return size; + } + + @Override + public ConditionQuery query(Condition condition) { + if (!condition.isRelation()) { + return super.query(condition); + } + + // Reset this.selfConditions cache + this.selfConditions = null; + + // Update condition fields + Relation relation = ((Condition.Relation) condition); + Object key = relation.key(); + RelationType relationType = relation.relation(); + + if (key == HugeKeys.OWNER_VERTEX) { + this.ownerVertex = (Id) relation.value(); + return this; + } + if (key == HugeKeys.DIRECTION) { + this.direction = (Directions) relation.value(); + return this; + } + if (key == HugeKeys.LABEL) { + Collection<Id> labels = null; + if (relationType == RelationType.EQ) { + labels = ImmutableList.of((Id) relation.value()); + } else if (relationType == RelationType.IN) { + @SuppressWarnings("unchecked") + Collection<Id> value = (Collection<Id>) relation.value(); + labels = value; + } else { + E.checkArgument(false, + "Unexpected relation type '%s' for '%s'", + relationType, key); + } + + if (this.edgeLabels.length == 0) { + this.edgeLabels = labels.toArray(new Id[0]); + } else { + Collection<Id> edgeLabels = CollectionUtil.intersect( + Arrays.asList(this.edgeLabels), + labels); + if (edgeLabels.isEmpty()) { + // Returns empty result if conditions are conflicting + this.limit(0L); + } + this.edgeLabels = edgeLabels.toArray(new Id[0]); + } + return this; + } + + return super.query(condition); + } + + @Override + public Collection<Condition> conditions() { + List<Condition> conds = this.selfConditions(); + if (super.conditionsSize() > 0) { + conds.addAll(super.conditions()); + } + return conds; + } + + private List<Condition> selfConditions() { + if (this.selfConditions != null) { + /* + * Return selfConditions cache if it has been collected before + * NOTE: it's also to keep the serialized condition value + */ + return this.selfConditions; + } + + List<Condition> conds = InsertionOrderUtil.newList(); + + conds.add(Condition.eq(HugeKeys.OWNER_VERTEX, this.ownerVertex)); + + if (this.direction == Directions.BOTH) { + conds.add(Condition.in(HugeKeys.DIRECTION, ImmutableList.of( + Directions.OUT, + Directions.IN))); + } else { + conds.add(Condition.eq(HugeKeys.DIRECTION, this.direction)); + } + + if (this.edgeLabels.length == 1) { + conds.add(Condition.eq(HugeKeys.LABEL, this.edgeLabels[0])); + } else if (this.edgeLabels.length > 1) { + conds.add(Condition.in(HugeKeys.LABEL, + Arrays.asList(this.edgeLabels))); + } + + this.selfConditions = conds; + return conds; + } + + @Override + public <T> T condition(Object key) { + T cond = this.selfCondition(key); + if (cond != null) { + return cond; + } + return super.condition(key); + } + + @SuppressWarnings("unchecked") + private <T> T selfCondition(Object key) { + if (key == HugeKeys.OWNER_VERTEX) { + return (T) this.ownerVertex; + } + if (key == HugeKeys.DIRECTION) { + return (T) this.direction; + } Review Comment: The logic in selfCondition() at line 186 should return an array for consistency when edgeLabels.length > 1, or throw the exception immediately instead of having an unreachable return statement. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@hugegraph.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@hugegraph.apache.org For additional commands, e-mail: issues-h...@hugegraph.apache.org