Skip to content

Commit

Permalink
Fix count and update in MongoDb stores
Browse files Browse the repository at this point in the history
* The `into()` query can't infer the type for entity if we provide `Object.class`
* The `updateFirst()` does not support sort queries any more - replace with `findAndModify()`
* Add `getMessageGroupCount()` into tests

**Cherry-pick to 5.3.x & 5.2.x**
  • Loading branch information
artembilan committed Aug 20, 2020
1 parent 750d721 commit 973276f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
Expand Down Expand Up @@ -247,9 +249,8 @@ public int getMessageCountForAllMessageGroups() {
@ManagedAttribute
public int getMessageGroupCount() {
Query query = Query.query(Criteria.where(MessageDocumentFields.GROUP_ID).exists(true));
return getMongoTemplate().getCollection(this.collectionName)
.distinct(MessageDocumentFields.GROUP_ID, query.getQueryObject(), Object.class)
.into(new ArrayList<>())
return getMongoTemplate()
.findDistinct(query, MessageDocumentFields.GROUP_ID, this.collectionName, Object.class)
.size();
}

Expand Down Expand Up @@ -278,7 +279,9 @@ public Collection<Message<?>> getMessagesForGroup(Object groupId) {
}

private void updateGroup(Object groupId, Update update) {
getMongoTemplate().updateFirst(groupOrderQuery(groupId), update, this.collectionName);
getMongoTemplate()
.findAndModify(groupOrderQuery(groupId), update, FindAndModifyOptions.none(), Map.class,
this.collectionName);
}

private static Update lastModifiedUpdate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,20 +429,15 @@ public Collection<Message<?>> getMessagesForGroup(Object groupId) {
@Override
@ManagedAttribute
public int getMessageCountForAllMessageGroups() {
Query query = Query.query(Criteria.where(MessageDocumentFields.MESSAGE_ID).exists(true)
.and(MessageDocumentFields.GROUP_ID).exists(true));
long count = this.template.count(query, this.collectionName);
Assert.isTrue(count <= Integer.MAX_VALUE, "Message count is out of Integer's range");
return (int) count;
Query query = Query.query(Criteria.where(GROUP_ID_KEY).exists(true));
return (int) this.template.count(query, this.collectionName);
}

@Override
@ManagedAttribute
public int getMessageGroupCount() {
Query query = Query.query(Criteria.where(MessageDocumentFields.GROUP_ID).exists(true));
return this.template.getCollection(this.collectionName)
.distinct(MessageDocumentFields.GROUP_ID, query.getQueryObject(), Object.class)
.into(new ArrayList<>())
Query query = Query.query(Criteria.where(GROUP_ID_KEY).exists(true));
return this.template.findDistinct(query, GROUP_ID_KEY, this.collectionName, Object.class)
.size();
}

Expand Down Expand Up @@ -472,7 +467,7 @@ private static Query whereGroupIdIs(Object groupId) {

private void updateGroup(Object groupId, Update update) {
Query query = whereGroupIdIs(groupId).with(Sort.by(Sort.Direction.DESC, GROUP_UPDATE_TIMESTAMP_KEY, SEQUENCE));
this.template.updateFirst(query, update, this.collectionName);
this.template.findAndModify(query, update, FindAndModifyOptions.none(), Map.class, this.collectionName);
}

private int getNextId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void testMessageGroupWithAddedMessagePrimitiveGroupId() {
assertThat(messageA.getHeaders().getId()).isEqualTo(retrievedMessage.getHeaders().getId());
// ensure that 'message_group' header that is only used internally is not propagated
assertThat(retrievedMessage.getHeaders().get("message_group")).isNull();
assertThat(store.getMessageGroupCount()).isEqualTo(1);
}

@Test
Expand Down

0 comments on commit 973276f

Please sign in to comment.