Skip to content

Commit

Permalink
Merge pull request #30118 from loicmathieu/per-entity-read-preference
Browse files Browse the repository at this point in the history
MongoDB with Panache: allow setting per collection read preference
  • Loading branch information
gsmet authored Jan 30, 2023
2 parents f927a94 + bb402b2 commit dd48b4f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public class MongoClientConfig {
public Optional<String> readConcern;

/**
* Configures the read preferences.
* Configures the read preference.
* Supported values are: {@code primary|primaryPreferred|secondary|secondaryPreferred|nearest}
*/
@ConfigItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@
* The name of the MongoDB client (if not set the default client will be used).
*/
String clientName() default "";

/**
* Configures the read preference for the collection of this entity.
* Supported values are: {@code primary|primaryPreferred|secondary|secondaryPreferred|nearest}
*/
String readPreference() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bson.codecs.EncoderContext;
import org.jboss.logging.Logger;

import com.mongodb.ReadPreference;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.ReplaceOptions;
Expand Down Expand Up @@ -218,8 +219,20 @@ public Uni<Void> delete(Object entity) {
public ReactiveMongoCollection mongoCollection(Class<?> entityClass) {
MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);
ReactiveMongoDatabase database = mongoDatabase(mongoEntity);
if (mongoEntity != null && !mongoEntity.collection().isEmpty()) {
return database.getCollection(mongoEntity.collection(), entityClass);
if (mongoEntity != null) {
ReactiveMongoCollection collection = mongoEntity.collection().isEmpty()
? database.getCollection(entityClass.getSimpleName(), entityClass)
: database.getCollection(mongoEntity.collection(), entityClass);
if (!mongoEntity.readPreference().isEmpty()) {
try {
collection = collection.withReadPreference(ReadPreference.valueOf(mongoEntity.readPreference()));
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("Illegal read preference " + mongoEntity.readPreference()
+ " configured in the @MongoEntity annotation of " + entityClass.getName() + "."
+ " Supported values are primary|primaryPreferred|secondary|secondaryPreferred|nearest");
}
}
return collection;
}
return database.getCollection(entityClass.getSimpleName(), entityClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bson.codecs.EncoderContext;
import org.jboss.logging.Logger;

import com.mongodb.ReadPreference;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
Expand Down Expand Up @@ -190,8 +191,20 @@ public void delete(Object entity) {
public MongoCollection mongoCollection(Class<?> entityClass) {
MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);
MongoDatabase database = mongoDatabase(mongoEntity);
if (mongoEntity != null && !mongoEntity.collection().isEmpty()) {
return database.getCollection(mongoEntity.collection(), entityClass);
if (mongoEntity != null) {
MongoCollection collection = mongoEntity.collection().isEmpty()
? database.getCollection(entityClass.getSimpleName(), entityClass)
: database.getCollection(mongoEntity.collection(), entityClass);
if (!mongoEntity.readPreference().isEmpty()) {
try {
collection = collection.withReadPreference(ReadPreference.valueOf(mongoEntity.readPreference()));
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("Illegal read preference " + mongoEntity.readPreference()
+ " configured in the @MongoEntity annotation of " + entityClass.getName() + "."
+ " Supported values are primary|primaryPreferred|secondary|secondaryPreferred|nearest");
}
}
return collection;
}
return database.getCollection(entityClass.getSimpleName(), entityClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.types.ObjectId
import java.time.LocalDate

@MongoEntity(collection = "TheBookEntity", clientName = "cl2")
@MongoEntity(collection = "TheBookEntity", clientName = "cl2", readPreference = "primary")
class BookEntity : PanacheMongoEntity() {
companion object : PanacheMongoCompanion<BookEntity> {
override fun findById(id: ObjectId): BookEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.bson.codecs.pojo.annotations.BsonProperty
import java.time.LocalDate
import java.util.ArrayList

@MongoEntity(collection = "TheBookEntity", clientName = "cl2")
@MongoEntity(collection = "TheBookEntity", clientName = "cl2", readPreference = "primary")
class ReactiveBookEntity : ReactivePanacheMongoEntity() {
companion object : ReactivePanacheMongoCompanion<ReactiveBookEntity>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;

@MongoEntity(collection = "TheBookEntity", clientName = "cl2")
@MongoEntity(collection = "TheBookEntity", clientName = "cl2", readPreference = "primary")
public class BookEntity extends PanacheMongoEntity {
@BsonProperty("bookTitle")
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import io.quarkus.mongodb.panache.common.MongoEntity;
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity;

@MongoEntity(collection = "TheBookEntity", clientName = "cl2")
@MongoEntity(collection = "TheBookEntity", clientName = "cl2", readPreference = "primary")
public class ReactiveBookEntity extends ReactivePanacheMongoEntity {
@BsonProperty("bookTitle")
private String title;
Expand Down

0 comments on commit dd48b4f

Please sign in to comment.