00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include "../pch.h"
00021
00022 #include "jsobj.h"
00023 #include "../util/timer.h"
00024
00025 namespace mongo {
00026
00027 class BSONObj;
00028 class BSONObjBuilder;
00029 class BufBuilder;
00030 class Client;
00031
00035 class Command {
00036 public:
00037
00038 enum LockType { READ = -1 , NONE = 0 , WRITE = 1 };
00039
00040 const string name;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 virtual bool run(const string& db, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) = 0;
00051
00052
00053
00054
00055
00056
00057 virtual LockType locktype() const = 0;
00058
00059
00060 virtual bool adminOnly() const {
00061 return false;
00062 }
00063
00064 void htmlHelp(stringstream&) const;
00065
00066
00067
00068
00069
00070
00071 virtual bool localHostOnlyIfNoAuth(const BSONObj& cmdObj) { return false; }
00072
00073
00074
00075
00076 virtual bool slaveOk() const = 0;
00077
00078
00079
00080
00081 virtual bool slaveOverrideOk() {
00082 return false;
00083 }
00084
00085
00086
00087
00088
00089
00090 virtual bool logTheOp() { return false; }
00091
00092 virtual void help( stringstream& help ) const;
00093
00094
00095
00096
00097 virtual bool requiresAuth() { return true; }
00098
00102 Command(const char *_name, bool webUI = false, const char *oldName = 0);
00103
00104 virtual ~Command() {}
00105
00106 protected:
00107 BSONObj getQuery( const BSONObj& cmdObj ) {
00108 if ( cmdObj["query"].type() == Object )
00109 return cmdObj["query"].embeddedObject();
00110 if ( cmdObj["q"].type() == Object )
00111 return cmdObj["q"].embeddedObject();
00112 return BSONObj();
00113 }
00114
00115 static void logIfSlow( const Timer& cmdTimer, const string& msg);
00116
00117 static map<string,Command*> * _commands;
00118 static map<string,Command*> * _commandsByBestName;
00119 static map<string,Command*> * _webCommands;
00120
00121 public:
00122 static const map<string,Command*>* commandsByBestName() { return _commandsByBestName; }
00123 static const map<string,Command*>* webCommands() { return _webCommands; }
00125 static bool runAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder);
00126 static LockType locktype( const string& name );
00127 static Command * findCommand( const string& name );
00128 };
00129
00130 bool _runCommands(const char *ns, BSONObj& jsobj, BufBuilder &b, BSONObjBuilder& anObjBuilder, bool fromRepl, int queryOptions);
00131
00132
00133 }