forked from moai/luamongo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.cpp
354 lines (311 loc) · 9.63 KB
/
utils.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#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 push_bsontype_table(lua_State* L, mongo::BSONType bsontype);
void lua_push_value(lua_State *L, const BSONElement &elem);
const char *bson_name(int type);
static int check_array(lua_State *L, int stackpos) {
int type = lua_type(L, stackpos);
int maxindex = 0;
if (type != LUA_TTABLE) {
return 0;
}
int tablelen = lua_objlen(L, stackpos);
lua_pushnil(L);
while (lua_next(L, stackpos-1) != 0) {
if (lua_isnumber(L, -2)) {
double index = lua_tonumber(L, -2);
if (index == floor(index)) {
int indexint = (int)floor(index);
maxindex = max(indexint, maxindex);
lua_pop(L, 1);
} else {
lua_pop(L, 2);
return 0;
}
} else {
lua_pop(L, 2);
return 0;
}
}
if (tablelen != maxindex) {
return 0;
}
return tablelen;
}
static void bson_to_array(lua_State *L, const BSONObj &obj) {
BSONObjIterator it = BSONObjIterator(obj);
lua_newtable(L);
int n = 1;
while (it.more()) {
BSONElement elem = it.next();
lua_push_value(L, elem);
lua_rawseti(L, -2, n++);
}
}
static void bson_to_table(lua_State *L, const BSONObj &obj) {
BSONObjIterator it = BSONObjIterator(obj);
lua_newtable(L);
while (it.more()) {
BSONElement elem = it.next();
const char *key = elem.fieldName();
lua_pushstring(L, key);
lua_push_value(L, elem);
lua_rawset(L, -3);
}
}
void lua_push_value(lua_State *L, const BSONElement &elem) {
int type = elem.type();
switch(type) {
case mongo::Undefined:
lua_pushnil(L);
break;
case mongo::NumberInt:
lua_pushinteger(L, elem.numberInt());
break;
case mongo::NumberLong:
case mongo::NumberDouble:
lua_pushnumber(L, elem.number());
break;
case mongo::Bool:
lua_pushboolean(L, elem.boolean());
break;
case mongo::String:
lua_pushstring(L, elem.valuestr());
break;
case mongo::Array:
bson_to_array(L, elem.embeddedObject());
break;
case mongo::Object:
bson_to_table(L, elem.embeddedObject());
break;
case mongo::Date:
push_bsontype_table(L, mongo::Date);
lua_pushnumber(L, elem.date());
lua_rawseti(L, -2, 1);
break;
case mongo::Timestamp:
push_bsontype_table(L, mongo::Date);
lua_pushnumber(L, elem.timestampTime());
lua_rawseti(L, -2, 1);
break;
case mongo::Symbol:
push_bsontype_table(L, mongo::Symbol);
lua_pushstring(L, elem.valuestr());
lua_rawseti(L, -2, 1);
break;
case mongo::RegEx:
push_bsontype_table(L, mongo::RegEx);
lua_pushstring(L, elem.regex());
lua_rawseti(L, -2, 1);
lua_pushstring(L, elem.regexFlags());
lua_rawseti(L, -2, 2);
break;
case mongo::jstOID:
push_bsontype_table(L, mongo::jstOID);
lua_pushstring(L, elem.__oid().str().c_str());
lua_rawseti(L, -2, 1);
break;
case mongo::jstNULL:
push_bsontype_table(L, mongo::jstNULL);
break;
case mongo::EOO:
break;
default:
luaL_error(L, LUAMONGO_UNSUPPORTED_BSON_TYPE, bson_name(type));
}
}
static void lua_append_bson(lua_State *L, const char *key, int stackpos, BSONObjBuilder *builder) {
int type = lua_type(L, stackpos);
if (type == LUA_TTABLE) {
int bsontype_found = luaL_getmetafield(L, stackpos, "__bsontype");
if (!bsontype_found) {
// not a special bsontype
// handle as a regular table, iterating keys
BSONObjBuilder b;
int arraylen = check_array(L, stackpos);
if (arraylen) {
for (int i = 0; i < arraylen; i++) {
lua_rawgeti(L, stackpos, i+1);
stringstream ss;
ss << i;
lua_append_bson(L, ss.str().c_str(), -1, &b);
lua_pop(L, 1);
}
builder->appendArray(key, b.obj());
} else {
lua_pushnil(L);
while (lua_next(L, stackpos-1) != 0) {
if (lua_isnumber(L, -2)) {
stringstream ss;
ss << lua_tonumber(L, -2);
lua_append_bson(L, ss.str().c_str(), -1, &b);
} else {
const char *k = lua_tostring(L, -2);
lua_append_bson(L, k, -1, &b);
}
lua_pop(L, 1);
}
builder->append(key, b.obj());
}
} else {
int bson_type = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, -1, 1);
switch (bson_type) {
case mongo::Date:
builder->appendDate(key, lua_tonumber(L, -1));
break;
case mongo::Timestamp:
builder->appendTimestamp(key);
break;
case mongo::RegEx: {
const char* regex = lua_tostring(L, -1);
lua_rawgeti(L, -2, 2); // options
const char* options = lua_tostring(L, -1);
lua_pop(L, 1);
builder->appendRegex(key, regex, options);
break;
}
case mongo::NumberInt:
builder->append(key, static_cast<int32_t>(lua_tointeger(L, -1)));
break;
case mongo::NumberLong:
builder->append(key, static_cast<long long int>(lua_tonumber(L, -1)));
break;
case mongo::Symbol:
builder->appendSymbol(key, lua_tostring(L, -1));
break;
case mongo::jstOID: {
OID oid;
oid.init(lua_tostring(L, -1));
builder->appendOID(key, &oid);
break;
}
case mongo::jstNULL:
builder->appendNull(key);
break;
default:
luaL_error(L, LUAMONGO_UNSUPPORTED_BSON_TYPE, luaL_typename(L, stackpos));
}
lua_pop(L, 1);
}
} else if (type == LUA_TNIL) {
builder->appendNull(key);
} else if (type == LUA_TNUMBER) {
double numval = lua_tonumber(L, stackpos);
if (numval == floor(numval)) {
// The numeric value looks like an integer, treat it as such.
// This is closer to how JSON datatypes behave.
int intval = lua_tointeger(L, stackpos);
builder->append(key, static_cast<int32_t>(intval));
} else {
builder->append(key, numval);
}
} else if (type == LUA_TBOOLEAN) {
builder->appendBool(key, lua_toboolean(L, stackpos));
} else if (type == LUA_TSTRING) {
builder->append(key, lua_tostring(L, stackpos));
} else {
luaL_error(L, LUAMONGO_UNSUPPORTED_LUA_TYPE, luaL_typename(L, stackpos));
}
}
void bson_to_lua(lua_State *L, const BSONObj &obj) {
if (obj.isEmpty()) {
lua_pushnil(L);
} else {
bson_to_table(L, obj);
}
}
// stackpos must be relative to the bottom, i.e., not negative
void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj) {
BSONObjBuilder builder;
lua_pushnil(L);
while (lua_next(L, stackpos) != 0) {
if (lua_type(L, -2) == LUA_TNUMBER) {
ostringstream ss;
ss << lua_tonumber(L, -2);
lua_append_bson(L, ss.str().c_str(), -1, &builder);
} else {
const char *k = lua_tostring(L, -2);
lua_append_bson(L, k, -1, &builder);
}
lua_pop(L, 1);
}
obj = builder.obj();
}
const char *bson_name(int type) {
const char *name;
switch(type) {
case mongo::EOO:
name = "EndOfObject";
break;
case mongo::NumberDouble:
name = "NumberDouble";
break;
case mongo::String:
name = "String";
break;
case mongo::Object:
name = "Object";
break;
case mongo::Array:
name = "Array";
break;
case mongo::BinData:
name = "BinData";
break;
case mongo::Undefined:
name = "Undefined";
break;
case mongo::jstOID:
name = "ObjectID";
break;
case mongo::Bool:
name = "Bool";
break;
case mongo::Date:
name = "Date";
break;
case mongo::jstNULL:
name = "NULL";
break;
case mongo::RegEx:
name = "RegEx";
break;
case mongo::DBRef:
name = "DBRef";
break;
case mongo::Code:
name = "Code";
break;
case mongo::Symbol:
name = "Symbol";
break;
case mongo::CodeWScope:
name = "CodeWScope";
break;
case mongo::NumberInt:
name = "NumberInt";
break;
case mongo::Timestamp:
name = "Timestamp";
break;
case mongo::NumberLong:
name = "NumberLong";
break;
default:
name = "UnknownType";
}
return name;
}