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

Add Kotlin Bulk Write API #1591

Merged
merged 5 commits into from
Jan 3, 2025
Merged
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
2 changes: 1 addition & 1 deletion config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ complexity:
active: true
excludes: ['**/test/**']
thresholdInFiles: 25
thresholdInClasses: 25
thresholdInClasses: 27
thresholdInInterfaces: 25
thresholdInObjects: 25
thresholdInEnums: 25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,25 @@ internal open class SyncMongoCluster(open val wrapped: MongoCluster) : JMongoClu
): ChangeStreamIterable<T> =
SyncChangeStreamIterable(wrapped.watch(clientSession.unwrapped(), pipeline, resultClass))

override fun bulkWrite(models: MutableList<out ClientNamespacedWriteModel>): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
override fun bulkWrite(models: MutableList<out ClientNamespacedWriteModel>): ClientBulkWriteResult = runBlocking {
wrapped.bulkWrite(models)
}

override fun bulkWrite(
models: MutableList<out ClientNamespacedWriteModel>,
options: ClientBulkWriteOptions
): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
): ClientBulkWriteResult = runBlocking { wrapped.bulkWrite(models, options) }

override fun bulkWrite(
clientSession: ClientSession,
models: MutableList<out ClientNamespacedWriteModel>
): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
): ClientBulkWriteResult = runBlocking { wrapped.bulkWrite(clientSession.unwrapped(), models) }

override fun bulkWrite(
clientSession: ClientSession,
models: MutableList<out ClientNamespacedWriteModel>,
options: ClientBulkWriteOptions
): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
): ClientBulkWriteResult = runBlocking { wrapped.bulkWrite(clientSession.unwrapped(), models, options) }

private fun ClientSession.unwrapped() = (this as SyncClientSession).wrapped
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@
*/
package com.mongodb.kotlin.client.coroutine

import com.mongodb.ClientBulkWriteException
import com.mongodb.ClientSessionOptions
import com.mongodb.MongoClientSettings
import com.mongodb.MongoException
import com.mongodb.ReadConcern
import com.mongodb.ReadPreference
import com.mongodb.WriteConcern
import com.mongodb.annotations.Alpha
import com.mongodb.annotations.Reason
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
import com.mongodb.client.model.bulk.ClientBulkWriteResult
import com.mongodb.client.model.bulk.ClientNamespacedDeleteManyModel
import com.mongodb.client.model.bulk.ClientNamespacedUpdateManyModel
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
import com.mongodb.reactivestreams.client.MongoCluster as JMongoCluster
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -307,4 +315,111 @@ public open class MongoCluster protected constructor(private val wrapped: JMongo
clientSession: ClientSession,
pipeline: List<Bson> = emptyList()
): ChangeStreamFlow<T> = watch(clientSession, pipeline, T::class.java)

/**
* Executes a client-level bulk write operation. This method is functionally equivalent to
* [bulkWrite(models, options)][bulkWrite] with the
* [default options][ClientBulkWriteOptions.clientBulkWriteOptions].
Comment on lines +320 to +322
Copy link
Member Author

@vbabanin vbabanin Jan 3, 2025

Choose a reason for hiding this comment

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

Kotlin does not support referencing methods with parameters, as noted here: KT-15984 and in their Kotlin Documentation.

Note that KDoc does not have any syntax for resolving overloaded members in links. Since Kotlin's documentation generation tool puts the documentation for all overloads of a function on the same page, identifying a specific overloaded function is not required for the link to work.

Therefore, links in KDoc are written with additional descriptive text to clarify the context of the reference.

*
* This operation supports [retryable writes][MongoClientSettings.getRetryWrites]. Depending on the number of
* `models`, encoded size of `models`, and the size limits in effect, executing this operation may require multiple
* `bulkWrite` commands. The eligibility for retries is determined per each `bulkWrite` command:
* [ClientNamespacedUpdateManyModel], [ClientNamespacedDeleteManyModel] in a command render it non-retryable.
*
* This operation is not supported by MongoDB Atlas Serverless instances.
*
* @param models The [individual write operations][ClientNamespacedWriteModel].
* @return The [ClientBulkWriteResult] if the operation is successful.
* @throws ClientBulkWriteException If and only if the operation is unsuccessful or partially unsuccessful, and
* there is at least one of the following pieces of information to report:
* [ClientBulkWriteException.getWriteConcernErrors], [ClientBulkWriteException.getWriteErrors],
* [ClientBulkWriteException.getPartialResult].
* @throws MongoException Only if the operation is unsuccessful.
* @see [BulkWrite command](https://www.mongodb.com/docs/manual/reference/command/bulkWrite/)
* @since 5.3
*/
public suspend fun bulkWrite(models: List<ClientNamespacedWriteModel>): ClientBulkWriteResult =
wrapped.bulkWrite(models).awaitSingle()

