2009-01-27 04:19:15 +01:00
|
|
|
|
2009-06-08 16:54:55 +02:00
|
|
|
friendlyEqual = function( a , b ){
|
|
|
|
if ( a == b )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( tojson( a ) == tojson( b ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert = function( msg ){
|
|
|
|
print( "assert: " + msg );
|
|
|
|
throw msg;
|
|
|
|
}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
assert = function( b , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
if ( b )
|
|
|
|
return;
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "assert failed : " + msg );
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
2009-09-14 20:32:04 +02:00
|
|
|
assert._debug = false;
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
assert.eq = function( a , b , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
if ( a == b )
|
|
|
|
return;
|
|
|
|
|
2009-06-08 16:54:55 +02:00
|
|
|
if ( ( a != null && b != null ) && friendlyEqual( a , b ) )
|
2009-02-24 23:09:21 +01:00
|
|
|
return;
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "[" + tojson( a ) + "] != [" + tojson( b ) + "] are not equal : " + msg );
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
2009-02-20 19:46:43 +01:00
|
|
|
assert.neq = function( a , b , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
2009-02-20 19:46:43 +01:00
|
|
|
if ( a != b )
|
|
|
|
return;
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "[" + a + "] != [" + b + "] are equal : " + msg );
|
2009-02-20 19:46:43 +01:00
|
|
|
}
|
|
|
|
|
2009-05-15 22:24:26 +02:00
|
|
|
assert.soon = function( f, msg, timeout, interval ) {
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-01-29 17:46:29 +01:00
|
|
|
var start = new Date();
|
2009-05-15 22:24:26 +02:00
|
|
|
timeout = timeout || 30000;
|
|
|
|
interval = interval || 200;
|
2009-01-29 17:46:29 +01:00
|
|
|
var last;
|
|
|
|
while( 1 ) {
|
2009-08-24 16:21:34 +02:00
|
|
|
|
|
|
|
if ( typeof( f ) == "string" ){
|
|
|
|
if ( eval( f ) )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( f() )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-12 17:19:39 +02:00
|
|
|
if ( ( new Date() ).getTime() - start.getTime() > timeout )
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "assert.soon failed: " + f + ", msg:" + msg );
|
2009-05-15 22:24:26 +02:00
|
|
|
sleep( interval );
|
2009-01-29 17:46:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 19:54:08 +01:00
|
|
|
assert.throws = function( func , params , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
2009-02-18 19:54:08 +01:00
|
|
|
try {
|
|
|
|
func.apply( null , params );
|
|
|
|
}
|
|
|
|
catch ( e ){
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "did not throw exception: " + msg );
|
2009-02-18 19:54:08 +01:00
|
|
|
}
|
|
|
|
|
2009-03-18 15:50:49 +01:00
|
|
|
assert.commandWorked = function( res , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-03-18 15:50:49 +01:00
|
|
|
if ( res.ok == 1 )
|
|
|
|
return;
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "command failed: " + tojson( res ) + " : " + msg );
|
2009-03-18 15:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.commandFailed = function( res , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-03-18 15:50:49 +01:00
|
|
|
if ( res.ok == 0 )
|
|
|
|
return;
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "command worked when it should have failed: " + tojson( res ) + " : " + msg );
|
2009-03-18 15:50:49 +01:00
|
|
|
}
|
|
|
|
|
2009-03-30 03:24:16 +02:00
|
|
|
assert.isnull = function( what , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-03-30 03:24:16 +02:00
|
|
|
if ( what == null )
|
|
|
|
return;
|
|
|
|
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( "supposed to null (" + ( msg || "" ) + ") was: " + tojson( what ) );
|
2009-03-30 03:24:16 +02:00
|
|
|
}
|
|
|
|
|
2009-06-07 04:53:16 +02:00
|
|
|
assert.lt = function( a , b , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-06-07 04:53:16 +02:00
|
|
|
if ( a < b )
|
|
|
|
return;
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( a + " is not less than " + b + " : " + msg );
|
2009-06-07 04:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.gt = function( a , b , msg ){
|
2009-09-14 20:32:04 +02:00
|
|
|
if ( assert._debug && msg ) print( "in assert for: " + msg );
|
|
|
|
|
2009-06-07 04:53:16 +02:00
|
|
|
if ( a > b )
|
|
|
|
return;
|
2009-08-14 19:33:45 +02:00
|
|
|
doassert( a + " is not greater than " + b + " : " + msg );
|
2009-06-07 04:53:16 +02:00
|
|
|
}
|
|
|
|
|
2009-10-16 03:19:24 +02:00
|
|
|
Object.extend = function( dst , src , deep ){
|
2009-01-27 04:19:15 +01:00
|
|
|
for ( var k in src ){
|
2009-10-16 03:19:24 +02:00
|
|
|
var v = src[k];
|
|
|
|
if ( deep && typeof(v) == "object" ){
|
|
|
|
v = Object.extend( typeof ( v.length ) == "number" ? [] : {} , v , true );
|
|
|
|
}
|
|
|
|
dst[k] = v;
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
2009-05-08 22:57:51 +02:00
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
argumentsToArray = function( a ){
|
|
|
|
var arr = [];
|
|
|
|
for ( var i=0; i<a.length; i++ )
|
|
|
|
arr[i] = a[i];
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
isString = function( x ){
|
|
|
|
return typeof( x ) == "string";
|
|
|
|
}
|
|
|
|
|
2009-02-20 16:46:23 +01:00
|
|
|
isNumber = function(x){
|
|
|
|
return typeof( x ) == "number";
|
|
|
|
}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
isObject = function( x ){
|
|
|
|
return typeof( x ) == "object";
|
|
|
|
}
|
|
|
|
|
|
|
|
String.prototype.trim = function() {
|
|
|
|
return this.replace(/^\s+|\s+$/g,"");
|
|
|
|
}
|
|
|
|
String.prototype.ltrim = function() {
|
|
|
|
return this.replace(/^\s+/,"");
|
|
|
|
}
|
|
|
|
String.prototype.rtrim = function() {
|
|
|
|
return this.replace(/\s+$/,"");
|
|
|
|
}
|
|
|
|
|
2009-01-27 20:27:41 +01:00
|
|
|
Date.timeFunc = function( theFunc , numTimes ){
|
|
|
|
|
|
|
|
var start = new Date();
|
|
|
|
|
|
|
|
numTimes = numTimes || 1;
|
|
|
|
for ( var i=0; i<numTimes; i++ ){
|
|
|
|
theFunc.apply( null , argumentsToArray( arguments ).slice( 2 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new Date()).getTime() - start.getTime();
|
|
|
|
}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
Date.prototype.tojson = function(){
|
|
|
|
return "\"" + this.toString() + "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExp.prototype.tojson = RegExp.prototype.toString;
|
|
|
|
|
2009-08-24 05:33:17 +02:00
|
|
|
Array.contains = function( a , x ){
|
|
|
|
for ( var i=0; i<a.length; i++ ){
|
|
|
|
if ( a[i] == x )
|
2009-04-21 13:35:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-24 05:33:17 +02:00
|
|
|
Array.unique = function( a ){
|
2009-04-21 13:35:00 +02:00
|
|
|
var u = [];
|
2009-08-24 05:33:17 +02:00
|
|
|
for ( var i=0; i<a.length; i++){
|
|
|
|
var o = a[i];
|
|
|
|
if ( ! Array.contains( u , o ) ){
|
2009-04-21 13:35:00 +02:00
|
|
|
u.push( o );
|
2009-08-24 05:33:17 +02:00
|
|
|
}
|
2009-04-21 13:35:00 +02:00
|
|
|
}
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2009-09-03 16:03:25 +02:00
|
|
|
Array.shuffle = function( arr ){
|
|
|
|
for ( var i=0; i<arr.length-1; i++ ){
|
|
|
|
var pos = i+Math.floor(Math.random()*(arr.length-i));
|
|
|
|
var save = arr[i];
|
|
|
|
arr[i] = arr[pos];
|
|
|
|
arr[pos] = save;
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2009-08-24 05:33:17 +02:00
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
Array.tojson = function( a , indent ){
|
|
|
|
if (!indent)
|
|
|
|
indent = "";
|
|
|
|
|
|
|
|
var s = "[\n";
|
|
|
|
indent += "\t";
|
2009-08-24 05:33:17 +02:00
|
|
|
for ( var i=0; i<a.length; i++){
|
2009-10-12 18:53:01 +02:00
|
|
|
s += indent + tojson( a[i], indent );
|
|
|
|
if ( i < a.length - 1 ){
|
|
|
|
s += ",\n";
|
2009-03-26 21:16:53 +01:00
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
2009-10-12 18:53:01 +02:00
|
|
|
if ( a.length == 0 ) {
|
|
|
|
s += indent;
|
|
|
|
}
|
|
|
|
|
|
|
|
indent = indent.substring(1);
|
|
|
|
s += "\n"+indent+"]";
|
2009-01-27 04:19:15 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-10-05 22:02:15 +02:00
|
|
|
Array.fetchRefs = function( arr , coll ){
|
|
|
|
var n = [];
|
|
|
|
for ( var i=0; i<arr.length; i ++){
|
|
|
|
var z = arr[i];
|
|
|
|
if ( coll && coll != z.getCollection() )
|
|
|
|
continue;
|
|
|
|
n.push( z.fetch() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-04 04:40:55 +02:00
|
|
|
if ( ! ObjectId.prototype )
|
|
|
|
ObjectId.prototype = {}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
ObjectId.prototype.toString = function(){
|
|
|
|
return this.str;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId.prototype.tojson = function(){
|
2009-10-12 18:53:01 +02:00
|
|
|
return "ObjectId(\"" + this.str + "\")";
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId.prototype.isObjectId = true;
|
|
|
|
|
2009-10-02 22:38:20 +02:00
|
|
|
if ( typeof( DBPointer ) != "undefined" ){
|
|
|
|
DBPointer.prototype.fetch = function(){
|
2009-08-21 07:36:41 +02:00
|
|
|
assert( this.ns , "need a ns" );
|
|
|
|
assert( this.id , "need an id" );
|
|
|
|
|
|
|
|
return db[ this.ns ].findOne( { _id : this.id } );
|
|
|
|
}
|
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
DBPointer.prototype.tojson = function(indent){
|
|
|
|
return tojson({"ns" : this.ns, "id" : this.id}, indent);
|
2009-08-21 07:36:41 +02:00
|
|
|
}
|
2009-10-05 22:02:15 +02:00
|
|
|
|
|
|
|
DBPointer.prototype.getCollection = function(){
|
|
|
|
return this.ns;
|
|
|
|
}
|
2009-06-03 19:18:50 +02:00
|
|
|
|
2009-10-02 22:38:20 +02:00
|
|
|
DBPointer.prototype.toString = function(){
|
|
|
|
return "DBPointer " + this.ns + ":" + this.id;
|
2009-08-21 07:36:41 +02:00
|
|
|
}
|
2009-06-03 19:18:50 +02:00
|
|
|
}
|
2009-08-21 07:36:41 +02:00
|
|
|
else {
|
2009-10-02 22:38:20 +02:00
|
|
|
print( "warning: no DBPointer" );
|
2009-06-03 19:18:50 +02:00
|
|
|
}
|
|
|
|
|
2009-10-02 23:07:07 +02:00
|
|
|
if ( typeof( DBRef ) != "undefined" ){
|
|
|
|
DBRef.prototype.fetch = function(){
|
|
|
|
assert( this.$ref , "need a ns" );
|
|
|
|
assert( this.$id , "need an id" );
|
|
|
|
|
|
|
|
return db[ this.$ref ].findOne( { _id : this.$id } );
|
|
|
|
}
|
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
DBRef.prototype.tojson = function(indent){
|
|
|
|
return tojson({"$ref" : this.$ref, "$id" : this.$id}, indent);
|
2009-10-05 22:02:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBRef.prototype.getCollection = function(){
|
|
|
|
return this.$ref;
|
2009-10-02 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBRef.prototype.toString = function(){
|
|
|
|
return this.tojson();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print( "warning: no DBRef" );
|
|
|
|
}
|
|
|
|
|
2009-08-21 07:36:41 +02:00
|
|
|
if ( typeof( BinData ) != "undefined" ){
|
|
|
|
BinData.prototype.tojson = function(){
|
|
|
|
return "BinData type: " + this.type + " len: " + this.len;
|
|
|
|
}
|
2009-06-03 19:18:50 +02:00
|
|
|
}
|
2009-08-21 07:36:41 +02:00
|
|
|
else {
|
|
|
|
print( "warning: no BinData" );
|
2009-06-05 15:54:35 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 22:11:52 +02:00
|
|
|
tojson = function( x, indent , nolint ){
|
2009-07-01 17:39:51 +02:00
|
|
|
if ( x == null )
|
|
|
|
return "null";
|
|
|
|
|
|
|
|
if ( x == undefined )
|
2009-10-12 18:53:01 +02:00
|
|
|
return "undefined";
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
if (!indent)
|
|
|
|
indent = "";
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
switch ( typeof x ){
|
|
|
|
|
2009-07-01 17:39:51 +02:00
|
|
|
case "string": {
|
|
|
|
var s = "\"";
|
|
|
|
for ( var i=0; i<x.length; i++ ){
|
|
|
|
if ( x[i] == '"' ){
|
|
|
|
s += "\\\"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s += x[i];
|
|
|
|
}
|
|
|
|
return s + "\"";
|
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
|
|
|
|
case "number":
|
|
|
|
case "boolean":
|
|
|
|
return "" + x;
|
|
|
|
|
2009-10-15 17:25:41 +02:00
|
|
|
case "object":{
|
|
|
|
var s = tojsonObject( x, indent , nolint );
|
2009-10-15 17:38:39 +02:00
|
|
|
if ( ( nolint == null || nolint == true ) && s.length < 70 && ( indent == null || indent.length == 0 ) ){
|
2009-10-15 17:25:41 +02:00
|
|
|
s = s.replace( /[\s\r\n ]+/gm , " " );
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-05-14 23:37:21 +02:00
|
|
|
case "function":
|
|
|
|
return x.toString();
|
|
|
|
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
default:
|
2009-05-14 23:37:21 +02:00
|
|
|
throw "tojson can't handle type " + ( typeof x );
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-10-12 22:11:52 +02:00
|
|
|
tojsonObject = function( x, indent , nolint ){
|
|
|
|
var lineEnding = nolint ? " " : "\n";
|
2009-10-12 23:20:19 +02:00
|
|
|
var tabSpace = nolint ? "" : "\t";
|
2009-10-12 22:11:52 +02:00
|
|
|
|
2009-03-17 20:48:46 +01:00
|
|
|
assert.eq( ( typeof x ) , "object" , "tojsonObject needs object, not [" + ( typeof x ) + "]" );
|
2009-10-12 18:53:01 +02:00
|
|
|
|
|
|
|
if (!indent)
|
|
|
|
indent = "";
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
if ( typeof( x.tojson ) == "function" && x.tojson != tojson ) {
|
2009-10-12 22:11:52 +02:00
|
|
|
return x.tojson(indent,nolint);
|
2009-10-12 18:53:01 +02:00
|
|
|
}
|
2009-08-24 05:33:17 +02:00
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
if ( typeof( x.constructor.tojson ) == "function" && x.constructor.tojson != tojson ) {
|
2009-10-12 22:11:52 +02:00
|
|
|
return x.constructor.tojson( x, indent , nolint );
|
2009-10-12 18:53:01 +02:00
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-08-11 10:54:58 +02:00
|
|
|
if ( x.toString() == "[object MaxKey]" )
|
|
|
|
return "{ $maxKey : 1 }";
|
|
|
|
if ( x.toString() == "[object MinKey]" )
|
|
|
|
return "{ $minKey : 1 }";
|
|
|
|
|
2009-10-12 22:11:52 +02:00
|
|
|
var s = "{" + lineEnding;
|
2009-10-12 18:53:01 +02:00
|
|
|
|
|
|
|
// push one level of indent
|
2009-10-12 22:11:52 +02:00
|
|
|
indent += tabSpace;
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
var total = 0;
|
|
|
|
for ( var k in x ) total++;
|
|
|
|
if ( total == 0 ) {
|
2009-10-12 22:27:35 +02:00
|
|
|
s += indent + lineEnding;
|
2009-10-12 18:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var num = 1;
|
2009-01-27 04:19:15 +01:00
|
|
|
for ( var k in x ){
|
2009-05-14 23:37:21 +02:00
|
|
|
|
|
|
|
var val = x[k];
|
|
|
|
if ( val == DB.prototype || val == DBCollection.prototype )
|
|
|
|
continue;
|
|
|
|
|
2009-10-12 22:11:52 +02:00
|
|
|
s += indent + "\"" + k + "\" : " + tojson( val, indent , nolint );
|
2009-10-12 18:53:01 +02:00
|
|
|
if (num != total) {
|
|
|
|
s += ",";
|
|
|
|
num++;
|
|
|
|
}
|
2009-10-12 22:11:52 +02:00
|
|
|
s += lineEnding;
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
2009-10-12 18:53:01 +02:00
|
|
|
// pop one level of indent
|
|
|
|
indent = indent.substring(1);
|
|
|
|
return s + indent + "}";
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
shellPrint = function( x ){
|
2009-02-06 18:31:24 +01:00
|
|
|
it = x;
|
2009-01-27 04:19:15 +01:00
|
|
|
if ( x != undefined )
|
|
|
|
shellPrintHelper( x );
|
|
|
|
|
|
|
|
if ( db ){
|
|
|
|
var e = db.getPrevError();
|
|
|
|
if ( e.err ) {
|
|
|
|
if( e.nPrev <= 1 )
|
|
|
|
print( "error on last call: " + tojson( e.err ) );
|
|
|
|
else
|
|
|
|
print( "an error " + tojson(e.err) + " occurred " + e.nPrev + " operations back in the command invocation" );
|
|
|
|
}
|
|
|
|
db.resetError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 05:39:32 +01:00
|
|
|
printjson = function(x){
|
|
|
|
print( tojson( x ) );
|
|
|
|
}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
shellPrintHelper = function( x ){
|
2009-08-17 22:54:47 +02:00
|
|
|
|
|
|
|
if ( typeof( x ) == "undefined" ){
|
|
|
|
|
|
|
|
if ( typeof( db ) != "undefined" && db.getLastError ){
|
|
|
|
var e = db.getLastError();
|
|
|
|
if ( e != null )
|
|
|
|
print( e );
|
|
|
|
}
|
|
|
|
|
2009-05-21 16:09:38 +02:00
|
|
|
return;
|
2009-08-17 22:54:47 +02:00
|
|
|
}
|
2009-05-21 16:09:38 +02:00
|
|
|
|
|
|
|
if ( x == null ){
|
|
|
|
print( "null" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
if ( typeof x != "object" )
|
|
|
|
return print( x );
|
|
|
|
|
|
|
|
var p = x.shellPrint;
|
|
|
|
if ( typeof p == "function" )
|
|
|
|
return x.shellPrint();
|
|
|
|
|
|
|
|
var p = x.tojson;
|
|
|
|
if ( typeof p == "function" )
|
|
|
|
print( x.tojson() );
|
|
|
|
else
|
|
|
|
print( tojson( x ) );
|
|
|
|
}
|
|
|
|
|
2009-06-12 02:15:31 +02:00
|
|
|
shellHelper = function( command , rest , shouldPrint ){
|
2009-01-27 04:19:15 +01:00
|
|
|
command = command.trim();
|
|
|
|
var args = rest.trim().replace(/;$/,"").split( "\s+" );
|
|
|
|
|
|
|
|
if ( ! shellHelper[command] )
|
|
|
|
throw "no command [" + command + "]";
|
|
|
|
|
2009-06-12 02:15:31 +02:00
|
|
|
var res = shellHelper[command].apply( null , args );
|
|
|
|
if ( shouldPrint ){
|
|
|
|
shellPrintHelper( res );
|
|
|
|
}
|
|
|
|
return res;
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
help = shellHelper.help = function(){
|
|
|
|
print( "HELP" );
|
2009-02-04 20:46:17 +01:00
|
|
|
print( "\t" + "show dbs show database names");
|
|
|
|
print( "\t" + "show collections show collections in current database");
|
|
|
|
print( "\t" + "show users show users in current database");
|
|
|
|
print( "\t" + "show profile show most recent system.profile entries with time >= 1ms");
|
|
|
|
print( "\t" + "use <db name> set curent database to <db name>" );
|
2009-01-27 04:19:15 +01:00
|
|
|
print( "\t" + "db.help() help on DB methods");
|
|
|
|
print( "\t" + "db.foo.help() help on collection methods");
|
2009-02-04 20:46:17 +01:00
|
|
|
print( "\t" + "db.foo.find() list objects in collection foo" );
|
|
|
|
print( "\t" + "db.foo.find( { a : 1 } ) list objects in foo where a == 1" );
|
2009-02-06 19:29:57 +01:00
|
|
|
print( "\t" + "it result of the last line evaluated; use to further iterate");
|
2009-01-27 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
shellHelper.use = function( dbname ){
|
|
|
|
db = db.getMongo().getDB( dbname );
|
|
|
|
print( "switched to db " + db.getName() );
|
|
|
|
}
|
|
|
|
|
2009-06-08 17:20:30 +02:00
|
|
|
shellHelper.it = function(){
|
|
|
|
if ( typeof( ___it___ ) == "undefined" || ___it___ == null ){
|
|
|
|
print( "no cursor" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shellPrintHelper( ___it___ );
|
|
|
|
}
|
|
|
|
|
2009-01-27 04:19:15 +01:00
|
|
|
shellHelper.show = function( what ){
|
|
|
|
assert( typeof what == "string" );
|
|
|
|
|
2009-02-04 20:46:17 +01:00
|
|
|
if( what == "profile" ) {
|
|
|
|
if( db.system.profile.count() == 0 ) {
|
|
|
|
print("db.system.profile is empty");
|
|
|
|
print("Use db.setProfilingLevel(2) will enable profiling");
|
|
|
|
print("Use db.system.profile.find() to show raw profile entries");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print();
|
|
|
|
db.system.profile.find({ millis : { $gt : 0 } }).sort({$natural:-1}).limit(5).forEach( function(x){print(""+x.millis+"ms " + String(x.ts).substring(0,24)); print(x.info); print("\n");} )
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:47:43 +02:00
|
|
|
if ( what == "users" ){
|
|
|
|
db.system.users.find().forEach( printjson );
|
|
|
|
return "";
|
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-02-03 20:42:53 +01:00
|
|
|
if ( what == "collections" || what == "tables" ) {
|
|
|
|
db.getCollectionNames().forEach( function(x){print(x)} );
|
|
|
|
return "";
|
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
|
2009-02-10 23:42:31 +01:00
|
|
|
if ( what == "dbs" ) {
|
|
|
|
db.getMongo().getDBNames().sort().forEach( function(x){print(x)} );
|
|
|
|
return "";
|
|
|
|
}
|
2009-01-27 04:19:15 +01:00
|
|
|
|
|
|
|
throw "don't know how to show [" + what + "]";
|
|
|
|
|
|
|
|
}
|
2009-05-06 21:30:05 +02:00
|
|
|
|
2009-06-08 16:54:55 +02:00
|
|
|
if ( typeof( Map ) == "undefined" ){
|
|
|
|
Map = function(){
|
2009-07-29 20:20:40 +02:00
|
|
|
this._data = {};
|
2009-06-08 16:54:55 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-06 21:30:05 +02:00
|
|
|
|
2009-07-29 20:20:40 +02:00
|
|
|
Map.hash = function( val ){
|
|
|
|
if ( ! val )
|
|
|
|
return val;
|
|
|
|
|
|
|
|
switch ( typeof( val ) ){
|
|
|
|
case 'string':
|
|
|
|
case 'number':
|
|
|
|
case 'date':
|
|
|
|
return val.toString();
|
|
|
|
case 'object':
|
|
|
|
case 'array':
|
|
|
|
var s = "";
|
|
|
|
for ( var k in val ){
|
|
|
|
s += k + val[k];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw "can't hash : " + typeof( val );
|
|
|
|
}
|
|
|
|
|
2009-06-08 16:54:55 +02:00
|
|
|
Map.prototype.put = function( key , value ){
|
|
|
|
var o = this._get( key );
|
|
|
|
var old = o.value;
|
|
|
|
o.value = value;
|
|
|
|
return old;
|
2009-05-06 21:30:05 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 16:54:55 +02:00
|
|
|
Map.prototype.get = function( key ){
|
|
|
|
return this._get( key ).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map.prototype._get = function( key ){
|
2009-07-29 20:20:40 +02:00
|
|
|
var h = Map.hash( key );
|
|
|
|
var a = this._data[h];
|
|
|
|
if ( ! a ){
|
|
|
|
a = [];
|
|
|
|
this._data[h] = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( var i=0; i<a.length; i++ ){
|
|
|
|
if ( friendlyEqual( key , a[i].key ) ){
|
|
|
|
return a[i];
|
2009-06-08 16:54:55 +02:00
|
|
|
}
|
2009-05-06 21:30:05 +02:00
|
|
|
}
|
2009-06-08 16:54:55 +02:00
|
|
|
var o = { key : key , value : null };
|
2009-07-29 20:20:40 +02:00
|
|
|
a.push( o );
|
2009-06-08 16:54:55 +02:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map.prototype.values = function(){
|
2009-07-29 20:20:40 +02:00
|
|
|
var all = [];
|
|
|
|
for ( var k in this._data ){
|
|
|
|
this._data[k].forEach( function(z){ all.push( z.value ); } );
|
|
|
|
}
|
|
|
|
return all;
|
2009-05-06 21:30:05 +02:00
|
|
|
}
|
2009-05-14 19:32:14 +02:00
|
|
|
|
2009-10-13 22:12:43 +02:00
|
|
|
if ( typeof( gc ) == "undefined" ){
|
|
|
|
gc = function(){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-14 19:32:14 +02:00
|
|
|
Math.sigFig = function( x , N ){
|
|
|
|
if ( ! N ){
|
|
|
|
N = 3;
|
|
|
|
}
|
|
|
|
var p = Math.pow( 10, N - Math.ceil( Math.log( Math.abs(x) ) / Math.log( 10 )) );
|
|
|
|
return Math.round(x*p)/p;
|
|
|
|
}
|
2009-08-11 10:54:58 +02:00
|
|
|
|