Skip to content
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

Support ReadPreference Option For Find #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
obj.setLimit(((Number)member.getValue()).intValue());
}
break;
case "readPreference":
if (member.getValue() instanceof String) {
obj.setReadPreference((String)member.getValue());
}
break;
case "skip":
if (member.getValue() instanceof Number) {
obj.setSkip(((Number)member.getValue()).intValue());
Expand Down Expand Up @@ -63,6 +68,9 @@ public static void toJson(FindOptions obj, java.util.Map<String, Object> json) {
json.put("hint", obj.getHint());
}
json.put("limit", obj.getLimit());
if (obj.getReadPreference() != null) {
json.put("readPreference", obj.getReadPreference());
}
json.put("skip", obj.getSkip());
if (obj.getSort() != null) {
json.put("sort", obj.getSort());
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/vertx/ext/mongo/FindOptions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.vertx.ext.mongo;

import com.mongodb.ReadPreference;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;

import java.util.Objects;

/**
* Options used to configure find operations.
*
Expand Down Expand Up @@ -32,6 +35,7 @@ public class FindOptions {
private int skip;
private int batchSize;
private String hint;
private ReadPreference readPreference;

/**
* Default constructor
Expand All @@ -43,6 +47,7 @@ public FindOptions() {
this.skip = DEFAULT_SKIP;
this.batchSize = DEFAULT_BATCH_SIZE;
this.hint = new String();
this.readPreference = null;
}

/**
Expand All @@ -57,6 +62,7 @@ public FindOptions(FindOptions options) {
this.skip = options.skip;
this.batchSize = options.batchSize;
this.hint = options.hint;
this.readPreference = options.readPreference;
}

/**
Expand Down Expand Up @@ -197,6 +203,33 @@ public FindOptions setHint(String hint) {
return this;
}

/**
* Get the readPreference. This determines the operation readPreference to use.
*
* @return the readPreference
*/
public ReadPreference getReadPreference(boolean returnObj) {
return readPreference;
}
public String getReadPreference() {
return readPreference == null ? null : readPreference.getName();
}

/**
* Set the readPreference
*
* @param readPreference the readPreference
* @return reference to this, for fluency
*/
public FindOptions setReadPreference(ReadPreference readPreference) {
this.readPreference = readPreference;
return this;
}
public FindOptions setReadPreference(String readPreferenceName) {
this.readPreference = (readPreferenceName == null ? null : ReadPreference.valueOf(readPreferenceName));
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -209,6 +242,7 @@ public boolean equals(Object o) {
if (batchSize != that.batchSize) return false;
if (fields != null ? !fields.equals(that.fields) : that.fields != null) return false;
if (hint != null ? !hint.equals(that.hint) : that.hint != null) return false;
if (!Objects.equals(readPreference, that.readPreference)) return false;
return sort != null ? sort.equals(that.sort) : that.sort == null;
}

Expand All @@ -220,6 +254,7 @@ public int hashCode() {
result = 31 * result + skip;
result = 31 * result + batchSize;
result = 31 * result + (hint != null ? hint.hashCode() : 0);
result = 31 * result + (readPreference != null ? readPreference.hashCode() : 0);
return result;
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/vertx/ext/mongo/MongoClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.ext.mongo;

import com.mongodb.MongoClientSettings;
import com.mongodb.ReadPreference;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.annotations.GenIgnore;
Expand Down Expand Up @@ -311,6 +312,19 @@ MongoClient bulkWriteWithOptions(String collection, List<BulkOperation> operatio
*/
Future<@Nullable JsonObject> findOne(String collection, JsonObject query, @Nullable JsonObject fields);

/**
* Like {@link #findOne(String, JsonObject, JsonObject, Handler)} but give an option to set ReadPreference
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@Fluent
MongoClient findOne(String collection, JsonObject query, @Nullable JsonObject fields, @Nullable ReadPreference readPreference, Handler<AsyncResult<@Nullable JsonObject>> resultHandler);

/**
* Like {@link #findOne(String, JsonObject, JsonObject)} but give an option to set ReadPreference
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
Future<@Nullable JsonObject> findOne(String collection, JsonObject query, @Nullable JsonObject fields, @Nullable ReadPreference readPreference);

/**
* Find a single matching document in the specified collection and update it.
* <p>
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.vertx.ext.mongo.impl;

import com.mongodb.MongoClientSettings;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.model.*;
Expand Down Expand Up @@ -353,15 +354,32 @@ public io.vertx.ext.mongo.MongoClient findOne(String collection, JsonObject quer

@Override
public Future<@Nullable JsonObject> findOne(String collection, JsonObject query, @Nullable JsonObject fields) {
return findOne(collection, query, fields, (ReadPreference) null);
}

@Override
public MongoClient findOne(String collection, JsonObject query, @Nullable JsonObject fields, @Nullable ReadPreference readPreference, Handler<AsyncResult<@Nullable JsonObject>> resultHandler) {
Future<JsonObject> future = findOne(collection, query, fields, readPreference);
setHandler(future, resultHandler);
return this;
}

@Override
public Future<@Nullable JsonObject> findOne(String collection, JsonObject query, @Nullable JsonObject fields, @Nullable ReadPreference readPreference) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");

JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);

Bson bquery = wrap(encodedQuery);
Bson bfields = wrap(fields);

Promise<JsonObject> promise = vertx.promise();
getCollection(collection).find(bquery).projection(bfields).first().subscribe(new SingleResultSubscriber<>(promise));
MongoCollection<JsonObject> coll = getCollection(collection);
if (readPreference != null) {
coll = coll.withReadPreference(readPreference);
}
coll.find(bquery).projection(bfields).first().subscribe(new SingleResultSubscriber<>(promise));
return promise.future().map(object -> object == null ? null : decodeKeyWhenUseObjectId(object));
}

Expand Down Expand Up @@ -983,6 +1001,9 @@ private JsonObject decodeKeyWhenUseObjectId(JsonObject json) {
private FindPublisher<JsonObject> doFind(String collection, JsonObject query, FindOptions options) {
MongoCollection<JsonObject> coll = getCollection(collection);
Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
if (options.getReadPreference() != null) {
coll = coll.withReadPreference(options.getReadPreference(true));
}
FindPublisher<JsonObject> find = coll.find(bquery, JsonObject.class);
if (options.getLimit() != -1) {
find.limit(options.getLimit());
Expand Down