Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.hugegraph.task.TaskCallable;
import org.apache.hugegraph.task.TaskCallable.SysTaskCallable;
import org.apache.hugegraph.task.TaskManager;
import org.apache.hugegraph.traversal.optimize.HugeConnectiveLabelStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeCountStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeGraphStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeVertexStepStrategy;
Expand Down Expand Up @@ -468,6 +469,10 @@ private static void registerPrivateActions() {
"createDefaultExecutor", "lambda$start$0", "start");
Reflection.registerFieldsToFilter(JsonSerializer.class, "LBUF_SIZE", "INSTANCE");
Reflection.registerMethodsToFilter(JsonSerializer.class, "writeIterator", "instance");
Reflection.registerFieldsToFilter(HugeConnectiveLabelStepStrategy.class,
"serialVersionUID", "INSTANCE");
Reflection.registerMethodsToFilter(HugeConnectiveLabelStepStrategy.class,
"instance");
Reflection.registerFieldsToFilter(HugeVertexStepStrategy.class, "serialVersionUID",
"INSTANCE");
Reflection.registerMethodsToFilter(HugeVertexStepStrategy.class, "instance");
Expand Down Expand Up @@ -559,6 +564,7 @@ private static void genRegisterPrivateActions() {
registerPrivateActions(LockManager.class);
registerPrivateActions(ServerReporter.class);
registerPrivateActions(JsonSerializer.class);
registerPrivateActions(HugeConnectiveLabelStepStrategy.class);
registerPrivateActions(HugeVertexStepStrategy.class);
registerPrivateActions(HugeGraphStepStrategy.class);
registerPrivateActions(HugeCountStepStrategy.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.hugegraph.schema.VertexLabel;
import org.apache.hugegraph.structure.HugeFeatures;
import org.apache.hugegraph.task.TaskScheduler;
import org.apache.hugegraph.traversal.optimize.HugeConnectiveLabelStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeCountStrategy;
import org.apache.hugegraph.traversal.optimize.HugeCountStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeGraphStepStrategy;
Expand Down Expand Up @@ -381,7 +382,8 @@ static void registerTraversalStrategies(Class<?> clazz) {
.getStrategies(Graph.class)
.clone();
strategies.removeStrategies(CountStrategy.class);
strategies.addStrategies(HugeCountStrategy.instance(),
strategies.addStrategies(HugeConnectiveLabelStepStrategy.instance(),
HugeCountStrategy.instance(),
HugeVertexStepStrategy.instance(),
HugeGraphStepStrategy.instance(),
HugeCountStepStrategy.instance(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.traversal.optimize;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;

import org.apache.tinkerpop.gremlin.process.traversal.Step;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.AndStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.OrStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;

public final class HugeConnectiveLabelStepStrategy
extends AbstractTraversalStrategy<TraversalStrategy.OptimizationStrategy>
implements TraversalStrategy.OptimizationStrategy {

private static final long serialVersionUID = 2532355470697047377L;

private static final HugeConnectiveLabelStepStrategy INSTANCE =
new HugeConnectiveLabelStepStrategy();

private HugeConnectiveLabelStepStrategy() {
// pass
}

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
Set<Traversal.Admin<?, ?>> visited =
Collections.newSetFromMap(new IdentityHashMap<>());
markConnectiveLabelSteps(traversal, visited);
}

@Override
public Set<Class<? extends TraversalStrategy.OptimizationStrategy>> applyPost() {
return Collections.singleton(InlineFilterStrategy.class);
}

public static HugeConnectiveLabelStepStrategy instance() {
return INSTANCE;
}

private static void markConnectiveLabelSteps(Traversal.Admin<?, ?> traversal,
Set<Traversal.Admin<?, ?>> visited) {
if (!visited.add(traversal)) {
return;
}

for (AndStep<?> step : TraversalHelper.getStepsOfClass(AndStep.class,
Comment thread
LegendPei marked this conversation as resolved.
traversal)) {
markConnectiveLabelChildren(step, step);
}
for (OrStep<?> step : TraversalHelper.getStepsOfClass(OrStep.class,
traversal)) {
markConnectiveLabelChildren(step, step);
}

for (Step<?, ?> step : traversal.getSteps()) {
if (!(step instanceof TraversalParent)) {
continue;
}
TraversalParent parent = (TraversalParent) step;
for (Traversal.Admin<?, ?> child : parent.getLocalChildren()) {
markConnectiveLabelSteps(child, visited);
}
for (Traversal.Admin<?, ?> child : parent.getGlobalChildren()) {
markConnectiveLabelSteps(child, visited);
}
}
}

private static void markConnectiveLabelChildren(Step<?, ?> step,
TraversalParent parent) {
if (!(step.getPreviousStep() instanceof HasStep)) {
return;
}
for (Traversal.Admin<?, ?> child : parent.getLocalChildren()) {
markPositiveLabelOnlyTraversal(child);
}
}

private static void markPositiveLabelOnlyTraversal(
Traversal.Admin<?, ?> traversal) {
for (Step<?, ?> step : traversal.getSteps()) {
if (!(step instanceof HasStep)) {
return;
}
HasStep<?> hasStep = (HasStep<?>) step;
if (!hasOnlyPositiveLabelContainers(hasStep)) {
return;
}
}

for (Step<?, ?> step : traversal.getSteps()) {
TraversalUtil.markConnectiveLabelStep(step);
}
}

private static boolean hasOnlyPositiveLabelContainers(HasStep<?> step) {
if (step.getHasContainers().isEmpty()) {
return false;
}
for (HasContainer has : step.getHasContainers()) {
Comment thread
LegendPei marked this conversation as resolved.
if (!TraversalUtil.isPositiveLabelContainer(has)) {
return false;
}
}
return true;
}
}
Loading
Loading