diff options
Diffstat (limited to 'client/examples')
-rw-r--r-- | client/examples/authTest.cpp | 11 | ||||
-rw-r--r-- | client/examples/clientTest.cpp | 35 | ||||
-rw-r--r-- | client/examples/first.cpp | 11 | ||||
-rw-r--r-- | client/examples/httpClientTest.cpp | 8 | ||||
-rw-r--r-- | client/examples/rs.cpp | 58 | ||||
-rw-r--r-- | client/examples/second.cpp | 2 | ||||
-rw-r--r-- | client/examples/tail.cpp | 40 | ||||
-rw-r--r-- | client/examples/tutorial.cpp | 64 | ||||
-rw-r--r-- | client/examples/whereExample.cpp | 10 |
9 files changed, 154 insertions, 85 deletions
diff --git a/client/examples/authTest.cpp b/client/examples/authTest.cpp index 77ce12d..71cdd39 100644 --- a/client/examples/authTest.cpp +++ b/client/examples/authTest.cpp @@ -22,7 +22,7 @@ using namespace mongo; int main( int argc, const char **argv ) { - + const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) @@ -37,17 +37,18 @@ int main( int argc, const char **argv ) { throw -11; } - { // clean up old data from any previous tests + { + // clean up old data from any previous tests conn.remove( "test.system.users" , BSONObj() ); } conn.insert( "test.system.users" , BSON( "user" << "eliot" << "pwd" << conn.createPasswordDigest( "eliot" , "bar" ) ) ); - + errmsg.clear(); bool ok = conn.auth( "test" , "eliot" , "bar" , errmsg ); if ( ! ok ) cout << errmsg << endl; - assert( ok ); + MONGO_assert( ok ); - assert( ! conn.auth( "test" , "eliot" , "bars" , errmsg ) ); + MONGO_assert( ! conn.auth( "test" , "eliot" , "bars" , errmsg ) ); } diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp index 83a556a..bd4432e 100644 --- a/client/examples/clientTest.cpp +++ b/client/examples/clientTest.cpp @@ -19,9 +19,14 @@ * a simple test for the c++ driver */ +// this header should be first to ensure that it includes cleanly in any context +#include "client/dbclient.h" + #include <iostream> -#include "client/dbclient.h" +#ifndef assert +# define assert(x) MONGO_assert(x) +#endif using namespace std; using namespace mongo; @@ -125,12 +130,14 @@ int main( int argc, const char **argv ) { } - { // ensure index + { + // ensure index assert( conn.ensureIndex( ns , BSON( "name" << 1 ) ) ); assert( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) ); } - { // hint related tests + { + // hint related tests assert( conn.findOne(ns, "{}")["name"].str() == "sara" ); assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" ); @@ -141,7 +148,7 @@ int main( int argc, const char **argv ) { try { conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")); } - catch ( ... ){ + catch ( ... ) { asserted = true; } assert( asserted ); @@ -153,7 +160,8 @@ int main( int argc, const char **argv ) { assert( conn.validate( ns ) ); } - { // timestamp test + { + // timestamp test const char * tsns = "test.tstest1"; conn.dropCollection( tsns ); @@ -185,32 +193,33 @@ int main( int argc, const char **argv ) { ( oldTime == found["ts"].timestampTime() && oldInc < found["ts"].timestampInc() ) ); } - - { // check that killcursors doesn't affect last error + + { + // check that killcursors doesn't affect last error assert( conn.getLastError().empty() ); - + BufBuilder b; b.appendNum( (int)0 ); // reserved b.appendNum( (int)-1 ); // invalid # of cursors triggers exception b.appendNum( (int)-1 ); // bogus cursor id - + Message m; m.setData( dbKillCursors, b.buf(), b.len() ); - + // say() is protected in DBClientConnection, so get superclass static_cast< DBConnector* >( &conn )->say( m ); - + assert( conn.getLastError().empty() ); } { list<string> l = conn.getDatabaseNames(); - for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){ + for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ) { cout << "db name : " << *i << endl; } l = conn.getCollectionNames( "test" ); - for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){ + for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ) { cout << "coll name : " << *i << endl; } } diff --git a/client/examples/first.cpp b/client/examples/first.cpp index f3b654f..ab5efb3 100644 --- a/client/examples/first.cpp +++ b/client/examples/first.cpp @@ -40,7 +40,7 @@ int main( int argc, const char **argv ) { throw -12; port = argv[ 2 ]; } - + mongo::DBClientConnection conn; string errmsg; if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { @@ -48,14 +48,15 @@ int main( int argc, const char **argv ) { throw -11; } - { // clean up old data from any previous tests + { + // clean up old data from any previous tests mongo::BSONObjBuilder query; conn.remove( "test.people" , query.obj() ); } insert( conn , "eliot" , 15 ); insert( conn , "sara" , 23 ); - + { mongo::BSONObjBuilder query; auto_ptr<mongo::DBClientCursor> cursor = conn.query( "test.people" , query.obj() ); @@ -66,14 +67,14 @@ int main( int argc, const char **argv ) { } } - + { mongo::BSONObjBuilder query; query.append( "name" , "eliot" ); mongo::BSONObj res = conn.findOne( "test.people" , query.obj() ); cout << res.isEmpty() << "\t" << res.jsonString() << endl; } - + { mongo::BSONObjBuilder query; query.append( "name" , "asd" ); diff --git a/client/examples/httpClientTest.cpp b/client/examples/httpClientTest.cpp index 5d6c429..4fa5fd8 100644 --- a/client/examples/httpClientTest.cpp +++ b/client/examples/httpClientTest.cpp @@ -23,7 +23,7 @@ using namespace mongo; int main( int argc, const char **argv ) { - + int port = 27017; if ( argc != 1 ) { if ( argc != 3 ) @@ -31,13 +31,13 @@ int main( int argc, const char **argv ) { port = atoi( argv[ 2 ] ); } port += 1000; - + stringstream ss; ss << "http://localhost:" << port << "/"; string url = ss.str(); - + cout << "[" << url << "]" << endl; HttpClient c; - assert( c.get( url ) == 200 ); + MONGO_assert( c.get( url ) == 200 ); } diff --git a/client/examples/rs.cpp b/client/examples/rs.cpp new file mode 100644 index 0000000..7813ec6 --- /dev/null +++ b/client/examples/rs.cpp @@ -0,0 +1,58 @@ +// rs.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 replica sets from c++ + */ + +#include "client/dbclient.h" +#include <iostream> + +using namespace mongo; +using namespace std; + +int main( int argc , const char ** argv ) { + string errmsg; + ConnectionString cs = ConnectionString::parse( "foo/127.0.0.1" , errmsg ); + if ( ! cs.isValid() ) { + cout << "error parsing url: " << errmsg << endl; + return 1; + } + + DBClientReplicaSet * conn = (DBClientReplicaSet*)cs.connect( errmsg ); + if ( ! conn ) { + cout << "error connecting: " << errmsg << endl; + return 2; + } + + string collName = "test.rs1"; + + conn->dropCollection( collName ); + while ( true ) { + try { + conn->update( collName , BSONObj() , BSON( "$inc" << BSON( "x" << 1 ) ) , true ); + cout << conn->findOne( collName , BSONObj() ) << endl; + cout << "\t A" << conn->slaveConn().findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ) << endl; + cout << "\t B " << conn->findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ) << endl; + } + catch ( std::exception& e ) { + cout << "ERROR: " << e.what() << endl; + } + sleepsecs( 1 ); + } + +} diff --git a/client/examples/second.cpp b/client/examples/second.cpp index 68eafaa..6cc2111 100644 --- a/client/examples/second.cpp +++ b/client/examples/second.cpp @@ -23,7 +23,7 @@ using namespace std; using namespace mongo; int main( int argc, const char **argv ) { - + const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) diff --git a/client/examples/tail.cpp b/client/examples/tail.cpp index 3738b4f..90e62d2 100644 --- a/client/examples/tail.cpp +++ b/client/examples/tail.cpp @@ -23,24 +23,24 @@ using namespace mongo; void tail(DBClientBase& conn, const char *ns) { - BSONElement lastId = minKey.firstElement(); - Query query = Query(); - - auto_ptr<DBClientCursor> c = - conn.query(ns, query, 0, 0, 0, QueryOption_CursorTailable); - - while( 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; - } + BSONElement lastId = minKey.firstElement(); + Query query = Query(); + + auto_ptr<DBClientCursor> c = + conn.query(ns, query, 0, 0, 0, QueryOption_CursorTailable); + + while( 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; + } } diff --git a/client/examples/tutorial.cpp b/client/examples/tutorial.cpp index 28e1b27..3cdf359 100644 --- a/client/examples/tutorial.cpp +++ b/client/examples/tutorial.cpp @@ -23,45 +23,45 @@ using namespace mongo; void printIfAge(DBClientConnection& c, int age) { - auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") ); - while( cursor->more() ) { - BSONObj p = cursor->next(); - cout << p.getStringField("name") << endl; - } + auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") ); + while( cursor->more() ) { + BSONObj p = cursor->next(); + cout << p.getStringField("name") << endl; + } } void run() { - DBClientConnection c; - c.connect("localhost"); //"192.168.58.1"); - cout << "connected ok" << endl; - BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); - c.insert("tutorial.persons", p); - p = BSON( "name" << "Jane" << "age" << 40 ); - c.insert("tutorial.persons", p); - p = BSON( "name" << "Abe" << "age" << 33 ); - c.insert("tutorial.persons", p); - p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" ); - c.insert("tutorial.persons", p); + DBClientConnection c; + c.connect("localhost"); //"192.168.58.1"); + cout << "connected ok" << endl; + BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); + c.insert("tutorial.persons", p); + p = BSON( "name" << "Jane" << "age" << 40 ); + c.insert("tutorial.persons", p); + p = BSON( "name" << "Abe" << "age" << 33 ); + c.insert("tutorial.persons", p); + p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" ); + c.insert("tutorial.persons", p); - c.ensureIndex("tutorial.persons", fromjson("{age:1}")); + c.ensureIndex("tutorial.persons", fromjson("{age:1}")); - cout << "count:" << c.count("tutorial.persons") << endl; + cout << "count:" << c.count("tutorial.persons") << endl; - auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj()); - while( cursor->more() ) { - cout << cursor->next().toString() << endl; - } + auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj()); + while( cursor->more() ) { + cout << cursor->next().toString() << endl; + } - cout << "\nprintifage:\n"; - printIfAge(c, 33); + cout << "\nprintifage:\n"; + printIfAge(c, 33); } -int main() { - try { - run(); - } - catch( DBException &e ) { - cout << "caught " << e.what() << endl; - } - return 0; +int main() { + try { + run(); + } + catch( DBException &e ) { + cout << "caught " << e.what() << endl; + } + return 0; } diff --git a/client/examples/whereExample.cpp b/client/examples/whereExample.cpp index a26d921..ce4174b 100644 --- a/client/examples/whereExample.cpp +++ b/client/examples/whereExample.cpp @@ -23,7 +23,7 @@ using namespace std; using namespace mongo; int main( int argc, const char **argv ) { - + const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) @@ -36,7 +36,7 @@ int main( int argc, const char **argv ) { if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { cout << "couldn't connect : " << errmsg << endl; throw -11; - } + } const char * ns = "test.where"; @@ -44,9 +44,9 @@ int main( int argc, const char **argv ) { conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) ); conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) ); - + auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() ); - + while ( cursor->more() ) { BSONObj obj = cursor->next(); cout << "\t" << obj.jsonString() << endl; @@ -64,5 +64,5 @@ int main( int argc, const char **argv ) { cout << "\t" << obj.jsonString() << endl; num++; } - assert( num == 1 ); + MONGO_assert( num == 1 ); } |