From 786e5137e1092943df843aa6fedc15068955960d Mon Sep 17 00:00:00 2001 From: evanchooly Date: Tue, 15 Oct 2024 00:35:23 -0400 Subject: [PATCH] update to new APIs in 5.2.0 --- .../java/dev/morphia/query/FindOptions.java | 248 ++++++++++-------- .../java/dev/morphia/test/OptionsTest.java | 6 +- 2 files changed, 146 insertions(+), 108 deletions(-) diff --git a/core/src/main/java/dev/morphia/query/FindOptions.java b/core/src/main/java/dev/morphia/query/FindOptions.java index 04758bb2df4..c9632fa3739 100644 --- a/core/src/main/java/dev/morphia/query/FindOptions.java +++ b/core/src/main/java/dev/morphia/query/FindOptions.java @@ -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; @@ -42,30 +43,53 @@ public final class FindOptions implements ReadConfigurable, 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 @@ -133,10 +157,45 @@ public FindIterable apply(FindIterable 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 * @@ -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 * @@ -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 * @@ -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. * @@ -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 * @@ -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 * @@ -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; } /** @@ -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; } /** @@ -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 * @@ -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; + } + } diff --git a/core/src/test/java/dev/morphia/test/OptionsTest.java b/core/src/test/java/dev/morphia/test/OptionsTest.java index c387df33fc9..99edc634732 100644 --- a/core/src/test/java/dev/morphia/test/OptionsTest.java +++ b/core/src/test/java/dev/morphia/test/OptionsTest.java @@ -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; @@ -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; @@ -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", @@ -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;