~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to db/dbeval.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* commands.cpp
 
2
   db "commands" (sent via db.$cmd.findOne(...))
 
3
 */
 
4
 
 
5
/**
 
6
*
 
7
*    This program is free software: you can redistribute it and/or  modify
 
8
*    it under the terms of the GNU Affero General Public License, version 3,
 
9
*    as published by the Free Software Foundation.
 
10
*
 
11
*    This program is distributed in the hope that it will be useful,
 
12
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
*    GNU Affero General Public License for more details.
 
15
*
 
16
*    You should have received a copy of the GNU Affero General Public License
 
17
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include "stdafx.h"
 
21
#include "query.h"
 
22
#include "pdfile.h"
 
23
#include "jsobj.h"
 
24
#include "../util/builder.h"
 
25
#include <time.h>
 
26
#include "introspect.h"
 
27
#include "btree.h"
 
28
#include "../util/lruishmap.h"
 
29
#include "json.h"
 
30
#include "repl.h"
 
31
#include "commands.h"
 
32
#include "cmdline.h"
 
33
 
 
34
#include "../scripting/engine.h"
 
35
 
 
36
namespace mongo {
 
37
 
 
38
    const int edebug=0;
 
39
 
 
40
    bool dbEval(const char *ns, BSONObj& cmd, BSONObjBuilder& result, string& errmsg) {
 
41
        BSONElement e = cmd.firstElement();
 
42
        uassert( 10046 ,  "eval needs Code" , e.type() == Code || e.type() == CodeWScope || e.type() == String );
 
43
 
 
44
        const char *code = 0;
 
45
        switch ( e.type() ) {
 
46
        case String:
 
47
        case Code:
 
48
            code = e.valuestr();
 
49
            break;
 
50
        case CodeWScope:
 
51
            code = e.codeWScopeCode();
 
52
            break;
 
53
        default:
 
54
            assert(0);
 
55
        }
 
56
        assert( code );
 
57
 
 
58
        if ( ! globalScriptEngine ) {
 
59
            errmsg = "db side execution is disabled";
 
60
            return false;
 
61
        }
 
62
 
 
63
        auto_ptr<Scope> s = globalScriptEngine->getPooledScope( ns );
 
64
        ScriptingFunction f = s->createFunction(code);
 
65
        if ( f == 0 ) {
 
66
            errmsg = (string)"compile failed: " + s->getError();
 
67
            return false;
 
68
        }
 
69
        
 
70
        if ( e.type() == CodeWScope )
 
71
            s->init( e.codeWScopeScopeData() );
 
72
        s->localConnect( cc().database()->name.c_str() );
 
73
 
 
74
        BSONObj args;
 
75
        {
 
76
            BSONElement argsElement = cmd.findElement("args");
 
77
            if ( argsElement.type() == Array ) {
 
78
                args = argsElement.embeddedObject();
 
79
                if ( edebug ) {
 
80
                    out() << "args:" << args.toString() << endl;
 
81
                    out() << "code:\n" << code << endl;
 
82
                }
 
83
            }
 
84
        }
 
85
 
 
86
        int res;
 
87
        {
 
88
            Timer t;
 
89
            res = s->invoke(f,args, cmdLine.quota ? 10 * 60 * 1000 : 0 );
 
90
            int m = t.millis();
 
91
            if ( m > cmdLine.slowMS ) {
 
92
                out() << "dbeval slow, time: " << dec << m << "ms " << ns << endl;
 
93
                if ( m >= 1000 ) log() << code << endl;
 
94
                else OCCASIONALLY log() << code << endl;
 
95
            }
 
96
        }
 
97
        if ( res ) {
 
98
            result.append("errno", (double) res);
 
99
            errmsg = "invoke failed: ";
 
100
            errmsg += s->getError();
 
101
            return false;
 
102
        }
 
103
        
 
104
        s->append( result , "retval" , "return" );
 
105
 
 
106
        return true;
 
107
    }
 
108
 
 
109
    class CmdEval : public Command {
 
110
    public:
 
111
        virtual bool slaveOk() {
 
112
            return false;
 
113
        }
 
114
        CmdEval() : Command("$eval") { }
 
115
        bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
 
116
            return dbEval(ns, cmdObj, result, errmsg);
 
117
        }
 
118
    } cmdeval;
 
119
 
 
120
} // namespace mongo