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

[RKOTLIN-612] MongoClient API #1593

Merged
merged 44 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
99489a4
Initial scaffolding of API
rorbech Dec 4, 2023
8e37043
API experiments
rorbech Dec 5, 2023
77c5a68
More API experiments
rorbech Dec 5, 2023
673b7b2
Clean up API experiments
rorbech Dec 6, 2023
4a60867
Liniting
rorbech Dec 8, 2023
a6a1fb0
Clean up internal function call
rorbech Dec 8, 2023
f4a35f9
Fix nullability of findOne-variants and add custom serialization tests
rorbech Dec 13, 2023
1e221b9
More testing
rorbech Dec 15, 2023
bc3acb8
Another round for linting
rorbech Dec 18, 2023
82c85ac
Add CHANGELOG entry
rorbech Dec 18, 2023
e1c00c8
More test coverage
rorbech Dec 18, 2023
e492629
More testing
rorbech Dec 18, 2023
4818ad2
Update test after fixing bson deserializer
rorbech Dec 19, 2023
caa9e6b
Minor clean ups
rorbech Dec 19, 2023
58ded6d
Updates according to review comments
rorbech Dec 19, 2023
d5591da
Updates according to review comments
rorbech Dec 19, 2023
490b8ef
Merge branch 'main' into cr/mongo-client
rorbech Dec 19, 2023
9e65380
Clean up experimental kbson serialization annotation usage
rorbech Dec 20, 2023
b1e740e
Add data classes for UpdateOne and UpdateMany responses
rorbech Dec 20, 2023
27d2e51
Add documentation of collection operations
rorbech Dec 20, 2023
f46cad5
Apply suggestions from code review
rorbech Jan 2, 2024
7ce74af
Link serialization
rorbech Feb 13, 2024
988e69a
Merge branch 'main' into cr/mongo-client
rorbech Feb 15, 2024
d09e23a
Merge remote-tracking branch 'origin/cr/mongo-client' into cr/mongo-c…
rorbech Feb 15, 2024
88750e8
Updates according to review comments
rorbech Feb 15, 2024
63a300e
Typed link serialization
rorbech Apr 5, 2024
8743faa
Merge branch 'main' into cr/mongo-client
rorbech Apr 5, 2024
22ab855
Clean up and linting
rorbech Apr 5, 2024
51e3e29
Test upcoming kbson release
rorbech Apr 10, 2024
9d2d6b4
Merge branch 'main' into cr/mongo-client
rorbech Apr 10, 2024
7006b9e
Fix CopyFromRealmTests
rorbech Apr 10, 2024
81013a2
Use public available kbson 0.4.0
rorbech Apr 10, 2024
ab6d33e
Increace device farm timeout
rorbech Apr 11, 2024
40e01be
Remove primary key type generic type paramenter
rorbech May 6, 2024
b2f8008
Updates according to review comments
rorbech May 13, 2024
08525c9
Merge branch 'main' into cr/mongo-client
rorbech May 13, 2024
c40fce4
Add support for serialization of collections in mixed
rorbech May 13, 2024
2be6411
Add tests for MongoDBSerializer
rorbech May 15, 2024
0d56526
Add missing PublishedApi annotations
rorbech May 15, 2024
4bcba20
Merge branch 'main' into cr/mongo-client
rorbech May 17, 2024
53db033
Remove debug logging
rorbech May 17, 2024
98f9340
Updates according to review comments
rorbech May 21, 2024
656ba49
Another round for linting
rorbech May 21, 2024
a70ae52
Merge branch 'main' into cr/mongo-client
rorbech May 22, 2024
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
Prev Previous commit
Next Next commit
Add data classes for UpdateOne and UpdateMany responses
  • Loading branch information
rorbech committed Dec 20, 2023
commit b1e740e7e8b3d2322a7e7dc9c2007c27ab1742d3
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,21 @@ public suspend fun MongoCollection<*, *>.deleteMany(filter: BsonDocument): Long
return deleteMany(filter)
}

