Skip to content

Commit eb4b830

Browse files
authored
DOCSP-46389: client bw (#189)
* DOCSP-46389: client bw * wip
1 parent 904348b commit eb4b830

File tree

8 files changed

+511
-44
lines changed

8 files changed

+511
-44
lines changed

examples/src/test/kotlin/BulkTest.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ import org.junit.jupiter.api.BeforeAll
2424
import org.junit.jupiter.api.Test
2525
import org.junit.jupiter.api.TestInstance
2626

27-
// :snippet-start: bulk-data-model
28-
data class Person(
29-
@BsonId val id: Int,
30-
val name: String,
31-
val age: Int? = null,
32-
val location: String? = null
33-
)
34-
// :snippet-end:
35-
3627
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
3728
internal class BulkTest {
29+
30+
// :snippet-start: bulk-data-model
31+
data class Person(
32+
@BsonId val id: Int,
33+
val name: String,
34+
val age: Int? = null,
35+
val location: String? = null
36+
)
37+
// :snippet-end:
38+
3839
companion object {
3940
val config = getConfig()
4041
val client = MongoClient.create(config.connectionUri)
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import com.mongodb.MongoNamespace
2+
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.bulk.ClientBulkWriteOptions
4+
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import config.getConfig
7+
import kotlinx.coroutines.runBlocking
8+
import org.bson.codecs.pojo.annotations.BsonId
9+
import org.junit.jupiter.api.AfterAll
10+
import org.junit.jupiter.api.Assertions.assertEquals
11+
import org.junit.jupiter.api.BeforeAll
12+
import org.junit.jupiter.api.TestInstance
13+
import kotlin.test.Ignore
14+
15+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
16+
internal class ClientBulkTest {
17+
18+
// :snippet-start: data-classes
19+
data class Person(
20+
@BsonId val id: Int,
21+
val name: String,
22+
)
23+
24+
data class Object(
25+
@BsonId val id: Int,
26+
val type: String,
27+
)
28+
// :snippet-end:
29+
30+
31+
companion object {
32+
val config = getConfig()
33+
val client = MongoClient.create(config.connectionUri)
34+
val database = client.getDatabase("sample_db")
35+
val personCollection = database.getCollection<Person>("people")
36+
val objectCollection = database.getCollection<Object>("objects")
37+
38+
@BeforeAll
39+
@JvmStatic
40+
fun beforeAll() {
41+
runBlocking {
42+
personCollection.insertOne(Person(1, "Sandy King"))
43+
objectCollection.insertOne(Object(1, "artist easel"))
44+
}
45+
}
46+
47+
@AfterAll
48+
@JvmStatic
49+
fun afterAll() {
50+
runBlocking {
51+
personCollection.drop()
52+
objectCollection.drop()
53+
client.close()
54+
}
55+
}
56+
}
57+
58+
// Ignoring tests because successful completion of
59+
// writes is blocked on https://jira.mongodb.org/browse/CLOUDP-288992
60+
@Ignore
61+
fun insertOperationTest() = runBlocking {
62+
// :snippet-start: insert-models
63+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
64+
65+
docsToInsert.add(ClientNamespacedWriteModel
66+
.insertOne(
67+
MongoNamespace("sample_db", "people"),
68+
Person(2, "Julia Smith")
69+
)
70+
)
71+
72+
docsToInsert.add(ClientNamespacedWriteModel
73+
.insertOne(
74+
MongoNamespace("sample_db", "objects"),
75+
Object(2, "washing machine")
76+
)
77+
)
78+
79+
val clientBulkResult = client.bulkWrite(docsToInsert)
80+
// :snippet-end:
81+
82+
// Junit test for the above code
83+
assertEquals(2, objectCollection.countDocuments())
84+
assertEquals(2, personCollection.countDocuments())
85+
}
86+
87+
@Ignore
88+
fun replaceOperationTest() = runBlocking {
89+
// :snippet-start: replace-models
90+
val docsReplacements = mutableListOf<ClientNamespacedWriteModel>()
91+
92+
docsReplacements.add(ClientNamespacedWriteModel
93+
.replaceOne(
94+
MongoNamespace("sample_db", "people"),
95+
Filters.eq(Person::id.name, 1),
96+
Person(1, "Frederic Hilbert")
97+
)
98+
)
99+
100+
docsReplacements.add(ClientNamespacedWriteModel
101+
.replaceOne(
102+
MongoNamespace("sample_db", "objects"),
103+
Filters.eq(Object::id.name, 1),
104+
Object(1, "ironing board")
105+
)
106+
)
107+
108+
val clientBulkResult = client.bulkWrite(docsReplacements)
109+
// :snippet-end:
110+
111+
// Junit test for the above code
112+
assertEquals(1, objectCollection.countDocuments())
113+
}
114+
115+
@Ignore
116+
fun orderOfOperationsTest() = runBlocking {
117+
// :snippet-start: options
118+
val namespace = MongoNamespace("sample_db", "people")
119+
120+
val options = ClientBulkWriteOptions
121+
.clientBulkWriteOptions()
122+
.ordered(false)
123+
124+
val bulkOperations = listOf(
125+
ClientNamespacedWriteModel.insertOne(
126+
namespace,
127+
Person(2, "Rudra Suraj")
128+
),
129+
// Causes duplicate key error
130+
ClientNamespacedWriteModel.insertOne(
131+
namespace,
132+
Person(2, "Wendy Zhang")
133+
),
134+
ClientNamespacedWriteModel.insertOne(
135+
namespace,
136+
Person(4, "Mario Bianchi")
137+
)
138+
)
139+
140+
val result = client.bulkWrite(bulkOperations, options)
141+
// :snippet-end:
142+
143+
// Junit test for the above code
144+
assertEquals(3, personCollection.countDocuments())
145+
}
146+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data class Person(
2+
@BsonId val id: Int,
3+
val name: String,
4+
)
5+
6+
data class Object(
7+
@BsonId val id: Int,
8+
val type: String,
9+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
val docsToInsert = mutableListOf<ClientNamespacedWriteModel>()
2+
3+
docsToInsert.add(ClientNamespacedWriteModel
4+
.insertOne(
5+
MongoNamespace("sample_db", "people"),
6+
Person(2, "Julia Smith")
7+
)
8+
)
9+
10+
docsToInsert.add(ClientNamespacedWriteModel
11+
.insertOne(
12+
MongoNamespace("sample_db", "objects"),
13+
Object(2, "washing machine")
14+
)
15+
)
16+
17+
val clientBulkResult = client.bulkWrite(docsToInsert)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
val namespace = MongoNamespace("sample_db", "people")
2+
3+
val options = ClientBulkWriteOptions
4+
.clientBulkWriteOptions()
5+
.ordered(false)
6+
7+
val bulkOperations = listOf(
8+
ClientNamespacedWriteModel.insertOne(
9+
namespace,
10+
Person(2, "Rudra Suraj")
11+
),
12+
// Causes duplicate key error
13+
ClientNamespacedWriteModel.insertOne(
14+
namespace,
15+
Person(2, "Wendy Zhang")
16+
),
17+
ClientNamespacedWriteModel.insertOne(
18+
namespace,
19+
Person(4, "Mario Bianchi")
20+
)
21+
)
22+
23+
val result = client.bulkWrite(bulkOperations, options)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
val docsReplacements = mutableListOf<ClientNamespacedWriteModel>()
2+
3+
docsReplacements.add(ClientNamespacedWriteModel
4+
.replaceOne(
5+
MongoNamespace("sample_db", "people"),
6+
Filters.eq(Person::id.name, 1),
7+
Person(1, "Frederic Hilbert")
8+
)
9+
)
10+
11+
docsReplacements.add(ClientNamespacedWriteModel
12+
.replaceOne(
13+
MongoNamespace("sample_db", "objects"),
14+
Filters.eq(Object::id.name, 1),
15+
Object(1, "ironing board")
16+
)
17+
)
18+
19+
val clientBulkResult = client.bulkWrite(docsReplacements)

0 commit comments

Comments
 (0)