0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/db/jsobj.h
2007-10-19 19:35:48 -04:00

119 lines
2.8 KiB
C++

// jsobj.h
#include "../stdafx.h"
#include "pdfile.h"
#pragma pack(push)
#pragma pack(1)
/* BinData = binary data types.
EOO = end of object
*/
enum JSType { EOO = 0, Number=1, String=2, Object=3, Array=4, BinData=5, Undefined=6, jstOID=7, Bool=8, Date=9 };
/* subtypes of BinData.
bdtCustom and above are ones that the JS compiler understands, but are
opaque to the database.
*/
enum BinDataType { Function=1, ByteArray=2, bdtCustom=128 };
/* Object id's are optional for JSObjects.
When present they should be the first object member added.
*/
struct OID {
long long a;
unsigned b;
bool operator==(const OID& r) { return a==r.a&&b==r.b; }
};
/* marshalled js object format:
<unsigned totalSize> {<byte JSType><string FieldName><Data>}* EOO
totalSize includes itself.
Data:
EOO: nothing follows
Undefined: nothing follows
OID: an OID object
Number: <double>
String: <unsigned strlen><string>
Object: a nested object, which terminates with EOO.
Array:
<unsigned length>
{Object}[length]
a nested object, which is the object properties of the array
BinData:
<byte subtype>
<unsigned len>
<byte[len] data>
*/
/* db operation message format
unsigned opid; // arbitary; will be echoed back
byte operation;
Update:
int reserved;
string collection; // name of the collection (namespace)
a series of JSObjects terminated with a null object (i.e., just EOO)
Insert:
int reserved;
string collection;
a series of JSObjects terminated with a null object (i.e., just EOO)
GetByOID:
int reserved;
string collection;
OID oid;
Query:
int reserved;
string collection;
unsigned nToReturn; // how many you want back as the beginning of the cursor data
string query;
GetMore:
int reserved;
unsigned cursorID;
unsigned nToReturn;
byte EOM
*/
/* db response format
GetByOID operation:
marshalled JSObject returned. always specified, even if an error.
Query or GetMore: see query.h
int reserved;
unsigned cursorID;
unsigned startOfs;
unsigned nReturned;
list of marshalled JSObjects;
*/
#pragma pack(pop)
class JSObj {
public:
JSObj(const char *_data) : data(_data) {
size = *((int*) data);
}
JSObj(Record *r) {
size = r->netLength();
data = r->data;
}
const char *objdata() { return data + 4; } // skip the length field.
int objsize() { return size - 4; }
OID* getOID() {
const char *p = objdata();
if( *p != jstOID )
return 0;
return (OID *) ++p;
}
int size;
const char *data;
};