예제 #1
0
파일: export.cpp 프로젝트: cwolfinger/mongo
    // Gets the string representation of a BSON object that can be correctly written to a CSV file
    string csvString (const BSONElement& object) {
        const char* binData; // Only used with BinData type

        switch (object.type()) {
        case MinKey:
            return "$MinKey";
        case MaxKey:
            return "$MaxKey";
        case NumberInt:
        case NumberDouble:
        case NumberLong:
        case Bool:
            return object.toString(false);
        case String:
        case Symbol:
            return csvEscape(object.toString(false), true);
        case Object:
            return csvEscape(object.jsonString(Strict, false));
        case Array:
            return csvEscape(object.jsonString(Strict, false));
        case BinData:
            int len;
            binData = object.binDataClean(len);
            return toHex(binData, len);
        case jstOID:
            return "ObjectID(" + object.OID().toString() + ")"; // OIDs are always 24 bytes
        case Date:
            return timeToISOString(object.Date() / 1000);
        case Timestamp:
            return csvEscape(object.jsonString(Strict, false));
        case RegEx:
            return csvEscape("/" + string(object.regex()) + "/" + string(object.regexFlags()));
        case Code:
            return csvEscape(object.toString(false));
        case CodeWScope:
            if (string(object.codeWScopeScopeData()) == "") {
                return csvEscape(object.toString(false));
            } else {
                return csvEscape(object.jsonString(Strict, false));
            }
        case EOO:
        case Undefined:
        case DBRef:
        case jstNULL:
            cerr << "Invalid BSON object type for CSV output: " << object.type() << endl;
            return "";
        }
        // Can never get here
        assert(false);
        return "";
    }
예제 #2
0
파일: jsobj.cpp 프로젝트: fes/mongo
 /* must be same type when called, unless both sides are #s 
 */
 int compareElementValues(const BSONElement& l, const BSONElement& r) {
     int f;
     double x;
     switch ( l.type() ) {
     case EOO:
     case Undefined:
     case jstNULL:
     case MaxKey:
     case MinKey:
         f = l.type() - r.type();
         if ( f<0 ) return -1;
         return f==0 ? 0 : 1;
     case Bool:
         return *l.value() - *r.value();
     case Timestamp:
     case Date:
         if ( l.date() < r.date() )
             return -1;
         return l.date() == r.date() ? 0 : 1;
     case NumberLong:
         if( r.type() == NumberLong ) {
             long long L = l._numberLong();
             long long R = r._numberLong();
             if( L < R ) return -1;
             if( L == R ) return 0;
             return 1;
         }
         // else fall through
     case NumberInt:
     case NumberDouble: {
         double left = l.number();
         double right = r.number();
         bool lNan = !( left <= numeric_limits< double >::max() &&
                      left >= -numeric_limits< double >::max() );
         bool rNan = !( right <= numeric_limits< double >::max() &&
                      right >= -numeric_limits< double >::max() );
         if ( lNan ) {
             if ( rNan ) {
                 return 0;
             } else {
                 return -1;
             }
         } else if ( rNan ) {
             return 1;
         }
         x = left - right;
         if ( x < 0 ) return -1;
         return x == 0 ? 0 : 1;
         }
     case jstOID:
         return memcmp(l.value(), r.value(), 12);
     case Code:
     case Symbol:
     case String:
         /* todo: utf version */
         return strcmp(l.valuestr(), r.valuestr());
     case Object:
     case Array:
         return l.embeddedObject().woCompare( r.embeddedObject() );
     case DBRef:
     case BinData: {
         int lsz = l.valuesize();
         int rsz = r.valuesize();
         if ( lsz - rsz != 0 ) return lsz - rsz;
         return memcmp(l.value(), r.value(), lsz);
     }
     case RegEx:
     {
         int c = strcmp(l.regex(), r.regex());
         if ( c )
             return c;
         return strcmp(l.regexFlags(), r.regexFlags());
     }
     case CodeWScope : {
         f = l.type() - r.type();
         if ( f )
             return f;
         f = strcmp( l.codeWScopeCode() , r.codeWScopeCode() );
         if ( f ) 
             return f;
         f = strcmp( l.codeWScopeScopeData() , r.codeWScopeScopeData() );
         if ( f ) 
             return f;
         return 0;
     }
     default:
         out() << "compareElementValues: bad type " << (int) l.type() << endl;
         assert(false);
     }
     return -1;
 }
