~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to client/examples/tail.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// tail.cpp
 
2
 
 
3
/*    Copyright 2009 10gen Inc.
 
4
 *
 
5
 *    Licensed under the Apache License, Version 2.0 (the "License");
 
6
 *    you may not use this file except in compliance with the License.
 
7
 *    You may obtain a copy of the License at
 
8
 *
 
9
 *    http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *    Unless required by applicable law or agreed to in writing, software
 
12
 *    distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *    See the License for the specific language governing permissions and
 
15
 *    limitations under the License.
 
16
 */
 
17
 
 
18
/* example of using a tailable cursor */
 
19
 
 
20
#include "../../client/dbclient.h"
 
21
#include "../../util/goodies.h"
 
22
 
 
23
using namespace mongo;
 
24
 
 
25
void foo() { }
 
26
 
 
27
/* "tail" the specified namespace, outputting elements as they are added. 
 
28
   _id values must be inserted in increasing order for this to work. (Some other 
 
29
   field could also be used.)
 
30
 
 
31
   Note: one could use a capped collection and $natural order to do something 
 
32
         similar, using sort({$natural:1}), and then not need to worry about 
 
33
         _id's being in order.
 
34
*/
 
35
void tail(DBClientBase& conn, const char *ns) {
 
36
  conn.ensureIndex(ns, fromjson("{_id:1}"));
 
37
  BSONElement lastId;
 
38
  Query query = Query().sort("_id");
 
39
  while( 1 ) {
 
40
    auto_ptr<DBClientCursor> c = conn.query(ns, query, 0, 0, 0, Option_CursorTailable);
 
41
    while( 1 ) {
 
42
      if( !c->more() ) { 
 
43
        if( c->isDead() ) {
 
44
          // we need to requery
 
45
          break;
 
46
        }
 
47
        sleepsecs(1);
 
48
      }
 
49
      BSONObj o = c->next();
 
50
      lastId = o["_id"];
 
51
      cout << o.toString() << endl;
 
52
    }
 
53
    query = QUERY( "_id" << GT << lastId ).sort("_id");
 
54
  }
 
55
}