Skip to content

Commit

Permalink
feat(java-shell): don't convert object from Java to JS and back if re…
Browse files Browse the repository at this point in the history
…sults are returned directly to user (#1077)
  • Loading branch information
kornilova203 authored Aug 24, 2021
1 parent 90d2a88 commit b5ed69e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<out T> internal constructor(protected var cursor: Value?, private var converter: MongoShellConverter?) : Iterator<T> {
private var currentIterationResult: List<T>? = 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<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<out Any?> {
var it = iterator
if (it == null) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ "acknowledged": true, "insertedId": "UNKNOWN" }
true
[ { "_id": <ObjectID>, "a": 1, "objectId": <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": <UUID> } ]
[ { "_id": <ObjectID>, "a": 1, "objectId": <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": <UUID> } ]

0 comments on commit b5ed69e

Please sign in to comment.