/**
* Executes a client-level bulk write operation.
*
* This operation supports [retryable writes][MongoClientSettings.getRetryWrites]. Depending on the number of
* `models`, encoded size of `models`, and the size limits in effect, executing this operation may require multiple
* `bulkWrite` commands. The eligibility for retries is determined per each `bulkWrite` command:
* [ClientNamespacedUpdateManyModel], [ClientNamespacedDeleteManyModel] in a command render it non-retryable.
*
* This operation is not supported by MongoDB Atlas Serverless instances.
*
* @param models The [individual write operations][ClientNamespacedWriteModel].
* @param options The [options][ClientBulkWriteOptions].
* @return The [ClientBulkWriteResult] if the operation is successful.
* @throws ClientBulkWriteException If and only if the operation is unsuccessful or partially unsuccessful, and
* there is at least one of the following pieces of information to report:
* [ClientBulkWriteException.getWriteConcernErrors], [ClientBulkWriteException.getWriteErrors],
* [ClientBulkWriteException.getPartialResult].
* @throws MongoException Only if the operation is unsuccessful.
* @see [BulkWrite command](https://www.mongodb.com/docs/manual/reference/command/bulkWrite/)
* @since 5.3
*/
public suspend fun bulkWrite(
models: List<ClientNamespacedWriteModel>,
options: ClientBulkWriteOptions
): ClientBulkWriteResult = wrapped.bulkWrite(models, options).awaitSingle()

/**
* Executes a client-level bulk write operation. This method is functionally equivalent to
* [bulkWrite(clientSession, models, options)][bulkWrite] with the
* [default options][ClientBulkWriteOptions.clientBulkWriteOptions].
*
* This operation supports [retryable writes][MongoClientSettings.getRetryWrites]. Depending on the number of
* `models`, encoded size of `models`, and the size limits in effect, executing this operation may require multiple
* `bulkWrite` commands. The eligibility for retries is determined per each `bulkWrite` command:
* [ClientNamespacedUpdateManyModel], [ClientNamespacedDeleteManyModel] in a command render it non-retryable.
*
* This operation is not supported by MongoDB Atlas Serverless instances.
*
* @param clientSession The [client session][ClientSession] with which to associate this operation.
* @param models The [individual write operations][ClientNamespacedWriteModel].
* @return The [ClientBulkWriteResult] if the operation is successful.
* @throws ClientBulkWriteException If and only if the operation is unsuccessful or partially unsuccessful, and
* there is at least one of the following pieces of information to report:
* [ClientBulkWriteException.getWriteConcernErrors], [ClientBulkWriteException.getWriteErrors],
* [ClientBulkWriteException.getPartialResult].
* @throws MongoException Only if the operation is unsuccessful.
* @see [BulkWrite command](https://www.mongodb.com/docs/manual/reference/command/bulkWrite/)
* @since 5.3
*/
public suspend fun bulkWrite(
clientSession: ClientSession,
models: List<ClientNamespacedWriteModel>
): ClientBulkWriteResult = wrapped.bulkWrite(clientSession.wrapped, models).awaitSingle()

