From b5ed69e73fedeb2b4f09c0ef5a953c08cf1e2545 Mon Sep 17 00:00:00 2001 From: Liudmila Kornilova Date: Tue, 24 Aug 2021 18:36:27 +0300 Subject: [PATCH] feat(java-shell): don't convert object from Java to JS and back if results are returned directly to user (#1077) --- .../src/main/kotlin/com/mongodb/mongosh/result/Cursor.kt | 8 ++++++++ .../src/main/kotlin/com/mongodb/mongosh/service/Cursor.kt | 8 ++++++-- .../src/test/resources/collection/insertOne.expected.txt | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/result/Cursor.kt b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/result/Cursor.kt index 143a94579..2c59fee2b 100644 --- a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/result/Cursor.kt +++ b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/result/Cursor.kt @@ -1,11 +1,19 @@ package com.mongodb.mongosh.result import com.mongodb.mongosh.MongoShellConverter +import com.mongodb.mongosh.get +import com.mongodb.mongosh.set import org.graalvm.polyglot.Value open class Cursor internal constructor(protected var cursor: Value?, private var converter: MongoShellConverter?) : Iterator { private var currentIterationResult: List? = null + init { + if (cursor?.get("_transform")?.isNull == true) { + cursor?.get("_cursor")?.set("resultsUsedInScript", false) + } + } + fun _asPrintable(): String = ArrayResult(currentIterationResult ?: it())._asPrintable() private fun it(): List { diff --git a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/Cursor.kt b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/Cursor.kt index d5f8ee28c..773ae1ed8 100644 --- a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/Cursor.kt +++ b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/Cursor.kt @@ -15,6 +15,10 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va @HostAccess.Export var closed = false + @JvmField + @HostAccess.Export + var resultsUsedInScript = true + private fun getOrCreateIterator(): MongoCursor { var it = iterator if (it == null) { @@ -172,13 +176,13 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va @HostAccess.Export override fun next(): Any? { val value = getOrCreateIterator().next() - return converter.toJs(value) + return if (resultsUsedInScript) converter.toJs(value) else value } @HostAccess.Export override fun tryNext(): Any? { val value = getOrCreateIterator().tryNext() - return converter.toJs(value) + return if (resultsUsedInScript) converter.toJs(value) else value } @HostAccess.Export diff --git a/packages/java-shell/src/test/resources/collection/insertOne.expected.txt b/packages/java-shell/src/test/resources/collection/insertOne.expected.txt index cb5bc7b87..4d1c4d299 100644 --- a/packages/java-shell/src/test/resources/collection/insertOne.expected.txt +++ b/packages/java-shell/src/test/resources/collection/insertOne.expected.txt @@ -1,3 +1,3 @@ { "acknowledged": true, "insertedId": "UNKNOWN" } true -[ { "_id": , "a": 1, "objectId": , "maxKey": {"$maxKey": 1}, "minKey": {"$minKey": 1}, "binData": {"$binary": {"base64": "MTIzNA==", "subType": "10"}}, "date": {"$date": {"$numberLong": "1355875200000"}}, "isoDate": {"$date": {"$numberLong": "1355875200000"}}, "numberInt": 24, "timestamp": {"$timestamp": {"t": 100, "i": 0}}, "undefined": null, "null": null, "uuid": } ] +[ { "_id": , "a": 1, "objectId": , "maxKey": {"$maxKey": 1}, "minKey": {"$minKey": 1}, "binData": {"$binary": {"base64": "MTIzNA==", "subType": "10"}}, "date": {"$date": {"$numberLong": "1355875200000"}}, "isoDate": {"$date": {"$numberLong": "1355875200000"}}, "numberInt": 24, "timestamp": Timestamp{value=429496729600, seconds=100, inc=0}, "undefined": null, "null": null, "uuid": } ]