Skip to content

Commit 9731860

Browse files
committed
fix: data (entity) graphs with loops were causing stack overflows
Fixed and switched from recursive solution to procedural solution.
1 parent dd35d44 commit 9731860

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/main/java/edu/kit/datamanager/ro_crate/reader/RoCrateReader.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,22 @@ protected Set<String> getDataEntityIds(RootDataEntity root, JsonNode graph) {
177177
if (root == null) { return Set.of(); }
178178
Map<String, Set<String>> network = makeEntityGraph(graph);
179179
Set<String> directDataEntities = new HashSet<>(root.hasPart);
180-
return Stream.concat(
181-
directDataEntities.stream(),
182-
directDataEntities.stream().flatMap(entity -> getDataEntityIdsRecursive(entity, network))
183-
).collect(Collectors.toSet());
184-
}
185180

186-
protected Stream<String> getDataEntityIdsRecursive(
187-
String parent,
188-
Map<String, Set<String>> network
189-
) {
190-
return Stream.concat(
191-
Stream.of(parent),
192-
network.getOrDefault(parent, new HashSet<>()).stream()
193-
.flatMap(s -> getDataEntityIdsRecursive(s, network))
194-
.filter(Objects::nonNull)
195-
);
181+
Stack<String> processingQueue = new Stack<>();
182+
processingQueue.addAll(directDataEntities);
183+
Set<String> result = new HashSet<>();
184+
185+
while (!processingQueue.empty()) {
186+
String currentId = processingQueue.pop();
187+
result.add(currentId);
188+
network.getOrDefault(currentId, new HashSet<>()).stream()
189+
.filter(subId -> !result.contains(subId)) // avoid loops!
190+
.forEach(subId -> {
191+
result.add(subId);
192+
processingQueue.add(subId);
193+
});
194+
}
195+
return result;
196196
}
197197

198198
protected String unpackId(JsonNode node) {

0 commit comments

Comments
 (0)