2
* Copyright (C) 2008 10gen Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Affero General Public License, version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Affero General Public License for more details.
13
* You should have received a copy of the GNU Affero General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "namespace.h"
22
#include "../util/message.h"
28
Query or GetMore: // see struct QueryResult
33
list of marshalled JSObjects;
39
struct QueryResult : public MsgData {
41
/* returned, with zero results, when getMore is called but the cursor id
42
is not valid at the server. */
43
ResultFlag_CursorNotFound = 1,
45
/* { $err : ... } is being returned */
46
ResultFlag_ErrSet = 2,
48
/* Have to update config from the server, usually $err is also set */
49
ResultFlag_ShardConfigStale = 4,
51
/* for backward compatability: this let's us know the server supports
52
the QueryOption_AwaitData option. if it doesn't, a repl slave client should sleep
53
a little between getMore's.
55
ResultFlag_AwaitCapable = 8
62
return (char *) (((int *)&nReturned)+1);
70
void setResultFlagsToOk() {
71
_resultFlags() = 0; // ResultFlag_AwaitCapable
76
/* For the database/server protocol, these objects and functions encapsulate
77
the various messages transmitted over the connection.
82
DbMessage(const Message& _m) : m(_m) {
83
theEnd = _m.data->_data + _m.data->dataLen();
84
int *r = (int *) _m.data->_data;
87
data = (const char *) r;
91
const char * getns() {
94
void getns(Namespace& ns) {
103
if ( nextjsobj == data )
104
nextjsobj += strlen(data) + 1; // skip namespace
105
int i = *((int *)nextjsobj);
109
long long pullInt64() const {
112
long long &pullInt64() {
113
if ( nextjsobj == data )
114
nextjsobj += strlen(data) + 1; // skip namespace
115
long long &i = *((long long *)nextjsobj);
121
return (OID *) (data + strlen(data) + 1); // skip namespace
124
void getQueryStuff(const char *&query, int& ntoreturn) {
125
int *i = (int *) (data + strlen(data) + 1);
128
query = (const char *) i;
131
/* for insert and update msgs */
133
return nextjsobj != 0;
135
BSONObj nextJsObj() {
136
if ( nextjsobj == data )
137
nextjsobj += strlen(data) + 1; // skip namespace
138
massert( 10304 , "Remaining data too small for BSON object", theEnd - nextjsobj > 3 );
139
BSONObj js(nextjsobj);
140
massert( 10305 , "Invalid object size", js.objsize() > 3 );
141
massert( 10306 , "Next object larger than available space",
142
js.objsize() < ( theEnd - data ) );
143
if ( objcheck && !js.valid() ) {
144
massert( 10307 , "bad object in message", false);
146
nextjsobj += js.objsize();
147
if ( nextjsobj >= theEnd )
152
const Message& msg() {
168
const char *nextjsobj;
175
/* a request to run a query, received from the database */
183
auto_ptr< FieldMatcher > fields;
185
/* parses the message into the above fields */
186
QueryMessage(DbMessage& d) {
188
ntoskip = d.pullInt();
189
ntoreturn = d.pullInt();
190
query = d.nextJsObj();
191
if ( d.moreJSObjs() ) {
192
BSONObj o = d.nextJsObj();
194
fields = auto_ptr< FieldMatcher >(new FieldMatcher() );
198
queryOptions = d.msg().data->dataAsInt();
204
#include "../client/dbclient.h"
208
inline void replyToQuery(int queryResultFlags,
209
AbstractMessagingPort* p, Message& requestMsg,
210
void *data, int size,
211
int nReturned, int startingFrom = 0,
212
long long cursorId = 0
215
b.skip(sizeof(QueryResult));
216
b.append(data, size);
217
QueryResult *qr = (QueryResult *) b.buf();
218
qr->_resultFlags() = queryResultFlags;
220
qr->setOperation(opReply);
221
qr->cursorId = cursorId;
222
qr->startingFrom = startingFrom;
223
qr->nReturned = nReturned;
225
Message *resp = new Message();
226
resp->setData(qr, true); // transport will free
227
p->reply(requestMsg, *resp, requestMsg.data->id);
232
//#include "bsonobj.h"
233
#include "instance.h"
237
/* object reply helper. */
238
inline void replyToQuery(int queryResultFlags,
239
AbstractMessagingPort* p, Message& requestMsg,
240
BSONObj& responseObj)
242
replyToQuery(queryResultFlags,
244
(void *) responseObj.objdata(), responseObj.objsize(), 1);
247
/* helper to do a reply using a DbResponse object */
248
inline void replyToQuery(int queryResultFlags, Message &m, DbResponse &dbresponse, BSONObj obj) {
250
b.skip(sizeof(QueryResult));
251
b.append((void*) obj.objdata(), obj.objsize());
252
QueryResult* msgdata = (QueryResult *) b.buf();
254
QueryResult *qr = msgdata;
255
qr->_resultFlags() = queryResultFlags;
257
qr->setOperation(opReply);
259
qr->startingFrom = 0;
261
Message *resp = new Message();
262
resp->setData(msgdata, true); // transport will free
263
dbresponse.response = resp;
264
dbresponse.responseTo = m.data->id;