Skip to content

Commit

Permalink
Adding a function to print display representation of a join set.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrszul committed Jan 2, 2025
1 parent 9ac68db commit 5bf0ade
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public interface DataRoot {
@Nonnull
ResourceType getParentResourceType();

default int depth() {
return 0;
}
@Nonnull
String toDisplayString();

@Value(staticConstructor = "of")
class ResourceRoot implements DataRoot {
Expand All @@ -51,6 +50,12 @@ public ResourceType getParentResourceType() {
public String getTag() {
return resourceType.toCode();
}

@Override
@Nonnull
public String toDisplayString() {
return resourceType.toCode();
}
}


Expand All @@ -65,10 +70,6 @@ default ResourceType getParentResourceType() {
@Nonnull
DataRoot getMaster();

default int depth() {
return getMaster().depth() + 1;
}

@Nonnull
JoinTag asTag();

Expand Down Expand Up @@ -101,6 +102,13 @@ public JoinTag asTag() {
return JoinTag.ReverseResolveTag.of(foreignResourceType, foreignKeyPath);
}

@Override
@Nonnull
public String toDisplayString() {
return foreignResourceType.toCode() + "<-" + foreignKeyPath;
}


public static ReverseResolveRoot ofResource(@Nonnull final ResourceType masterType,
@Nonnull final ResourceType foreignResourceType,
@Nonnull final String foreignResourcePath) {
Expand All @@ -126,17 +134,19 @@ public ResourceType getResourceType() {
return foreignResourceType;
}

@Override
public int depth() {
return master.depth() + 1;
}

@Nonnull
@Override
public JoinTag asTag() {
return JoinTag.ResolveTag.of(foreignResourceType);
}

@Override
@Nonnull
public String toDisplayString() {
return masterResourcePath + "->" + foreignResourceType.toCode();
}


public static ResolveRoot ofResource(@Nonnull final ResourceType masterType,
@Nonnull final ResourceType foreignResourceType,
@Nonnull final String masterResourcePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import lombok.AllArgsConstructor;
import lombok.Value;

@Value()
@Value
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class JoinSet {

Expand Down Expand Up @@ -59,7 +59,7 @@ static List<DataRoot> toPath(@Nonnull final DataRoot root) {
}

@Nonnull
public static List<JoinSet> mergeRoots(@Nonnull final List<List<DataRoot>> paths) {
private static List<JoinSet> mergeRoots(@Nonnull final List<List<DataRoot>> paths) {
// convert to paths and then group by recurively by common prefixes
// we have got it already somwhere else
final Map<DataRoot, List<List<DataRoot>>> suffixesByHeads = paths.stream()
Expand All @@ -74,10 +74,47 @@ public static List<JoinSet> mergeRoots(@Nonnull final List<List<DataRoot>> paths
.toList();
}

/**
* Merges a set of roots into a tree of join sets by recursively grouping by common prefixes.
*
* @param roots the roots to merge
* @return the merged join sets
*/
@Nonnull
public static List<JoinSet> mergeRoots(@Nonnull final Set<DataRoot> roots) {
// convert to paths and then group by recurively by common prefixes
// we have got it already somwhere else
// convert to paths and then group by recursively by common prefixes
return mergeRoots(roots.stream().map(JoinSet::toPath).toList());
}

/**
* Returns a string representation of the join set as a tree.
*/
@Nonnull
public String toTreeString() {
return toTreeString(0);
}

/**
* Prints a string representation of the join set as a tree.
*/
public void printTree() {
System.out.println(toTreeString());
}

@Nonnull
private String toTreeString(final int depth) {
final StringBuilder sb = new StringBuilder();
sb.append(" ".repeat(depth > 0
? depth - 1
: 0));
sb.append(depth > 0
? "+-"
: "");
sb.append(master.toDisplayString());
sb.append("\n");
children.forEach(child -> sb.append(child.toTreeString(depth + 1)));
return sb.toString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public Dataset<Row> createInitialDataset() {
// createInitialDataset(), null)
// : createInitialDataset();

final JoinSet subjectJoinSet = JoinSet.mergeRoots(joinRoots).iterator().next();
System.out.println("Subject join set: \n" + subjectJoinSet.toTreeString());
Dataset<Row> resolvedDataset = resolveJoins(
JoinSet.mergeRoots(joinRoots).iterator().next(),
subjectJoinSet,
createInitialDataset());

System.out.println("Resolved dataset:");
Expand Down Expand Up @@ -419,7 +421,6 @@ private Dataset<Row> computeReverseJoin(@Nonnull final Dataset<Row> parentDatase
@Nonnull
private Dataset<Row> resolveJoins(@Nonnull final JoinSet joinSet,
@Nonnull final Dataset<Row> parentDataset) {

// now just reduce current children
return joinSet.getChildren().stream()
.reduce(parentDataset, (dataset, subset) ->
Expand Down

0 comments on commit 5bf0ade

Please sign in to comment.