~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to db/dbmessage.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
*    Copyright (C) 2008 10gen Inc.
 
3
*
 
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.
 
7
*
 
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.
 
12
*
 
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/>.
 
15
*/
 
16
 
 
17
#pragma once
 
18
 
 
19
#include "storage.h"
 
20
#include "jsobj.h"
 
21
#include "namespace.h"
 
22
#include "../util/message.h"
 
23
 
 
24
namespace mongo {
 
25
 
 
26
    /* db response format
 
27
 
 
28
       Query or GetMore: // see struct QueryResult
 
29
          int resultFlags;
 
30
          int64 cursorID;
 
31
          int startingFrom;
 
32
          int nReturned;
 
33
          list of marshalled JSObjects;
 
34
    */
 
35
 
 
36
    extern bool objcheck;
 
37
    
 
38
#pragma pack(1)
 
39
    struct QueryResult : public MsgData {
 
40
        enum ResultFlagType {
 
41
            /* returned, with zero results, when getMore is called but the cursor id 
 
42
               is not valid at the server. */
 
43
            ResultFlag_CursorNotFound = 1,   
 
44
 
 
45
            /* { $err : ... } is being returned */
 
46
            ResultFlag_ErrSet = 2,           
 
47
 
 
48
            /* Have to update config from the server, usually $err is also set */
 
49
            ResultFlag_ShardConfigStale = 4,  
 
50
 
 
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.
 
54
            */
 
55
            ResultFlag_AwaitCapable = 8
 
56
        };
 
57
 
 
58
        long long cursorId;
 
59
        int startingFrom;
 
60
        int nReturned;
 
61
        const char *data() {
 
62
            return (char *) (((int *)&nReturned)+1);
 
63
        }
 
64
        int resultFlags() {
 
65
            return dataAsInt();
 
66
        }
 
67
        int& _resultFlags() {
 
68
            return dataAsInt();
 
69
        }
 
70
        void setResultFlagsToOk() { 
 
71
            _resultFlags() = 0; // ResultFlag_AwaitCapable
 
72
        }
 
73
    };
 
74
#pragma pack()
 
75
 
 
76
    /* For the database/server protocol, these objects and functions encapsulate
 
77
       the various messages transmitted over the connection.
 
78
    */
 
79
 
 
80
    class DbMessage {
 
81
    public:
 
82
        DbMessage(const Message& _m) : m(_m) {
 
83
            theEnd = _m.data->_data + _m.data->dataLen();
 
84
            int *r = (int *) _m.data->_data;
 
85
            reserved = *r;
 
86
            r++;
 
87
            data = (const char *) r;
 
88
            nextjsobj = data;
 
89
        }
 
90
 
 
91
        const char * getns() {
 
92
            return data;
 
93
        }
 
94
        void getns(Namespace& ns) {
 
95
            ns = data;
 
96
        }
 
97
        
 
98
        
 
99
        void resetPull(){
 
100
            nextjsobj = data;
 
101
        }
 
102
        int pullInt() {
 
103
            if ( nextjsobj == data )
 
104
                nextjsobj += strlen(data) + 1; // skip namespace
 
105
            int i = *((int *)nextjsobj);
 
106
            nextjsobj += 4;
 
107
            return i;
 
108
        }
 
109
        long long pullInt64() const {
 
110
            return pullInt64();
 
111
        }
 
112
        long long &pullInt64() {
 
113
            if ( nextjsobj == data )
 
114
                nextjsobj += strlen(data) + 1; // skip namespace
 
115
            long long &i = *((long long *)nextjsobj);
 
116
            nextjsobj += 8;
 
117
            return i;
 
118
        }
 
119
 
 
120
        OID* getOID() {
 
121
            return (OID *) (data + strlen(data) + 1); // skip namespace
 
122
        }
 
123
 
 
124
        void getQueryStuff(const char *&query, int& ntoreturn) {
 
125
            int *i = (int *) (data + strlen(data) + 1);
 
126
            ntoreturn = *i;
 
127
            i++;
 
128
            query = (const char *) i;
 
129
        }
 
130
 
 
131
        /* for insert and update msgs */
 
132
        bool moreJSObjs() {
 
133
            return nextjsobj != 0;
 
134
        }
 
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);
 
145
            }            
 
146
            nextjsobj += js.objsize();
 
147
            if ( nextjsobj >= theEnd )
 
