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

90 lines
2.5 KiB
C
Raw Normal View History

2010-07-18 19:41:20 +02:00
/** @file oplogreader.h */
#pragma once
#include "../client/dbclient.h"
#include "../client/constants.h"
namespace mongo {
/* started abstracting out the querying of the primary/master's oplog
still fairly awkward but a start.
*/
class OplogReader {
auto_ptr<DBClientConnection> _conn;
auto_ptr<DBClientCursor> cursor;
public:
void reset() {
cursor.reset();
}
void resetConnection() {
cursor.reset();
_conn.reset();
}
DBClientConnection* conn() { return _conn.get(); }
BSONObj findOne(const char *ns, Query& q) {
return conn()->findOne(ns, q);
}
2010-07-18 21:02:46 +02:00
BSONObj getLastOp(const char *ns) {
return findOne(ns, Query().sort( BSON( "$natural" << -1 ) ));
}
2010-07-18 19:41:20 +02:00
/* ok to call if already connected */
bool connect(string hostname);
void getReady() {
if( cursor.get() && cursor->isDead() ) {
log() << "repl: old cursor isDead, initiating a new one" << endl;
reset();
}
}
bool haveCursor() { return cursor.get() != 0; }
void tailingQuery(const char *ns, BSONObj& query) {
assert( !haveCursor() );
log(2) << "repl: " << ns << ".find(" << query.toString() << ')' << endl;
cursor = _conn->query( ns, query, 0, 0, 0,
QueryOption_CursorTailable | QueryOption_SlaveOk | QueryOption_OplogReplay |
QueryOption_AwaitData
);
}
2010-07-20 04:32:43 +02:00
void tailingQueryGTE(const char *ns, OpTime t) {
BSONObjBuilder q;
q.appendDate("$gte", t.asDate());
BSONObjBuilder query;
query.append("ts", q.done());
tailingQuery(ns, query.done());
}
2010-07-18 19:41:20 +02:00
bool more() {
assert( cursor.get() );
return cursor->more();
}
/* old mongod's can't do the await flag... */
bool awaitCapable() {
return cursor->hasResultFlag(ResultFlag_AwaitCapable);
}
void peek(vector<BSONObj>& v, int n) {
if( cursor.get() )
cursor->peek(v,n);
}
2010-07-20 04:32:43 +02:00
BSONObj nextSafe() { return cursor->nextSafe(); }
2010-07-18 19:41:20 +02:00
BSONObj next() {
return cursor->next();
}
void putBack(BSONObj op) {
cursor->putBack(op);
}
};
}