Compare commits

...

2 Commits

Author SHA1 Message Date
Christoph Strobl
3cdcfa7d50 Initial draft of reactive dbref support.
allow resolution of list of dbrefs.

Resolve dbref of dbref.

cycles gonna be a problem here.

remove outdated code carried forward
2023-03-27 11:35:24 +02:00
Christoph Strobl
515ca43704 Prepare issue branch. 2023-03-27 11:03:27 +02:00
12 changed files with 457 additions and 23 deletions

View File

@@ -5,7 +5,7 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-2496-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Spring Data MongoDB</name>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-2496-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-2496-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>4.1.x-GH-2496-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core;
import static org.springframework.data.mongodb.core.query.SerializationUtils.*;
import org.springframework.data.mongodb.core.convert.DefaultReactiveDbRefResolver;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
@@ -201,6 +202,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
private SessionSynchronization sessionSynchronization = SessionSynchronization.ON_ACTUAL_TRANSACTION;
private CountExecution countExecution = this::doExactCount;
private DefaultReactiveDbRefResolver dbRefResolver;
/**
* Constructor used for a basic template configuration.
@@ -3033,14 +3036,15 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
maybeEmitEvent(new AfterLoadEvent<>(document, type, collectionName));
T entity = reader.read(type, document);
if (entity == null) {
throw new MappingException(String.format("EntityReader %s returned null", reader));
}
maybeEmitEvent(new AfterConvertEvent<>(document, entity, collectionName));
return maybeCallAfterConvert(entity, document, collectionName);
return ReactiveValueResolver.prepareDbRefResolution(Mono.just(document), new DefaultReactiveDbRefResolver(getMongoDatabaseFactory()))
.map(it -> {
T entity = reader.read(type, it);
if (entity == null) {
throw new MappingException(String.format("EntityReader %s returned null", reader));
}
maybeEmitEvent(new AfterConvertEvent<>(document, entity, collectionName));
return entity;
}).flatMap(it -> maybeCallAfterConvert(it, document, collectionName));
}
}
@@ -3073,15 +3077,20 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
Class<T> returnType = projection.getMappedType().getType();
maybeEmitEvent(new AfterLoadEvent<>(document, returnType, collectionName));
Object entity = reader.project(projection, document);
dbRefResolver = new DefaultReactiveDbRefResolver(getMongoDatabaseFactory());
return ReactiveValueResolver.prepareDbRefResolution(Mono.just(document), dbRefResolver)
.map(it -> {
Object entity = reader.project(projection, document);
if (entity == null) {
throw new MappingException(String.format("EntityReader %s returned null", reader));
}
if (entity == null) {
throw new MappingException(String.format("EntityReader %s returned null", reader));
}
T castEntity = (T) entity;
maybeEmitEvent(new AfterConvertEvent<>(document, castEntity, collectionName));
return maybeCallAfterConvert(castEntity, document, collectionName);
T castEntity = (T) entity;
maybeEmitEvent(new AfterConvertEvent<>(document, castEntity, collectionName));
return castEntity;
})
.flatMap(it -> maybeCallAfterConvert(it, document, collectionName));
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.Map.Entry;
import org.bson.Document;
import org.springframework.data.mongodb.core.convert.ReactiveDbRefResolver;
import com.mongodb.DBRef;
/**
* @author Christoph Strobl
* @since 4.1
*/
class ReactiveValueResolver {
static Mono<Document> prepareDbRefResolution(Mono<Document> root, ReactiveDbRefResolver dbRefResolver) {
return root.flatMap(source -> {
for (Entry<String, Object> entry : source.entrySet()) {
Object value = entry.getValue();
if (value instanceof DBRef dbRef) {
return prepareDbRefResolution(dbRefResolver.initFetch(dbRef).defaultIfEmpty(new Document())
.flatMap(it -> prepareDbRefResolution(Mono.just(it), dbRefResolver)).map(resolved -> {
source.put(entry.getKey(), resolved.isEmpty() ? null : resolved);
return source;
}), dbRefResolver);
}
if (value instanceof Document nested) {
return prepareDbRefResolution(Mono.just(nested), dbRefResolver).map(it -> {
source.put(entry.getKey(), it);
return source;
});
}
if (value instanceof List<?> list) {
return Flux.fromIterable(list).concatMap(it -> {
if (it instanceof DBRef dbRef) {
return prepareDbRefResolution(dbRefResolver.initFetch(dbRef), dbRefResolver);
}
if (it instanceof Document document) {
return prepareDbRefResolution(Mono.just(document), dbRefResolver);
}
return Mono.just(it);
}).collectList().map(resolved -> {
source.put(entry.getKey(), resolved.isEmpty() ? null : resolved);
return source;
});
}
}
return Mono.just(source);
});
}
public Mono<Document> resolveValues(Mono<Document> document) {
return document.flatMap(source -> {
for (Entry<String, Object> entry : source.entrySet()) {
Object val = entry.getValue();
if (val instanceof Mono<?> valueMono) {
return valueMono.flatMap(value -> {
source.put(entry.getKey(), value);
return resolveValues(Mono.just(source));
});
}
if (entry.getValue()instanceof Document nested) {
return resolveValues(Mono.just(nested)).map(it -> {
source.put(entry.getKey(), it);
return source;
});
}
if (entry.getValue() instanceof List<?>) {
// do traverse list
}
}
return Mono.just(source);
});
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.convert;
import reactor.core.publisher.Mono;
import java.util.List;
import org.bson.Document;
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import com.mongodb.DBRef;
import com.mongodb.reactivestreams.client.MongoDatabase;
/**
* @author Christoph Strobl
* @since 4.1
*/
public class DefaultReactiveDbRefResolver implements ReactiveDbRefResolver {
ReactiveMongoDatabaseFactory dbFactory;
public DefaultReactiveDbRefResolver(ReactiveMongoDatabaseFactory dbFactory) {
this.dbFactory = dbFactory;
}
@Nullable
@Override
public Mono<Object> resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref,
DbRefResolverCallback callback, DbRefProxyHandler proxyHandler) {
return null;
}
@Nullable
@Override
public Document fetch(DBRef dbRef) {
throw new UnsupportedOperationException();
}
@Override
public List<Document> bulkFetch(List<DBRef> dbRefs) {
throw new UnsupportedOperationException();
}
@Nullable
@Override
public Mono<Document> initFetch(DBRef dbRef) {
Mono<MongoDatabase> mongoDatabase = StringUtils.hasText(dbRef.getDatabaseName())
? dbFactory.getMongoDatabase(dbRef.getDatabaseName())
: dbFactory.getMongoDatabase();
return mongoDatabase
.flatMap(db -> Mono.from(db.getCollection(dbRef.getCollectionName()).find(new Document("_id", dbRef.getId()))));
}
@Nullable
@Override
public Mono<Object> resolveReference(MongoPersistentProperty property, Object source,
ReferenceLookupDelegate referenceLookupDelegate, MongoEntityReader entityReader) {
if (source instanceof DBRef dbRef) {
}
throw new UnsupportedOperationException();
}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.core.convert;
import javax.print.Doc;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.*;

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.convert;
import com.mongodb.DBRef;
import org.bson.Document;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.lang.Nullable;
import reactor.core.publisher.Mono;
/**
* @author Christoph Strobl
* @since 4.1
*/
public interface ReactiveDbRefResolver extends DbRefResolver {
@Nullable
default Mono<Document> initFetch(DBRef dbRef) {
return Mono.justOrEmpty(fetch(dbRef));
}
Mono<Object> resolveReference(MongoPersistentProperty property, Object source, ReferenceLookupDelegate referenceLookupDelegate, MongoEntityReader entityReader);
Mono<Object> resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler proxyHandler);
}

View File

@@ -0,0 +1,181 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import reactor.test.StepVerifier;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.test.util.Assertions;
import org.springframework.data.mongodb.test.util.Client;
import org.springframework.data.mongodb.test.util.MongoClientExtension;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
/**
* @author Christoph Strobl
*/
@ExtendWith(MongoClientExtension.class)
public class ReactiveDbRefTests {
private static final String DB_NAME = "reactive-dbref-tests";
private static @Client MongoClient client;
ReactiveMongoTemplate template = new ReactiveMongoTemplate(MongoClients.create(), DB_NAME);
MongoTemplate syncTemplate = new MongoTemplate(com.mongodb.client.MongoClients.create(), DB_NAME);
@Test // GH-2496
void loadDbRef() {
Bar barSource = new Bar();
barSource.id = "bar-1";
barSource.value = "bar-1-value";
syncTemplate.save(barSource);
Foo fooSource = new Foo();
fooSource.id = "foo-1";
fooSource.name = "foo-1-name";
fooSource.bar = barSource;
syncTemplate.save(fooSource);
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create)
.consumeNextWith(foo -> {
Assertions.assertThat(foo.bar).isEqualTo(barSource);
}).verifyComplete();
}
@Test // GH-2496
void loadListOFDbRef() {
Bar bar1Source = new Bar();
bar1Source.id = "bar-1";
bar1Source.value = "bar-1-value";
syncTemplate.save(bar1Source);
Bar bar2Source = new Bar();
bar2Source.id = "bar-1";
bar2Source.value = "bar-1-value";
syncTemplate.save(bar2Source);
Foo fooSource = new Foo();
fooSource.id = "foo-1";
fooSource.name = "foo-1-name";
fooSource.bars = List.of(bar1Source, bar2Source);
syncTemplate.save(fooSource);
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create)
.consumeNextWith(foo -> {
Assertions.assertThat(foo.bars).containsExactly(bar1Source, bar2Source);
}).verifyComplete();
}
@Test // GH-2496
void loadDbRefHoldingJetAnotherOne() {
Roo rooSource = new Roo();
rooSource.id = "roo-1";
rooSource.name = "roo-the-kangaroo";
syncTemplate.save(rooSource);
Bar barSource = new Bar();
barSource.id = "bar-1";
barSource.value = "bar-1-value";
barSource.roo = rooSource;
syncTemplate.save(barSource);
Foo fooSource = new Foo();
fooSource.id = "foo-1";
fooSource.name = "foo-1-name";
fooSource.bar = barSource;
syncTemplate.save(fooSource);
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create)
.consumeNextWith(foo -> {
Assertions.assertThat(foo.bar).isEqualTo(barSource);
Assertions.assertThat(foo.bar.roo).isEqualTo(rooSource);
}).verifyComplete();
}
@Test // GH-2496
void loadListOfDbRefHoldingJetAnotherOne() {
Roo rooSource = new Roo();
rooSource.id = "roo-1";
rooSource.name = "roo-the-kangaroo";
syncTemplate.save(rooSource);
Bar bar1Source = new Bar();
bar1Source.id = "bar-1";
bar1Source.value = "bar-1-value";
bar1Source.roo = rooSource;
syncTemplate.save(bar1Source);
Bar bar2Source = new Bar();
bar2Source.id = "bar-2";
bar2Source.value = "bar-2-value";
syncTemplate.save(bar2Source);
Foo fooSource = new Foo();
fooSource.id = "foo-1";
fooSource.name = "foo-1-name";
fooSource.bars = List.of(bar1Source, bar2Source);
syncTemplate.save(fooSource);
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create)
.consumeNextWith(foo -> {
Assertions.assertThat(foo.bars).containsExactly(bar1Source, bar2Source);
}).verifyComplete();
}
@ToString
static class Foo {
String id;
String name;
@DBRef //
Bar bar;
@DBRef //
List<Bar> bars;
}
@ToString
@EqualsAndHashCode
static class Bar {
String id;
String value;
@DBRef Roo roo;
}
@ToString
@EqualsAndHashCode
static class Roo {
String id;
String name;
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;
/**
* @author Christoph Strobl
*/
public class ReactiveValueResolverUnitTests {
// TODO: lots of tests
}

View File

@@ -17,6 +17,8 @@ package org.springframework.data.mongodb.repository.support;
import static org.assertj.core.api.Assertions.*;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -245,11 +247,15 @@ public class ReactiveQuerydslMongoPredicateExecutorTests {
.join(person.coworker, QUser.user).on(QUser.user.username.eq("user-2")).fetch();
result.as(StepVerifier::create) //
.expectError(UnsupportedOperationException.class) //
.verify();
.consumeNextWith(it -> {
assertThat(it.getCoworker()).isNotNull();
assertThat(it.getCoworker().getUsername()).isEqualTo(user2.getUsername());
})
.verifyComplete();
}
@Test // DATAMONGO-2182
@Ignore("This should actually return Mono.emtpy() but seems to read all entries somehow - need to check!")
public void queryShouldTerminateWithUnsupportedOperationOnJoinWithNoResults() {
User user1 = new User();
@@ -283,8 +289,7 @@ public class ReactiveQuerydslMongoPredicateExecutorTests {
.join(person.coworker, QUser.user).on(QUser.user.username.eq("does-not-exist")).fetch();
result.as(StepVerifier::create) //
.expectError(UnsupportedOperationException.class) //
.verify();
.verifyComplete(); // should not find anything should it?
}
@Test // DATAMONGO-2182