Skip to content

Commit

Permalink
[JPA] Optimisation ordonnancement des batchs
Browse files Browse the repository at this point in the history
  • Loading branch information
gideruette committed Mar 21, 2024
1 parent 0a52d84 commit 8767f5e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
43 changes: 38 additions & 5 deletions TopModel.Generator.Jpa/FlowTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class FlowTree
{
public FlowTree(List<DataFlow> flows)
{
var hasIndependantFlow = flows.Where(f => !flows.Intersect(f.DependsOn).Any() && !flows.SelectMany(fl => fl.DependsOn).Contains(f)).Any() && flows.Count() > 1;
var hasIndependantFlow = Graps(flows).Count() > 1;
RootFlows = hasIndependantFlow ? new() : flows.Where(f => !flows.Intersect(f.DependsOn).Any()).ToList();
while (flows.Any(f => !Flows.Contains(f)))
{
Expand Down Expand Up @@ -84,8 +84,12 @@ public string ToFlow(int indentLevel)
}
else
{
result += $" //\n{indent}.{next}(";
result += $" //\n{indent}{baseIndent}new FlowBuilder<Flow>(\"{string.Join('-', Subflows.SelectMany(s => s.Flows).Select(r => r.Name))}\")";
if (RootFlows.Any())
{
result += $" //\n{indent}.{next}(";
result += $" //\n{indent}{baseIndent}new FlowBuilder<Flow>(\"{string.Join('-', Subflows.SelectMany(s => s.Flows).Select(r => r.Name))}\")";
}

result += $" //\n{indent}{baseIndent}.split(taskExecutor)";
result += $" //\n{indent}{baseIndent}.add(";
foreach (var subflow in Subflows)
Expand All @@ -97,12 +101,41 @@ public string ToFlow(int indentLevel)
}
}

result += $" //\n{indent}{baseIndent})";
result += $" //\n{indent}{baseIndent}.end()";
if (RootFlows.Any())
{
result += $" //\n{indent}{baseIndent})";
result += $" //\n{indent}{baseIndent}.end()";
}

result += $" //\n{indent})";
}

result += $" //\n{indent}.build()";
return result;
}

private List<List<DataFlow>> Graps(List<DataFlow> flows)
{
var graps = new List<List<DataFlow>>();
while (flows.Any(f => !graps.SelectMany(t => t).Contains(f)))
{
var f = flows.Where(f => !graps.SelectMany(t => t).Contains(f)).First();
var subFlowDataFlows = new List<DataFlow>();
var stack = new Queue<DataFlow>();
stack.Enqueue(f);
var flowsToAdd = new List<DataFlow>();
while (stack.TryDequeue(out var s))
{
subFlowDataFlows.Add(s);
foreach (var depFlow in flows.Where(f => !graps.SelectMany(t => t).Concat(subFlowDataFlows).Contains(f)).Where(f => s.DependsOn.Contains(f) || f.DependsOn.Contains(s)))
{
stack.Enqueue(depFlow);
}
}

graps.Add(subFlowDataFlows);
}

return graps;
}
}
11 changes: 8 additions & 3 deletions TopModel.Generator.Jpa/SpringDataFlowGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private static void WriteBeanFlow(JavaWriter fw, DataFlow dataFlow)
fw.AddImport("org.springframework.batch.core.job.flow.Flow");
fw.AddImport("org.springframework.beans.factory.annotation.Qualifier");
fw.AddImport("org.springframework.batch.core.Step");
fw.AddImport("org.springframework.batch.core.job.builder.FlowBuilder");

fw.WriteLine();
fw.WriteLine(1, @$"@Bean(""{dataFlow.Name.ToPascalCase()}Flow"")");
Expand All @@ -74,6 +73,7 @@ private static void WriteBeanFlow(JavaWriter fw, DataFlow dataFlow)
}

fw.WriteLine(1, @$" @Qualifier(""{dataFlow.Name.ToPascalCase()}Step"") Step {dataFlow.Name.ToCamelCase()}Step) {{");
fw.AddImport("org.springframework.batch.core.job.builder.FlowBuilder");
fw.WriteLine(2, @$"return new FlowBuilder<Flow>(""{dataFlow.Name.ToPascalCase()}Flow"") //");
var isFirst = true;
if (dataFlow.Hooks.Contains(FlowHook.BeforeFlow))
Expand Down Expand Up @@ -344,7 +344,6 @@ private void WriteModuleConfig(string module, IEnumerable<DataFlow> flows)
fw.AddImport("org.springframework.batch.core.job.flow.Flow");
fw.AddImport("org.springframework.batch.core.job.builder.JobBuilder");
fw.AddImport("org.springframework.batch.core.launch.support.RunIdIncrementer");
fw.AddImport("org.springframework.batch.core.job.builder.FlowBuilder");
fw.AddImport("org.springframework.core.task.TaskExecutor");
fw.AddImport("org.springframework.context.annotation.Import");
fw.WriteLine();
Expand All @@ -366,7 +365,13 @@ private void WriteModuleConfig(string module, IEnumerable<DataFlow> flows)
fw.WriteLine(2, $@" .incrementer(new RunIdIncrementer()) //");

var flowTree = new FlowTree(flows.ToList());
fw.WriteLine(2, $" .start({flowTree.ToFlow(4)})");
var toFlow = flowTree.ToFlow(4);
if (toFlow.Contains("FlowBuilder"))
{
fw.AddImport("org.springframework.batch.core.job.builder.FlowBuilder");
}

fw.WriteLine(2, $" .start({toFlow})");
fw.WriteLine(2, " .end() //");
fw.WriteLine(2, " .build();");
fw.WriteLine(1, "}");
Expand Down

0 comments on commit 8767f5e

Please sign in to comment.