forked from moai/luamongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_cursor.cpp
208 lines (173 loc) · 4.84 KB
/
mongo_cursor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <client/dbclient.h>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include <compat-5.1.h>
#endif
};
#include "utils.h"
#include "common.h"
using namespace mongo;
extern void bson_to_lua(lua_State *L, const BSONObj &obj);
namespace {
inline DBClientCursor* userdata_to_cursor(lua_State* L, int index) {
void *ud = luaL_checkudata(L, index, LUAMONGO_CURSOR);
DBClientCursor *cursor = *((DBClientCursor **)ud);
return cursor;
}
} // anonymous namespace
/*
* cursor,err = db:query(ns, query)
*/
int cursor_create(lua_State *L, DBClientBase *connection, const char *ns,
const Query &query, int nToReturn, int nToSkip,
const BSONObj *fieldsToReturn, int queryOptions, int batchSize) {
int resultcount = 1;
try {
auto_ptr<DBClientCursor> autocursor = connection->query(
ns, query, nToReturn, nToSkip,
fieldsToReturn, queryOptions, batchSize);
if (!autocursor.get()) {
lua_pushnil(L);
lua_pushstring(L, LUAMONGO_ERR_CONNECTION_LOST);
return 2;
}
DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
*cursor = autocursor.get();
autocursor.release();
luaL_getmetatable(L, LUAMONGO_CURSOR);
lua_setmetatable(L, -2);
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, LUAMONGO_ERR_QUERY_FAILED, e.what());
resultcount = 2;
}
return resultcount;
}
/*
* res = cursor:next()
*/
static int cursor_next(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
if (cursor->more()) {
bson_to_lua(L, cursor->next());
} else {
lua_pushnil(L);
}
return 1;
}
static int result_iterator(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, lua_upvalueindex(1));
if (cursor->more()) {
bson_to_lua(L, cursor->next());
} else {
lua_pushnil(L);
}
return 1;
}
/*
* iter_func = cursor:results()
*/
static int cursor_results(lua_State *L) {
lua_pushcclosure(L, result_iterator, 1);
return 1;
}
/*
* has_more = cursor:has_more(in_current_batch)
* pass true to call moreInCurrentBatch (mongo >=1.5)
*/
static int cursor_has_more(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
bool in_current_batch = lua_toboolean(L, 2);
if (in_current_batch)
lua_pushboolean(L, cursor->moreInCurrentBatch());
else
lua_pushboolean(L, cursor->more());
return 1;
}
/*
* it_count = cursor:itcount()
*/
static int cursor_itcount(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
lua_pushinteger(L, cursor->itcount());
return 1;
}
/*
* is_dead = cursor:is_dead()
*/
static int cursor_is_dead(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
lua_pushboolean(L, cursor->isDead());
return 1;
}
/*
* is_tailable = cursor:is_tailable()
*/
static int cursor_is_tailable(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
lua_pushboolean(L, cursor->tailable());
return 1;
}
/*
* has_result_flag = cursor:has_result_flag()
*/
static int cursor_has_result_flag(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
int flag = lua_tointeger(L, 2);
lua_pushboolean(L, cursor->hasResultFlag(flag));
return 1;
}
/*
* id = cursor:get_id()
*/
static int cursor_get_id(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
lua_pushnumber(L, cursor->getCursorId());
return 1;
}
/*
* __gc
*/
static int cursor_gc(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
delete cursor;
return 0;
}
/*
* __tostring
*/
static int cursor_tostring(lua_State *L) {
DBClientCursor *cursor = userdata_to_cursor(L, 1);
lua_pushfstring(L, "%s: %p", LUAMONGO_CURSOR, cursor);
return 1;
}
int mongo_cursor_register(lua_State *L) {
static const luaL_Reg cursor_methods[] = {
{"next", cursor_next},
{"results", cursor_results},
{"has_more", cursor_has_more},
{"itcount", cursor_itcount},
{"is_dead", cursor_is_dead},
{"is_tailable", cursor_is_tailable},
{"has_result_flag", cursor_has_result_flag},
{"get_id", cursor_get_id},
{NULL, NULL}
};
static const luaL_Reg cursor_class_methods[] = {
{NULL, NULL}
};
luaL_newmetatable(L, LUAMONGO_CURSOR);
luaL_register(L, 0, cursor_methods);
lua_pushvalue(L,-1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, cursor_gc);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, cursor_tostring);
lua_setfield(L, -2, "__tostring");
luaL_register(L, LUAMONGO_CURSOR, cursor_class_methods);
return 1;
}