Compare commits

...

6 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
Christoph Strobl
d70a9ed7c6 Update visibility of ConversionContext.
The ConversionContext should not be package private due to its usage in protected method signatures.

Closes: #4345
2023-03-24 13:40:48 +01:00
Mark Paluch
27de3680b4 Fix reverse keyset scrolling.
We now use gt/lt instead of gte/lte to avoid duplicates during reverse keyset scrolling.

Closes #4343
2023-03-24 11:14:03 +01:00
Christoph Strobl
244b949b6d After release cleanups.
See #4295
2023-03-20 15:05:35 +01:00
Christoph Strobl
29b9123f5b Prepare next development iteration.
See #4295
2023-03-20 15:05:33 +01:00
14 changed files with 516 additions and 46 deletions

10
pom.xml
View File

@@ -5,7 +5,7 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-M3</version>
<version>4.1.x-GH-2496-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Spring Data MongoDB</name>
@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data.build</groupId>
<artifactId>spring-data-parent</artifactId>
<version>3.1.0-M3</version>
<version>3.1.0-SNAPSHOT</version>
</parent>
<modules>
@@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>3.1.0-M3</springdata.commons>
<springdata.commons>3.1.0-SNAPSHOT</springdata.commons>
<mongo>4.9.0</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
@@ -145,8 +145,8 @@
<repositories>
<repository>
<id>spring-libs-milestone</id>
<url>https://repo.spring.io/libs-milestone</url>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.1.0-M3</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-M3</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-M3</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

@@ -63,18 +63,18 @@ class ScrollUtils {
KeysetScrollPosition keyset = query.getKeyset();
KeysetScrollDirector director = KeysetScrollDirector.of(keyset.getDirection());
director.postPostProcessResults(result);
List<T> resultsToUse = director.postPostProcessResults(result, query.getLimit());
IntFunction<KeysetScrollPosition> positionFunction = value -> {
T last = result.get(value);
T last = resultsToUse.get(value);
Entity<T> entity = operations.forEntity(last);
Map<String, Object> keys = entity.extractKeys(sortObject, sourceType);
return KeysetScrollPosition.of(keys);
};
return createWindow(result, query.getLimit(), positionFunction);
return Window.from(resultsToUse, positionFunction, hasMoreElements(result, query.getLimit()));
}
static <T> Window<T> createWindow(List<T> result, int limit, IntFunction<? extends ScrollPosition> positionFunction) {
@@ -187,13 +187,14 @@ class ScrollUtils {
return queryObject;
}
public <T> void postPostProcessResults(List<T> result) {
}
protected String getComparator(int sortOrder) {
return sortOrder == 1 ? "$gt" : "$lt";
}
protected <T> List<T> postPostProcessResults(List<T> list, int limit) {
return getFirst(limit, list);
}
}
/**
@@ -219,20 +220,48 @@ class ScrollUtils {
}
@Override
protected String getComparator(int sortOrder) {
public <T> List<T> postPostProcessResults(List<T> list, int limit) {
// use gte/lte to include the object at the cursor/keyset so that
// we can include it in the result to check whether there is a next object.
// It needs to be filtered out later on.
return sortOrder == 1 ? "$gte" : "$lte";
}
@Override
public <T> void postPostProcessResults(List<T> result) {
// flip direction of the result list as we need to accomodate for the flipped sort order for proper offset
// querying.
Collections.reverse(result);
Collections.reverse(list);
return getLast(limit, list);
}
}
/**
* Return the first {@code count} items from the list.
*
* @param count
* @param list
* @return
* @param <T>
*/
static <T> List<T> getFirst(int count, List<T> list) {
if (count > 0 && list.size() > count) {
return list.subList(0, count);
}
return list;
}
/**
* Return the last {@code count} items from the list.
*
* @param count
* @param list
* @return
* @param <T>
*/
static <T> List<T> getLast(int count, List<T> list) {
if (count > 0 && list.size() > count) {
return list.subList(list.size() - (count), list.size());
}
return list;
}
}

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.*;
@@ -2145,7 +2146,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
*
* @since 3.4.3
*/
interface ConversionContext {
protected interface ConversionContext {
/**
* Converts a source object into {@link TypeInformation target}.

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

@@ -184,11 +184,18 @@ class MongoTemplateScrollTests {
assertThat(window.isLast()).isTrue();
assertThat(window).hasSize(6);
KeysetScrollPosition scrollPosition = (KeysetScrollPosition) window.positionAt(window.size() - 1);
KeysetScrollPosition scrollPosition = (KeysetScrollPosition) window.positionAt(window.size() - 2);
KeysetScrollPosition reversePosition = KeysetScrollPosition.of(scrollPosition.getKeys(), Direction.Backward);
window = template.scroll(q.with(reversePosition).limit(2), Person.class);
assertThat(window).hasSize(2);
assertThat(window).containsOnly(jane_42, john20);
assertThat(window.hasNext()).isTrue();
assertThat(window.isLast()).isFalse();
window = template.scroll(q.with(window.positionAt(0)).limit(2), Person.class);
assertThat(window).hasSize(2);
assertThat(window).containsOnly(john20, john40_1);
assertThat(window.hasNext()).isTrue();

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