Skip to content

Commit a15d488

Browse files
committed
DATAMONGO-2148 - Polishing.
Add author tag. Add logging for ReactiveMongoTemplate.count(…) and findDistinct(…) operations. Fix variable names. Original pull request: #620.
1 parent 4465158 commit a15d488

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
* @author Borislav Rangelov
151151
* @author duozhilin
152152
* @author Andreas Zink
153+
* @author Cimon Lucas
153154
*/
154155
@SuppressWarnings("deprecation")
155156
public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {
@@ -875,6 +876,11 @@ public <T> List<T> findDistinct(Query query, String field, String collectionName
875876

876877
MongoIterable<?> result = execute(collectionName, (collection) -> {
877878

879+
if (LOGGER.isDebugEnabled()) {
880+
LOGGER.debug("Executing findDistinct using query {} for field: {} in collection: {}",
881+
serializeToJsonSafely(mappedQuery), field, collectionName);
882+
}
883+
878884
DistinctIterable<T> iterable = collection.distinct(mappedFieldName, mappedQuery, mongoDriverCompatibleType);
879885

880886
return query.getCollation().map(Collation::toMongoCollation).map(iterable::collation).orElse(iterable);

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

+28-12
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,10 @@ public <T> Flux<T> execute(ReactiveSessionCallback<T> action, Consumer<ClientSes
496496
session.startTransaction();
497497
}
498498

499-
return Flux
500-
.usingWhen(Mono.just(session), //
501-
s -> ReactiveMongoTemplate.this.withSession(action, s), //
502-
ClientSession::commitTransaction, //
503-
ClientSession::abortTransaction) //
499+
return Flux.usingWhen(Mono.just(session), //
500+
s -> ReactiveMongoTemplate.this.withSession(action, s), //
501+
ClientSession::commitTransaction, //
502+
ClientSession::abortTransaction) //
504503
.doFinally(signalType -> doFinally.accept(session));
505504
});
506505
}
@@ -742,18 +741,22 @@ public Mono<Boolean> exists(Query query, String collectionName) {
742741
* (non-Javadoc)
743742
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#exists(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
744743
*/
745-
public Mono<Boolean> exists(final Query query, @Nullable Class<?> entityClass, String collectionName) {
744+
public Mono<Boolean> exists(Query query, @Nullable Class<?> entityClass, String collectionName) {
746745

747746
if (query == null) {
748747
throw new InvalidDataAccessApiUsageException("Query passed in to exist can't be null");
749748
}
750749

751750
return createFlux(collectionName, collection -> {
752751

753-
Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), getPersistentEntity(entityClass));
754-
FindPublisher<Document> findPublisher = collection.find(mappedQuery, Document.class)
752+
Document filter = queryMapper.getMappedObject(query.getQueryObject(), getPersistentEntity(entityClass));
753+
FindPublisher<Document> findPublisher = collection.find(filter, Document.class)
755754
.projection(new Document("_id", 1));
756755

756+
if (LOGGER.isDebugEnabled()) {
757+
LOGGER.debug("exists: {} in collection: {}", serializeToJsonSafely(filter), collectionName);
758+
}
759+
757760
findPublisher = query.getCollation().map(Collation::toMongoCollation).map(findPublisher::collation)
758761
.orElse(findPublisher);
759762

@@ -835,6 +838,11 @@ public <T> Flux<T> findDistinct(Query query, String field, String collectionName
835838

836839
Flux<?> result = execute(collectionName, collection -> {
837840

841+
if (LOGGER.isDebugEnabled()) {
842+
LOGGER.debug("Executing findDistinct using query {} for field: {} in collection: {}",
843+
serializeToJsonSafely(mappedQuery), field, collectionName);
844+
}
845+
838846
DistinctPublisher<T> publisher = collection.distinct(mappedFieldName, mappedQuery, mongoDriverCompatibleType);
839847

840848
return query.getCollation().map(Collation::toMongoCollation).map(publisher::collation).orElse(publisher);
@@ -1151,7 +1159,7 @@ public Mono<Long> count(Query query, @Nullable Class<?> entityClass, String coll
11511159

11521160
return createMono(collectionName, collection -> {
11531161

1154-
final Document Document = query == null ? null
1162+
Document filter = query == null ? null
11551163
: queryMapper.getMappedObject(query.getQueryObject(),
11561164
entityClass == null ? null : mappingContext.getPersistentEntity(entityClass));
11571165

@@ -1160,7 +1168,11 @@ public Mono<Long> count(Query query, @Nullable Class<?> entityClass, String coll
11601168
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
11611169
}
11621170

1163-
return collection.count(Document, options);
1171+
if (LOGGER.isDebugEnabled()) {
1172+
LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName);
1173+
}
1174+
1175+
return collection.count(filter, options);
11641176
});
11651177
}
11661178

@@ -3165,7 +3177,7 @@ public Mono<Long> count(Query query, @Nullable Class<?> entityClass, String coll
31653177

31663178
return createMono(collectionName, collection -> {
31673179

3168-
final Document Document = query == null ? null
3180+
Document filter = query == null ? null
31693181
: delegate.queryMapper.getMappedObject(query.getQueryObject(),
31703182
entityClass == null ? null : delegate.mappingContext.getPersistentEntity(entityClass));
31713183

@@ -3174,7 +3186,11 @@ public Mono<Long> count(Query query, @Nullable Class<?> entityClass, String coll
31743186
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
31753187
}
31763188

3177-
return collection.countDocuments(Document, options);
3189+
if (LOGGER.isDebugEnabled()) {
3190+
LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName);
3191+
}
3192+
3193+
return collection.countDocuments(filter, options);
31783194
});
31793195
}
31803196
}

spring-data-mongodb/src/test/resources/logback.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
</encoder>
88
</appender>
99

10-
<!--
10+
<!--
1111
<logger name="org.springframework" level="debug" />
1212
-->
13-
14-
<logger name="org.springframework.data.mongodb.core.aggregation" level="debug" />
13+
14+
<logger name="org.springframework.data.mongodb.core" level="error"/>
1515

1616
<root level="error">
1717
<appender-ref ref="console" />
1818
</root>
1919

20-
</configuration>
20+
</configuration>

0 commit comments

Comments
 (0)