148
                nextjsobj = 0;
 
149
            return js;
 
150
        }
 
151
 
 
152
        const Message& msg() {
 
153
            return m;
 
154
        }
 
155
 
 
156
        void markSet(){
 
157
            mark = nextjsobj;
 
158
        }
 
159
        
 
160
        void markReset(){
 
161
            nextjsobj = mark;
 
162
        }
 
163
 
 
164
    private:
 
165
        const Message& m;
 
166
        int reserved;
 
167
        const char *data;
 
168
        const char *nextjsobj;
 
169
        const char *theEnd;
 
170
 
 
171
        const char * mark;
 
172
    };
 
173
 
 
174
 
 
175
    /* a request to run a query, received from the database */
 
176
    class QueryMessage {
 
177
    public:
 
178
        const char *ns;
 
179
        int ntoskip;
 
180
        int ntoreturn;
 
181
        int queryOptions;
 
182
        BSONObj query;
 
183
        auto_ptr< FieldMatcher > fields;
 
184
        
 
185
        /* parses the message into the above fields */
 
186
        QueryMessage(DbMessage& d) {
 
187
            ns = d.getns();
 
188
            ntoskip = d.pullInt();
 
189
            ntoreturn = d.pullInt();
 
190
            query = d.nextJsObj();
 
191
            if ( d.moreJSObjs() ) {
 
192
                BSONObj o = d.nextJsObj();
 
193
                if (!o.isEmpty()){
 
194
                    fields = auto_ptr< FieldMatcher >(new FieldMatcher() );
 
195
                    fields->add( o );
 
196
                }
 
197
            }
 
198
            queryOptions = d.msg().data->dataAsInt();
 
199
        }
 
200
    };
 
201
 
 
202
} // namespace mongo
 
203
 
 
204
#include "../client/dbclient.h"
 
205
 
 
206
namespace mongo {
 
207
 
 
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
 
213
                            ) {
 
214
        BufBuilder b(32768);
 
215
        b.skip(sizeof(QueryResult));
 
216
        b.append(data, size);
 
217
        QueryResult *qr = (QueryResult *) b.buf();
 
218
        qr->_resultFlags() = queryResultFlags;
 
219
        qr->len = b.len();
 
220
        qr->setOperation(opReply);
 
221
        qr->cursorId = cursorId;
 
222
        qr->startingFrom = startingFrom;
 
223
        qr->nReturned = nReturned;
 
224
        b.decouple();
 
225
        Message *resp = new Message();
 
226
        resp->setData(qr, true); // transport will free
 
227
        p->reply(requestMsg, *resp, requestMsg.data->id);
 
228
    }
 
229
 
 
230
} // namespace mongo
 
231
 
 
232
//#include "bsonobj.h"
 
233
#include "instance.h"
 
234
 
 
235
namespace mongo {
 
236
 
 
237
    /* object reply helper. */
 
238
    inline void replyToQuery(int queryResultFlags,
 
239
                             AbstractMessagingPort* p, Message& requestMsg,
 
240
                             BSONObj& responseObj)
 
241
    {
 
242
        replyToQuery(queryResultFlags,
 
243
                     p, requestMsg,
 
244
                     (void *) responseObj.objdata(), responseObj.objsize(), 1);
 
245
    }
 
246
 
 
247
    /* helper to do a reply using a DbResponse object */
 
248
    inline void replyToQuery(int queryResultFlags, Message &m, DbResponse &dbresponse, BSONObj obj) {
 
249
        BufBuilder b;
 
250
        b.skip(sizeof(QueryResult));
 
251
        b.append((void*) obj.objdata(), obj.objsize());
 
252
        QueryResult* msgdata = (QueryResult *) b.buf();
 
253
        b.decouple();
 
254
        QueryResult *qr = msgdata;
 
255
        qr->_resultFlags() = queryResultFlags;
 
256
        qr->len = b.len();
 
257
        qr->setOperation(opReply);
 
258
        qr->cursorId = 0;
 
259
        qr->startingFrom = 0;
 
260
        qr->nReturned = 1;
 
261
        Message *resp = new Message();
 
262
        resp->setData(msgdata, true); // transport will free
 
263
        dbresponse.response = resp;
 
264
        dbresponse.responseTo = m.data->id;
 
265
    }
 
266
 
 
267
} // namespace mongo