/**
* Executes a client-level bulk write operation.
*
* This operation supports [retryable writes][MongoClientSettings.getRetryWrites]. Depending on the number of
* `models`, encoded size of `models`, and the size limits in effect, executing this operation may require multiple
* `bulkWrite` commands. The eligibility for retries is determined per each `bulkWrite` command:
* [ClientNamespacedUpdateManyModel], [ClientNamespacedDeleteManyModel] in a command render it non-retryable.
*
* This operation is not supported by MongoDB Atlas Serverless instances.
*
* @param clientSession The [client session][ClientSession] with which to associate this operation.
* @param models The [individual write operations][ClientNamespacedWriteModel].
* @param options The [options][ClientBulkWriteOptions].
* @return The [ClientBulkWriteResult] if the operation is successful.
* @throws ClientBulkWriteException If and only if the operation is unsuccessful or partially unsuccessful, and
* there is at least one of the following pieces of information to report:
* [ClientBulkWriteException.getWriteConcernErrors], [ClientBulkWriteException.getWriteErrors],
* [ClientBulkWriteException.getPartialResult].
* @throws MongoException Only if the operation is unsuccessful.
* @see [BulkWrite command](https://www.mongodb.com/docs/manual/reference/command/bulkWrite/)
* @since 5.3
*/
public suspend fun bulkWrite(
clientSession: ClientSession,
models: List<ClientNamespacedWriteModel>,
options: ClientBulkWriteOptions
): ClientBulkWriteResult = wrapped.bulkWrite(clientSession.wrapped, models, options).awaitSingle()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package com.mongodb.kotlin.client.coroutine

import com.mongodb.ClientSessionOptions
import com.mongodb.MongoNamespace
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
import com.mongodb.reactivestreams.client.MongoClient as JMongoClient
import kotlin.reflect.full.declaredFunctions
import kotlin.test.assertEquals
Expand Down Expand Up @@ -166,4 +169,29 @@ class MongoClientTest {
verify(wrapped, times(2)).watch(clientSession.wrapped, pipeline, BsonDocument::class.java)
verifyNoMoreInteractions(wrapped)
}

@Test
fun shouldCallTheUnderlyingBulkWrite() {
val mongoClient = MongoClient(wrapped)
val requests = listOf(ClientNamespacedWriteModel.insertOne(MongoNamespace("test.test"), Document()))
val options = ClientBulkWriteOptions.clientBulkWriteOptions().bypassDocumentValidation(true)

whenever(wrapped.bulkWrite(requests)).doReturn(Mono.fromCallable { mock() })
whenever(wrapped.bulkWrite(requests, options)).doReturn(Mono.fromCallable { mock() })
whenever(wrapped.bulkWrite(clientSession.wrapped, requests)).doReturn(Mono.fromCallable { mock() })
whenever(wrapped.bulkWrite(clientSession.wrapped, requests, options)).doReturn(Mono.fromCallable { mock() })

runBlocking {
mongoClient.bulkWrite(requests)
mongoClient.bulkWrite(requests, options)
mongoClient.bulkWrite(clientSession, requests)
mongoClient.bulkWrite(clientSession, requests, options)
}

verify(wrapped).bulkWrite(requests)
verify(wrapped).bulkWrite(requests, options)
verify(wrapped).bulkWrite(clientSession.wrapped, requests)
verify(wrapped).bulkWrite(clientSession.wrapped, requests, options)
verifyNoMoreInteractions(wrapped)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,39 +113,24 @@ internal open class SyncMongoCluster(open val wrapped: MongoCluster) : JMongoClu
): ChangeStreamIterable<T> =
SyncChangeStreamIterable(wrapped.watch(clientSession.unwrapped(), pipeline, resultClass))

override fun bulkWrite(models: MutableList<out ClientNamespacedWriteModel>): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
override fun bulkWrite(models: MutableList<out ClientNamespacedWriteModel>): ClientBulkWriteResult =
wrapped.bulkWrite(models)

override fun bulkWrite(
models: MutableList<out ClientNamespacedWriteModel>,
options: ClientBulkWriteOptions
): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
): ClientBulkWriteResult = wrapped.bulkWrite(models, options)

override fun bulkWrite(
clientSession: ClientSession,
models: MutableList<out ClientNamespacedWriteModel>
): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
): ClientBulkWriteResult = wrapped.bulkWrite(clientSession.unwrapped(), models)

override fun bulkWrite(
clientSession: ClientSession,
models: MutableList<out ClientNamespacedWriteModel>,
options: ClientBulkWriteOptions
): ClientBulkWriteResult {
org.junit.jupiter.api.Assumptions.assumeTrue(
java.lang.Boolean.parseBoolean(toString()), "BULK-TODO Kotlin implement")
TODO("BULK-TODO Kotlin implement")
}
): ClientBulkWriteResult = wrapped.bulkWrite(clientSession.unwrapped(), models, options)

private fun ClientSession.unwrapped() = (this as SyncClientSession).wrapped
}
Loading