Files
mongo/db/oplogreader.h

95 lines
2.6 KiB
C
Raw Normal View History

2010-07-18 13:41:20 -04:00
/** @file oplogreader.h */
#pragma once
#include "../client/dbclient.h"
#include "../client/constants.h"
2010-07-23 14:52:14 -04:00
#include "dbhelpers.h"
2010-07-18 13:41:20 -04:00
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:
2010-07-21 13:13:36 -04:00
void resetCursor() {
2010-07-18 13:41:20 -04:00
cursor.reset();
}
void resetConnection() {
cursor.reset();
_conn.reset();
}
DBClientConnection* conn() { return _conn.get(); }
2010-07-21 16:30:32 -04:00
BSONObj findOne(const char *ns, const Query& q) {
2010-07-18 13:41:20 -04:00
return conn()->findOne(ns, q);
}
2010-07-18 15:02:46 -04:00
BSONObj getLastOp(const char *ns) {
2010-07-23 14:52:14 -04:00
return findOne(ns, Query().sort(reverseNaturalObj));
2010-07-18 15:02:46 -04:00
}
2010-07-18 13:41:20 -04:00
/* ok to call if already connected */
bool connect(string hostname);
2010-07-20 13:37:09 -04:00
void tailCheck() {
2010-07-18 13:41:20 -04:00
if( cursor.get() && cursor->isDead() ) {
2010-07-20 13:37:09 -04:00
log() << "repl: old cursor isDead, will initiate a new one" << endl;
2010-07-21 13:13:36 -04:00
resetCursor();
2010-07-18 13:41:20 -04:00
}
}
bool haveCursor() { return cursor.get() != 0; }
2010-07-19 22:40:22 -04:00
void tailingQuery(const char *ns, const BSONObj& query) {
2010-07-18 13:41:20 -04:00
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-19 22:32:43 -04: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 13:41:20 -04:00
bool more() {
assert( cursor.get() );
return cursor->more();
}
2010-07-20 11:05:27 -04:00
bool moreInCurrentBatch() {
assert( cursor.get() );
return cursor->moreInCurrentBatch();
}
2010-07-18 13:41:20 -04:00
/* 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-19 22:32:43 -04:00
BSONObj nextSafe() { return cursor->nextSafe(); }
2010-07-18 13:41:20 -04:00
BSONObj next() {
return cursor->next();
}
void putBack(BSONObj op) {
cursor->putBack(op);
}
};
}