00001
00002
00003
00020 #pragma once
00021
00022
00023 #include "../client/dbclient.h"
00024 #include "curop-inl.h"
00025 #include "security.h"
00026 #include "cmdline.h"
00027 #include "client.h"
00028
00029 namespace mongo {
00030
00031 extern string dbExecCommand;
00032
00033 struct DiagLog {
00034 ofstream *f;
00035
00036
00037
00038 int level;
00039 mongo::mutex mutex;
00040
00041 DiagLog() : f(0) , level(0), mutex("DiagLog") { }
00042 void init() {
00043 if ( ! f && level ) {
00044 log() << "diagLogging = " << level << endl;
00045 stringstream ss;
00046 ss << dbpath << "/diaglog." << hex << time(0);
00047 string name = ss.str();
00048 f = new ofstream(name.c_str(), ios::out | ios::binary);
00049 if ( ! f->good() ) {
00050 problem() << "couldn't open log stream" << endl;
00051 throw 1717;
00052 }
00053 }
00054 }
00058 int setLevel( int newLevel ) {
00059 int old = level;
00060 level = newLevel;
00061 init();
00062 return old;
00063 }
00064 void flush() {
00065 if ( level ) {
00066 scoped_lock lk(mutex);
00067 f->flush();
00068 }
00069 }
00070 void write(char *data,int len) {
00071 if ( level & 1 ) {
00072 scoped_lock lk(mutex);
00073 f->write(data,len);
00074 }
00075 }
00076 void readop(char *data, int len) {
00077 if ( level & 2 ) {
00078 bool log = (level & 4) == 0;
00079 OCCASIONALLY log = true;
00080 if ( log ) {
00081 scoped_lock lk(mutex);
00082 assert( f );
00083 f->write(data,len);
00084 }
00085 }
00086 }
00087 };
00088
00089 extern DiagLog _diaglog;
00090
00091
00092
00093
00094 struct DbResponse {
00095 Message *response;
00096 MSGID responseTo;
00097 const char *exhaust;
00098 DbResponse(Message *r, MSGID rt) : response(r), responseTo(rt), exhaust(0) { }
00099 DbResponse() {
00100 response = 0;
00101 exhaust = 0;
00102 }
00103 ~DbResponse() { delete response; }
00104 };
00105
00106 void assembleResponse( Message &m, DbResponse &dbresponse, const SockAddr &client = unknownAddress );
00107
00108 void getDatabaseNames( vector< string > &names , const string& usePath = dbpath );
00109
00110
00111
00112
00113 bool replHasDatabases();
00114
00118 class DBDirectClient : public DBClientBase {
00119 public:
00120 virtual auto_ptr<DBClientCursor> query(const string &ns, Query query, int nToReturn = 0, int nToSkip = 0,
00121 const BSONObj *fieldsToReturn = 0, int queryOptions = 0);
00122
00123 virtual bool isFailed() const {
00124 return false;
00125 }
00126 virtual string toString() {
00127 return "DBDirectClient";
00128 }
00129 virtual string getServerAddress() const {
00130 return "localhost";
00131 }
00132 virtual bool call( Message &toSend, Message &response, bool assertOk=true , string * actualServer = 0 );
00133 virtual void say( Message &toSend );
00134 virtual void sayPiggyBack( Message &toSend ) {
00135
00136 return say( toSend );
00137 }
00138
00139 virtual void killCursor( long long cursorID );
00140
00141 virtual bool callRead( Message& toSend , Message& response ) {
00142 return call( toSend , response );
00143 }
00144
00145 virtual unsigned long long count(const string &ns, const BSONObj& query = BSONObj(), int options=0, int limit=0, int skip=0 );
00146
00147 virtual ConnectionString::ConnectionType type() const { return ConnectionString::MASTER; }
00148 };
00149
00150 extern int lockFile;
00151 #ifdef WIN32
00152 extern HANDLE lockFileHandle;
00153 #endif
00154 void acquirePathLock();
00155 void maybeCreatePidFile();
00156
00157 }