Skip to content

Commit 2cdb42c

Browse files
committed
Polishing.
Avoid modification of $or filter list when creating keyset criteria. See #5159 Original pull request: #5160
1 parent bc997aa commit 2cdb42c

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

  • spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public Document getFieldsObject(Document fieldsObject, Document sortObject) {
149149
public Document createQuery(KeysetScrollPosition keyset, Document queryObject, Document sortObject) {
150150

151151
Map<String, Object> keysetValues = keyset.getKeys();
152-
List<Document> or = (List<Document>) queryObject.getOrDefault("$or", new ArrayList<>());
153152
List<String> sortKeys = new ArrayList<>(sortObject.keySet());
154153

155154
// first query doesn't come with a keyset
@@ -161,6 +160,21 @@ public Document createQuery(KeysetScrollPosition keyset, Document queryObject, D
161160
throw new IllegalStateException("KeysetScrollPosition does not contain all keyset values");
162161
}
163162

163+
List<Document> or = getKeysetCriteria(queryObject, sortObject, sortKeys, keysetValues);
164+
if (or.isEmpty()) {
165+
return queryObject;
166+
}
167+
168+
Document filterQuery = new Document(queryObject);
169+
filterQuery.put("$or", or);
170+
return filterQuery;
171+
}
172+
173+
private List<Document> getKeysetCriteria(Document queryObject, Document sortObject, List<String> sortKeys,
174+
Map<String, Object> keysetValues) {
175+
176+
List<Document> or = new ArrayList<>((List<Document>) queryObject.getOrDefault("$or", Collections.emptyList()));
177+
164178
// build matrix query for keyset paging that contains sort^2 queries
165179
// reflecting a query that follows sort order semantics starting from the last returned keyset
166180
for (int i = 0; i < sortKeys.size(); i++) {
@@ -190,13 +204,7 @@ public Document createQuery(KeysetScrollPosition keyset, Document queryObject, D
190204
}
191205
}
192206

193-
if (or.isEmpty()) {
194-
return queryObject;
195-
}
196-
197-
Document filterQuery = new Document(queryObject);
198-
filterQuery.put("$or", or);
199-
return filterQuery;
207+
return or;
200208
}
201209

202210
protected String getComparator(int sortOrder) {

0 commit comments

Comments
 (0)