DATAMONGO-2360 - Polishing.
Apply index hints in ReactiveMongoTemplate.count(…). Original pull request: #788.
This commit is contained in:
@@ -1167,6 +1167,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
|||||||
if (query != null) {
|
if (query != null) {
|
||||||
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
|
query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation);
|
||||||
}
|
}
|
||||||
|
if (StringUtils.hasText(query.getHint())) {
|
||||||
|
options.hint(Document.parse(query.getHint()));
|
||||||
|
}
|
||||||
|
|
||||||
if (LOGGER.isDebugEnabled()) {
|
if (LOGGER.isDebugEnabled()) {
|
||||||
LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName);
|
LOGGER.debug("Executing count: {} in collection: {}", serializeToJsonSafely(filter), collectionName);
|
||||||
|
|||||||
@@ -914,12 +914,12 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
|
|||||||
public void countShouldApplyQueryHintIfPresent() {
|
public void countShouldApplyQueryHintIfPresent() {
|
||||||
|
|
||||||
Document queryHint = new Document("age", 1);
|
Document queryHint = new Document("age", 1);
|
||||||
template.count(new BasicQuery("{}").withHint(queryHint), AutogenerateableId.class);
|
template.count(new BasicQuery("{}").withHint(queryHint.toJson()), AutogenerateableId.class);
|
||||||
|
|
||||||
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
|
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
|
||||||
verify(collection).count(any(), options.capture());
|
verify(collection).count(any(), options.capture());
|
||||||
|
|
||||||
assertThat(options.getValue().getHint()).isEqualTo(queryHint);
|
assertThat(options.getValue().getHint(), is(equalTo(queryHint)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1733
|
@Test // DATAMONGO-1733
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import org.mockito.ArgumentCaptor;
|
|||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId;
|
import org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId;
|
||||||
@@ -52,6 +53,7 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||||||
import org.springframework.data.mongodb.core.query.Update;
|
import org.springframework.data.mongodb.core.query.Update;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
|
import com.mongodb.client.model.CountOptions;
|
||||||
import com.mongodb.client.model.DeleteOptions;
|
import com.mongodb.client.model.DeleteOptions;
|
||||||
import com.mongodb.client.model.FindOneAndDeleteOptions;
|
import com.mongodb.client.model.FindOneAndDeleteOptions;
|
||||||
import com.mongodb.client.model.FindOneAndUpdateOptions;
|
import com.mongodb.client.model.FindOneAndUpdateOptions;
|
||||||
@@ -343,6 +345,19 @@ public class ReactiveMongoTemplateUnitTests {
|
|||||||
verify(findPublisher, never()).projection(any());
|
verify(findPublisher, never()).projection(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAMONGO-2360
|
||||||
|
public void countShouldApplyQueryHintIfPresent() {
|
||||||
|
|
||||||
|
when(collection.count(any(Bson.class), any(CountOptions.class))).thenReturn(Mono.empty());
|
||||||
|
Document queryHint = new Document("age", 1);
|
||||||
|
template.count(new Query().withHint(queryHint.toJson()), Person.class, "star-wars").subscribe();
|
||||||
|
|
||||||
|
ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
|
||||||
|
verify(collection).count(any(), options.capture());
|
||||||
|
|
||||||
|
assertThat(options.getValue().getHint(), is(equalTo(queryHint)));
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@org.springframework.data.mongodb.core.mapping.Document(collection = "star-wars")
|
@org.springframework.data.mongodb.core.mapping.Document(collection = "star-wars")
|
||||||
static class Person {
|
static class Person {
|
||||||
|
|||||||
Reference in New Issue
Block a user