Skip to content

Commit

Permalink
refactor: minor cleanup in pattern matching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Dec 9, 2024
1 parent 9959167 commit 06417ce
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MatchEdgeTraverser {
public MatchEdgeTraverser(OResult lastUpstreamRecord, EdgeTraversal edge) {
this.sourceRecord = lastUpstreamRecord;
this.edge = edge;
this.item = edge.edge.item;
this.item = edge.edge.getItem();
}

public MatchEdgeTraverser(OResult lastUpstreamRecord, OMatchPathItem item) {
Expand Down Expand Up @@ -53,12 +53,13 @@ public OResult next(OCommandContext ctx) {
result.setProperty(prop, sourceRecord.getProperty(prop));
}
result.setProperty(endPointAlias, toResult(nextElement));
if (edge.edge.item.getFilter().getDepthAlias() != null) {
result.setProperty(edge.edge.item.getFilter().getDepthAlias(), nextR.getMetadata("$depth"));
if (edge.edge.getItem().getFilter().getDepthAlias() != null) {
result.setProperty(
edge.edge.getItem().getFilter().getDepthAlias(), nextR.getMetadata("$depth"));
}
if (edge.edge.item.getFilter().getPathAlias() != null) {
if (edge.edge.getItem().getFilter().getPathAlias() != null) {
result.setProperty(
edge.edge.item.getFilter().getPathAlias(), nextR.getMetadata("$matchPath"));
edge.edge.getItem().getFilter().getPathAlias(), nextR.getMetadata("$matchPath"));
}
return result;
}
Expand All @@ -78,14 +79,14 @@ protected Object toResult(OIdentifiable nextElement) {
}

protected String getStartingPointAlias() {
return this.edge.edge.out.alias;
return this.edge.edge.getOut().getAlias();
}

protected String getEndpointAlias() {
if (this.item != null) {
return this.item.getFilter().getAlias();
}
return this.edge.edge.in.alias;
return this.edge.edge.getIn().getAlias();
}

protected void init(OCommandContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public String prettyPrint(int depth, int indent) {
}

private String getAlias() {
return this.node.alias;
return this.node.getAlias();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class MatchReverseEdgeTraverser extends MatchEdgeTraverser {

public MatchReverseEdgeTraverser(OResult lastUpstreamRecord, EdgeTraversal edge) {
super(lastUpstreamRecord, edge);
this.startingPointAlias = edge.edge.in.alias;
this.endPointAlias = edge.edge.out.alias;
this.startingPointAlias = edge.edge.getIn().getAlias();
this.endPointAlias = edge.edge.getOut().getAlias();
}

protected String targetClassName(OMatchPathItem item, OCommandContext iCommandContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public OExecutionStream createNextResultSet(OResult lastUpstreamRecord, OCommand
}

protected MatchEdgeTraverser createTraverser(OResult lastUpstreamRecord) {
if (edge.edge.item instanceof OMultiMatchPathItem) {
if (edge.edge.getItem() instanceof OMultiMatchPathItem) {
return new MatchMultiEdgeTraverser(lastUpstreamRecord, edge);
} else if (edge.edge.item instanceof OFieldMatchPathItem) {
} else if (edge.edge.getItem() instanceof OFieldMatchPathItem) {
return new MatchFieldTraverser(lastUpstreamRecord, edge);
} else if (edge.out) {
return new MatchEdgeTraverser(lastUpstreamRecord, edge);
Expand All @@ -55,14 +55,14 @@ public String prettyPrint(int depth, int indent) {
}
result.append(spaces);
result.append(" ");
result.append("{" + edge.edge.out.alias + "}");
if (edge.edge.item instanceof OFieldMatchPathItem) {
result.append("{" + edge.edge.getOut().getAlias() + "}");
if (edge.edge.getItem() instanceof OFieldMatchPathItem) {
result.append(".");
result.append(((OFieldMatchPathItem) edge.edge.item).getField());
result.append(((OFieldMatchPathItem) edge.edge.getItem()).getField());
} else {
result.append(edge.edge.item.getMethod());
result.append(edge.edge.getItem().getMethod());
}
result.append("{" + edge.edge.in.alias + "}");
result.append("{" + edge.edge.getIn().getAlias() + "}");
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,9 @@ private void manageNotPatterns(
throw new OCommandExecutionException(
"This kind of NOT expression is not supported (yet): " + item.toString());
}
PatternEdge edge = new PatternEdge();
edge.item = item;
edge.out = new PatternNode();
edge.out.alias = lastFilter.getAlias();
edge.in = new PatternNode();
edge.in.alias = item.getFilter().getAlias();
PatternNode in = new PatternNode(item.getFilter().getAlias());
PatternNode out = new PatternNode(lastFilter.getAlias());
PatternEdge edge = new PatternEdge(item, in, out);
EdgeTraversal traversal = new EdgeTraversal(edge, true);
MatchStep step = new MatchStep(context, traversal, enableProfiling);
steps.add(step);
Expand Down Expand Up @@ -301,27 +298,27 @@ private OInternalExecutionPlan createPlanForPattern(
boolean first = true;
if (sortedEdges.size() > 0) {
for (EdgeTraversal edge : sortedEdges) {
if (edge.edge.out.alias != null) {
edge.setLeftClass(aliasClasses.get(edge.edge.out.alias));
edge.setLeftCluster(aliasClusters.get(edge.edge.out.alias));
edge.setLeftRid(aliasRids.get(edge.edge.out.alias));
edge.setLeftClass(aliasClasses.get(edge.edge.out.alias));
edge.setLeftFilter(aliasFilters.get(edge.edge.out.alias));
if (edge.edge.getOut().getAlias() != null) {
edge.setLeftClass(aliasClasses.get(edge.edge.getOut().getAlias()));
edge.setLeftCluster(aliasClusters.get(edge.edge.getOut().getAlias()));
edge.setLeftRid(aliasRids.get(edge.edge.getOut().getAlias()));
edge.setLeftClass(aliasClasses.get(edge.edge.getOut().getAlias()));
edge.setLeftFilter(aliasFilters.get(edge.edge.getOut().getAlias()));
}
addStepsFor(plan, edge, context, first, profilingEnabled);
first = false;
}
} else {
PatternNode node = pattern.getAliasToNode().values().iterator().next();
if (prefetchedAliases.contains(node.alias)) {
if (prefetchedAliases.contains(node.getAlias())) {
// from prefetch
plan.chain(new MatchFirstStep(context, node, profilingEnabled));
} else {
// from actual execution plan
String clazz = aliasClasses.get(node.alias);
String cluster = aliasClusters.get(node.alias);
ORid rid = aliasRids.get(node.alias);
OWhereClause filter = aliasFilters.get(node.alias);
String clazz = aliasClasses.get(node.getAlias());
String cluster = aliasClusters.get(node.getAlias());
ORid rid = aliasRids.get(node.getAlias());
OWhereClause filter = aliasFilters.get(node.getAlias());
OSelectStatement select = createSelectStatement(clazz, cluster, rid, filter);
plan.chain(
new MatchFirstStep(
Expand Down Expand Up @@ -455,25 +452,25 @@ private void updateScheduleStartingAt(
// into them.
visitedNodes.add(startNode);
for (Set<String> dependencies : remainingDependencies.values()) {
dependencies.remove(startNode.alias);
dependencies.remove(startNode.getAlias());
}

Map<PatternEdge, Boolean> edges = new LinkedHashMap<PatternEdge, Boolean>();
for (PatternEdge outEdge : startNode.out) {
for (PatternEdge outEdge : startNode.getOut()) {
edges.put(outEdge, true);
}
for (PatternEdge inEdge : startNode.in) {
if (inEdge.item.isBidirectional()) {
for (PatternEdge inEdge : startNode.getIn()) {
if (inEdge.getItem().isBidirectional()) {
edges.put(inEdge, false);
}
}

for (Map.Entry<PatternEdge, Boolean> edgeData : edges.entrySet()) {
PatternEdge edge = edgeData.getKey();
boolean isOutbound = edgeData.getValue();
PatternNode neighboringNode = isOutbound ? edge.in : edge.out;
PatternNode neighboringNode = isOutbound ? edge.getIn() : edge.getOut();

if (!remainingDependencies.get(neighboringNode.alias).isEmpty()) {
if (!remainingDependencies.get(neighboringNode.getAlias()).isEmpty()) {
// Unsatisfied dependencies, ignore this neighboring node.
continue;
}
Expand Down Expand Up @@ -501,7 +498,7 @@ private void updateScheduleStartingAt(
// not allowed
// to flip their directionality, so we leave them as-is.
boolean traversalDirection;
if (startNode.optional || edge.item.isBidirectional()) {
if (startNode.isOptional() || edge.getItem().isBidirectional()) {
traversalDirection = !isOutbound;
} else {
traversalDirection = isOutbound;
Expand All @@ -510,7 +507,7 @@ private void updateScheduleStartingAt(
visitedEdges.add(edge);
resultingSchedule.add(new EdgeTraversal(edge, traversalDirection));
}
} else if (!startNode.optional || isOptionalChain(startNode, edge, neighboringNode)) {
} else if (!startNode.isOptional() || isOptionalChain(startNode, edge, neighboringNode)) {
// If the neighboring node wasn't visited, we don't expand the optional node into it, hence
// the above check.
// Instead, we'll allow the neighboring node to add the edge we failed to visit, via the
Expand Down Expand Up @@ -548,10 +545,10 @@ private boolean isOptionalChain(

visitedEdges.add(edge);

if (neighboringNode.out != null) {
for (PatternEdge patternEdge : neighboringNode.out) {
if (neighboringNode.getOut() != null) {
for (PatternEdge patternEdge : neighboringNode.getOut()) {
if (!visitedEdges.contains(patternEdge)
&& !isOptionalChain(neighboringNode, patternEdge, patternEdge.in, visitedEdges)) {
&& !isOptionalChain(neighboringNode, patternEdge, patternEdge.getIn(), visitedEdges)) {
return false;
}
}
Expand All @@ -572,15 +569,15 @@ private Map<String, Set<String>> getDependencies(Pattern pattern) {
for (PatternNode node : pattern.aliasToNode.values()) {
Set<String> currentDependencies = new HashSet<String>();

OWhereClause filter = aliasFilters.get(node.alias);
OWhereClause filter = aliasFilters.get(node.getAlias());
if (filter != null && filter.getBaseExpression() != null) {
List<String> involvedAliases = filter.getBaseExpression().getMatchPatternInvolvedAliases();
if (involvedAliases != null) {
currentDependencies.addAll(involvedAliases);
}
}

result.put(node.alias, currentDependencies);
result.put(node.getAlias(), currentDependencies);
}

return result;
Expand All @@ -601,11 +598,11 @@ private void addStepsFor(
boolean first,
boolean profilingEnabled) {
if (first) {
PatternNode patternNode = edge.out ? edge.edge.out : edge.edge.in;
String clazz = this.aliasClasses.get(patternNode.alias);
String cluster = this.aliasClusters.get(patternNode.alias);
ORid rid = this.aliasRids.get(patternNode.alias);
OWhereClause where = aliasFilters.get(patternNode.alias);
PatternNode patternNode = edge.out ? edge.edge.getOut() : edge.edge.getIn();
String clazz = this.aliasClasses.get(patternNode.getAlias());
String cluster = this.aliasClusters.get(patternNode.getAlias());
ORid rid = this.aliasRids.get(patternNode.getAlias());
OWhereClause where = aliasFilters.get(patternNode.getAlias());
OSelectStatement select = new OSelectStatement(-1);
select.setTarget(new OFromClause(-1));
select.getTarget().setItem(new OFromItem(-1));
Expand All @@ -626,7 +623,7 @@ private void addStepsFor(
select.createExecutionPlan(subContxt, profilingEnabled),
profilingEnabled));
}
if (edge.edge.in.isOptionalNode()) {
if (edge.edge.getIn().isOptionalNode()) {
foundOptional = true;
plan.chain(new OptionalMatchStep(context, edge, profilingEnabled));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public String prettyPrint(int depth, int indent) {
}
result.append(spaces);
result.append(" ");
result.append("{" + edge.edge.out.alias + "}");
result.append(edge.edge.item.getMethod());
result.append("{" + edge.edge.in.alias + "}");
result.append("{" + edge.edge.getOut().getAlias() + "}");
result.append(edge.edge.getItem().getMethod());
result.append("{" + edge.edge.getIn().getAlias() + "}");
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,44 @@

/** Created by luigidellaquila on 28/07/15. */
public class PatternEdge {
public PatternNode in;
public PatternNode out;
public OMatchPathItem item;
private final PatternNode in;
private final PatternNode out;
private final OMatchPathItem item;

public PatternEdge(OMatchPathItem item, PatternNode in, PatternNode out) {
this.item = item;
this.in = in;
this.out = out;
}

public Iterable<OIdentifiable> executeTraversal(
OMatchStatement.MatchContext matchContext,
OCommandContext iCommandContext,
OIdentifiable startingPoint,
int depth) {
return item.executeTraversal(matchContext, iCommandContext, startingPoint, depth);
return getItem().executeTraversal(matchContext, iCommandContext, startingPoint, depth);
}

@Override
public String toString() {
return "{as: " + in.alias + "}" + item.toString();
return "{as: "
+ getOut().getAlias()
+ "}"
+ getItem().toString()
+ "{as: "
+ getIn().getAlias()
+ "}";
}

public OMatchPathItem getItem() {
return item;
}

public PatternNode getOut() {
return out;
}

public PatternNode getIn() {
return in;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,52 @@

/** Created by luigidellaquila on 28/07/15. */
public class PatternNode {
public String alias;
public Set<PatternEdge> out = new LinkedHashSet<PatternEdge>();
public Set<PatternEdge> in = new LinkedHashSet<PatternEdge>();
public int centrality = 0;
public boolean optional = false;
private final String alias;
private final Set<PatternEdge> out = new LinkedHashSet<PatternEdge>();
private final Set<PatternEdge> in = new LinkedHashSet<PatternEdge>();
private int centrality = 0;
private boolean optional = false;

public PatternNode(String alias) {
this.alias = alias;
}

public int addEdge(OMatchPathItem item, PatternNode to) {
PatternEdge edge = new PatternEdge();
edge.item = item;
edge.out = this;
edge.in = to;
this.out.add(edge);
to.in.add(edge);
PatternEdge edge = new PatternEdge(item, to, this);
this.getOut().add(edge);
to.getIn().add(edge);
return 1;
}

public boolean isOptionalNode() {
return isOptional();
}

public boolean isOptional() {
return optional;
}

public void setOptional(boolean optional) {
this.optional = optional;
}

public int getCentrality() {
return centrality;
}

public void setCentrality(int centrality) {
this.centrality = centrality;
}

public Set<PatternEdge> getIn() {
return in;
}

public Set<PatternEdge> getOut() {
return out;
}

public String getAlias() {
return alias;
}
}
Loading

0 comments on commit 06417ce

Please sign in to comment.