Files
mongo/db/dbmessage.h

283 lines
8.2 KiB
C
Raw Normal View History

// dbmessage.h
2008-09-29 18:00:53 -04:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
2008-09-29 18:00:53 -04:00
* 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.
2008-12-28 20:28:49 -05:00
*
2008-09-29 18:00:53 -04:00
* 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.
2008-12-28 20:28:49 -05:00
*
2008-09-29 18:00:53 -04:00
* 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/>.
*/
2009-02-06 14:11:02 -05:00
#pragma once
2010-02-04 18:06:04 -05:00
#include "diskloc.h"
2008-09-29 18:00:53 -04:00
#include "jsobj.h"
2010-11-04 09:07:28 -04:00
#include "namespace-inl.h"
#include "../util/net/message.h"
2010-07-18 13:34:16 -04:00
#include "../client/constants.h"
#include "instance.h"
2008-09-29 18:00:53 -04:00
2009-01-14 17:09:51 -05:00
namespace mongo {
2009-02-13 20:04:13 -05:00
/* db response format
Query or GetMore: // see struct QueryResult
int resultFlags;
int64 cursorID;
int startingFrom;
int nReturned;
list of marshalled JSObjects;
*/
2011-06-20 16:17:32 -04:00
/* db request message format
unsigned opid; // arbitary; will be echoed back
byte operation;
int options;
then for:
dbInsert:
string collection;
a series of JSObjects
dbDelete:
string collection;
int flags=0; // 1=DeleteSingle
JSObject query;
dbUpdate:
string collection;
int flags; // 1=upsert
JSObject query;
JSObject objectToUpdate;
objectToUpdate may include { $inc: <field> } or { $set: ... }, see struct Mod.
dbQuery:
string collection;
int nToSkip;
int nToReturn; // how many you want back as the beginning of the cursor data (0=no limit)
// greater than zero is simply a hint on how many objects to send back per "cursor batch".
// a negative number indicates a hard limit.
JSObject query;
[JSObject fieldsToReturn]
dbGetMore:
string collection; // redundant, might use for security.
int nToReturn;
int64 cursorID;
dbKillCursors=2007:
int n;
int64 cursorIDs[n];
Note that on Update, there is only one object, which is different
from insert where you can pass a list of objects to insert in the db.
Note that the update field layout is very similar layout to Query.
*/
2009-02-13 20:04:13 -05:00
#pragma pack(1)
struct QueryResult : public MsgData {
long long cursorId;
int startingFrom;
int nReturned;
const char *data() {
return (char *) (((int *)&nReturned)+1);
}
int resultFlags() {
2009-02-13 20:04:13 -05:00
return dataAsInt();
}
int& _resultFlags() {
return dataAsInt();
}
2011-01-04 00:40:41 -05:00
void setResultFlagsToOk() {
_resultFlags() = ResultFlag_AwaitCapable;
}
void initializeResultFlags() {
_resultFlags() = 0;
}
2009-02-13 20:04:13 -05:00
};
2009-02-13 20:04:13 -05:00
#pragma pack()
/* For the database/server protocol, these objects and functions encapsulate
the various messages transmitted over the connection.
2010-06-06 15:46:55 -04:00
See http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
*/
class DbMessage {
public:
2011-02-05 17:51:17 -05:00
DbMessage(const Message& _m) : m(_m) , mark(0) {
2010-05-12 15:26:00 -07:00
// for received messages, Message has only one buffer
theEnd = _m.singleData()->_data + _m.header()->dataLen();
2010-06-06 15:46:55 -04:00
char *r = _m.singleData()->_data;
reserved = (int *) r;
data = r + 4;
nextjsobj = data;
}
2008-12-28 20:28:49 -05:00
/** the 32 bit field before the ns
* track all bit usage here as its cross op
* 0: InsertOption_ContinueOnError
* 1: fromWriteback
*/
2010-06-06 15:46:55 -04:00
int& reservedField() { return *reserved; }
const char * getns() const {
return data;
}
void getns(Namespace& ns) const {
ns = data;
}
const char * afterNS() const {
return data + strlen( data ) + 1;
}
2011-01-04 00:40:41 -05:00
int getInt( int num ) const {
const int * foo = (const int*)afterNS();
return foo[num];
}
int getQueryNToReturn() const {
return getInt( 1 );
}
2011-02-05 17:51:17 -05:00
/**
* get an int64 at specified offsetBytes after ns
*/
long long getInt64( int offsetBytes ) const {
const char * x = afterNS();
x += offsetBytes;
const long long * ll = (const long long*)x;
return ll[0];
}
2011-01-04 00:40:41 -05:00
void resetPull() { nextjsobj = data; }
2010-06-06 15:46:55 -04:00
int pullInt() const { return pullInt(); }
int& pullInt() {
if ( nextjsobj == data )
nextjsobj += strlen(data) + 1; // skip namespace
2010-06-06 15:46:55 -04:00
int& i = *((int *)nextjsobj);
nextjsobj += 4;
return i;
}
2009-03-10 15:06:47 -04:00
long long pullInt64() const {
return pullInt64();
}
long long &pullInt64() {
if ( nextjsobj == data )
nextjsobj += strlen(data) + 1; // skip namespace
2009-03-10 15:06:47 -04:00
long long &i = *((long long *)nextjsobj);
nextjsobj += 8;
return i;
}
2008-12-28 20:28:49 -05:00
OID* getOID() const {
return (OID *) (data + strlen(data) + 1); // skip namespace
}
2008-12-28 20:28:49 -05:00
void getQueryStuff(const char *&query, int& ntoreturn) {
int *i = (int *) (data + strlen(data) + 1);
ntoreturn = *i;
i++;
query = (const char *) i;
}
2008-12-28 20:28:49 -05:00
/* for insert and update msgs */
bool moreJSObjs() const {
return nextjsobj != 0;
}
BSONObj nextJsObj() {
if ( nextjsobj == data ) {
nextjsobj += strlen(data) + 1; // skip namespace
massert( 13066 , "Message contains no documents", theEnd > nextjsobj );
}
2010-06-03 16:50:51 -04:00
massert( 10304 , "Client Error: Remaining data too small for BSON object", theEnd - nextjsobj > 3 );
BSONObj js(nextjsobj);
2010-06-03 16:50:51 -04:00
massert( 10305 , "Client Error: Invalid object size", js.objsize() > 3 );
massert( 10306 , "Client Error: Next object larger than space left in message",
2011-01-04 00:40:41 -05:00
js.objsize() < ( theEnd - data ) );
2011-06-26 18:28:45 -04:00
if ( cmdLine.objcheck && !js.valid() ) {
2010-06-03 16:50:51 -04:00
massert( 10307 , "Client Error: bad object in message", false);
2011-01-04 00:40:41 -05:00
}
2009-01-21 11:14:35 -05:00
nextjsobj += js.objsize();
if ( nextjsobj >= theEnd )
nextjsobj = 0;
return js;
2008-12-28 20:28:49 -05:00
}
2010-06-06 15:46:55 -04:00
const Message& msg() const { return m; }
2008-10-21 16:13:48 -04:00
2011-01-04 00:40:41 -05:00
void markSet() {
mark = nextjsobj;
}
2011-01-04 00:40:41 -05:00
void markReset() {
2011-02-05 17:51:17 -05:00
assert( mark );
nextjsobj = mark;
}
private:
const Message& m;
2010-06-06 15:46:55 -04:00
int* reserved;
const char *data;
const char *nextjsobj;
const char *theEnd;
const char * mark;
public:
enum ReservedOptions {
Reserved_InsertOption_ContinueOnError = 1 << 0 ,
Reserved_FromWriteback = 1 << 1
};
};
/* a request to run a query, received from the database */
class QueryMessage {
public:
const char *ns;
int ntoskip;
int ntoreturn;
int queryOptions;
BSONObj query;
BSONObj fields;
2011-01-04 00:40:41 -05:00
/* parses the message into the above fields */
QueryMessage(DbMessage& d) {
ns = d.getns();
ntoskip = d.pullInt();
ntoreturn = d.pullInt();
query = d.nextJsObj();
if ( d.moreJSObjs() ) {
fields = d.nextJsObj();
}
2010-05-12 15:26:00 -07:00
queryOptions = d.msg().header()->dataAsInt();
2008-10-21 16:13:48 -04:00
}
};
void replyToQuery(int queryResultFlags,
AbstractMessagingPort* p, Message& requestMsg,
void *data, int size,
int nReturned, int startingFrom = 0,
long long cursorId = 0
);
2009-01-14 17:09:51 -05:00
2009-01-28 18:08:02 -05:00
/* object reply helper. */
void replyToQuery(int queryResultFlags,
AbstractMessagingPort* p, Message& requestMsg,
BSONObj& responseObj);
2009-01-14 17:09:51 -05:00
2009-01-28 18:08:02 -05:00
/* helper to do a reply using a DbResponse object */
void replyToQuery(int queryResultFlags, Message &m, DbResponse &dbresponse, BSONObj obj);
2009-01-28 18:08:02 -05:00
2010-07-23 16:06:06 -04:00
2009-01-14 17:09:51 -05:00
} // namespace mongo