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

Use Bson and not Document in MongoDB with Panache methods #42726

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

loicmathieu
Copy link
Contributor

Fixes #42658

@loicmathieu
Copy link
Contributor Author

As discussed here I prefere to replace existing method parameters instead of adding overloads as there is already a lot of overloads for these classes and interfaces that already have a lot of methods.

This implies non-binary compatibility: code needs to be recompiled to work properly.

@gsmet gsmet changed the title Use Bson and not Document in MondoDB with Panache methods Use Bson and not Document in MongoDB with Panache methods Aug 25, 2024
Copy link
Member

@gsmet gsmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple of questions as the initial request was about the filters.

While I don't have any issue with using Bson for other things, I would like to make sure it makes sense.

return null;
}

@Override
protected PanacheUpdate createUpdate(MongoCollection collection, Class<?> entityClass, Document docUpdate) {
protected PanacheUpdate createUpdate(MongoCollection collection, Class<?> entityClass, Bson docUpdate) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to use Bson here? I would expect the docUpdate to be a Document? But I'm not familiar with the Bson API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, under the cover it uses PanacheUpdateImpl that was already using Bson in its signature. Always better to use interfaces instead of implementation type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in this case, the interface is low level and the Document has a real semantic (it's a Document, not some random Bson object).

Anyway, I won't oppose to it, I was just asking.

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for sort, it also makes sense to have Bson?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for document update, under the cover it uses already Bson.
Moreover, there is a com.mongodb.client.model.Sorts helper that return the Bson interface.

So you can for ex do Sorts.ascending("fileName").

@loicmathieu
Copy link
Contributor Author

@gsmet a bit of backgroud, the Bson interface was introduced in MongoDB driver 4.2, prior to 4.2 there was two ways to create a BSON document: the Document class and the BsonDocument class, both existed since driver 3.0 and has been retrofitted in 4.2 to implement the interface (as I think a lot of existing methods that was dealing with one of the two existing classes).

I don't remember if we started MongoDB with Panache prior to 4.2 or not, but I was not aware of this interface at this time, whether it was not yet created or it was brand new I don't know.

It's been a long time since I wanted to switch to exposing the interface as it unites 2 types, and there are helpers inside the driver that use it. So now that we are doing it, I think it's time to use it in all exposed methods of our API.

By the way, we use both classes inside the implementation, so it's a clear sign that we should use the Bson interface in method signatures as it allows us to be more flexible.

Copy link
Member

@FroMage FroMage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me :)

@gsmet gsmet marked this pull request as ready for review August 26, 2024 15:59
Copy link
Member

@gsmet gsmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can proceed and live with the binary incompatibility.

But I won't backport it :).

Copy link

quarkus-bot bot commented Aug 26, 2024

Status for workflow Quarkus CI

This is the status report for running Quarkus CI on commit f3fee58.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

You can consult the Develocity build scans.

@gsmet gsmet merged commit 19b56cb into quarkusio:main Aug 27, 2024
36 checks passed
@quarkus-bot quarkus-bot bot added this to the 3.16 - main milestone Aug 27, 2024
@quarkus-bot quarkus-bot bot added the kind/enhancement New feature or request label Aug 27, 2024
@loicmathieu loicmathieu deleted the feat/mongodb-panache-bson branch September 11, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use org.bson.conversions.Bson instead of org.bson.Document in PanacheMongoEntityBase
3 participants