-
Notifications
You must be signed in to change notification settings - Fork 12
Introduce batch support. #136
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
Open
vbabanin
wants to merge
29
commits into
main
Choose a base branch
from
HIBERNATE-40
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,850
−129
Open
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4b65eb2
Introduce batch support.
vbabanin 5b17d58
Add tests and refactor MongoStatements.
vbabanin 3f1ebcb
Merge branch 'main' into HIBERNATE-40
vbabanin ded2bb7
Rename tests.
vbabanin 92d9c41
Rename variables.
vbabanin b78cb77
Rename methods, add tests.
vbabanin a7f7792
Revert to private.
vbabanin 7372d56
Add JDBC exception handling.
vbabanin 330de0f
Apply spotless.
vbabanin 5c81496
Apply spotless.
vbabanin e4d7afd
Revert SqlTransientException handling.
vbabanin 183aafc
Remove SQlConsumer.
vbabanin 9577603
Fix issues.
vbabanin 76acbc5
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 39df511
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 11a2b07
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 510bc2d
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 6faa9cb
Fix batch update count reporting.
vbabanin b27ef0e
Merge branch 'main' into HIBERNATE-40
vbabanin 7c8a702
Make NULL_SQL_STATE private.
vbabanin 51103be
Remove final in parameters.
vbabanin 4317e2f
Remove catch.
vbabanin 2b5be78
Use constants.
vbabanin 40c75cf
Remove accidental use of NO_ERROR_CODE.
vbabanin 91a88f1
Consolidate batch execution logic in one place.
vbabanin 9435785
Remove redundunt methods.
vbabanin 46d4682
Update src/main/java/com/mongodb/hibernate/jdbc/MongoStatement.java
vbabanin afd8853
Update src/main/java/com/mongodb/hibernate/jdbc/MongoStatement.java
vbabanin 18a18f7
Apply suggestions from code review
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
417 changes: 382 additions & 35 deletions
417
...tegrationTest/java/com/mongodb/hibernate/jdbc/MongoPreparedStatementIntegrationTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
...ntegrationTest/java/com/mongodb/hibernate/query/mutation/BatchUpdateIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /* | ||
| * Copyright 2025-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.hibernate.query.mutation; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.bson.RawBsonDocument.parse; | ||
|
|
||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.hibernate.junit.InjectMongoCollection; | ||
| import com.mongodb.hibernate.query.AbstractQueryIntegrationTests; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import java.util.List; | ||
| import org.bson.BsonDocument; | ||
| import org.hibernate.cfg.AvailableSettings; | ||
| import org.hibernate.engine.spi.SessionImplementor; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
| import org.hibernate.testing.orm.junit.Setting; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @DomainModel(annotatedClasses = BatchUpdateIntegrationTests.Item.class) | ||
| @ServiceRegistry(settings = @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "3")) | ||
| class BatchUpdateIntegrationTests extends AbstractQueryIntegrationTests { | ||
|
|
||
| private static final String COLLECTION_NAME = "items"; | ||
| private static final int ENTITIES_TO_PERSIST_COUNT = 5; | ||
|
|
||
| @InjectMongoCollection(COLLECTION_NAME) | ||
| private static MongoCollection<BsonDocument> collection; | ||
|
|
||
| @BeforeEach | ||
| void beforeEach() { | ||
| getTestCommandListener().clear(); | ||
| } | ||
|
|
||
| @Test | ||
| void testBatchInsert() { | ||
| getSessionFactoryScope().inTransaction(session -> { | ||
| for (int i = 1; i <= ENTITIES_TO_PERSIST_COUNT; i++) { | ||
| session.persist(new Item(i, String.valueOf(i))); | ||
| } | ||
| session.flush(); | ||
| assertActualCommandsInOrder( | ||
| parse( | ||
| """ | ||
| { | ||
| "insert": "items", | ||
| "ordered": true, | ||
| "documents": [ | ||
| { "_id": 1, "string": "1"}, | ||
| { "_id": 2, "string": "2"}, | ||
| { "_id": 3, "string": "3"} | ||
| ] | ||
| } | ||
| """), | ||
| parse( | ||
| """ | ||
| { | ||
| "insert": "items", | ||
| "ordered": true, | ||
| "documents": [ | ||
| { "_id": 4, "string": "4"}, | ||
| { "_id": 5, "string": "5"} | ||
| ] | ||
| } | ||
| """)); | ||
| }); | ||
|
|
||
| assertThat(collection.find()) | ||
| .containsExactlyElementsOf(List.of( | ||
| BsonDocument.parse("{ _id: 1, string: '1' }"), | ||
| BsonDocument.parse("{ _id: 2, string: '2' }"), | ||
| BsonDocument.parse("{ _id: 3, string: '3' }"), | ||
| BsonDocument.parse("{ _id: 4, string: '4' }"), | ||
| BsonDocument.parse("{ _id: 5, string: '5' }"))); | ||
| } | ||
|
|
||
| @Test | ||
| void testBatchUpdate() { | ||
| getSessionFactoryScope().inTransaction(session -> { | ||
| insertTestData(session); | ||
| for (int i = 1; i <= ENTITIES_TO_PERSIST_COUNT; i++) { | ||
| Item item = session.find(Item.class, i); | ||
| item.string = "u" + i; | ||
| } | ||
| session.flush(); | ||
| assertActualCommandsInOrder( | ||
| parse( | ||
| """ | ||
| { | ||
| "update": "items", | ||
| "ordered": true, | ||
| "updates": [ | ||
| { "q": { "_id": { "$eq": 1 } }, "u": { "$set": { "string": "u1" } }, "multi": true }, | ||
| { "q": { "_id": { "$eq": 2 } }, "u": { "$set": { "string": "u2" } }, "multi": true }, | ||
| { "q": { "_id": { "$eq": 3 } }, "u": { "$set": { "string": "u3" } }, "multi": true } | ||
| ] | ||
| } | ||
| """), | ||
| parse( | ||
| """ | ||
| { | ||
| "update": "items", | ||
| "ordered": true, | ||
| "updates": [ | ||
| { "q": { "_id": { "$eq": 4 } }, "u": { "$set": { "string": "u4" } }, "multi": true }, | ||
| { "q": { "_id": { "$eq": 5 } }, "u": { "$set": { "string": "u5" } }, "multi": true } | ||
| ] | ||
| } | ||
| """)); | ||
| }); | ||
|
|
||
| assertThat(collection.find()) | ||
| .containsExactlyElementsOf(java.util.List.of( | ||
| BsonDocument.parse("{ _id: 1, string: 'u1' }"), | ||
| BsonDocument.parse("{ _id: 2, string: 'u2' }"), | ||
| BsonDocument.parse("{ _id: 3, string: 'u3' }"), | ||
| BsonDocument.parse("{ _id: 4, string: 'u4' }"), | ||
| BsonDocument.parse("{ _id: 5, string: 'u5' }"))); | ||
| } | ||
|
|
||
| @Test | ||
| void testBatchDelete() { | ||
| getSessionFactoryScope().inTransaction(session -> { | ||
| insertTestData(session); | ||
| for (int i = 1; i <= ENTITIES_TO_PERSIST_COUNT; i++) { | ||
| var item = session.find(Item.class, i); | ||
| session.remove(item); | ||
| } | ||
| session.flush(); | ||
| assertActualCommandsInOrder( | ||
| parse( | ||
| """ | ||
| { | ||
| "delete": "items", | ||
| "ordered": true, | ||
| "deletes": [ | ||
| {"q": {"_id": {"$eq": 1}}, "limit": 0}, | ||
| {"q": {"_id": {"$eq": 2}}, "limit": 0}, | ||
| {"q": {"_id": {"$eq": 3}}, "limit": 0} | ||
| ] | ||
| } | ||
| """), | ||
| parse( | ||
| """ | ||
| { | ||
| "delete": "items", | ||
| "ordered": true, | ||
| "deletes": [ | ||
| {"q": {"_id": {"$eq": 4}}, "limit": 0}, | ||
| {"q": {"_id": {"$eq": 5}}, "limit": 0} | ||
| ] | ||
| } | ||
| """)); | ||
| }); | ||
|
|
||
| assertThat(collection.find()).isEmpty(); | ||
| } | ||
|
|
||
| private void insertTestData(final SessionImplementor session) { | ||
| for (int i = 1; i <= 5; i++) { | ||
| session.persist(new Item(i, String.valueOf(i))); | ||
| } | ||
| session.flush(); | ||
| getTestCommandListener().clear(); | ||
| } | ||
|
|
||
| @Entity | ||
| @Table(name = COLLECTION_NAME) | ||
| static class Item { | ||
| @Id | ||
| int id; | ||
|
|
||
| String string; | ||
|
|
||
| Item() {} | ||
|
|
||
| Item(int id, String string) { | ||
| this.id = id; | ||
| this.string = string; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional]
This check may not help us at all, as batching parameterless commands is hardly practical, but neither will it hurt, and it makes it clear that we have to clone specifically because of the parameter value setters.