diff options
author | Dwight <dmerriman@gmail.com> | 2009-02-09 16:24:47 -0500 |
---|---|---|
committer | Dwight <dmerriman@gmail.com> | 2009-02-09 16:24:47 -0500 |
commit | 52ac9edf310f747e1ac3a14c741c4e07821fb876 (patch) | |
tree | fc2a917b8b0af4b8a3bb863d33a0e930edc1ba16 | |
parent | fadc7ef3bbd7a96346211cc4c1ac2d58104e8427 (diff) | |
download | mongo-52ac9edf310f747e1ac3a14c741c4e07821fb876.tar.gz |
count() for c++ client
-rw-r--r-- | client/dbclient.cpp | 8 | ||||
-rw-r--r-- | client/dbclient.h | 2 | ||||
-rw-r--r-- | client/examples/tutorial.cpp | 4 | ||||
-rw-r--r-- | db/namespace.h | 60 | ||||
-rw-r--r-- | db/query.cpp | 3 |
5 files changed, 43 insertions, 34 deletions
diff --git a/client/dbclient.cpp b/client/dbclient.cpp index c81c1d0dd50..a8f3383b9ec 100644 --- a/client/dbclient.cpp +++ b/client/dbclient.cpp @@ -95,11 +95,11 @@ namespace mongo { return runCommand(dbname, b.done(), *info); } - unsigned long long DBClientWithCommands::count(const char *ns, BSONObj query) { - BSONObj cmd = BSON( "count" << ns << "query" << query ); - string db = nsToClient(ns); + unsigned long long DBClientWithCommands::count(const char *_ns, BSONObj query) { + NamespaceString ns(_ns); + BSONObj cmd = BSON( "count" << ns.coll << "query" << query ); BSONObj res; - if( !runCommand(db.c_str(), cmd, res) ) + if( !runCommand(ns.db.c_str(), cmd, res) ) uasserted(string("count fails:") + res.toString()); return res.getIntField("n"); } diff --git a/client/dbclient.h b/client/dbclient.h index e2b5e5c790a..090f72481d1 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -269,7 +269,7 @@ namespace mongo { /** count number of objects in collection ns that match the query criteria specified throws UserAssertion if database returns an error */ - unsigned long long count(const char *ns, BSONObj query); + unsigned long long count(const char *ns, BSONObj query = emptyObj); string createPasswordDigest( const char * username , const char * clearTextPassword ); diff --git a/client/examples/tutorial.cpp b/client/examples/tutorial.cpp index b5f2eb8788e..df38883533d 100644 --- a/client/examples/tutorial.cpp +++ b/client/examples/tutorial.cpp @@ -7,12 +7,12 @@ using namespace mongo; void run() { DBClientConnection c; - c.connect("localhost"); + c.connect("192.168.58.1"); cout << "connected ok" << endl; BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); c.insert("tutorial.persons", p); - cout << "count:" << c.count( + cout << "count:" << c.count("tutorial.persons") << endl; } int main() { diff --git a/db/namespace.h b/db/namespace.h index de98d443f98..94af22cd94d 100644 --- a/db/namespace.h +++ b/db/namespace.h @@ -32,12 +32,42 @@ namespace mongo { #pragma pack(1) +// "database.a.b.c" -> "database" + const int MaxClientLen = 256; + inline void nsToClient(const char *ns, char *database) { + const char *p = ns; + char *q = database; + while ( *p != '.' ) { + if ( *p == 0 ) + break; + *q++ = *p++; + } + *q = 0; + if (q-database>=MaxClientLen) { + problem() << "nsToClient: ns too long. terminating, buf overrun condition" << endl; + dbexit(60); + } + } + inline string nsToClient(const char *ns) { + char buf[MaxClientLen]; + nsToClient(ns, buf); + return buf; + } + class NamespaceString { public: - NamespaceString( const char * c ) : _str( c ){} - NamespaceString( const string& s ) : _str( s.c_str() ){} - - const char * _str; + string db; + string coll; + private: + void init(const char *ns) { + const char *p = strchr(ns, '.'); + if( p == 0 ) return; + db = string(ns, p - ns); + coll = p + 1; + } + public: + NamespaceString( const char * ns ) { init(ns); } + NamespaceString( const string& ns ) { init(ns.c_str()); } }; class Namespace { @@ -410,26 +440,4 @@ namespace mongo { extern const char *dbpath; -// "database.a.b.c" -> "database" - const int MaxClientLen = 256; - inline void nsToClient(const char *ns, char *database) { - const char *p = ns; - char *q = database; - while ( *p != '.' ) { - if ( *p == 0 ) - break; - *q++ = *p++; - } - *q = 0; - if (q-database>=MaxClientLen) { - problem() << "nsToClient: ns too long. terminating, buf overrun condition" << endl; - dbexit(60); - } - } - inline string nsToClient(const char *ns) { - char buf[MaxClientLen]; - nsToClient(ns, buf); - return buf; - } - } // namespace mongo diff --git a/db/query.cpp b/db/query.cpp index 20507ccd2a4..b88ae843692 100644 --- a/db/query.cpp +++ b/db/query.cpp @@ -529,7 +529,8 @@ namespace mongo { BSONObj empty_obj = fromjson("{}"); /* { count: "collectionname"[, query: <query>] } - returns -1 on error. + returns -1 on ns does not exist error. + -2 on other errors */ int runCount(const char *ns, BSONObj& cmd, string& err) { NamespaceDetails *d = nsdetails(ns); |