summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorablack12 <annie.black@10gen.com>2018-10-01 11:50:04 -0400
committerablack12 <annie.black@10gen.com>2018-10-02 16:18:22 -0400
commit2d379ce39872fdfc04e6775ed8adea7ccdd1d1c1 (patch)
treea0c75ef1ba3a89197af55e125dc52975cc5b2220 /src/mongo
parent17648f3539352e4395910600dd87ccb048bb2e36 (diff)
downloadmongo-2d379ce39872fdfc04e6775ed8adea7ccdd1d1c1.tar.gz
SERVER-30773 optimize bsonWoCompare/bsonBinaryEqual
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/scripting/mozjs/bson.cpp55
1 files changed, 24 insertions, 31 deletions
diff --git a/src/mongo/scripting/mozjs/bson.cpp b/src/mongo/scripting/mozjs/bson.cpp
index de47d0a56aa..623dfeab2ef 100644
--- a/src/mongo/scripting/mozjs/bson.cpp
+++ b/src/mongo/scripting/mozjs/bson.cpp
@@ -25,7 +25,6 @@
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
-
#include "mongo/platform/basic.h"
#include "mongo/scripting/mozjs/bson.h"
@@ -50,8 +49,19 @@ const JSFunctionSpec BSONInfo::freeFunctions[3] = {
MONGO_ATTACH_JS_FUNCTION(bsonWoCompare), MONGO_ATTACH_JS_FUNCTION(bsonBinaryEqual), JS_FS_END,
};
+
namespace {
+BSONObj getBSONFromArg(JSContext* cx, JS::HandleValue arg, bool isBSON) {
+ if (isBSON) {
+ return ValueWriter(cx, arg).toBSON();
+ }
+ JS::RootedObject rout(cx, JS_NewPlainObject(cx));
+ ObjectWrapper object(cx, rout);
+ object.setValue("a", arg);
+ return object.toBSON();
+}
+
/**
* Holder for bson objects which tracks state for the js wrapper
*
@@ -251,26 +261,18 @@ std::tuple<BSONObj*, bool> BSONInfo::originalBSON(JSContext* cx, JS::HandleObjec
return out;
}
+
void BSONInfo::Functions::bsonWoCompare::call(JSContext* cx, JS::CallArgs args) {
if (args.length() != 2)
uasserted(ErrorCodes::BadValue, "bsonWoCompare needs 2 arguments");
- if (!args.get(0).isObject())
- uasserted(ErrorCodes::BadValue, "first argument to bsonWoCompare must be an object");
-
- if (!args.get(1).isObject())
- uasserted(ErrorCodes::BadValue, "second argument to bsonWoCompare must be an object");
-
- JS::RootedObject rout1(cx, JS_NewPlainObject(cx));
- JS::RootedObject rout2(cx, JS_NewPlainObject(cx));
- ObjectWrapper object1(cx, rout1);
- ObjectWrapper object2(cx, rout2);
-
- object1.setValue("a", args.get(0));
- object2.setValue("a", args.get(1));
+ // If either argument is not proper BSON, then we wrap both objects.
+ auto scope = getScope(cx);
+ bool isBSON = scope->getProto<BSONInfo>().instanceOf(args.get(0)) &&
+ scope->getProto<BSONInfo>().instanceOf(args.get(1));
- BSONObj bsonObject1 = object1.toBSON();
- BSONObj bsonObject2 = object2.toBSON();
+ BSONObj bsonObject1 = getBSONFromArg(cx, args.get(0), isBSON);
+ BSONObj bsonObject2 = getBSONFromArg(cx, args.get(1), isBSON);
args.rval().setInt32(bsonObject1.woCompare(bsonObject2));
}
@@ -279,22 +281,13 @@ void BSONInfo::Functions::bsonBinaryEqual::call(JSContext* cx, JS::CallArgs args
if (args.length() != 2)
uasserted(ErrorCodes::BadValue, "bsonBinaryEqual needs 2 arguments");
- if (!args.get(0).isObject())
- uasserted(ErrorCodes::BadValue, "first argument to bsonBinaryEqual must be an object");
-
- if (!args.get(1).isObject())
- uasserted(ErrorCodes::BadValue, "second argument to bsonBinaryEqual must be an object");
-
- JS::RootedObject rout1(cx, JS_NewPlainObject(cx));
- JS::RootedObject rout2(cx, JS_NewPlainObject(cx));
- ObjectWrapper object1(cx, rout1);
- ObjectWrapper object2(cx, rout2);
-
- object1.setValue("a", args.get(0));
- object2.setValue("a", args.get(1));
+ // If either argument is not a proper BSON, then we wrap both objects.
+ auto scope = getScope(cx);
+ bool isBSON = scope->getProto<BSONInfo>().instanceOf(args.get(0)) &&
+ scope->getProto<BSONInfo>().instanceOf(args.get(1));
- BSONObj bsonObject1 = object1.toBSON();
- BSONObj bsonObject2 = object2.toBSON();
+ BSONObj bsonObject1 = getBSONFromArg(cx, args.get(0), isBSON);
+ BSONObj bsonObject2 = getBSONFromArg(cx, args.get(1), isBSON);
args.rval().setBoolean(bsonObject1.binaryEqual(bsonObject2));
}