#include "bson/stringdata.h"
namespace mongo {
struct JSFile{ const char* name; const StringData& source; };
namespace JSFiles{
const StringData _jscode_raw_utils =
"__quiet = false;\n"
"__magicNoPrint = { __magicNoPrint : 1111 }\n"
"\n"
"chatty = function(s){\n"
"if ( ! __quiet )\n"
"print( s );\n"
"}\n"
"\n"
"friendlyEqual = function( a , b ){\n"
"if ( a == b )\n"
"return true;\n"
"\n"
"a = tojson(a,false,true);\n"
"b = tojson(b,false,true);\n"
"\n"
"if ( a == b )\n"
"return true;\n"
"\n"
"var clean = function( s ){\n"
"s = s.replace( /NumberInt\\((\\-?\\d+)\\)/g , \"$1\" );\n"
"return s;\n"
"}\n"
"\n"
"a = clean(a);\n"
"b = clean(b);\n"
"\n"
"if ( a == b )\n"
"return true;\n"
"\n"
"return false;\n"
"}\n"
"\n"
"printStackTrace = function(){\n"
"try{\n"
"throw new Error(\"Printing Stack Trace\");\n"
"} catch (e) {\n"
"print(e.stack);\n"
"}\n"
"}\n"
"\n"
"/**\n"
"*
Set the shell verbosity. If verbose the shell will display more information about command results. >\n"
"*
Default is off.
\n"
"* @param {Bool} verbosity on / off\n"
"*/\n"
"setVerboseShell = function( value ) {\n"
"if( value == undefined ) value = true;\n"
"_verboseShell = value;\n"
"}\n"
"\n"
"doassert = function (msg) {\n"
"if (msg.indexOf(\"assert\") == 0)\n"
"print(msg);\n"
"else\n"
"print(\"assert: \" + msg);\n"
"printStackTrace();\n"
"throw msg;\n"
"}\n"
"\n"
"assert = function( b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( b )\n"
"return;\n"
"doassert( msg == undefined ? \"assert failed\" : \"assert failed : \" + msg );\n"
"}\n"
"\n"
"assert.automsg = function( b ) {\n"
"assert( eval( b ), b );\n"
"}\n"
"\n"
"assert._debug = false;\n"
"\n"
"assert.eq = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( a == b )\n"
"return;\n"
"\n"
"if ( ( a != null && b != null ) && friendlyEqual( a , b ) )\n"
"return;\n"
"\n"
"doassert( \"[\" + tojson( a ) + \"] != [\" + tojson( b ) + \"] are not equal : \" + msg );\n"
"}\n"
"\n"
"assert.eq.automsg = function( a, b ) {\n"
"assert.eq( eval( a ), eval( b ), \"[\" + a + \"] != [\" + b + \"]\" );\n"
"}\n"
"\n"
"assert.neq = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"if ( a != b )\n"
"return;\n"
"\n"
"doassert( \"[\" + a + \"] != [\" + b + \"] are equal : \" + msg );\n"
"}\n"
"\n"
"assert.contains = function( o, arr, msg ){\n"
"var wasIn = false\n"
"\n"
"if( ! arr.length ){\n"
"for( i in arr ){\n"
"wasIn = arr[i] == o || ( ( arr[i] != null && o != null ) && friendlyEqual( arr[i] , o ) )\n"
"return;\n"
"if( wasIn ) break\n"
"}\n"
"}\n"
"else {\n"
"for( var i = 0; i < arr.length; i++ ){\n"
"wasIn = arr[i] == o || ( ( arr[i] != null && o != null ) && friendlyEqual( arr[i] , o ) )\n"
"if( wasIn ) break\n"
"}\n"
"}\n"
"\n"
"if( ! wasIn ) doassert( tojson( o ) + \" was not in \" + tojson( arr ) + \" : \" + msg )\n"
"}\n"
"\n"
"assert.repeat = function( f, msg, timeout, interval ) {\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"var start = new Date();\n"
"timeout = timeout || 30000;\n"
"interval = interval || 200;\n"
"var last;\n"
"while( 1 ) {\n"
"\n"
"if ( typeof( f ) == \"string\" ){\n"
"if ( eval( f ) )\n"
"return;\n"
"}\n"
"else {\n"
"if ( f() )\n"
"return;\n"
"}\n"
"\n"
"if ( ( new Date() ).getTime() - start.getTime() > timeout )\n"
"break;\n"
"sleep( interval );\n"
"}\n"
"}\n"
"\n"
"assert.soon = function( f, msg, timeout /*ms*/, interval ) {\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"var start = new Date();\n"
"timeout = timeout || 30000;\n"
"interval = interval || 200;\n"
"var last;\n"
"while( 1 ) {\n"
"\n"
"if ( typeof( f ) == \"string\" ){\n"
"if ( eval( f ) )\n"
"return;\n"
"}\n"
"else {\n"
"if ( f() )\n"
"return;\n"
"}\n"
"\n"
"if ( ( new Date() ).getTime() - start.getTime() > timeout )\n"
"doassert( \"assert.soon failed: \" + f + \", msg:\" + msg );\n"
"sleep( interval );\n"
"}\n"
"}\n"
"\n"
"assert.throws = function( func , params , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( params && typeof( params ) == \"string\" )\n"
"throw \"2nd argument to assert.throws has to be an array\"\n"
"\n"
"try {\n"
"func.apply( null , params );\n"
"}\n"
"catch ( e ){\n"
"return e;\n"
"}\n"
"\n"
"doassert( \"did not throw exception: \" + msg );\n"
"}\n"
"\n"
"assert.throws.automsg = function( func, params ) {\n"
"assert.throws( func, params, func.toString() );\n"
"}\n"
"\n"
"assert.commandWorked = function( res , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( res.ok == 1 )\n"
"return;\n"
"\n"
"doassert( \"command failed: \" + tojson( res ) + \" : \" + msg );\n"
"}\n"
"\n"
"assert.commandFailed = function( res , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( res.ok == 0 )\n"
"return;\n"
"\n"
"doassert( \"command worked when it should have failed: \" + tojson( res ) + \" : \" + msg );\n"
"}\n"
"\n"
"assert.isnull = function( what , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( what == null )\n"
"return;\n"
"\n"
"doassert( \"supposed to null (\" + ( msg || \"\" ) + \") was: \" + tojson( what ) );\n"
"}\n"
"\n"
"assert.lt = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( a < b )\n"
"return;\n"
"doassert( a + \" is not less than \" + b + \" : \" + msg );\n"
"}\n"
"\n"
"assert.gt = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( a > b )\n"
"return;\n"
"doassert( a + \" is not greater than \" + b + \" : \" + msg );\n"
"}\n"
"\n"
"assert.lte = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( a <= b )\n"
"return;\n"
"doassert( a + \" is not less than or eq \" + b + \" : \" + msg );\n"
"}\n"
"\n"
"assert.gte = function( a , b , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if ( a >= b )\n"
"return;\n"
"doassert( a + \" is not greater than or eq \" + b + \" : \" + msg );\n"
"}\n"
"\n"
"assert.between = function( a, b, c, msg, inclusive ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
"\n"
"if( ( inclusive == undefined || inclusive == true ) &&\n"
"a <= b && b <= c ) return;\n"
"else if( a < b && b < c ) return;\n"
"\n"
"doassert( b + \" is not between \" + a + \" and \" + c + \" : \" + msg );\n"
"}\n"
"\n"
"assert.betweenIn = function( a, b, c, msg ){ assert.between( a, b, c, msg, true ) }\n"
"assert.betweenEx = function( a, b, c, msg ){ assert.between( a, b, c, msg, false ) }\n"
"\n"
"assert.close = function( a , b , msg , places ){\n"
"if (places === undefined) {\n"
"places = 4;\n"
"}\n"
"if (Math.round((a - b) * Math.pow(10, places)) === 0) {\n"
"return;\n"
"}\n"
"doassert( a + \" is not equal to \" + b + \" within \" + places +\n"
"\" places, diff: \" + (a-b) + \" : \" + msg );\n"
"};\n"
"\n"
"Object.extend = function( dst , src , deep ){\n"
"for ( var k in src ){\n"
"var v = src[k];\n"
"if ( deep && typeof(v) == \"object\" ){\n"
"if ( \"floatApprox\" in v ) { // convert NumberLong properly\n"
"eval( \"v = \" + tojson( v ) );\n"
"} else {\n"
"v = Object.extend( typeof ( v.length ) == \"number\" ? [] : {} , v , true );\n"
"}\n"
"}\n"
"dst[k] = v;\n"
"}\n"
"return dst;\n"
"}\n"
"\n"
"Object.merge = function( dst, src, deep ){\n"
"var clone = Object.extend( {}, dst, deep )\n"
"return Object.extend( clone, src, deep )\n"
"}\n"
"\n"
"argumentsToArray = function( a ){\n"
"var arr = [];\n"
"for ( var i=0; i 0 ? '-' : '+'; // This is correct\n"
"ofs += (ofsmin/60).zeroPad(2)\n"
"ofs += (ofsmin%60).zeroPad(2)\n"
"}\n"
"}\n"
"\n"
"return 'ISODate(\"'+year+'-'+month+'-'+date+'T'+hour+':'+minute+':'+sec+ofs+'\")';\n"
"}\n"
"\n"
"Date.printAsUTC = true;\n"
"\n"
"\n"
"ISODate = function(isoDateStr){\n"
"if (!isoDateStr)\n"
"return new Date();\n"
"\n"
"var isoDateRegex = /(\\d{4})-?(\\d{2})-?(\\d{2})([T ](\\d{2})(:?(\\d{2})(:?(\\d{2}(\\.\\d+)?))?)?(Z|([+-])(\\d{2}):?(\\d{2})?)?)?/;\n"
"var res = isoDateRegex.exec(isoDateStr);\n"
"\n"
"if (!res)\n"
"throw \"invalid ISO date\";\n"
"\n"
"var year = parseInt(res[1],10) || 1970; // this should always be present\n"
"var month = (parseInt(res[2],10) || 1) - 1;\n"
"var date = parseInt(res[3],10) || 0;\n"
"var hour = parseInt(res[5],10) || 0;\n"
"var min = parseInt(res[7],10) || 0;\n"
"var sec = parseFloat(res[9]) || 0;\n"
"var ms = Math.round((sec%1) * 1000)\n"
"sec -= ms/1000\n"
"\n"
"var time = Date.UTC(year, month, date, hour, min, sec, ms);\n"
"\n"
"if (res[11] && res[11] != 'Z'){\n"
"var ofs = 0;\n"
"ofs += (parseInt(res[13],10) || 0) * 60*60*1000; // hours\n"
"ofs += (parseInt(res[14],10) || 0) * 60*1000; // mins\n"
"if (res[12] == '+') // if ahead subtract\n"
"ofs *= -1;\n"
"\n"
"time += ofs\n"
"}\n"
"\n"
"return new Date(time);\n"
"}\n"
"\n"
"RegExp.prototype.tojson = RegExp.prototype.toString;\n"
"\n"
"Array.contains = function( a , x ){\n"
"for ( var i=0; i>>>>>>>>>>>>>> skipping \" + x.name);\n"
"return;\n"
"}\n"
"\n"
"params[ i % n ].push( x.name );\n"
"++i;\n"
"}\n"
");\n"
"\n"
"// randomize ordering of the serialTests\n"
"params[ 0 ] = Array.shuffle( params[ 0 ] );\n"
"\n"
"for( var i in params ) {\n"
"params[ i ].unshift( i );\n"
"}\n"
"\n"
"return params;\n"
"}\n"
"\n"
"// runs a set of test files\n"
"// first argument is an identifier for this tester, remaining arguments are file names\n"
"ParallelTester.fileTester = function() {\n"
"var args = argumentsToArray( arguments );\n"
"var suite = args.shift();\n"
"args.forEach(\n"
"function( x ) {\n"
"print(\" S\" + suite + \" Test : \" + x + \" ...\");\n"
"var time = Date.timeFunc( function() { load(x); }, 1);\n"
"print(\" S\" + suite + \" Test : \" + x + \" \" + time + \"ms\" );\n"
"}\n"
");\n"
"}\n"
"\n"
"// params: array of arrays, each element of which consists of a function followed\n"
"// by zero or more arguments to that function. Each function and its arguments will\n"
"// be called in a separate thread.\n"
"// msg: failure message\n"
"// newScopes: if true, each thread starts in a fresh scope\n"
"assert.parallelTests = function( params, msg, newScopes ) {\n"
"newScopes = newScopes || false;\n"
"var wrapper = function( fun, argv ) {\n"
"eval (\n"
"\"var z = function() {\" +\n"
"\"var __parallelTests__fun = \" + fun.toString() + \";\" +\n"
"\"var __parallelTests__argv = \" + tojson( argv ) + \";\" +\n"
"\"var __parallelTests__passed = false;\" +\n"
"\"try {\" +\n"
"\"__parallelTests__fun.apply( 0, __parallelTests__argv );\" +\n"
"\"__parallelTests__passed = true;\" +\n"
"\"} catch ( e ) {\" +\n"
"\"print( '********** Parallel Test FAILED: ' + tojson(e) );\" +\n"
"\"}\" +\n"
"\"return __parallelTests__passed;\" +\n"
"\"}\"\n"
");\n"
"return z;\n"
"}\n"
"var runners = new Array();\n"
"for( var i in params ) {\n"
"var param = params[ i ];\n"
"var test = param.shift();\n"
"var t;\n"
"if ( newScopes )\n"
"t = new ScopedThread( wrapper( test, param ) );\n"
"else\n"
"t = new Thread( wrapper( test, param ) );\n"
"runners.push( t );\n"
"}\n"
"\n"
"runners.forEach( function( x ) { x.start(); } );\n"
"var nFailed = 0;\n"
"// v8 doesn't like it if we exit before all threads are joined (SERVER-529)\n"
"runners.forEach( function( x ) { if( !x.returnData() ) { ++nFailed; } } );\n"
"assert.eq( 0, nFailed, msg );\n"
"}\n"
"}\n"
"\n"
"tojsononeline = function( x ){\n"
"return tojson( x , \" \" , true );\n"
"}\n"
"\n"
"tojson = function( x, indent , nolint ){\n"
"if ( x === null )\n"
"return \"null\";\n"
"\n"
"if ( x === undefined )\n"
"return \"undefined\";\n"
"\n"
"if (!indent)\n"
"indent = \"\";\n"
"\n"
"switch ( typeof x ) {\n"
"case \"string\": {\n"
"var s = \"\\\"\";\n"
"for ( var i=0; i 1) {\n"
"print(n + \"\\t\" + size[n] / 1024 / 1024 / 1024 + \"GB\");\n"
"} else {\n"
"print(n + \"\\t(empty)\");\n"
"}\n"
"});\n"
"//db.getMongo().getDBNames().sort().forEach(function (x) { print(x) });\n"
"return \"\";\n"
"}\n"
"\n"
"if (what == \"log\" ) {\n"
"var n = \"global\";\n"
"if ( args.length > 0 )\n"
"n = args[0]\n"
"\n"
"var res = db.adminCommand( { getLog : n } )\n"
"for ( var i=0; i= 1 || cross_prod <= -1){\n"
"// fun with floats\n"
"assert( Math.abs(cross_prod)-1 < 1e-6 );\n"
"return cross_prod > 0 ? 0 : Math.PI;\n"
"}\n"
"\n"
"return Math.acos(cross_prod);\n"
"}\n"
"\n"
"rs = function () { return \"try rs.help()\"; }\n"
"\n"
"rs.help = function () {\n"
"print(\"\\trs.status() { replSetGetStatus : 1 } checks repl set status\");\n"
"print(\"\\trs.initiate() { replSetInitiate : null } initiates set with default settings\");\n"
"print(\"\\trs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg\");\n"
"print(\"\\trs.conf() get the current configuration object from local.system.replset\");\n"
"print(\"\\trs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)\");\n"
"print(\"\\trs.add(hostportstr) add a new member to the set with default attributes (disconnects)\");\n"
"print(\"\\trs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)\");\n"
"print(\"\\trs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)\");\n"
"print(\"\\trs.stepDown([secs]) step down as primary (momentarily) (disconnects)\");\n"
"print(\"\\trs.freeze(secs) make a node ineligible to become primary for the time specified\");\n"
"print(\"\\trs.remove(hostportstr) remove a host from the replica set (disconnects)\");\n"
"print(\"\\trs.slaveOk() shorthand for db.getMongo().setSlaveOk()\");\n"
"print();\n"
"print(\"\\tdb.isMaster() check who is primary\");\n"
"print();\n"
"print(\"\\treconfiguration helpers disconnect from the database so the shell will display\");\n"
"print(\"\\tan error, even if the command succeeds.\");\n"
"print(\"\\tsee also http://:28017/_replSet for additional diagnostic info\");\n"
"}\n"
"rs.slaveOk = function (value) { return db.getMongo().setSlaveOk(value); }\n"
"rs.status = function () { return db._adminCommand(\"replSetGetStatus\"); }\n"
"rs.isMaster = function () { return db.isMaster(); }\n"
"rs.initiate = function (c) { return db._adminCommand({ replSetInitiate: c }); }\n"
"rs._runCmd = function (c) {\n"
"// after the command, catch the disconnect and reconnect if necessary\n"
"var res = null;\n"
"try {\n"
"res = db.adminCommand(c);\n"
"}\n"
"catch (e) {\n"
"if ((\"\" + e).indexOf(\"error doing query\") >= 0) {\n"
"// closed connection. reconnect.\n"
"db.getLastErrorObj();\n"
"var o = db.getLastErrorObj();\n"
"if (o.ok) {\n"
"print(\"reconnected to server after rs command (which is normal)\");\n"
"}\n"
"else {\n"
"printjson(o);\n"
"}\n"
"}\n"
"else {\n"
"print(\"shell got exception during repl set operation: \" + e);\n"
"print(\"in some circumstances, the primary steps down and closes connections on a reconfig\");\n"
"}\n"
"return \"\";\n"
"}\n"
"return res;\n"
"}\n"
"rs.reconfig = function (cfg, options) {\n"
"cfg.version = rs.conf().version + 1;\n"
"cmd = { replSetReconfig: cfg };\n"
"for (var i in options) {\n"
"cmd[i] = options[i];\n"
"}\n"
"return this._runCmd(cmd);\n"
"}\n"
"rs.add = function (hostport, arb) {\n"
"var cfg = hostport;\n"
"\n"
"var local = db.getSisterDB(\"local\");\n"
"assert(local.system.replset.count() <= 1, \"error: local.system.replset has unexpected contents\");\n"
"var c = local.system.replset.findOne();\n"
"assert(c, \"no config object retrievable from local.system.replset\");\n"
"\n"
"c.version++;\n"
"\n"
"var max = 0;\n"
"for (var i in c.members)\n"
"if (c.members[i]._id > max) max = c.members[i]._id;\n"
"if (isString(hostport)) {\n"
"cfg = { _id: max + 1, host: hostport };\n"
"if (arb)\n"
"cfg.arbiterOnly = true;\n"
"}\n"
"c.members.push(cfg);\n"
"return this._runCmd({ replSetReconfig: c });\n"
"}\n"
"rs.stepDown = function (secs) { return db._adminCommand({ replSetStepDown:(secs === undefined) ? 60:secs}); }\n"
"rs.freeze = function (secs) { return db._adminCommand({replSetFreeze:secs}); }\n"
"rs.addArb = function (hn) { return this.add(hn, true); }\n"
"rs.conf = function () { return db.getSisterDB(\"local\").system.replset.findOne(); }\n"
"rs.config = function () { return rs.conf(); }\n"
"\n"
"rs.remove = function (hn) {\n"
"var local = db.getSisterDB(\"local\");\n"
"assert(local.system.replset.count() <= 1, \"error: local.system.replset has unexpected contents\");\n"
"var c = local.system.replset.findOne();\n"
"assert(c, \"no config object retrievable from local.system.replset\");\n"
"c.version++;\n"
"\n"
"for (var i in c.members) {\n"
"if (c.members[i].host == hn) {\n"
"c.members.splice(i, 1);\n"
"return db._adminCommand({ replSetReconfig : c});\n"
"}\n"
"}\n"
"\n"
"return \"error: couldn't find \"+hn+\" in \"+tojson(c.members);\n"
"};\n"
"\n"
"rs.debug = {};\n"
"\n"
"rs.debug.nullLastOpWritten = function(primary, secondary) {\n"
"var p = connect(primary+\"/local\");\n"
"var s = connect(secondary+\"/local\");\n"
"s.getMongo().setSlaveOk();\n"
"\n"
"var secondToLast = s.oplog.rs.find().sort({$natural : -1}).limit(1).next();\n"
"var last = p.runCommand({findAndModify : \"oplog.rs\",\n"
"query : {ts : {$gt : secondToLast.ts}},\n"
"sort : {$natural : 1},\n"
"update : {$set : {op : \"n\"}}});\n"
"\n"
"if (!last.value.o || !last.value.o._id) {\n"
"print(\"couldn't find an _id?\");\n"
"}\n"
"else {\n"
"last.value.o = {_id : last.value.o._id};\n"
"}\n"
"\n"
"print(\"nulling out this op:\");\n"
"printjson(last);\n"
"};\n"
"\n"
"rs.debug.getLastOpWritten = function(server) {\n"
"var s = db.getSisterDB(\"local\");\n"
"if (server) {\n"
"s = connect(server+\"/local\");\n"
"}\n"
"s.getMongo().setSlaveOk();\n"
"\n"
"return s.oplog.rs.find().sort({$natural : -1}).limit(1).next();\n"
"};\n"
"\n"
"\n"
"help = shellHelper.help = function (x) {\n"
"if (x == \"mr\") {\n"
"print(\"\\nSee also http://www.mongodb.org/display/DOCS/MapReduce\");\n"
"print(\"\\nfunction mapf() {\");\n"
"print(\" // 'this' holds current document to inspect\");\n"
"print(\" emit(key, value);\");\n"
"print(\"}\");\n"
"print(\"\\nfunction reducef(key,value_array) {\");\n"
"print(\" return reduced_value;\");\n"
"print(\"}\");\n"
"print(\"\\ndb.mycollection.mapReduce(mapf, reducef[, options])\");\n"
"print(\"\\noptions\");\n"
"print(\"{[query : ]\");\n"
"print(\" [, sort : ]\");\n"
"print(\" [, limit : ]\");\n"
"print(\" [, out : ]\");\n"
"print(\" [, keeptemp: ]\");\n"
"print(\" [, finalize : ]\");\n"
"print(\" [, scope :