summaryrefslogtreecommitdiff
path: root/client/examples/tail.cpp
diff options
context:
space:
mode:
authorAntonin Kral <a.kral@bobek.cz>2010-01-31 08:32:52 +0100
committerAntonin Kral <a.kral@bobek.cz>2010-01-31 08:32:52 +0100
commit4eefaf421bfeddf040d96a3dafb12e09673423d7 (patch)
treecb2e5ccc7f98158894f977ff131949da36673591 /client/examples/tail.cpp
downloadmongodb-4eefaf421bfeddf040d96a3dafb12e09673423d7.tar.gz
Imported Upstream version 1.3.1
Diffstat (limited to 'client/examples/tail.cpp')
-rw-r--r--client/examples/tail.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/client/examples/tail.cpp b/client/examples/tail.cpp
new file mode 100644
index 0000000..e844b32
--- /dev/null
+++ b/client/examples/tail.cpp
@@ -0,0 +1,55 @@
+// tail.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* example of using a tailable cursor */
+
+#include "../../client/dbclient.h"
+#include "../../util/goodies.h"
+
+using namespace mongo;
+
+void foo() { }
+
+/* "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.)
+
+ 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);
+ }
+ BSONObj o = c->next();
+ lastId = o["_id"];
+ cout << o.toString() << endl;
+ }
+ query = QUERY( "_id" << GT << lastId ).sort("_id");
+ }
+}