예제 #3
0
파일: dbeval.cpp 프로젝트: catap/mongo
    bool dbEval(const char *ns, BSONObj& cmd, BSONObjBuilder& result, string& errmsg) {
        BSONElement e = cmd.firstElement();
        uassert( "eval needs Code" , e.type() == Code || e.type() == CodeWScope || e.type() == String );

        const char *code = 0;
        switch ( e.type() ) {
        case String:
        case Code:
            code = e.valuestr();
            break;
        case CodeWScope:
            code = e.codeWScopeCode();
            break;
        default:
            assert(0);
        }
        assert( code );

        if ( ! globalScriptEngine ) {
            errmsg = "db side execution is disabled";
            return false;
        }

        auto_ptr<Scope> s = globalScriptEngine->getPooledScope( ns );
        ScriptingFunction f = s->createFunction(code);
        if ( f == 0 ) {
            errmsg = (string)"compile failed: " + s->getError();
            return false;
        }
        
        if ( e.type() == CodeWScope )
            s->init( e.codeWScopeScopeData() );
        s->localConnect( database->name.c_str() );

        BSONObj args;
        {
            BSONElement argsElement = cmd.findElement("args");
            if ( argsElement.type() == Array ) {
                args = argsElement.embeddedObject();
                if ( edebug ) {
                    out() << "args:" << args.toString() << endl;
                    out() << "code:\n" << code << endl;
                }
            }
        }

        int res;
        {
            Timer t;
            res = s->invoke(f,args, 10 * 60 * 1000);
            int m = t.millis();
            if ( m > 100 ) {
                out() << "dbeval slow, time: " << dec << m << "ms " << ns << endl;
                if ( m >= 1000 ) log() << code << endl;
                else OCCASIONALLY log() << code << endl;
            }
        }
        if ( res ) {
            result.append("errno", (double) res);
            errmsg = "invoke failed: ";
            errmsg += s->getError();
            return false;
        }
        
        s->append( result , "retval" , "return" );

        return true;
    }
예제 #4
0
파일: group.cpp 프로젝트: Inderjeet26/mongo
        bool run(const string& dbname, BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl ) {

            if ( !globalScriptEngine ) {
                errmsg = "server-side JavaScript execution is disabled";
                return false;
            }
            
            /* db.$cmd.findOne( { group : <p> } ) */
            const BSONObj& p = jsobj.firstElement().embeddedObjectUserCheck();

            BSONObj q;
            if ( p["cond"].type() == Object )
                q = p["cond"].embeddedObject();
            else if ( p["condition"].type() == Object )
                q = p["condition"].embeddedObject();
            else
                q = getQuery( p );

            if ( p["ns"].type() != String ) {
                errmsg = "ns has to be set";
                return false;
            }

            string ns = dbname + "." + p["ns"].String();

            BSONObj key;
            string keyf;
            if ( p["key"].type() == Object ) {
                key = p["key"].embeddedObjectUserCheck();
                if ( ! p["$keyf"].eoo() ) {
                    errmsg = "can't have key and $keyf";
                    return false;
                }
            }
            else if ( p["$keyf"].type() ) {
                keyf = p["$keyf"]._asCode();
            }
            else {
                // no key specified, will use entire object as key
            }

            BSONElement reduce = p["$reduce"];
            if ( reduce.eoo() ) {
                errmsg = "$reduce has to be set";
                return false;
            }

            BSONElement initial = p["initial"];
            if ( initial.type() != Object ) {
                errmsg = "initial has to be an object";
                return false;
            }


            string finalize;
            if (p["finalize"].type())
                finalize = p["finalize"]._asCode();

            return group( dbname , ns , q ,
                          key , keyf , reduce._asCode() , reduce.type() != CodeWScope ? 0 : reduce.codeWScopeScopeData() ,
                          initial.embeddedObject() , finalize ,
                          errmsg , result );
        }