2007-10-20 01:35:48 +02:00
|
|
|
// query.h
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../grid/message.h"
|
2007-10-28 21:38:06 +01:00
|
|
|
#include "jsobj.h"
|
2007-11-05 01:17:42 +01:00
|
|
|
#include "storage.h"
|
2007-10-20 01:35:48 +02:00
|
|
|
|
2007-10-28 19:42:59 +01:00
|
|
|
/* requests:
|
|
|
|
|
2007-10-30 10:50:14 +01:00
|
|
|
dbDelete
|
|
|
|
int reserved=0;
|
|
|
|
string collection;
|
|
|
|
int flags=0; // 1=DeleteSingle
|
|
|
|
JSObject query;
|
|
|
|
dbUpdate:
|
|
|
|
int reserved;
|
|
|
|
string collection;
|
|
|
|
int flags; // 1=upsert
|
|
|
|
JSObject query;
|
|
|
|
JSObject objectToUpdate;
|
2007-12-25 02:59:58 +01:00
|
|
|
objectToUpdate may include { $inc: <field> }.
|
2007-10-30 10:50:14 +01:00
|
|
|
dbQuery:
|
2007-10-28 19:42:59 +01:00
|
|
|
int reserved;
|
|
|
|
string collection;
|
2008-02-01 01:10:52 +01:00
|
|
|
int nToSkip;
|
2007-10-28 19:42:59 +01:00
|
|
|
int nToReturn; // how many you want back as the beginning of the cursor data
|
|
|
|
JSObject query;
|
2007-11-05 04:34:37 +01:00
|
|
|
[JSObject fieldsToReturn]
|
2007-10-30 10:50:14 +01:00
|
|
|
dbGetMore:
|
2007-11-04 22:17:44 +01:00
|
|
|
int reserved;
|
|
|
|
string collection; // redundant, might use for security.
|
2007-10-28 19:42:59 +01:00
|
|
|
int nToReturn;
|
2007-11-04 22:17:44 +01:00
|
|
|
int64 cursorID;
|
2008-03-05 22:51:27 +01:00
|
|
|
dbKillCursors=2007:
|
2008-02-13 06:26:39 +01:00
|
|
|
int reserved;
|
|
|
|
int n;
|
|
|
|
int64 cursorIDs[n];
|
2007-10-30 10:50:14 +01:00
|
|
|
|
|
|
|
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.
|
2007-10-28 19:42:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* db response format
|
|
|
|
|
2007-10-20 01:35:48 +02:00
|
|
|
Query or GetMore:
|
|
|
|
int reserved;
|
2007-10-28 19:42:59 +01:00
|
|
|
int64 cursorID;
|
|
|
|
int startingFrom;
|
2007-10-28 21:38:06 +01:00
|
|
|
int nReturned; // 0=infinity
|
2007-10-20 01:35:48 +02:00
|
|
|
list of marshalled JSObjects;
|
2007-10-28 21:38:06 +01:00
|
|
|
*/
|
2007-10-20 01:35:48 +02:00
|
|
|
|
2007-11-19 15:41:28 +01:00
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
2007-10-20 01:35:48 +02:00
|
|
|
struct QueryResult : public MsgData {
|
2007-10-28 19:42:59 +01:00
|
|
|
long long cursorId;
|
|
|
|
int startingFrom;
|
2007-10-20 01:35:48 +02:00
|
|
|
int nReturned;
|
2007-10-28 21:38:06 +01:00
|
|
|
const char *data() { return (char *) (((int *)&nReturned)+1); }
|
2007-10-20 01:35:48 +02:00
|
|
|
};
|
|
|
|
|
2007-11-19 15:41:28 +01:00
|
|
|
#pragma pack(pop)
|
|
|
|
|
2007-11-04 22:17:44 +01:00
|
|
|
QueryResult* getMore(const char *ns, int ntoreturn, long long cursorid);
|
2007-11-05 04:34:37 +01:00
|
|
|
|
2008-02-24 20:59:03 +01:00
|
|
|
// caller must free() returned QueryResult.
|
2008-02-01 01:10:52 +01:00
|
|
|
QueryResult* runQuery(const char *ns, int ntoskip, int ntoreturn,
|
2007-12-01 17:44:42 +01:00
|
|
|
JSObj j, auto_ptr< set<string> > fieldFilter,
|
|
|
|
stringstream&);
|
2007-10-20 01:35:48 +02:00
|
|
|
|
2008-03-17 01:59:19 +01:00
|
|
|
void updateObjects(const char *ns, JSObj updateobj, JSObj pattern, bool upsert, stringstream& ss);
|
2007-10-30 10:50:14 +01:00
|
|
|
void deleteObjects(const char *ns, JSObj pattern, bool justOne);
|
2007-11-04 22:17:44 +01:00
|
|
|
|
2008-02-26 22:50:29 +01:00
|
|
|
class ClientCursor;
|
|
|
|
typedef map<long long, ClientCursor*> CCMap;
|
|
|
|
extern CCMap clientCursors; /* cursorid -> ClientCursor */
|
|
|
|
|
2007-11-11 20:21:02 +01:00
|
|
|
/* Cursor -- and its derived classes -- are our internal cursors.
|
|
|
|
|
|
|
|
ClientCursor is a wrapper that represents a cursorid from our client
|
|
|
|
application's perspective.
|
|
|
|
*/
|
2007-11-04 22:17:44 +01:00
|
|
|
class Cursor;
|
|
|
|
class ClientCursor {
|
2008-02-28 00:17:48 +01:00
|
|
|
friend class CursInspector;
|
2007-11-04 22:17:44 +01:00
|
|
|
public:
|
2008-02-29 18:00:17 +01:00
|
|
|
ClientCursor() {
|
|
|
|
cursorid=0; pos=0; nextAtThisLocation=0;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
cout << "clientcursor() " << cursorid << endl;
|
|
|
|
#endif
|
|
|
|
}
|
2007-11-05 01:17:42 +01:00
|
|
|
~ClientCursor();
|
2007-11-04 22:17:44 +01:00
|
|
|
long long cursorid;
|
|
|
|
string ns;
|
|
|
|
auto_ptr<JSMatcher> matcher;
|
|
|
|
auto_ptr<Cursor> c;
|
|
|
|
int pos;
|
2007-11-05 01:17:42 +01:00
|
|
|
DiskLoc lastLoc;
|
2007-11-05 04:34:37 +01:00
|
|
|
auto_ptr< set<string> > filter;
|
2007-11-05 01:17:42 +01:00
|
|
|
|
2007-11-11 20:21:02 +01:00
|
|
|
/* report to us that a new clientcursor exists so we can track it. You still
|
|
|
|
do the initial updateLocation() yourself.
|
|
|
|
*/
|
|
|
|
static void add(ClientCursor*);
|
|
|
|
|
2008-02-26 22:50:29 +01:00
|
|
|
static bool erase(long long cursorid);
|
|
|
|
|
|
|
|
static ClientCursor* find(long long id) {
|
|
|
|
CCMap::iterator it = clientCursors.find(id);
|
|
|
|
if( it == clientCursors.end() ) {
|
|
|
|
cout << "ClientCursor::find(): cursor not found in map " << id << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2007-11-11 20:21:02 +01:00
|
|
|
/* call when cursor's location changes so that we can update the
|
|
|
|
cursorsbylocation map. if you are locked and internally iterating, only
|
|
|
|
need to call when you are ready to "unlock".
|
|
|
|
*/
|
2007-11-05 01:17:42 +01:00
|
|
|
void updateLocation();
|
2007-11-04 22:17:44 +01:00
|
|
|
|
2008-02-26 22:50:29 +01:00
|
|
|
private:
|
|
|
|
void addToByLocation(DiskLoc cl);
|
|
|
|
static void cleanupByLocation(DiskLoc loc, long long cursorid);
|
2008-02-28 00:17:48 +01:00
|
|
|
public:
|
|
|
|
ClientCursor *nextAtThisLocation;
|
2008-02-26 22:50:29 +01:00
|
|
|
};
|
2007-11-11 20:21:02 +01:00
|
|
|
|
|
|
|
long long allocCursorId();
|