/**
* Wrapper of results of an [updateOne] call.
*
* @param updated boolean indicating that a document was updated.
* @param upsertedId primary key of the new document if created.
*/
public data class UpdateOneResult<R>(val updated: Boolean, val upsertedId: R?)
public suspend inline fun <T : Any, reified R> MongoCollection<T, R>.updateOne(
filter: BsonDocument,
update: BsonDocument,
upsert: Boolean = false
): Pair<Boolean, R?> {
): UpdateOneResult<R> {
isType<MongoCollectionImpl<*, *>>(this)
return updateOne(filter, update, upsert).let { (updated, asdf) ->
updated to asdf?.let { decodeFromBsonValue(it) }
return updateOne(filter, update, upsert).let { (updated, upsertedId) ->
UpdateOneResult(updated, upsertedId?.let { decodeFromBsonValue(it) })
}
}

Expand All @@ -133,18 +140,25 @@ public suspend inline fun <reified R> MongoCollection<*, *>.updateOne(
filter: BsonDocument,
update: BsonDocument,
upsert: Boolean = false
): Pair<Boolean, R?> {
): UpdateOneResult<R> {
return (this as MongoCollection<BsonValue, R>).updateOne(filter, update, upsert)
}

/**
* Wrapper of results of an [updateMany] call.
*
* @param modifiedCount number of documents that was updated by the operation.
* @param upsertedId primary key of the new document if created.
*/
public data class UpdateManyResult<R>(val modifiedCount: Long, val upsertedId: R?)
public suspend inline fun <T : Any, reified R : Any> MongoCollection<T, R>.updateMany(
filter: BsonDocument,
update: BsonDocument,
upsert: Boolean = false
): Pair<Long, R?> {
): UpdateManyResult<R> {
isType<MongoCollectionImpl<*, *>>(this)
return updateMany(filter, update, upsert).let { (updatedCount, upsertedId) ->
updatedCount to upsertedId?.let { decodeFromBsonValue(it) }
UpdateManyResult(updatedCount, upsertedId?.let { decodeFromBsonValue(it) })
}
}

Expand All @@ -153,7 +167,7 @@ public suspend inline fun <reified R : Any> MongoCollection<*, *>.updateMany(
filter: BsonDocument,
update: BsonDocument,
upsert: Boolean = false
): Pair<Long, R?> {
): UpdateManyResult<R> {
return (this as MongoCollection<BsonValue, R>).updateMany(filter, update, upsert)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,38 +596,43 @@ abstract sealed class MongoCollectionTests {
assertEquals(4, collection.count())

// Update no match
val updateWithoutMatch = collection.updateOne(
collection.updateOne(
BsonDocument("""{ "name": "NOMATCH"}"""),
BsonDocument("\$set", BsonDocument("""{ "name": "UPDATED"}""")),
)
assertEquals(false to null, updateWithoutMatch)
).let { (updated, upsertedId) ->
assertFalse(updated)
assertNull(upsertedId)
}

// Update with match match
val updateWithMatch = collection.updateOne(
collection.updateOne(
BsonDocument("""{ "name": "object-1"}"""),
clementetb marked this conversation as resolved.
Show resolved Hide resolved
BsonDocument("\$set", BsonDocument("""{ "name": "object-2"}""")),
)
assertEquals(true to null, updateWithMatch)
).let { (updated, upsertedId) ->
assertTrue(updated)
assertNull(upsertedId)
}
assertEquals(4, collection.count())
assertEquals(3, collection.count(filter = BsonDocument("""{"name": "object-1"}""")))
assertEquals(1, collection.count(filter = BsonDocument("""{"name": "object-2"}""")))

// Upsert no match
val upsertWithoutMatch = collection.updateOne(
collection.updateOne(
BsonDocument("""{ "name": "object-3"}"""), BsonDocument(""" { "name": "object-2", "_id" : ${Random.nextInt()}}"""), upsert = true
)
upsertWithoutMatch.let { (updated, upsertedId) ->
).let { (updated, upsertedId) ->
assertFalse(updated)
assertIs<Int>(upsertedId)
}
assertEquals(5, collection.count())
assertEquals(2, collection.count(filter = BsonDocument("""{"name": "object-2"}""")))

// Upsert with match
val upsertWithMatch = collection.updateOne(
collection.updateOne(
BsonDocument("""{ "name": "object-2"}"""), BsonDocument(""" { "name": "object-3"}"""), upsert = true
)
assertEquals(true to null, upsertWithMatch)
).let { (updated, upsertedId) ->
assertTrue(updated)
assertNull(upsertedId)
}
assertEquals(5, collection.count())
assertEquals(1, collection.count(filter = BsonDocument("""{"name": "object-2"}""")))
}
Expand Down Expand Up @@ -666,38 +671,46 @@ abstract sealed class MongoCollectionTests {
)
assertEquals(4, collection.count())
// Update with no match
val updateWithoutMatch = collection.updateMany(
collection.updateMany(
BsonDocument("""{"name": "NOMATCH"}"""),
BsonDocument("""{"name": "UPDATED"}"""),
)
assertEquals(0L to null, updateWithoutMatch)
).let { (modifiedCount, upsertedId) ->
assertEquals(0L, modifiedCount)
assertNull(upsertedId)
}
assertEquals(0, collection.count(filter = BsonDocument("""{"name": "UPDATED"}""")))
assertEquals(4, collection.count())

// Update with match
val updateWithMatch = collection.updateMany(
collection.updateMany(
BsonDocument("""{ "name": "x"}"""),
BsonDocument("""{ "name": "UPDATED"}"""),
)
assertEquals(2L to null, updateWithMatch)
).let { (modifiedCount, upsertedId) ->
assertEquals(2L, modifiedCount)
assertNull(upsertedId)
}
assertEquals(2, collection.count(filter = BsonDocument("""{"name": "UPDATED"}""")))
assertEquals(4, collection.count())

// Upsert no match
val upsertWithoutMatch = collection.updateMany(
BsonDocument("""{ "name": "NOMATCH"}"""), BsonDocument(""" { "name": "UPSERTED", "_id" : ${Random.nextInt()}}"""), upsert = true
)
upsertWithoutMatch.let {
assertEquals(0, it.first)
assertIs<Int>(it.second)
collection.updateMany(
BsonDocument("""{ "name": "NOMATCH"}"""),
BsonDocument(""" { "name": "UPSERTED", "_id" : ${Random.nextInt()}}"""),
upsert = true
).let { (modifiedCount, upsertedId) ->
assertEquals(0L, modifiedCount)
assertIs<Int>(upsertedId)
}
assertEquals(5, collection.count())
assertEquals(1, collection.count(filter = BsonDocument("""{"name": "UPSERTED"}""")))

// Upsert with match
val upsertWithMatch = collection.updateMany(
collection.updateMany(
BsonDocument("""{ "name": "y"}"""), BsonDocument(""" { "name": "z"}"""), upsert = true
)
assertEquals(1L to null, upsertWithMatch)
).let { (modifiedCount, upsertedId) ->
assertEquals(1L, modifiedCount)
assertNull(upsertedId)
}
assertEquals(5, collection.count())
assertEquals(0, collection.count(filter = BsonDocument("""{"name": "y"}""")))
}
Expand All @@ -708,8 +721,8 @@ abstract sealed class MongoCollectionTests {
BsonDocument("""{ "name": "object-3"}"""),
BsonDocument(""" { "name": "object-2", "_id" : ${Random.nextInt()}}"""),
upsert = true
).let { (updated, upsertedId) ->
assertEquals(0, updated)
).let { (modifiedCount, upsertedId) ->
assertEquals(0, modifiedCount)
assertIs<BsonValue>(upsertedId)
}
}
Expand Down