diff options
author | Dwight <dmerriman@gmail.com> | 2008-10-14 14:02:43 -0400 |
---|---|---|
committer | Dwight <dmerriman@gmail.com> | 2008-10-14 14:02:43 -0400 |
commit | 87d0ed26c42d0f6da68bad6dccd2aae8d8e33516 (patch) | |
tree | 05b3cb2fb105c5cd418d01be9ff718a3c2190040 | |
parent | cdc9ae445c94ccea86940df6c84f94c669796569 (diff) | |
download | mongo-87d0ed26c42d0f6da68bad6dccd2aae8d8e33516.tar.gz |
allow an array for sort order
-rw-r--r-- | db/dbclient.cpp | 4 | ||||
-rw-r--r-- | db/jsobj.h | 13 | ||||
-rw-r--r-- | db/query.cpp | 32 | ||||
-rw-r--r-- | db/query.h | 4 | ||||
-rw-r--r-- | dbgrid/gridconfig.cpp | 3 | ||||
-rw-r--r-- | dbgrid/gridconfig.h | 2 | ||||
-rw-r--r-- | util/sock.cpp | 10 | ||||
-rw-r--r-- | util/sock.h | 7 |
8 files changed, 64 insertions, 11 deletions
diff --git a/db/dbclient.cpp b/db/dbclient.cpp index c1e9e7d3aca..ef1b3bf27e6 100644 --- a/db/dbclient.cpp +++ b/db/dbclient.cpp @@ -41,7 +41,7 @@ bool DBClientConnection::connect(const char *serverAddress, string& errmsg) { int port = DBPort; - string ip = hostbyname_nonreentrant(serverAddress); + string ip = hostbyname(serverAddress); if( ip.empty() ) ip = serverAddress; @@ -50,7 +50,7 @@ bool DBClientConnection::connect(const char *serverAddress, string& errmsg) { //cout << "port string:" << ip.substr( idx ) << endl; port = atoi( ip.substr( idx + 1 ).c_str() ); ip = ip.substr( 0 , idx ); - ip = hostbyname_nonreentrant(ip.c_str()); + ip = hostbyname(ip.c_str()); } if( ip.empty() ) diff --git a/db/jsobj.h b/db/jsobj.h index 63de422371f..8fcaa7baf66 100644 --- a/db/jsobj.h +++ b/db/jsobj.h @@ -483,6 +483,19 @@ private: const char *theend; }; +/* iterator a JSObj which is an array, in array order. +class JSArrayIter { +public: + JSElemIter(const JSObj& jso) { +... + } + bool more() { return ... } + Element next() { +... + } +}; +*/ + #include <pcrecpp.h> class RegexMatcher { diff --git a/db/query.cpp b/db/query.cpp index 2e80da08976..28a11f10386 100644 --- a/db/query.cpp +++ b/db/query.cpp @@ -527,6 +527,28 @@ int runCount(const char *ns, JSObj& cmd, string& err) { return count; } +/* [ { a : 1 } , { b : 1 } ] -> { a : 1, b : 1 } +*/ +inline JSObj transformOrderFromArrayFormat(JSObj order) { + /* note: this is slow, but that is ok as order will have very few pieces */ + JSObjBuilder b; + char p[2] = "0"; + + while( 1 ) { + JSObj j = order.getObjectField(p); + if( j.isEmpty() ) + break; + Element e = j.firstElement(); + uassert("bad order array", !e.eoo()); + uassert("bad order array [2]", e.isNumber()); + b.append(e); + (*p)++; + uassert("too many ordering elements", *p <= '9'); + } + + return b.doneAndDecouple(); +} + QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoreturn, JSObj jsobj, auto_ptr< set<string> > filter, stringstream& ss, int queryOptions) { @@ -555,7 +577,15 @@ QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoret uassert("not master", isMaster() || (queryOptions & Option_SlaveOk)); JSObj query = jsobj.getObjectField("query"); - JSObj order = jsobj.getObjectField("orderby"); + JSObj order; + { + Element e = jsobj.findElement("orderby"); + if( !e.eoo() ) { + order = e.embeddedObject(); + if( e.type() == Array ) + order = transformOrderFromArrayFormat(order); + } + } if( query.isEmpty() && order.isEmpty() ) query = jsobj; diff --git a/db/query.h b/db/query.h index e5e19754ac4..81eb6688944 100644 --- a/db/query.h +++ b/db/query.h @@ -47,7 +47,7 @@ dbQuery: string collection; int nToSkip; - int nToReturn; // how many you want back as the beginning of the cursor data + int nToReturn; // how many you want back as the beginning of the cursor data (0=no limit) JSObject query; [JSObject fieldsToReturn] dbGetMore: @@ -71,7 +71,7 @@ int resultFlags = 0; int64 cursorID; int startingFrom; - int nReturned; // 0=infinity + int nReturned; list of marshalled JSObjects; */ diff --git a/dbgrid/gridconfig.cpp b/dbgrid/gridconfig.cpp index 2e25acba24c..56af17b15d6 100644 --- a/dbgrid/gridconfig.cpp +++ b/dbgrid/gridconfig.cpp @@ -23,3 +23,6 @@ #include "connpool.h" #include "gridconfig.h" +GridDB::GridDB() { + +} diff --git a/dbgrid/gridconfig.h b/dbgrid/gridconfig.h index df41b56c955..9e5330aa27c 100644 --- a/dbgrid/gridconfig.h +++ b/dbgrid/gridconfig.h @@ -26,7 +26,9 @@ class GridDB { public: enum { Port = 30000 }; + GridDB(); }; +extern GridDB gridDB; /* Machine is the concept of a host that runs the db process. */ diff --git a/util/sock.cpp b/util/sock.cpp index 9c22fa58139..b88cb5bb08e 100644 --- a/util/sock.cpp +++ b/util/sock.cpp @@ -19,6 +19,16 @@ #include "stdafx.h" #include "sock.h" +static boost::mutex sock_mutex; +// .empty() if err +string hostbyname(const char *hostname) { + boostlock lk(sock_mutex); + struct hostent *h; + h = gethostbyname(hostname); + if( h == 0 ) return ""; + return inet_ntoa( *((struct in_addr *)(h->h_addr)) ); +} + void sendtest() { cout << "sendtest\n"; SockAddr me(27016); diff --git a/util/sock.h b/util/sock.h index 58913027fcd..9187fe84785 100644 --- a/util/sock.h +++ b/util/sock.h @@ -72,12 +72,7 @@ inline void prebindOptions( int sock ){ #endif // .empty() if err -inline string hostbyname_nonreentrant(const char *hostname) { - struct hostent *h; - h = gethostbyname(hostname); - if( h == 0 ) return ""; - return inet_ntoa( *((struct in_addr *)(h->h_addr)) ); -} +string hostbyname(const char *hostname); struct SockAddr { SockAddr() { addressSize = sizeof(sockaddr_in); memset(&sa, 0, sizeof(sa)); } |