Skip to content

Commit

Permalink
update to new APIs in 5.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Oct 15, 2024
1 parent 8c509fe commit 786e513
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 108 deletions.
248 changes: 141 additions & 107 deletions core/src/main/java/dev/morphia/query/FindOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mongodb.ReadPreference;
import com.mongodb.assertions.Assertions;
import com.mongodb.client.FindIterable;
import com.mongodb.client.cursor.TimeoutMode;
import com.mongodb.client.model.Collation;

import dev.morphia.annotations.internal.MorphiaInternal;
Expand Down Expand Up @@ -42,30 +43,53 @@ public final class FindOptions implements ReadConfigurable<FindOptions>, Collect
private Boolean allowDiskUse;
private int batchSize;

private boolean disableValidation = false;

private int limit;
private long maxTimeMS;
private long maxAwaitTimeMS;
private int skip;
private Document variables;
private Document sort;
private CursorType cursorType;
private boolean noCursorTimeout;
private boolean partial;
private Collation collation;

private String collection;

private BsonValue comment;

private CursorType cursorType;

private boolean disableValidation = false;

private Document hint;

private String hintString;

private int limit;

private Document max;

private long maxAwaitTimeMS;

private long maxTimeMS;

private Document min;
private boolean returnKey;
private boolean showRecordId;
private ReadConcern readConcern;
private ReadPreference readPreference;

private boolean noCursorTimeout;

private boolean partial;

private Projection projection;

private String queryLogId;
private String collection;

private ReadConcern readConcern;

private ReadPreference readPreference;

private boolean returnKey;

private boolean showRecordId;

private int skip;

private Document sort;

private TimeoutMode timeoutMode;

private Document variables;

/**
* Enables writing to temporary files on the server. When set to true, the server
Expand Down Expand Up @@ -133,10 +157,45 @@ public <T> FindIterable<T> apply(FindIterable<T> iterable, Mapper mapper, Class<
}
iterable.sort(mapped);
}
tryInvoke(v4_6_0, () -> iterable.let(variables));
iterable.let(variables);
iterable.timeoutMode(timeoutMode);
return iterable;
}

/**
* This is an internal method. It's implementation and presence are subject to change.
*
* @return this
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public boolean isLogQuery() {
return queryLogId != null;
}

/**
* This is an experimental method. It's implementation and presence are subject to change.
*
* @return this
*/
public FindOptions logQuery() {
queryLogId = new ObjectId().toString();
comment(Sofia.loggedQuery(queryLogId));
return this;
}

/**
* Sets the comment to log with the query
*
* @param comment the comment
* @return this
*/
public FindOptions comment(String comment) {
this.comment = new BsonString(comment);
return this;
}

/**
* Sets the batch size
*
Expand Down Expand Up @@ -175,17 +234,6 @@ public String collection() {
return collection;
}

/**
* Sets the comment to log with the query
*
* @param comment the comment
* @return this
*/
public FindOptions comment(String comment) {
this.comment = new BsonString(comment);
return this;
}

/**
* Sets the comment to log with the query
*
Expand Down Expand Up @@ -302,6 +350,37 @@ public boolean equals(Object o) {
&& Objects.equals(queryLogId, that.queryLogId);
}

/**
* @hidden
* @morphia.internal
*/
@MorphiaInternal
@Override
public String toString() {
return new StringJoiner(", ", FindOptions.class.getSimpleName() + "[", "]")
.add("allowDiskUse=" + allowDiskUse)
.add("batchSize=" + batchSize)
.add("limit=" + limit)
.add("maxTimeMS=" + maxTimeMS)
.add("maxAwaitTimeMS=" + maxAwaitTimeMS)
.add("skip=" + skip)
.add("sort=" + sort)
.add("cursorType=" + cursorType)
.add("noCursorTimeout=" + noCursorTimeout)
.add("partial=" + partial)
.add("collation=" + collation)
.add("comment='" + comment + "'")
.add("hint=" + hint)
.add("max=" + max)
.add("min=" + min)
.add("returnKey=" + returnKey)
.add("showRecordId=" + showRecordId)
.add("readPreference=" + readPreference)
.add("queryLogId='" + queryLogId + "'")
.add("projection=" + projection)
.toString();
}

