Skip to content

Commit

Permalink
Refactor MATCH traversal procedures and fix some NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed Nov 21, 2017
1 parent a438b57 commit c72d20c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 30 deletions.
Expand Up @@ -116,13 +116,14 @@ protected Iterable<OResultInternal> executeTraversal(OCommandContext iCommandCon


if (whileCondition == null && maxDepth == null) {// in this case starting point is not returned and only one level depth is if (whileCondition == null && maxDepth == null) {// in this case starting point is not returned and only one level depth is
// evaluated // evaluated
Iterable<OIdentifiable> queryResult = traversePatternEdge(startingPoint, iCommandContext); Iterable<OResultInternal> queryResult = traversePatternEdge(startingPoint, iCommandContext);


for (OIdentifiable origin : queryResult) { for (OResultInternal origin : queryResult) {
Object previousMatch = iCommandContext.getVariable("$currentMatch"); Object previousMatch = iCommandContext.getVariable("$currentMatch");
iCommandContext.setVariable("$currentMatch", origin); OElement elem = origin.toElement();
if (matchesFilters(iCommandContext, filter, origin) && matchesClass(iCommandContext, className, origin)) { iCommandContext.setVariable("$currentMatch", elem);
result.add(new OResultInternal(origin)); if (matchesFilters(iCommandContext, filter, elem) && matchesClass(iCommandContext, className, elem)) {
result.add(origin);
} }
iCommandContext.setVariable("$currentMatch", previousMatch); iCommandContext.setVariable("$currentMatch", previousMatch);
} }
Expand All @@ -144,9 +145,9 @@ protected Iterable<OResultInternal> executeTraversal(OCommandContext iCommandCon
if ((maxDepth == null || depth < maxDepth) && (whileCondition == null || whileCondition if ((maxDepth == null || depth < maxDepth) && (whileCondition == null || whileCondition
.matchesFilters(startingPoint, iCommandContext))) { .matchesFilters(startingPoint, iCommandContext))) {


Iterable<OIdentifiable> queryResult = traversePatternEdge(startingPoint, iCommandContext); Iterable<OResultInternal> queryResult = traversePatternEdge(startingPoint, iCommandContext);


for (OIdentifiable origin : queryResult) { for (OResultInternal origin : queryResult) {
// if(origin.equals(startingPoint)){ // if(origin.equals(startingPoint)){
// continue; // continue;
// } // }
Expand All @@ -156,9 +157,11 @@ protected Iterable<OResultInternal> executeTraversal(OCommandContext iCommandCon
if (pathToHere != null) { if (pathToHere != null) {
newPath.addAll(pathToHere); newPath.addAll(pathToHere);
} }
newPath.add(origin.getIdentity());


Iterable<OResultInternal> subResult = executeTraversal(iCommandContext, item, origin, depth + 1, newPath); OElement elem = origin.toElement();
newPath.add(elem.getIdentity());

Iterable<OResultInternal> subResult = executeTraversal(iCommandContext, item, elem, depth + 1, newPath);
if (subResult instanceof Collection) { if (subResult instanceof Collection) {
result.addAll((Collection<? extends OResultInternal>) subResult); result.addAll((Collection<? extends OResultInternal>) subResult);
} else { } else {
Expand Down Expand Up @@ -210,7 +213,7 @@ protected boolean matchesFilters(OCommandContext iCommandContext, OWhereClause f


//TODO refactor this method to receive the item. //TODO refactor this method to receive the item.


protected Iterable<OIdentifiable> traversePatternEdge(OIdentifiable startingPoint, OCommandContext iCommandContext) { protected Iterable<OResultInternal> traversePatternEdge(OIdentifiable startingPoint, OCommandContext iCommandContext) {


Iterable possibleResults = null; Iterable possibleResults = null;
if (this.item.getFilter() != null) { if (this.item.getFilter() != null) {
Expand All @@ -226,7 +229,28 @@ protected Iterable<OIdentifiable> traversePatternEdge(OIdentifiable startingPoin
} }


Object qR = this.item.getMethod().execute(startingPoint, possibleResults, iCommandContext); Object qR = this.item.getMethod().execute(startingPoint, possibleResults, iCommandContext);
return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((OIdentifiable) qR); if (qR == null) {
return Collections.EMPTY_LIST;
}
if (qR instanceof OIdentifiable) {
return Collections.singleton(new OResultInternal((OIdentifiable) qR));
}
if (qR instanceof Iterable) {
Iterable iterable = (Iterable) qR;
List<OResultInternal> result = new ArrayList<>();
for (Object o : iterable) {
if (o instanceof OIdentifiable) {
result.add(new OResultInternal((OIdentifiable) o));
} else if (o instanceof OResultInternal) {
result.add((OResultInternal) o);
}
else{
throw new UnsupportedOperationException();
}
}
return result;
}
return Collections.EMPTY_LIST;
} }


} }
Expand Up @@ -2,12 +2,10 @@


import com.orientechnologies.orient.core.command.OCommandContext; import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable; import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.sql.parser.*; import com.orientechnologies.orient.core.sql.parser.*;


import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;


/** /**
* Created by luigidellaquila on 14/10/16. * Created by luigidellaquila on 14/10/16.
Expand All @@ -17,7 +15,7 @@ public MatchMultiEdgeTraverser(OResult lastUpstreamRecord, EdgeTraversal edge) {
super(lastUpstreamRecord, edge); super(lastUpstreamRecord, edge);
} }


protected Iterable<OIdentifiable> traversePatternEdge(OIdentifiable startingPoint, OCommandContext iCommandContext) { protected Iterable<OResultInternal> traversePatternEdge(OIdentifiable startingPoint, OCommandContext iCommandContext) {


Iterable possibleResults = null; Iterable possibleResults = null;
// if (this.edge.edge.item.getFilter() != null) { // if (this.edge.edge.item.getFilter() != null) {
Expand All @@ -33,14 +31,14 @@ protected Iterable<OIdentifiable> traversePatternEdge(OIdentifiable startingPoin
// } // }


OMultiMatchPathItem item = (OMultiMatchPathItem) this.item; OMultiMatchPathItem item = (OMultiMatchPathItem) this.item;
List<Object> result = new ArrayList<>(); List<OResultInternal> result = new ArrayList<>();


List<Object> nextStep = new ArrayList<>(); List<Object> nextStep = new ArrayList<>();
nextStep.add(startingPoint); nextStep.add(startingPoint);


Object oldCurrent = iCommandContext.getVariable("$current"); Object oldCurrent = iCommandContext.getVariable("$current");
for (OMatchPathItem sub : item.getItems()) { for (OMatchPathItem sub : item.getItems()) {
List<Object> rightSide = new ArrayList<>(); List<OResultInternal> rightSide = new ArrayList<>();
for (Object o : nextStep) { for (Object o : nextStep) {
OWhereClause whileCond = sub.getFilter() == null ? null : sub.getFilter().getWhileCondition(); OWhereClause whileCond = sub.getFilter() == null ? null : sub.getFilter().getWhileCondition();


Expand All @@ -55,28 +53,52 @@ protected Iterable<OIdentifiable> traversePatternEdge(OIdentifiable startingPoin
current = ((OResult) current).getElement().orElse(null); current = ((OResult) current).getElement().orElse(null);
} }
MatchEdgeTraverser subtraverser = new MatchEdgeTraverser(null, sub); MatchEdgeTraverser subtraverser = new MatchEdgeTraverser(null, sub);
subtraverser.executeTraversal(iCommandContext, sub, (OIdentifiable) current, 0, null).forEach(x -> rightSide.add(x.getElement().get())); subtraverser.executeTraversal(iCommandContext, sub, (OIdentifiable) current, 0, null).forEach(x -> rightSide.add(x));


} else { } else {
iCommandContext.setVariable("$current", o); iCommandContext.setVariable("$current", o);
Object nextSteps = method.execute(o, possibleResults, iCommandContext); Object nextSteps = method.execute(o, possibleResults, iCommandContext);
if (nextSteps instanceof Collection) { if (nextSteps instanceof Collection) {
rightSide.addAll((Collection<?>) nextSteps); ((Collection) nextSteps).stream().map(x -> toOResultInternal(x)).filter(Objects::nonNull)
} else if (nextSteps instanceof OIdentifiable || nextSteps instanceof OResult) { .forEach(i -> rightSide.add((OResultInternal) i));
rightSide.add(nextSteps); } else if (nextSteps instanceof OIdentifiable) {
rightSide.add(new OResultInternal((OIdentifiable) nextSteps));
} else if (nextSteps instanceof OResultInternal) {
rightSide.add((OResultInternal) nextSteps);
} else if (nextSteps instanceof Iterable) { } else if (nextSteps instanceof Iterable) {
((Iterable) nextSteps).forEach(x -> rightSide.add(x)); for (Object step : (Iterable) nextSteps) {
OResultInternal converted = toOResultInternal(step);
if (converted != null) {
rightSide.add(converted);
}
}
} else if (nextSteps instanceof Iterator) { } else if (nextSteps instanceof Iterator) {
((Iterator) nextSteps).forEachRemaining(x -> rightSide.add(x)); Iterator iterator = (Iterator) nextSteps;
while (iterator.hasNext()) {
OResultInternal converted = toOResultInternal(iterator.next());
if (converted != null) {
rightSide.add(converted);
}
}
} }
} }
} }
nextStep = rightSide; nextStep = (List) rightSide;
result = rightSide; result = rightSide;
} }


iCommandContext.setVariable("$current", oldCurrent); iCommandContext.setVariable("$current", oldCurrent);
// return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((OIdentifiable) qR); // return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((OIdentifiable) qR);
return (Iterable) result; return (Iterable) result;
} }

private OResultInternal toOResultInternal(Object x) {
if (x instanceof OResultInternal) {
return (OResultInternal) x;
}
if (x instanceof OIdentifiable) {
return new OResultInternal((OIdentifiable) x);
}
throw new OCommandExecutionException("Cannot execute traversal on " + x);
}
} }
Expand Up @@ -5,7 +5,9 @@
import com.orientechnologies.orient.core.sql.parser.OMatchPathItem; import com.orientechnologies.orient.core.sql.parser.OMatchPathItem;
import com.orientechnologies.orient.core.sql.parser.OWhereClause; import com.orientechnologies.orient.core.sql.parser.OWhereClause;


import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;


/** /**
* Created by luigidellaquila on 15/10/16. * Created by luigidellaquila on 15/10/16.
Expand All @@ -30,10 +32,34 @@ protected OWhereClause getTargetFilter(OMatchPathItem item) {
} }


@Override @Override
protected Iterable<OIdentifiable> traversePatternEdge(OIdentifiable startingPoint, OCommandContext iCommandContext) { protected Iterable<OResultInternal> traversePatternEdge(OIdentifiable startingPoint, OCommandContext iCommandContext) {


Object qR = this.item.getMethod().executeReverse(startingPoint, iCommandContext); Object qR = this.item.getMethod().executeReverse(startingPoint, iCommandContext);
return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((OIdentifiable) qR); if(qR==null){
return Collections.emptyList();
}
if(qR instanceof OResultInternal){
return Collections.singleton((OResultInternal) qR);
}
if(qR instanceof OIdentifiable){
return Collections.singleton(new OResultInternal((OIdentifiable) qR));
}
if (qR instanceof Iterable) {
Iterable iterable = (Iterable) qR;
List<OResultInternal> result = new ArrayList<>();
for (Object o : iterable) {
if (o instanceof OIdentifiable) {
result.add(new OResultInternal((OIdentifiable) o));
} else if (o instanceof OResultInternal) {
result.add((OResultInternal) o);
}
else{
throw new UnsupportedOperationException();
}
}
return result;
}
return Collections.EMPTY_LIST;
} }


@Override @Override
Expand Down
Expand Up @@ -12,6 +12,8 @@
public class OMatchPathItemFirst extends OMatchPathItem { public class OMatchPathItemFirst extends OMatchPathItem {
protected OFunctionCall function; protected OFunctionCall function;


protected OMethodCall methodWrapper;

public OMatchPathItemFirst(int id) { public OMatchPathItemFirst(int id) {
super(id); super(id);
} }
Expand All @@ -38,13 +40,15 @@ protected Iterable<OIdentifiable> traversePatternEdge(OMatchStatement.MatchConte
return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((OIdentifiable) qR); return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((OIdentifiable) qR);
} }


@Override public OMatchPathItem copy() { @Override
public OMatchPathItem copy() {
OMatchPathItemFirst result = (OMatchPathItemFirst) super.copy(); OMatchPathItemFirst result = (OMatchPathItemFirst) super.copy();
result.function = function == null ? null : function.copy(); result.function = function == null ? null : function.copy();
return result; return result;
} }


@Override public boolean equals(Object o) { @Override
public boolean equals(Object o) {
if (!super.equals(o)) { if (!super.equals(o)) {
return false; return false;
} }
Expand All @@ -56,7 +60,8 @@ protected Iterable<OIdentifiable> traversePatternEdge(OMatchStatement.MatchConte
return true; return true;
} }


@Override public int hashCode() { @Override
public int hashCode() {
int result = super.hashCode(); int result = super.hashCode();
result = 31 * result + (function != null ? function.hashCode() : 0); result = 31 * result + (function != null ? function.hashCode() : 0);
return result; return result;
Expand All @@ -69,4 +74,18 @@ public OFunctionCall getFunction() {
public void setFunction(OFunctionCall function) { public void setFunction(OFunctionCall function) {
this.function = function; this.function = function;
} }

@Override
public OMethodCall getMethod() {
if (methodWrapper == null) {
synchronized (this) {
if (methodWrapper == null) {
methodWrapper = new OMethodCall(-1);
methodWrapper.params = function.params;
methodWrapper.methodName = function.name;
}
}
}
return methodWrapper;
}
} }

0 comments on commit c72d20c

Please sign in to comment.