Skip to content

Commit 10488ea

Browse files
committed
[Fix #484] Removing SortedArrayList
Maybe for larger list that has to be kept sorte that one is useful, but in this case is faster to add everything unsorted (all insertions are really fast) and then sort (there is only one sort of a small amount of items) Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
1 parent 74fc958 commit 10488ea

File tree

4 files changed

+33
-165
lines changed

4 files changed

+33
-165
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/TaskContext.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,6 @@ public TaskContext(JsonNode input, WorkflowPosition position) {
4141
this(input, null, position, Instant.now(), input, input, input, null, new HashMap<>());
4242
}
4343

44-
public TaskContext<T> copy() {
45-
return new TaskContext<T>(
46-
rawInput,
47-
task,
48-
position.copy(),
49-
startedAt,
50-
input,
51-
output,
52-
rawOutput,
53-
flowDirective,
54-
new HashMap<>(contextVariables));
55-
}
56-
5744
public TaskContext(JsonNode input, TaskContext<?> taskContext, T task) {
5845
this(
5946
input,
@@ -88,6 +75,19 @@ private TaskContext(
8875
this.contextVariables = contextVariables;
8976
}
9077

78+
public TaskContext<T> copy() {
79+
return new TaskContext<T>(
80+
rawInput,
81+
task,
82+
position.copy(),
83+
startedAt,
84+
input,
85+
output,
86+
rawOutput,
87+
flowDirective,
88+
new HashMap<>(contextVariables));
89+
}
90+
9191
public void input(JsonNode input) {
9292
this.input = input;
9393
this.rawOutput = input;

impl/core/src/main/java/io/serverlessworkflow/impl/executors/ForkExecutor.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424
import io.serverlessworkflow.impl.WorkflowDefinition;
2525
import io.serverlessworkflow.impl.WorkflowState;
2626
import io.serverlessworkflow.impl.WorkflowUtils;
27-
import io.serverlessworkflow.impl.generic.SortedArrayList;
2827
import io.serverlessworkflow.impl.json.JsonUtils;
2928
import java.lang.reflect.UndeclaredThrowableException;
29+
import java.util.ArrayList;
3030
import java.util.HashMap;
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.concurrent.ExecutionException;
3434
import java.util.concurrent.ExecutorService;
3535
import java.util.concurrent.Future;
3636
import java.util.stream.Collectors;
37+
import java.util.stream.Stream;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -47,8 +48,6 @@ protected ForkExecutor(ForkTask task, WorkflowDefinition definition) {
4748
service = definition.executorService();
4849
}
4950

50-
private record BranchContext(String taskName, TaskContext<?> taskContext) {}
51-
5251
@Override
5352
protected void internalExecute(WorkflowContext workflow, TaskContext<ForkTask> taskContext) {
5453
ForkTaskConfiguration forkConfig = task.getFork();
@@ -62,13 +61,10 @@ protected void internalExecute(WorkflowContext workflow, TaskContext<ForkTask> t
6261
item.getName(),
6362
service.submit(() -> executeBranch(workflow, taskContext.copy(), item, i)));
6463
}
65-
List<BranchContext> results =
66-
new SortedArrayList<>(
67-
(arg1, arg2) ->
68-
arg1.taskContext.completedAt().compareTo(arg2.taskContext.completedAt()));
64+
List<Map.Entry<String, TaskContext<?>>> results = new ArrayList<>();
6965
for (Map.Entry<String, Future<TaskContext<?>>> entry : futures.entrySet()) {
7066
try {
71-
results.add(new BranchContext(entry.getKey(), entry.getValue().get()));
67+
results.add(Map.entry(entry.getKey(), entry.getValue().get()));
7268
} catch (ExecutionException ex) {
7369
Throwable cause = ex.getCause();
7470
if (cause instanceof RuntimeException) {
@@ -83,19 +79,22 @@ protected void internalExecute(WorkflowContext workflow, TaskContext<ForkTask> t
8379
ex);
8480
}
8581
}
86-
if (!results.isEmpty()) {
87-
taskContext.rawOutput(
88-
forkConfig.isCompete()
89-
? results.get(0).taskContext().output()
90-
: JsonUtils.fromValue(
91-
results.stream()
92-
.map(
93-
e ->
94-
JsonUtils.mapper()
95-
.createObjectNode()
96-
.set(e.taskName(), e.taskContext().output()))
97-
.collect(Collectors.toList())));
98-
}
82+
Stream<Map.Entry<String, TaskContext<?>>> sortedStream =
83+
results.stream()
84+
.sorted(
85+
(arg1, arg2) ->
86+
arg1.getValue().completedAt().compareTo(arg2.getValue().completedAt()));
87+
taskContext.rawOutput(
88+
forkConfig.isCompete()
89+
? sortedStream.map(e -> e.getValue().output()).findFirst().orElseThrow()
90+
: JsonUtils.fromValue(
91+
sortedStream
92+
.map(
93+
e ->
94+
JsonUtils.mapper()
95+
.createObjectNode()
96+
.set(e.getKey(), e.getValue().output()))
97+
.collect(Collectors.toList())));
9998
}
10099
}
101100

impl/core/src/main/java/io/serverlessworkflow/impl/generic/SortedArrayList.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

impl/core/src/test/java/io/serverlessworkflow/impl/SortedListTest.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)