0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/instance.h

161 lines
5.1 KiB
C
Raw Normal View History

// instance.h : Global state functions.
//
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2008-12-29 02:28:49 +01:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2008-12-29 02:28:49 +01:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
2009-02-20 17:08:22 +01:00
#include "../client/dbclient.h"
2009-03-03 22:10:42 +01:00
#include "curop.h"
2009-01-23 16:11:11 +01:00
#include "security.h"
2009-08-25 16:24:44 +02:00
#include "cmdline.h"
#include "connection.h"
2009-01-23 16:11:11 +01:00
2009-01-14 23:09:51 +01:00
namespace mongo {
extern CurOp currentOp;
2009-03-03 22:10:42 +01:00
// turn on or off the oplog.* files which the db can generate.
2008-12-29 02:28:49 +01:00
// these files are for diagnostic purposes and are unrelated to
// local.oplog.$main used by replication.
2008-12-29 02:28:49 +01:00
//
#define OPLOG if( 0 )
int getOpLogging();
extern string dbExecCommand;
#define OPWRITE if( getOpLogging() & 1 ) _oplog.write((char *) m.data, m.data->len);
#define OPREAD if( getOpLogging() & 2 ) _oplog.readop((char *) m.data, m.data->len);
struct OpLog {
ofstream *f;
OpLog() : f(0) { }
void init() {
OPLOG {
stringstream ss;
ss << "oplog." << hex << time(0);
string name = ss.str();
f = new ofstream(name.c_str(), ios::out | ios::binary);
if ( ! f->good() ) {
problem() << "couldn't open log stream" << endl;
throw 1717;
}
2008-12-29 02:28:49 +01:00
}
}
void flush() {
OPLOG f->flush();
}
void write(char *data,int len) {
OPLOG f->write(data,len);
}
void readop(char *data, int len) {
OPLOG {
bool log = (getOpLogging() & 4) == 0;
OCCASIONALLY log = true;
if ( log )
f->write(data,len);
}
}
};
/* we defer response until we unlock. don't want a blocked socket to
keep things locked.
*/
struct DbResponse {
Message *response;
MSGID responseTo;
DbResponse(Message *r, MSGID rt) : response(r), responseTo(rt) {
}
DbResponse() {
response = 0;
}
~DbResponse() {
delete response;
}
};
static SockAddr unknownAddress( "0.0.0.0", 0 );
bool assembleResponse( Message &m, DbResponse &dbresponse, const sockaddr_in &client = unknownAddress.sa );
void receivedKillCursors(Message& m);
void receivedUpdate(Message& m, stringstream& ss);
2009-05-04 21:46:23 +02:00
void receivedDelete(Message& m, stringstream& ss);
void receivedInsert(Message& m, stringstream& ss);
void receivedGetMore(DbResponse& dbresponse, /*AbstractMessagingPort& dbMsgPort, */Message& m, stringstream& ss);
void receivedQuery(DbResponse& dbresponse, /*AbstractMessagingPort& dbMsgPort, */Message& m, stringstream& ss, bool logit);
void getDatabaseNames( vector< string > &names );
2009-04-01 18:26:31 +02:00
// must call with db lock
void registerListenerSocket( int socket );
// --- local client ---
2009-03-25 22:08:36 +01:00
class DBDirectClient : public DBClientBase {
virtual bool isFailed() const {
return false;
}
virtual string toString() {
return "DBDirectClient";
}
2009-03-25 22:08:36 +01:00
virtual string getServerAddress() const{
return "localhost"; // TODO: should this have the port?
}
virtual bool call( Message &toSend, Message &response, bool assertOk=true );
virtual void say( Message &toSend );
virtual void sayPiggyBack( Message &toSend ) {
// don't need to piggy back when connected locally
return say( toSend );
}
2009-01-23 16:11:11 +01:00
class AlwaysAuthorized : public AuthenticationInfo {
virtual bool isAuthorized( const char *dbname ) {
return true;
}
};
/* TODO: this looks bad that auth is set to always. is that really always safe? */
class SavedContext {
2009-01-23 16:11:11 +01:00
public:
SavedContext() {
dblock lk;
if ( database )
oldName = database->name;
Connection *c = currentConnection.get();
oldAuth = c->ai;
// careful, don't want to free this:
c->ai = &always;
2009-01-23 16:11:11 +01:00
}
~SavedContext() {
Connection *c = currentConnection.get();
c->ai = oldAuth;
if ( !oldName.empty() ) {
dblock lk;
setClientTempNs( oldName.c_str() );
}
2009-01-23 16:11:11 +01:00
}
private:
static AlwaysAuthorized always;
AuthenticationInfo *oldAuth;
string oldName;
2009-01-23 16:11:11 +01:00
};
};
extern int lockFile;
void acquirePathLock();
2009-02-09 21:57:30 +01:00
2009-01-14 23:09:51 +01:00
} // namespace mongo