Browse Source

Fix sort logging in `MongoTemplate`.

Original pull request: #4892
Closes #4860
4.4.x
Christoph Strobl 11 months ago committed by Mark Paluch
parent
commit
279a28de33
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java
  2. 19
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  3. 28
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SortingQueryCursorPreparer.java

7
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java

@ -205,7 +205,7 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation { @@ -205,7 +205,7 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
* @author Christoph Strobl
* @since 2.0
*/
static class DelegatingQueryCursorPreparer implements CursorPreparer {
static class DelegatingQueryCursorPreparer implements SortingQueryCursorPreparer {
private final @Nullable CursorPreparer delegate;
private Optional<Integer> limit = Optional.empty();
@ -231,6 +231,11 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation { @@ -231,6 +231,11 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
public ReadPreference getReadPreference() {
return delegate.getReadPreference();
}
@Override
public Document getSortObject() {
return delegate instanceof SortingQueryCursorPreparer sqcp ? sqcp.getSortObject() : null;
}
}
/**

19
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

@ -2596,7 +2596,7 @@ public class MongoTemplate @@ -2596,7 +2596,7 @@ public class MongoTemplate
if (LOGGER.isDebugEnabled()) {
Document mappedSort = getMappedSortObject(query, entityClass);
Document mappedSort = preparer instanceof SortingQueryCursorPreparer sqcp ? getMappedSortObject(sqcp.getSortObject(), entity) : null;
LOGGER.debug(String.format("find using query: %s fields: %s sort: %s for class: %s in collection: %s",
serializeToJsonSafely(mappedQuery), mappedFields, serializeToJsonSafely(mappedSort), entityClass,
collectionName));
@ -2621,9 +2621,10 @@ public class MongoTemplate @@ -2621,9 +2621,10 @@ public class MongoTemplate
QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields));
Document mappedFields = queryContext.getMappedFields(entity, projection);
Document mappedQuery = queryContext.getMappedQuery(entity);
Document mappedSort = getMappedSortObject(query, sourceClass);
if (LOGGER.isDebugEnabled()) {
Document mappedSort = preparer instanceof SortingQueryCursorPreparer sqcp ? getMappedSortObject(sqcp.getSortObject(), sourceClass) : null;
LOGGER.debug(String.format("find using query: %s fields: %s sort: %s for class: %s in collection: %s",
serializeToJsonSafely(mappedQuery), mappedFields, serializeToJsonSafely(mappedSort), sourceClass,
collectionName));
@ -2988,12 +2989,17 @@ public class MongoTemplate @@ -2988,12 +2989,17 @@ public class MongoTemplate
@Nullable
private Document getMappedSortObject(Document sortObject, Class<?> type) {
return getMappedSortObject(sortObject, mappingContext.getPersistentEntity(type));
}
@Nullable
private Document getMappedSortObject(Document sortObject, MongoPersistentEntity<?> entity) {
if (ObjectUtils.isEmpty(sortObject)) {
return null;
}
return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
return queryMapper.getMappedSort(sortObject, entity);
}
/**
@ -3346,7 +3352,7 @@ public class MongoTemplate @@ -3346,7 +3352,7 @@ public class MongoTemplate
}
}
class QueryCursorPreparer implements CursorPreparer {
class QueryCursorPreparer implements SortingQueryCursorPreparer {
private final Query query;
@ -3444,6 +3450,11 @@ public class MongoTemplate @@ -3444,6 +3450,11 @@ public class MongoTemplate
return cursorToUse;
}
@Nullable
@Override
public Document getSortObject() {
return sortObject;
}
}
/**

28
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SortingQueryCursorPreparer.java

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
/*
* Copyright 2025 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 org.bson.Document;
import org.springframework.lang.Nullable;
/**
* @author Christoph Strobl
*/
interface SortingQueryCursorPreparer extends CursorPreparer {
@Nullable
Document getSortObject();
}
Loading…
Cancel
Save