Skip to content

Commit

Permalink
Use Bson and not Document in MondoDB with Panache methods
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu authored and danielsoro committed Sep 20, 2024
1 parent 813a92b commit 5ff82a1
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.bson.Document;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.conversions.Bson;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
Expand Down Expand Up @@ -130,12 +131,12 @@ private void assertPerson(final Person expected, final Person value) {
private static class CustomMongoOperations extends MongoOperations<Object, PanacheUpdate> {

@Override
protected Object createQuery(MongoCollection<?> collection, ClientSession session, Document query, Document sortDoc) {
protected Object createQuery(MongoCollection<?> collection, ClientSession session, Bson query, Bson sortDoc) {
return null;
}

@Override
protected PanacheUpdate createUpdate(MongoCollection collection, Class<?> entityClass, Document docUpdate) {
protected PanacheUpdate createUpdate(MongoCollection collection, Class<?> entityClass, Bson docUpdate) {
return null;
}

Expand All @@ -155,12 +156,12 @@ protected Stream<?> stream(Object queryType) {
private static class CustomReactiveMongoOperations extends ReactiveMongoOperations<Object, PanacheUpdate> {

@Override
protected Object createQuery(ReactiveMongoCollection collection, Document query, Document sortDoc) {
protected Object createQuery(ReactiveMongoCollection collection, Bson query, Bson sortDoc) {
return null;
}

@Override
protected PanacheUpdate createUpdate(ReactiveMongoCollection<?> collection, Class<?> entityClass, Document docUpdate) {
protected PanacheUpdate createUpdate(ReactiveMongoCollection<?> collection, Class<?> entityClass, Bson docUpdate) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Map;

import org.bson.Document;
import org.bson.conversions.Bson;

import io.quarkus.panache.common.Parameters;

Expand Down Expand Up @@ -43,10 +43,10 @@ public interface PanacheUpdate {
/**
* Execute the update query with the update document.
*
* @param query a {@link org.bson.Document} query
* @param query a {@link org.bson.conversions.Bson} query
* @return the number of entities updated.
*/
public long where(Document query);
public long where(Bson query);

/**
* Execute an update on all documents with the update document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Map;

import org.bson.Document;
import org.bson.conversions.Bson;

import io.quarkus.panache.common.Parameters;
import io.smallrye.mutiny.Uni;
Expand Down Expand Up @@ -43,10 +43,10 @@ public interface ReactivePanacheUpdate {
/**
* Execute the update query with the update document.
*
* @param query a {@link org.bson.Document} query
* @param query a {@link Bson} query
* @return the number of entities updated.
*/
public Uni<Long> where(Document query);
public Uni<Long> where(Bson query);

/**
* Execute an update on all documents with the update document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.EncoderContext;
import org.bson.conversions.Bson;
import org.jboss.logging.Logger;

import com.mongodb.ReadPreference;
Expand Down Expand Up @@ -56,10 +57,10 @@ public abstract class ReactiveMongoOperations<QueryType, UpdateType> {

private static final Map<String, String> defaultDatabaseName = new ConcurrentHashMap<>();

protected abstract QueryType createQuery(ReactiveMongoCollection collection, Document query, Document sortDoc);
protected abstract QueryType createQuery(ReactiveMongoCollection collection, Bson query, Bson sortDoc);

protected abstract UpdateType createUpdate(ReactiveMongoCollection<?> collection, Class<?> entityClass,
Document docUpdate);
Bson docUpdate);

protected abstract Uni<?> list(QueryType query);

Expand Down Expand Up @@ -284,7 +285,7 @@ private Uni<Void> update(ReactiveMongoCollection collection, Object entity) {

private Uni<Void> update(ReactiveMongoCollection collection, List<Object> entities) {
List<Uni<Void>> unis = entities.stream().map(entity -> update(collection, entity)).collect(Collectors.toList());
return Uni.combine().all().unis(unis).combinedWith(u -> null);
return Uni.combine().all().unis(unis).with(u -> null);
}

private Uni<Void> persistOrUpdate(ReactiveMongoCollection collection, Object entity) {
Expand Down Expand Up @@ -391,8 +392,8 @@ public QueryType find(Class<?> entityClass, String query, Object... params) {

public QueryType find(Class<?> entityClass, String query, Sort sort, Object... params) {
String bindQuery = bindFilter(entityClass, query, params);
Document docQuery = Document.parse(bindQuery);
Document docSort = sortToDocument(sort);
Bson docQuery = Document.parse(bindQuery);
Bson docSort = sortToDocument(sort);
ReactiveMongoCollection collection = mongoCollection(entityClass);
return createQuery(collection, docQuery, docSort);
}
Expand Down Expand Up @@ -490,8 +491,8 @@ public QueryType find(Class<?> entityClass, String query, Map<String, Object> pa

public QueryType find(Class<?> entityClass, String query, Sort sort, Map<String, Object> params) {
String bindQuery = bindFilter(entityClass, query, params);
Document docQuery = Document.parse(bindQuery);
Document docSort = sortToDocument(sort);
Bson docQuery = Document.parse(bindQuery);
Bson docSort = sortToDocument(sort);
ReactiveMongoCollection collection = mongoCollection(entityClass);
return createQuery(collection, docQuery, docSort);
}
Expand All @@ -504,19 +505,19 @@ public QueryType find(Class<?> entityClass, String query, Sort sort, Parameters
return find(entityClass, query, sort, params.map());
}

public QueryType find(Class<?> entityClass, Document query, Sort sort) {
public QueryType find(Class<?> entityClass, Bson query, Sort sort) {
ReactiveMongoCollection collection = mongoCollection(entityClass);
Document sortDoc = sortToDocument(sort);
Bson sortDoc = sortToDocument(sort);
return createQuery(collection, query, sortDoc);
}

public QueryType find(Class<?> entityClass, Document query, Document sort) {
public QueryType find(Class<?> entityClass, Bson query, Bson sort) {
ReactiveMongoCollection collection = mongoCollection(entityClass);
return createQuery(collection, query, sort);
}

public QueryType find(Class<?> entityClass, Document query) {
return find(entityClass, query, (Document) null);
public QueryType find(Class<?> entityClass, Bson query) {
return find(entityClass, query, (Bson) null);
}

public Uni<List<?>> list(Class<?> entityClass, String query, Object... params) {
Expand Down Expand Up @@ -544,12 +545,12 @@ public Uni<List<?>> list(Class<?> entityClass, String query, Sort sort, Paramete
}

//specific Mongo query
public Uni<List<?>> list(Class<?> entityClass, Document query) {
public Uni<List<?>> list(Class<?> entityClass, Bson query) {
return (Uni) list(find(entityClass, query));
}

//specific Mongo query
public Uni<List<?>> list(Class<?> entityClass, Document query, Document sort) {
public Uni<List<?>> list(Class<?> entityClass, Bson query, Bson sort) {
return (Uni) list(find(entityClass, query, sort));
}

Expand Down Expand Up @@ -578,12 +579,12 @@ public Multi<?> stream(Class<?> entityClass, String query, Sort sort, Parameters
}

//specific Mongo query
public Multi<?> stream(Class<?> entityClass, Document query) {
public Multi<?> stream(Class<?> entityClass, Bson query) {
return stream(find(entityClass, query));
}

//specific Mongo query
public Multi<?> stream(Class<?> entityClass, Document query, Document sort) {
public Multi<?> stream(Class<?> entityClass, Bson query, Bson sort) {
return stream(find(entityClass, query, sort));
}

Expand All @@ -594,11 +595,11 @@ public QueryType findAll(Class<?> entityClass) {

public QueryType findAll(Class<?> entityClass, Sort sort) {
ReactiveMongoCollection collection = mongoCollection(entityClass);
Document sortDoc = sortToDocument(sort);
Bson sortDoc = sortToDocument(sort);
return createQuery(collection, null, sortDoc);
}

private Document sortToDocument(Sort sort) {
private Bson sortToDocument(Sort sort) {
if (sort == null) {
return null;
}
Expand Down Expand Up @@ -662,7 +663,7 @@ public Uni<Long> count(Class<?> entityClass, String query, Parameters params) {
}

//specific Mongo query
public Uni<Long> count(Class<?> entityClass, Document query) {
public Uni<Long> count(Class<?> entityClass, Bson query) {
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
if (Panache.getCurrentSession() != null) {
return collection.countDocuments(Panache.getCurrentSession(), query);
Expand All @@ -680,7 +681,7 @@ public Uni<Long> deleteAll(Class<?> entityClass) {

public Uni<Boolean> deleteById(Class<?> entityClass, Object id) {
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
Document query = new Document().append(ID, id);
Bson query = new Document().append(ID, id);
if (Panache.getCurrentSession() != null) {
return collection.deleteOne(Panache.getCurrentSession(), query).map(results -> results.getDeletedCount() == 1);
}
Expand Down Expand Up @@ -712,7 +713,7 @@ public Uni<Long> delete(Class<?> entityClass, String query, Parameters params) {
}

//specific Mongo query
public Uni<Long> delete(Class<?> entityClass, Document query) {
public Uni<Long> delete(Class<?> entityClass, Bson query) {
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
if (Panache.getCurrentSession() != null) {
return collection.deleteMany(Panache.getCurrentSession(), query).map(DeleteResult::getDeletedCount);
Expand All @@ -732,20 +733,20 @@ public UpdateType update(Class<?> entityClass, String update, Object... params)
return executeUpdate(entityClass, update, params);
}

public UpdateType update(Class<?> entityClass, Document update) {
public UpdateType update(Class<?> entityClass, Bson update) {
return createUpdate(mongoCollection(entityClass), entityClass, update);
}

private UpdateType executeUpdate(Class<?> entityClass, String update, Object... params) {
String bindUpdate = bindUpdate(entityClass, update, params);
Document docUpdate = Document.parse(bindUpdate);
Bson docUpdate = Document.parse(bindUpdate);
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
return createUpdate(collection, entityClass, docUpdate);
}

private UpdateType executeUpdate(Class<?> entityClass, String update, Map<String, Object> params) {
String bindUpdate = bindUpdate(entityClass, update, params);
Document docUpdate = Document.parse(bindUpdate);
Bson docUpdate = Document.parse(bindUpdate);
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
return createUpdate(collection, entityClass, docUpdate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Map;

import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;

import io.quarkus.mongodb.panache.common.reactive.Panache;
Expand Down Expand Up @@ -47,7 +46,7 @@ public Uni<Long> where(String query, Parameters params) {
}

@Override
public Uni<Long> where(Document query) {
public Uni<Long> where(Bson query) {
return executeUpdate(query);
}

Expand Down
Loading

0 comments on commit 5ff82a1

Please sign in to comment.