diff options
author | Antonin Kral <a.kral@bobek.cz> | 2010-08-11 12:38:57 +0200 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2010-08-11 12:38:57 +0200 |
commit | 7645618fd3914cb8a20561625913c20d49504a49 (patch) | |
tree | 8370f846f58f6d71165b7a0e2eda04648584ec76 /client/examples/tail.cpp | |
parent | 68c73c3c7608b4c87f07440dc3232801720b1168 (diff) | |
download | mongodb-7645618fd3914cb8a20561625913c20d49504a49.tar.gz |
Imported Upstream version 1.6.0
Diffstat (limited to 'client/examples/tail.cpp')
-rw-r--r-- | client/examples/tail.cpp | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/client/examples/tail.cpp b/client/examples/tail.cpp index e844b32..3738b4f 100644 --- a/client/examples/tail.cpp +++ b/client/examples/tail.cpp @@ -22,34 +22,25 @@ using namespace mongo; -void foo() { } +void tail(DBClientBase& conn, const char *ns) { + BSONElement lastId = minKey.firstElement(); + Query query = Query(); -/* "tail" the specified namespace, outputting elements as they are added. - _id values must be inserted in increasing order for this to work. (Some other - field could also be used.) + auto_ptr<DBClientCursor> c = + conn.query(ns, query, 0, 0, 0, QueryOption_CursorTailable); - Note: one could use a capped collection and $natural order to do something - similar, using sort({$natural:1}), and then not need to worry about - _id's being in order. -*/ -void tail(DBClientBase& conn, const char *ns) { - conn.ensureIndex(ns, fromjson("{_id:1}")); - BSONElement lastId; - Query query = Query().sort("_id"); while( 1 ) { - auto_ptr<DBClientCursor> c = conn.query(ns, query, 0, 0, 0, Option_CursorTailable); - while( 1 ) { - if( !c->more() ) { - if( c->isDead() ) { - // we need to requery - break; - } - sleepsecs(1); + if( !c->more() ) { + if( c->isDead() ) { + break; // we need to requery + } + + // all data (so far) exhausted, wait for more + sleepsecs(1); + continue; } BSONObj o = c->next(); lastId = o["_id"]; cout << o.toString() << endl; - } - query = QUERY( "_id" << GT << lastId ).sort("_id"); } } |