Skip to content

Commit

Permalink
Put support ElementValueComparator back for mixed index.
Browse files Browse the repository at this point in the history
Issue : 872

Signed-off-by: David Clement <[email protected]>
  • Loading branch information
davidclement90 committed Mar 10, 2018
1 parent ec5825e commit ae357ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer;

import org.javatuples.Pair;
Expand Down Expand Up @@ -68,7 +69,7 @@ static boolean validJanusGraphHas(HasContainer has) {
}

static boolean validJanusGraphHas(Iterable<HasContainer> has) {
for (HasContainer h : has) {
for (final HasContainer h : has) {
if (!validJanusGraphHas(h)) return false;
}
return true;
Expand All @@ -77,21 +78,26 @@ static boolean validJanusGraphHas(Iterable<HasContainer> has) {
static boolean validJanusGraphOrder(OrderGlobalStep ostep, Traversal rootTraversal,
boolean isVertexOrder) {
final List<Pair<Traversal.Admin, Object>> comparators = ostep.getComparators();
for(Pair<Traversal.Admin, Object> comp : comparators) {
for(final Pair<Traversal.Admin, Object> comp : comparators) {
final String key;
if (comp.getValue0() instanceof ElementValueTraversal &&
comp.getValue1() instanceof Order) {
final String key = ((ElementValueTraversal) comp.getValue0()).getPropertyKey();
final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin());
final PropertyKey pKey = tx.getPropertyKey(key);
if(pKey == null
|| !(Comparable.class.isAssignableFrom(pKey.dataType()))
|| (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) {
return false;
}
key = ((ElementValueTraversal) comp.getValue0()).getPropertyKey();
} else if (comp.getValue1() instanceof ElementValueComparator) {
final ElementValueComparator evc = (ElementValueComparator) comp.getValue1();
if (!(evc.getValueComparator() instanceof Order)) return false;
key = evc.getPropertyKey();
} else {
// do not fold comparators that include nested traversals that are not simple ElementValues
return false;
}
final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin());
final PropertyKey pKey = tx.getPropertyKey(key);
if (pKey == null
|| !(Comparable.class.isAssignableFrom(pKey.dataType()))
|| (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) {
return false;
}
}

return true;
Expand Down Expand Up @@ -138,7 +144,7 @@ static void foldInHasContainer(final HasStepFolder janusgraphStep, final Travers
Step<?, ?> currentStep = janusgraphStep.getNextStep();
while (true) {
if (currentStep instanceof HasContainerHolder) {
Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers();
final Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers();
if (validJanusGraphHas(containers)) {
janusgraphStep.addAll(containers);
currentStep.getLabels().forEach(janusgraphStep::addLabel);
Expand Down Expand Up @@ -178,17 +184,20 @@ static void foldInOrder(final HasStepFolder janusgraphStep, final Traversal.Admi
currentStep = currentStep.getNextStep();
}

if (lastOrder != null) {
if (validJanusGraphOrder(lastOrder, rootTraversal, isVertexOrder)) {
//Add orders to HasStepFolder
for (Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>> comp :
(List<Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>>>) ((OrderGlobalStep) lastOrder).getComparators()) {
ElementValueTraversal evt = (ElementValueTraversal) comp.getValue0();
if (lastOrder != null && validJanusGraphOrder(lastOrder, rootTraversal, isVertexOrder)) {
//Add orders to HasStepFolder
for (final Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>> comp :
(List<Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>>>) ((OrderGlobalStep) lastOrder).getComparators()) {
if (comp.getValue0() instanceof ElementValueTraversal) {
final ElementValueTraversal evt = (ElementValueTraversal) comp.getValue0();
janusgraphStep.orderBy(evt.getPropertyKey(), (Order) comp.getValue1());
} else {
final ElementValueComparator evc = (ElementValueComparator) comp.getValue1();
janusgraphStep.orderBy(evc.getPropertyKey(), (Order) evc.getValueComparator());
}
lastOrder.getLabels().forEach(janusgraphStep::addLabel);
traversal.removeStep(lastOrder);
}
lastOrder.getLabels().forEach(janusgraphStep::addLabel);
traversal.removeStep(lastOrder);
}
}

Expand Down Expand Up @@ -218,7 +227,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

OrderEntry that = (OrderEntry) o;
final OrderEntry that = (OrderEntry) o;

if (key != null ? !key.equals(that.key) : that.key != null) return false;
return order == that.order;
Expand All @@ -241,11 +250,11 @@ public String toString() {
}

static <E extends Ranging> void foldInRange(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal) {
Step<?, ?> nextStep = JanusGraphTraversalUtil.getNextNonIdentityStep(janusgraphStep);
final Step<?, ?> nextStep = JanusGraphTraversalUtil.getNextNonIdentityStep(janusgraphStep);

if (nextStep instanceof RangeGlobalStep) {
RangeGlobalStep range = (RangeGlobalStep) nextStep;
int limit = QueryUtil.convertLimit(range.getHighRange());
final RangeGlobalStep range = (RangeGlobalStep) nextStep;
final int limit = QueryUtil.convertLimit(range.getHighRange());
janusgraphStep.setLimit(QueryUtil.mergeLimits(limit, janusgraphStep.getLimit()));
if (range.getLowRange() == 0) { //Range can be removed since there is no offset
nextStep.getLabels().forEach(janusgraphStep::addLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.FilterRankingStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy;
Expand Down Expand Up @@ -137,6 +138,8 @@ public static Iterable<Object[]> generateTestParameters() {
{g.V().hasId(1).hasLabel("Person"), g_V(T.id, 1, "~label", eq("Person")), Collections.emptyList()},
{g.V().hasLabel("Person").has("lang", "java").order().by("name"),
g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.incr)), Collections.emptyList()},
{g.V().hasLabel("Person").has("lang", "java").order().by(new ElementValueComparator("name", Order.incr)),
g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.incr)), Collections.emptyList()},
// same as above, different order
{g.V().hasLabel("Person").has("lang", "java").order().by("name", Order.decr),
g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.decr)), Collections.emptyList()},
Expand Down

0 comments on commit ae357ec

Please sign in to comment.