/**
* Sets the index hint
*
Expand Down Expand Up @@ -335,18 +414,6 @@ public FindOptions hintString(String hint) {
return this;
}

/**
* This is an internal method. It's implementation and presence are subject to change.
*
* @return this
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public boolean isLogQuery() {
return queryLogId != null;
}

/**
* Add top-level variables to the operation. A null value means no variables are set.
*
Expand All @@ -363,16 +430,6 @@ public FindOptions let(Document variables) {
return this;
}

/**
* @return the query log id used for retrieving the logged query
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public String queryLogId() {
return queryLogId;
}

/**
* Sets the limit
*
Expand All @@ -384,17 +441,6 @@ public FindOptions limit(int limit) {
return this;
}

/**
* This is an experimental method. It's implementation and presence are subject to change.
*
* @return this
*/
public FindOptions logQuery() {
queryLogId = new ObjectId().toString();
comment(Sofia.loggedQuery(queryLogId));
return this;
}

/**
* Sets the max index value
*
Expand Down Expand Up @@ -480,16 +526,13 @@ public FindOptions partial(boolean partial) {
}

/**
* @return the projection
* @return the query log id used for retrieving the logged query
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public Projection projection() {
if (projection == null) {
projection = new Projection(this);
}
return projection;
public String queryLogId() {
return queryLogId;
}

/**
Expand Down Expand Up @@ -558,46 +601,39 @@ public FindOptions skip(int skip) {
}

/**
* Sets to the sort to use
*
* @param meta the meta data to sort by
* @return this
* @since 2.0
*/
public FindOptions sort(Meta meta) {
projection().project(meta);
return sort(meta.toDatabase());
}

/**
* @return the projection
* @hidden
* @morphia.internal
*/
@MorphiaInternal
@Override
public String toString() {
return new StringJoiner(", ", FindOptions.class.getSimpleName() + "[", "]")
.add("allowDiskUse=" + allowDiskUse)
.add("batchSize=" + batchSize)
.add("limit=" + limit)
.add("maxTimeMS=" + maxTimeMS)
.add("maxAwaitTimeMS=" + maxAwaitTimeMS)
.add("skip=" + skip)
.add("sort=" + sort)
.add("cursorType=" + cursorType)
.add("noCursorTimeout=" + noCursorTimeout)
.add("partial=" + partial)
.add("collation=" + collation)
.add("comment='" + comment + "'")
.add("hint=" + hint)
.add("max=" + max)
.add("min=" + min)
.add("returnKey=" + returnKey)
.add("showRecordId=" + showRecordId)
.add("readPreference=" + readPreference)
.add("queryLogId='" + queryLogId + "'")
.add("projection=" + projection)
.toString();
public Projection projection() {
if (projection == null) {
projection = new Projection(this);
}
return projection;
}

/**
* Sets to the sort to use
*
* @param meta the meta data to sort by
* @param sort the sort document
* @return this
* @since 2.0
*/
public FindOptions sort(Meta meta) {
projection().project(meta);
return sort(meta.toDatabase());
public FindOptions sort(Document sort) {
this.sort = new Document(sort);
return this;
}

/**
Expand All @@ -618,17 +654,6 @@ public FindOptions sort(Meta meta, Sort... sorts) {
return this;
}

/**
* Sets to the sort to use
*
* @param sort the sort document
* @return this
*/
public FindOptions sort(Document sort) {
this.sort = new Document(sort);
return this;
}

/**
* Sets to the sort to use
*
Expand All @@ -643,4 +668,13 @@ public FindOptions sort(Sort... sorts) {
return this;
}

public TimeoutMode timeoutMode() {
return timeoutMode;
}

public FindOptions timeoutMode(TimeoutMode timeoutMode) {
this.timeoutMode = timeoutMode;
return this;
}

}
6 changes: 5 additions & 1 deletion core/src/test/java/dev/morphia/test/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.FindOneAndDeleteOptions;
import com.mongodb.client.model.FindOneAndUpdateOptions;
Expand All @@ -23,6 +24,7 @@
import dev.morphia.query.FindAndDeleteOptions;
import dev.morphia.query.FindOptions;

import org.bson.BsonValue;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.testng.Assert;
Expand All @@ -32,7 +34,7 @@
public class OptionsTest {
@Test
public void aggregationOptions() {
beanScan(com.mongodb.AggregationOptions.class, AggregationOptions.class, List.of("builder",
beanScan(AggregateIterable.class, AggregationOptions.class, List.of("builder",
"getAllowDiskUse",
"getBatchSize",
"getBypassDocumentValidation",
Expand Down Expand Up @@ -117,6 +119,8 @@ private Class<?>[] convert(Class<?>[] types) {
for (int i = 0; i < types.length; i++) {
if (types[i].equals(Bson.class)) {
types[i] = Document.class;
} else if (types[i].equals(BsonValue.class)) {
types[i] = String.class;
}
}
return types;
Expand Down

0 comments on commit 786e513

Please sign in to comment.