Fix sort logging in MongoTemplate.
Original pull request: #4892 Closes #4860
This commit is contained in:
committed by
Mark Paluch
parent
2702706f5b
commit
279a28de33
@@ -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 {
|
||||
public ReadPreference getReadPreference() {
|
||||
return delegate.getReadPreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document getSortObject() {
|
||||
return delegate instanceof SortingQueryCursorPreparer sqcp ? sqcp.getSortObject() : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
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
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
||||
|
||||
class QueryCursorPreparer implements CursorPreparer {
|
||||
class QueryCursorPreparer implements SortingQueryCursorPreparer {
|
||||
|
||||
private final Query query;
|
||||
|
||||
@@ -3444,6 +3450,11 @@ public class MongoTemplate
|
||||
return cursorToUse;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Document getSortObject() {
|
||||
return sortObject;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user