Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plan printer fixes #892

Merged
merged 2 commits into from
Jun 4, 2019
Merged
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 @@ -57,7 +57,8 @@ public String render(PlanRepresentation plan)
{
StringBuilder output = new StringBuilder();
NodeRepresentation root = plan.getRoot();
return writeTextOutput(output, plan, Indent.newInstance(!root.getChildren().isEmpty()), root);
boolean hasChildren = hasChildren(root, plan);
return writeTextOutput(output, plan, Indent.newInstance(level, hasChildren), root);
}

private String writeTextOutput(StringBuilder output, PlanRepresentation plan, Indent indent, NodeRepresentation node)
Expand Down Expand Up @@ -99,13 +100,7 @@ private String writeTextOutput(StringBuilder output, PlanRepresentation plan, In

for (Iterator<NodeRepresentation> iterator = children.iterator(); iterator.hasNext(); ) {
NodeRepresentation child = iterator.next();

boolean hasChildren = child.getChildren().stream()
.map(plan::getNode)
.filter(Optional::isPresent)
.count() > 0;

writeTextOutput(output, plan, indent.forChild(!iterator.hasNext(), hasChildren), child);
writeTextOutput(output, plan, indent.forChild(!iterator.hasNext(), hasChildren(child, plan)), child);
}

return output.toString();
Expand Down Expand Up @@ -254,6 +249,13 @@ private String printEstimates(PlanRepresentation plan, NodeRepresentation node)
return output.toString();
}

private static boolean hasChildren(NodeRepresentation node, PlanRepresentation plan)
{
return node.getChildren().stream()
.map(plan::getNode)
.anyMatch(Optional::isPresent);
}

private static String formatAsLong(double value)
{
if (isFinite(value)) {
Expand Down Expand Up @@ -318,9 +320,10 @@ private static class Indent
private final String nextLinesPrefix;
private final boolean hasChildren;

public static Indent newInstance(boolean hasChildren)
public static Indent newInstance(int level, boolean hasChildren)
{
return new Indent("", "", hasChildren);
String indent = indentString(level);
return new Indent(indent, indent, hasChildren);
}

private Indent(String firstLinePrefix, String nextLinesPrefix, boolean hasChildren)
Expand Down