Skip to content

Commit

Permalink
baby steps to the final recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed May 12, 2024
1 parent 4066d27 commit 65f4b24
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,6 @@
<module>kotlin</module>
<module>validation</module>
<module>examples</module>
<!-- <module>rewrite</module>-->
<module>rewrite</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public class PipelineRewrite extends Recipe {

private static final String AGGREGATION = "dev.morphia.aggregation.Aggregation";

@Override
public String getDisplayName() {
return "Aggregation pipeline rewrite";
Expand All @@ -26,53 +28,54 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
var matchers = List.of(
new MethodMatcher("dev.morphia.aggregation.Aggregation addFields(dev.morphia.aggregation.stages.AddFields)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation autoBucket(dev.morphia.aggregation.stages.AutoBucket)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation bucket(dev.morphia.aggregation.stages.Bucket)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation changeStream()"),
new MethodMatcher("dev.morphia.aggregation.Aggregation changeStream(dev.morphia.aggregation.stages.ChangeStream)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation collStats(dev.morphia.aggregation.stages.CollectionStats)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation count(dev.morphia.aggregation.stages.Count)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation currentOp(dev.morphia.aggregation.stages.CountOp)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation densify(dev.morphia.aggregation.stages.Densify)"),
new MethodMatcher(
"dev.morphia.aggregation.Aggregation documents(dev.morphia.aggregation.expressions.impls.DocumentExpression)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation facet(dev.morphia.aggregation.stages.Facet)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation fill(dev.morphia.aggregation.stages.Fill)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation geoNear(dev.morphia.aggregation.stages.GeoNear)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation graphLookup(dev.morphia.aggregation.stages.GraphLookup)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation group(dev.morphia.aggregation.stages.Group)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation indexStats(dev.morphia.aggregation.stages.IndexStats)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation limit(long)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation lookup(dev.morphia.aggregation.stages.Lookup)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation match(dev.morphia.aggregation.stages.Match)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation planCacheStats()"),
new MethodMatcher("dev.morphia.aggregation.Aggregation project(dev.morphia.aggregation.stages.Projection)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation redact(dev.morphia.aggregation.stages.Redact)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation replaceRoot(dev.morphia.aggregation.stages.ReplaceRoot)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation replaceWith(dev.morphia.aggregation.stages.ReplaceWith)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation sample(dev.morphia.aggregation.stages.Sample)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation set(dev.morphia.aggregation.stages.Set)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation skip(long)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation sort(dev.morphia.aggregation.stages.Sort)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation sortByCount(dev.morphia.aggregation.stages.SortByCount)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation unionWith(Class,Stage...)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation unionWith(String,Stage...)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation unset(dev.morphia.aggregation.stages.Unset)"),
new MethodMatcher("dev.morphia.aggregation.Aggregation unwind(dev.morphia.aggregation.stages.Unwind)"));
new MethodMatcher(AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)"),
new MethodMatcher(AGGREGATION + " autoBucket(dev.morphia.aggregation.stages.AutoBucket)"),
new MethodMatcher(AGGREGATION + " bucket(dev.morphia.aggregation.stages.Bucket)"),
new MethodMatcher(AGGREGATION + " changeStream()"),
new MethodMatcher(AGGREGATION + " changeStream(dev.morphia.aggregation.stages.ChangeStream)"),
new MethodMatcher(AGGREGATION + " collStats(dev.morphia.aggregation.stages.CollectionStats)"),
new MethodMatcher(AGGREGATION + " count(dev.morphia.aggregation.stages.Count)"),
new MethodMatcher(AGGREGATION + " currentOp(dev.morphia.aggregation.stages.CountOp)"),
new MethodMatcher(AGGREGATION + " densify(dev.morphia.aggregation.stages.Densify)"),
new MethodMatcher(AGGREGATION + " documents(dev.morphia.aggregation.expressions.impls.DocumentExpression)"),
new MethodMatcher(AGGREGATION + " facet(dev.morphia.aggregation.stages.Facet)"),
new MethodMatcher(AGGREGATION + " fill(dev.morphia.aggregation.stages.Fill)"),
new MethodMatcher(AGGREGATION + " geoNear(dev.morphia.aggregation.stages.GeoNear)"),
new MethodMatcher(AGGREGATION + " graphLookup(dev.morphia.aggregation.stages.GraphLookup)"),
new MethodMatcher(AGGREGATION + " group(dev.morphia.aggregation.stages.Group)"),
new MethodMatcher(AGGREGATION + " indexStats(dev.morphia.aggregation.stages.IndexStats)"),
new MethodMatcher(AGGREGATION + " limit(long)"),
new MethodMatcher(AGGREGATION + " lookup(dev.morphia.aggregation.stages.Lookup)"),
new MethodMatcher(AGGREGATION + " match(dev.morphia.aggregation.stages.Match)"),
new MethodMatcher(AGGREGATION + " planCacheStats()"),
new MethodMatcher(AGGREGATION + " project(dev.morphia.aggregation.stages.Projection)"),
new MethodMatcher(AGGREGATION + " redact(dev.morphia.aggregation.stages.Redact)"),
new MethodMatcher(AGGREGATION + " replaceRoot(dev.morphia.aggregation.stages.ReplaceRoot)"),
new MethodMatcher(AGGREGATION + " replaceWith(dev.morphia.aggregation.stages.ReplaceWith)"),
new MethodMatcher(AGGREGATION + " sample(dev.morphia.aggregation.stages.Sample)"),
new MethodMatcher(AGGREGATION + " set(dev.morphia.aggregation.stages.Set)"),
new MethodMatcher(AGGREGATION + " skip(long)"),
new MethodMatcher(AGGREGATION + " sort(dev.morphia.aggregation.stages.Sort)"),
new MethodMatcher(AGGREGATION + " sortByCount(dev.morphia.aggregation.stages.SortByCount)"),
new MethodMatcher(AGGREGATION + " unionWith(Class,Stage...)"),
new MethodMatcher(AGGREGATION + " unionWith(String,Stage...)"),
new MethodMatcher(AGGREGATION + " unset(dev.morphia.aggregation.stages.Unset)"),
new MethodMatcher(AGGREGATION + " unwind(dev.morphia.aggregation.stages.Unwind)"));

return new JavaIsoVisitor<>() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public MethodInvocation visitMethodInvocation(MethodInvocation method, @NotNull ExecutionContext context) {
if (matchers.stream().anyMatch(matcher->matcher.matches(method))) {
System.out.println("method = " + method);
Expression expression = method.getArguments().get(0);
System.out.println("expression = " + expression);
method.withName()
return super.visitMethodInvocation(method, context);
public MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation, @NotNull ExecutionContext context) {
if (matchers.stream().anyMatch(matcher -> matcher.matches(methodInvocation))) {
Expression expression = methodInvocation.getArguments().get(0);
System.out.println("method = " + methodInvocation.getMethodType());
// System.out.println("expression = " + expression);

return super.visitMethodInvocation(methodInvocation
.withName(methodInvocation.getName().withSimpleName("pipeline")),
context);
} else {
return super.visitMethodInvocation(method, context);
return super.visitMethodInvocation(methodInvocation, context);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ public void update(Aggregation<?> aggregation) {
public class UnwrapTest {
public void update(Aggregation<?> aggregation) {
aggregation.pipeline(
group(id("author")).field("count", sum(value(1))),
sort().ascending("_id"))
aggregation
.pipeline(group(id("author")).field("count", sum(value(1))))
.pipeline(sort().ascending("_id"))
.execute(Document.class);
}
}
"""));
}
}

0 comments on commit 65f4b24

Please sign in to comment.