Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ public static String describeType(Class<?> type) {
return "long";
} else if (Double.class.isAssignableFrom(type)) {
return "double";
} else if (Boolean.class.isAssignableFrom(type)) {
return "bool";
} else if (ObjectId.class.isAssignableFrom(type)) {
return "objectId";
} else if (Instant.class.isAssignableFrom(type)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package de.bwaldvogel.mongo.backend.aggregation.stage;

import static java.util.stream.Collectors.toList;
import static de.bwaldvogel.mongo.backend.Constants.ID_FIELD;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

import de.bwaldvogel.mongo.MongoCollection;
import de.bwaldvogel.mongo.MongoDatabase;
import de.bwaldvogel.mongo.backend.Missing;
import de.bwaldvogel.mongo.backend.Utils;
import de.bwaldvogel.mongo.backend.ValueComparator;
import de.bwaldvogel.mongo.backend.aggregation.Expression;
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.exception.FailedToParseException;
import de.bwaldvogel.mongo.exception.MongoServerError;

public class GraphLookupStage extends AbstractLookupStage {

Expand All @@ -24,6 +31,7 @@ public class GraphLookupStage extends AbstractLookupStage {
private static final Set<String> CONFIGURATION_KEYS = Set.of(
FROM, START_WITH, CONNECT_FROM_FIELD, CONNECT_TO_FIELD, AS, MAX_DEPTH, DEPTH_FIELD);

private final Object startWith;
private final String connectFromField;
private final String connectToField;
private final String asField;
Expand All @@ -35,8 +43,10 @@ public class GraphLookupStage extends AbstractLookupStage {
public GraphLookupStage(Document configuration, MongoDatabase mongoDatabase) {
String from = readStringConfigurationProperty(configuration, FROM);
collection = mongoDatabase.resolveCollection(from, false);
readStringConfigurationProperty(configuration, FROM);
readVariableConfigurationProperty(configuration, START_WITH);
if (!configuration.containsKey(START_WITH)) {
throw new MongoServerError(40105, "$graphLookup requires 'from', 'as', 'startWith', 'connectFromField', and 'connectToField' to be specified.");
}
startWith = configuration.get(START_WITH);
connectFromField = readStringConfigurationProperty(configuration, CONNECT_FROM_FIELD);
connectToField = readStringConfigurationProperty(configuration, CONNECT_TO_FIELD);
asField = readStringConfigurationProperty(configuration, AS);
Expand All @@ -50,37 +60,22 @@ public String name() {
return "$graphLookup";
}

Integer readOptionalIntegerConfigurationProperty(Document configuration, String name) {
private Integer readOptionalIntegerConfigurationProperty(Document configuration, String name) {
Object value = configuration.get(name);
if (value == null) {
return null;
}
if (value instanceof Integer integer) {
return integer;
}
throw new FailedToParseException("'" + name + "' option to \" + stageName + \" must be a integer, but was type " + Utils.describeType(value));
return switch (value) {
case null -> null;
case Integer integer -> integer;
default -> throw new MongoServerError(40100, name + " must be numeric, found type: " + Utils.describeType(value));
};
}

String readOptionalStringConfigurationProperty(Document configuration, String name) {
private String readOptionalStringConfigurationProperty(Document configuration, String name) {
Object value = configuration.get(name);
if (value == null) {
return null;
}
if (value instanceof String string) {
return string;
}
throw new FailedToParseException("'" + name + "' option to \" + stageName + \" must be a string, but was type " + Utils.describeType(value));
}

void readVariableConfigurationProperty(Document configuration, String name) {
Object value = configuration.get(name);
if (value == null) {
throw new FailedToParseException("missing '" + name + "' option to $graphLookup stage specification: " + configuration);
}
if (value instanceof String) {
return;
}
throw new FailedToParseException("'" + name + "' option to $graphLookup must be a string, but was type " + Utils.describeType(value));
return switch (value) {
case null -> null;
case String string -> string;
default -> throw new MongoServerError(40103, "expected string as argument for " + name + ", found: " + Utils.describeType(value));
};
}

@Override
Expand All @@ -89,37 +84,54 @@ public Stream<Document> apply(Stream<Document> stream) {
}

private Document connectRemoteDocument(Document document) {
Object value = document.get(connectFromField);
List<Document> documentList = new ArrayList<>();
findLinkedDocuments(0, documentList, value);
Object value = Expression.evaluateDocument(startWith, document);
Document result = document.clone();
result.put(asField, documentList);
result.put(asField, findLinkedDocuments(value));
return result;
}

private List<Document> findLinkedDocuments(long depth, final List<Document> linked, Object value) {
private List<Document> findLinkedDocuments(Object value) {
List<Document> linked = new ArrayList<>();
Deque<SearchValue> pending = new ArrayDeque<>();
enqueueValues(pending, value, 0);
Set<Object> visitedIds = new TreeSet<>(ValueComparator.asc());

if (maxDepth != null && depth > maxDepth) {
return linked;
while (!pending.isEmpty()) {
SearchValue current = pending.removeFirst();
if (maxDepth != null && current.depth() > maxDepth) {
continue;
}
for (Document newDocument : lookupDocuments(current.value())) {
if (!visitedIds.add(newDocument.get(ID_FIELD))) {
continue;
}
Object newValue = Utils.getSubdocumentValueCollectionAware(newDocument, connectFromField);
enqueueValues(pending, newValue, current.depth() + 1);
Document result = newDocument.clone();
if (depthField != null) {
result.put(depthField, current.depth());
}
linked.add(result);
}
}
return linked;
}

if (value instanceof List<?> list) {
return list.stream()
.flatMap(item -> findLinkedDocuments(depth + 1, linked, item).stream())
.collect(toList());
}
Document query = new Document(connectToField, value);
private List<Document> lookupDocuments(Object value) {
// Unlike a plain equality query, a null graph link must not match missing fields.
Object condition = value == null ? new Document("$eq", null).append("$exists", true) : value;
return collection.handleQueryAsStream(new Document(connectToField, condition)).toList();
}

List<Document> newlyLinkedDocuments = collection.handleQueryAsStream(query).collect(toList());
for (Document newDocument : newlyLinkedDocuments) {
Object newValue = newDocument.get(connectFromField);
if (depthField != null) {
newDocument.put(depthField, depth);
private static void enqueueValues(Deque<SearchValue> pending, Object value, long depth) {
List<?> values = value instanceof List<?> list ? list : Collections.singletonList(value);
for (Object item : values) {
if (!(item instanceof Missing)) {
pending.addLast(new SearchValue(item, depth));
}
linked.add(0, newDocument);
findLinkedDocuments(depth + 1, linked, newValue);
}
return linked;
}

private record SearchValue(Object value, long depth) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.bwaldvogel.mongo.TestUtils.json;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static org.mockito.Mockito.when;

import java.util.stream.Stream;
Expand Down Expand Up @@ -33,13 +34,37 @@ void testGraphLookupObjectThatHasLinkedDocuments() {
configureEmployeesCollection("name: 'Mike'", "_id: 1, name: 'Mike'");
Document document = json("name: 'Bob', manager: 'Dave'");

Stream<Document> result = graphLookupStage.apply(Stream.of(document));
Document result = graphLookupStage.apply(Stream.of(document)).findFirst().orElseThrow();

assertThat(result).containsExactly(
json("name: 'Bob', manager: 'Dave', managers: [{_id: 1, name: 'Mike', 'order': NumberLong(1)}, {_id: 2, name: 'Dave', 'manager': 'Mike', 'order': NumberLong(0)}]")
assertThat(result).containsEntry("name", "Bob").containsEntry("manager", "Dave");
assertThat(result.get("managers")).asInstanceOf(list(Document.class)).containsExactlyInAnyOrder(
json("_id: 1, name: 'Mike', order: NumberLong(1)"),
json("_id: 2, name: 'Dave', manager: 'Mike', order: NumberLong(0)")
);
}

@Test
void testGraphLookupDoesNotModifyInputOrLinkedDocuments() {
GraphLookupStage graphLookupStage = buildGraphLookupStage("""
from: 'employees',
startWith: '$manager',
connectFromField: 'manager',
connectToField: 'name',
as: 'managers',
depthField: 'order'
""");
Document employee = json("_id: 2, name: 'Dave', order: 42");
when(employeesCollection.handleQueryAsStream(json("name: 'Dave'")))
.thenAnswer(invocation -> Stream.of(employee));
Document input = json("name: 'Bob', manager: 'Dave', managers: ['previous value']");

assertThat(graphLookupStage.apply(Stream.of(input))).containsExactly(
json("name: 'Bob', manager: 'Dave', managers: [{_id: 2, name: 'Dave', order: NumberLong(0)}]")
);
assertThat(input).isEqualTo(json("name: 'Bob', manager: 'Dave', managers: ['previous value']"));
assertThat(employee).isEqualTo(json("_id: 2, name: 'Dave', order: 42"));
}

private GraphLookupStage buildGraphLookupStage(String jsonDocument) {
return new GraphLookupStage(json(jsonDocument), database);
}
Expand Down
Loading
Loading