Skip to content

DATAMONGO-1480 - Add support for noCursorTimeout in Query. #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1480-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1480-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1480-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1480-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1480-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1480-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Meta;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
Expand Down Expand Up @@ -139,6 +140,7 @@
* @author Christoph Strobl
* @author Doménique Tilleuil
* @author Niko Schmuck
* @author Mark Paluch
*/
@SuppressWarnings("deprecation")
public class MongoTemplate implements MongoOperations, ApplicationContextAware {
Expand Down Expand Up @@ -2249,7 +2251,7 @@ public DBObject doInCollection(DBCollection collection) throws MongoException, D
* @author Thomas Darimont
*/

static interface DbObjectCallback<T> {
interface DbObjectCallback<T> {

T doWith(DBObject object);
}
Expand Down Expand Up @@ -2347,23 +2349,49 @@ public DBCursor prepare(DBCursor cursor) {
DBCursor cursorToUse = cursor.copy();

try {

if (query.getSkip() > 0) {
cursorToUse = cursorToUse.skip(query.getSkip());
}

if (query.getLimit() > 0) {
cursorToUse = cursorToUse.limit(query.getLimit());
}

if (query.getSortObject() != null) {
DBObject sortDbo = type != null ? getMappedSortObject(query, type) : query.getSortObject();
cursorToUse = cursorToUse.sort(sortDbo);
}

if (StringUtils.hasText(query.getHint())) {
cursorToUse = cursorToUse.hint(query.getHint());
}

if (query.getMeta().hasValues()) {

for (Entry<String, Object> entry : query.getMeta().values()) {
cursorToUse = cursorToUse.addSpecial(entry.getKey(), entry.getValue());
}

for (Meta.CursorOption option : query.getMeta().getFlags()) {

switch (option) {
case EXHAUST:
cursorToUse = cursorToUse.addOption(Bytes.QUERYOPTION_EXHAUST);
break;
case NO_TIMEOUT:
cursorToUse = cursorToUse.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
break;
case PARTIAL:
cursorToUse = cursorToUse.addOption(Bytes.QUERYOPTION_PARTIAL);
break;
case SLAVE_OK:
cursorToUse = cursorToUse.addOption(Bytes.QUERYOPTION_SLAVEOK);
break;
default:
throw new IllegalArgumentException(String.format("%s is no supported flag.", option));
}
}
}

} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 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.
Expand All @@ -17,8 +17,10 @@

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.util.Assert;
Expand All @@ -30,6 +32,7 @@
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @since 1.6
*/
public class Meta {
Expand All @@ -39,12 +42,13 @@ private enum MetaKey {

private String key;

private MetaKey(String key) {
MetaKey(String key) {
this.key = key;
}
}

private final Map<String, Object> values = new LinkedHashMap<String, Object>(2);
private final Set<CursorOption> flags = new LinkedHashSet<CursorOption>();

/**
* @return {@literal null} if not set.
Expand Down Expand Up @@ -120,11 +124,32 @@ public boolean getSnapshot() {
return getValue(MetaKey.SNAPSHOT.key, false);
}

/**
* Add {@link CursorOption} influencing behavior of the {@link com.mongodb.DBCursor}.
*
* @param option must not be {@literal null}.
* @return
* @since 1.10
*/
public boolean addFlag(CursorOption option) {

Assert.notNull(option, "CursorOption must not be null!");
return this.flags.add(option);
}

/**
* @return never {@literal null}.
* @since 1.10
*/
public Set<CursorOption> getFlags() {
return flags;
}

/**
* @return
*/
public boolean hasValues() {
return !this.values.isEmpty();
return !this.values.isEmpty() || !this.flags.isEmpty();
}

/**
Expand Down Expand Up @@ -169,7 +194,10 @@ private <T> T getValue(String key, T defaultValue) {
*/
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.values);

int hash = ObjectUtils.nullSafeHashCode(this.values);
hash += ObjectUtils.nullSafeHashCode(this.flags);
return hash;
}

/*
Expand All @@ -188,6 +216,35 @@ public boolean equals(Object obj) {
}

Meta other = (Meta) obj;
return ObjectUtils.nullSafeEquals(this.values, other.values);
if (!ObjectUtils.nullSafeEquals(this.values, other.values)) {
return false;
}
return ObjectUtils.nullSafeEquals(this.flags, other.flags);
}

/**
* {@link CursorOption} represents {@code OP_QUERY} wire protocol flags to change the behavior of queries.
*
* @author Christoph Strobl
* @since 1.10
*/
public enum CursorOption {

/** Prevents the server from timing out idle cursors. */
NO_TIMEOUT,

/**
* Sets the cursor to return all data returned by the query at once rather than splitting the results into batches.
*/
EXHAUST,

/** Allows querying of a replica slave. */
SLAVE_OK,

/**
* Sets the cursor to return partial data from a query against a sharded cluster in which some shards do not respond
* rather than throwing an error.
*/
PARTIAL
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 the original author or authors.
* Copyright 2010-2016 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.
Expand Down Expand Up @@ -41,6 +41,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
public class Query {

Expand Down Expand Up @@ -336,6 +337,50 @@ public Query useSnapshot() {
return this;
}

/**
* @return
* @see org.springframework.data.mongodb.core.query.Meta.CursorOption#NO_TIMEOUT
* @since 1.10
*/
public Query noCursorTimeout() {

meta.addFlag(Meta.CursorOption.NO_TIMEOUT);
return this;
}

/**
* @return
* @see org.springframework.data.mongodb.core.query.Meta.CursorOption#EXHAUST
* @since 1.10
*/
public Query exhaust() {

meta.addFlag(Meta.CursorOption.EXHAUST);
return this;
}

/**
* @return
* @see org.springframework.data.mongodb.core.query.Meta.CursorOption#SLAVE_OK
* @since 1.10
*/
public Query slaveOk() {

meta.addFlag(Meta.CursorOption.SLAVE_OK);
return this;
}

/**
* @return
* @see org.springframework.data.mongodb.core.query.Meta.CursorOption#PARTIAL
* @since 1.10
*/
public Query partialResults() {

meta.addFlag(Meta.CursorOption.PARTIAL);
return this;
}

/**
* @return never {@literal null}.
* @since 1.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@
*/
boolean snapshot() default false;

/**
* Set {@link org.springframework.data.mongodb.core.query.Meta.CursorOption} to be used when executing query.
*
* @return never {@literal null}.
* @since 1.10
*/
org.springframework.data.mongodb.core.query.Meta.CursorOption[] flags() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -248,6 +249,13 @@ public org.springframework.data.mongodb.core.query.Meta getQueryMetaAttributes()
metaAttributes.setSnapshot(meta.snapshot());
}

if (!ObjectUtils.isEmpty(meta.flags())) {

for (org.springframework.data.mongodb.core.query.Meta.CursorOption option : meta.flags()) {
metaAttributes.addFlag(option);
}
}

return metaAttributes;
}
}
Loading