diff options
Diffstat (limited to 'db/dbmessage.h')
-rw-r--r-- | db/dbmessage.h | 158 |
1 files changed, 82 insertions, 76 deletions
diff --git a/db/dbmessage.h b/db/dbmessage.h index b7e3bc22128..8076f73fa79 100644 --- a/db/dbmessage.h +++ b/db/dbmessage.h @@ -1,15 +1,15 @@ /** * Copyright (C) 2008 10gen Inc. -* +* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. -* +* * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. -* +* * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ @@ -18,96 +18,102 @@ #include "jsobj.h" #include "namespace.h" -/* For the database/server protocol, these objects and functions encapsulate +/* For the database/server protocol, these objects and functions encapsulate the various messages transmitted over the connection. */ class DbMessage { public: - DbMessage(const Message& _m) : m(_m) { - theEnd = _m.data->_data + _m.data->dataLen(); - int *r = (int *) _m.data->_data; - reserved = *r; - r++; - data = (const char *) r; - nextjsobj = data; - } - - const char * getns() { return data; } - void getns(Namespace& ns) { - ns = data; - } - - int pullInt() { - if( nextjsobj == data ) - nextjsobj += strlen(data) + 1; // skip namespace - int i = *((int *)nextjsobj); - nextjsobj += 4; - return i; - } - long long pullInt64() { - if( nextjsobj == data ) - nextjsobj += strlen(data) + 1; // skip namespace - long long i = *((long long *)nextjsobj); - nextjsobj += 8; - return i; - } - - OID* getOID() { - return (OID *) (data + strlen(data) + 1); // skip namespace - } - - void getQueryStuff(const char *&query, int& ntoreturn) { - int *i = (int *) (data + strlen(data) + 1); - ntoreturn = *i; - i++; - query = (const char *) i; - } - - /* for insert and update msgs */ - bool moreJSObjs() { return nextjsobj != 0; } - BSONObj nextJsObj() { - if( nextjsobj == data ) - nextjsobj += strlen(data) + 1; // skip namespace - BSONObj js(nextjsobj); - assert( js.objsize() < ( theEnd - data ) ); - if( js.objsize() <= 0 ) - nextjsobj = null; - else { - nextjsobj += js.objsize(); - if( nextjsobj >= theEnd ) - nextjsobj = 0; - } - return js; - } - - const Message& msg() { return m; } + DbMessage(const Message& _m) : m(_m) { + theEnd = _m.data->_data + _m.data->dataLen(); + int *r = (int *) _m.data->_data; + reserved = *r; + r++; + data = (const char *) r; + nextjsobj = data; + } + + const char * getns() { + return data; + } + void getns(Namespace& ns) { + ns = data; + } + + int pullInt() { + if ( nextjsobj == data ) + nextjsobj += strlen(data) + 1; // skip namespace + int i = *((int *)nextjsobj); + nextjsobj += 4; + return i; + } + long long pullInt64() { + if ( nextjsobj == data ) + nextjsobj += strlen(data) + 1; // skip namespace + long long i = *((long long *)nextjsobj); + nextjsobj += 8; + return i; + } + + OID* getOID() { + return (OID *) (data + strlen(data) + 1); // skip namespace + } + + void getQueryStuff(const char *&query, int& ntoreturn) { + int *i = (int *) (data + strlen(data) + 1); + ntoreturn = *i; + i++; + query = (const char *) i; + } + + /* for insert and update msgs */ + bool moreJSObjs() { + return nextjsobj != 0; + } + BSONObj nextJsObj() { + if ( nextjsobj == data ) + nextjsobj += strlen(data) + 1; // skip namespace + BSONObj js(nextjsobj); + assert( js.objsize() < ( theEnd - data ) ); + if ( js.objsize() <= 0 ) + nextjsobj = null; + else { + nextjsobj += js.objsize(); + if ( nextjsobj >= theEnd ) + nextjsobj = 0; + } + return js; + } + + const Message& msg() { + return m; + } private: - const Message& m; - int reserved; - const char *data; - const char *nextjsobj; - const char *theEnd; + const Message& m; + int reserved; + const char *data; + const char *nextjsobj; + const char *theEnd; }; /* a request to run a query, received from the database */ -class QueryMessage { +class QueryMessage { public: const char *ns; int ntoskip; int ntoreturn; int queryOptions; BSONObj query; - auto_ptr< set<string> > fields; + auto_ptr< set<string> > fields; /* parses the message into the above fields */ - QueryMessage(DbMessage& d) { + QueryMessage(DbMessage& d) { ns = d.getns(); ntoskip = d.pullInt(); ntoreturn = d.pullInt(); query = d.nextJsObj(); - if( d.moreJSObjs() ) { + if ( d.moreJSObjs() ) { fields = auto_ptr< set<string> >(new set<string>()); d.nextJsObj().getFieldNames(*fields); } @@ -115,13 +121,13 @@ public: } }; -#include "../client/dbclient.h" +#include "../client/dbclient.h" inline void replyToQuery(MessagingPort& p, Message& requestMsg, void *data, int size, - int nReturned, int startingFrom = 0, + int nReturned, int startingFrom = 0, long long cursorId = 0 - ) { + ) { BufBuilder b(32768); b.skip(sizeof(QueryResult)); b.append(data, size); @@ -144,9 +150,9 @@ inline void replyToQuery(MessagingPort& p, Message& requestMsg, //#include "bsonobj.h" inline void replyToQuery(MessagingPort& p, Message& requestMsg, - BSONObj& responseObj) + BSONObj& responseObj) { replyToQuery( - p, requestMsg, + p, requestMsg, (void *) responseObj.objdata(), responseObj.objsize(), 1); } |